Skip to content

fix(ffi)!: no name the C++ generator makes is one C++ reserves - #929

Draft
kixelated wants to merge 2 commits into
mainfrom
quest/m1/ffi-export/cpp-reserved-names
Draft

kixelated wants to merge 2 commits into
mainfrom
quest/m1/ffi-export/cpp-reserved-names

Conversation

@kixelated

@kixelated kixelated commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m1/ffi-export/cpp-reserved-names.md, planned in kixel-ai/rust2#870 and added as a follow-up by kixel-ai/rust2#925.

Summary

No name the C++ generator makes from a Rust name is one C++ reserves for the implementation ([lex.name]: one holding __ anywhere, or starting with _ and a capital). The strict C++ build now runs with -Wreserved-identifier.

  • Reserved Rust names: cpp::escape drops a reserved name's leading _s and keeps one _ of each run, then escapes the result as any other name. So the api fixture's __all__ is api::all_ (was __all___), serde's derived __deserializer parameter in moq is deserializer (was __deserializer_), and _Bool is Bool. A lowercase _x is left alone, since C++ reserves it only in the global namespace, which holds no API name.
  • No stacked underscores: a clash used to append _ until free, so the generator's own escapes made __: delete__, new___, default__, the == operand a__, and a parameter c_by__ past the header's locals. A clash now takes a step: _, or 2 after a trailing _. So delete beside delete_ is delete_2, and a past the arena a_ is a_2. This applies to named, parameter, and a macro family's escape (unix_ is unix_2).
  • Namespace names stay apart: Header::name escaped each C name alone, so two C names escaping alike (__all__ and all_, or already delete and delete_ at namespace scope) would have declared one C++ name twice. It now looks up a map that named builds over every C name the ABI declares, kept apart from each other and from rt.hpp's names. This needed Abi::without_jni, which every language but Kotlin uses, to keep its claims' names (it dropped them).
  • The strict build (cpp_strict in ffi/tests/fixtures.rs) adds -Wreserved-identifier as a warning and requires the reserved names it reports to be exactly RESERVED_FROM_C. Those are the 13 names made of C names, which the new follow-up removes, after which the list and the -Wno-error go.

Decisions

The user was away, so I made these calls myself. Each is open to review.

  1. The C header's reserved names are a follow-up, not this PR. Turning the flag on showed that most reserved names in the strict build come from the C header, which every C++ program includes, and from C++ names built on C names. C resolves a clash by appending _ and joins with _, so api_Status_ (apart from the runtime's status) gives api_Status__eq, API_STATUS__MOVED, and C++'s Status__debug. A Rust _deprecated gives api__deprecated. Removing them renames C symbols (in clash cases only; none of moq's) and changes C's naming scheme, which is beyond this [S] C++ quest. The new quest owns it, with a Maintainer: decision on renaming those symbols.
  2. The flag is on now, with a ratchet, rather than waiting for the C quest. Any new reserved name, or one leaving the list, fails the test. The alternative of -Werror would need the C header fixed first, and leaving the flag off would let C++'s own reserved names come back.
  3. A clash steps to 2 after a trailing _ (delete_2, new_2, a_2), rather than appending a letter or counting from the start. Every name escapes the same way, so a stable suffix keeps the rule simple, and 2 reads as "the second". A third clash gives x_2_. trailing-underscore-names, which gives every language C++'s rule, should take this step too.
  4. Unreserving drops leading _s and collapses runs, as the Plan recommended. The Plan's other option, a trailing _ "where that clears the reservation", never applies: appending keeps a reserved name reserved.
  5. Namespace names yield to rt.hpp's names (detail, result, future, ...). A Rust item named like one of them would otherwise have shadowed it. No current crate does, and the api and moq surfaces show no such rename.

