Skip to content

[Rust] Do not escape bit set choice names used in accessors - #1121

Open
s1amese2003 wants to merge 1 commit into
aeron-io:masterfrom
s1amese2003:fix/rust-set-choice-keyword
Open

[Rust] Do not escape bit set choice names used in accessors#1121
s1amese2003 wants to merge 1 commit into
aeron-io:masterfrom
s1amese2003:fix/rust-set-choice-keyword

Conversation

@s1amese2003

Copy link
Copy Markdown
Contributor

Fixes #1118.

What

A set choice named after a Rust keyword generates accessors that do not parse:

pub fn get_r#type(&self) -> bool { ... }
pub fn set_r#type(&mut self, value: bool) -> &mut Self { ... }
error: prefix `get_r` is unknown
  --> src/flags.rs:16:12
   |
16 |     pub fn get_r#type(&self) -> bool {
   |            ^^^^^ unknown prefix

The generated Debug implementation calls the same names, and prints the
escaping in its labels.

Why

generateSingleBitSet formats the choice name with formatFunctionName, which
escapes a name that shadows a Rust keyword as a raw identifier. A choice is only
ever reached through a get_ or set_ prefixed accessor, so the prefixed name
is already a plain identifier and cannot shadow a keyword. Escaping the choice
name is therefore never needed, and it places the r# in the middle of the
accessor, where it does not parse.

For the same schema C++ generates type() and Go generates Type..., so Rust
was the only language mangling these names.

Checks

  • ./gradlew runRustTests — 36 passed, 0 failed
  • ./gradlew :sbe-tool:test — pass
  • ./gradlew :sbe-tool:checkstyleMain :sbe-tool:checkstyleTest — pass

Regenerating every Rust codec before and after the change leaves all 137
generated files byte-identical except issue1118/src/flags.rs. The
non-keyword choice in the new schema, and the existing bit sets such as
OptionalExtras, are unchanged.

Reverting only the generator change makes runRustTests fail to compile, so the
new schema keeps this covered.

A choice named after a Rust keyword generated accessors such as
get_r#type and set_r#type, which do not parse: the raw identifier escaping
ends up in the middle of the accessor name and rustc reports
"error: prefix `get_r` is unknown". The generated Debug implementation
called the same names, and printed the escaping in its labels.

A choice is only ever reached through a get_ or set_ prefixed accessor, so
the prefixed name is a plain identifier and can never shadow a keyword. The
choice name therefore does not need escaping, and escaping it is what breaks
the result. C++ generates type() and Go generates Type... for the same
schema, so Rust was the only language mangling these names.

Resolves aeron-io#1118
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.

[Rust] Set choice named Type generates invalid accessor names

1 participant