Validation

  • just ci passes.
  • New unit tests in ffi/src/cpp.rs:
    • no_name_is_reserved: every identifier of up to five of _, a, A, 1, plus keyword and macro families, escapes (and as a parameter) to an unreserved name. One scope of all 1,300 of them is named distinct and unreserved.
    • the_namespace_keeps_escapes_apart: runs headers on the without_jni ABI over free functions __all__, all_, all, delete, and delete_, and checks each C++ function calls its own C one. With the without_jni claims fix reverted, it fails (ns___all__ is not all_2).
    • reserved_names_escape_unreserved, plus the updated expectations of the existing naming tests.
  • The api fixture's strict build: before, -Wreserved-identifier reported 26 distinct names. After, it reports exactly the 13 in RESERVED_FROM_C, all from the C header or made of a C name. tests/api.cpp calls api::all_(1) == 3 and api::_deprecated(1) == 2.
  • End to end on a scratch crate with __all__, all_, all, a__b(__x, x_), struct _Bool, and Escaped::{delete, delete_}: exported, its glue built, and a C++ program compiled with Clang 23 at -Wall -Wextra -Wshadow -Wreserved-identifier. Every reserved-identifier warning comes from resv.h (the C header), none from a .hpp. The program calls all_2, all_, all, a_b(x, x_), delete_2, delete_, and Bool, and each returns its own Rust function's value. The first run of this check is what found the dropped without_jni claims.
  • just ffi surface: the api fixture's cpp.hpp changes only in the renames above. moq's changes in one parameter (__deserializer_ → deserializer).

Public API

Breaking, hence the !, for C++ only:

  • A Rust name holding __, or starting with _ and a capital, has a new C++ name (__all__ → all_, _Bool → Bool).
  • A name that clashed twice or more takes _2 where it took __ (delete__ → delete_2, a parameter c_by_ → c_by_2, default__ → default_2).
  • moq: mux_container_ts_Mpegts::deserialize's parameter is deserializer. A parameter name is no break for a caller.
  • C, the other bindings, and the wire are unchanged.

To migrate, use the renamed member the binding's API lock reports.

Caused by

No single PR. Reserved Rust names have passed through since the C++ generator began. kixel-ai/rust2#923 escaped them by appending _, which keeps them reserved. kixel-ai/rust2#914 made named stack _s past a clash, as parameter already did. Found by a person in the kixel-ai/rust2#925 audit.

Overlap

kixel-ai/rust2#919 (ffi-apart) rewrites the same branch of cpp::escape to escape every system header macro, with abi::unmacroed appending _s per family (unix__, __GNUC____). Whichever lands second merges the other. The merge should keep this PR's two rules: reserved names unreserve first, and no step makes __. That means unmacroed's count for C++ should step as step does.

Follow-ups

  • quest/m1/ffi-export/c-reserved-names.md [M]: the C header declares no name C or C++ reserves, nor does C++ make one from a C name, and RESERVED_FROM_C goes. Open decision: whether C symbols holding __ today (api_Status__eq) may be renamed, a break for a C caller of a clashing name. I recommend yes, since none of moq's symbols hold one.

Models

Size: S
Implementer: Claude Code, claude-opus-5-5, unknown effort (arm A)

@coderabbitai

coderabbitai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A Rust name C++ reserves (holding `__`, or `_` and a capital) drops its leading
`_`s and keeps one `_` per run, so `__all__` is `all_`. A clash steps with `_`
then `2` rather than stacking underscores, so `delete` beside `delete_` is
`delete_2`. Namespace names now resolve through `named` over every C name, apart
from each other and the runtime's. The strict C++ build turns on
`-Wreserved-identifier` and pins the names still made of C names to the new
c-reserved-names quest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title C++: a Rust name C++ reserves exports under one it does not fix(ffi)!: no name the C++ generator makes is one C++ reserves Nov 20, 2026
@codspeed

codspeed Bot commented Nov 20, 2026

Copy link
Copy Markdown

Merging this PR will regress 2 benchmarks

1 warning

⚠️ 28 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚡ 2 improved benchmarks
❌ 2 regressed benchmarks
✅ 81 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ woken 8.9 s 11.1 s -19.75%
❌ payload[4096] 543.8 ms 667.6 ms -18.54%
⚡ ready 5.1 s 3.5 s +46.33%
⚡ write 902.6 ms 774.4 ms +16.55%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing quest/m1/ffi-export/cpp-reserved-names (b96d558) with main (4b095a7)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant