Skip to content

Fix niche-optimized enum construction in cc_bindings_from_rs.#1122

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_925397284
Jun 2, 2026
Merged

Fix niche-optimized enum construction in cc_bindings_from_rs.#1122
copybara-service[bot] merged 1 commit into
mainfrom
test_925397284

Conversation

@copybara-service
Copy link
Copy Markdown

@copybara-service copybara-service Bot commented Jun 2, 2026

Fix niche-optimized enum construction in cc_bindings_from_rs.

The enums_test (TestQrError) added in this CL was initially failing
because niche-optimized enums were being constructed in C++ using their
semantic discriminant (retrieved via discriminant_for_variant), which
evaluates to 0 for QrError::DataTooLong instead of the actual
in-memory niche tag value. This meant that QrError::MakeDataTooLong()
was constructing a value that on the Rust side was interpreted as
Structured(AtLeast2Pieces) instead of DataTooLong.

The fix is switching generate_struct_and_union.rs to use the
compiler's native layout helper tcx.tag_for_variant(...), which
correctly returns the exact in-memory niche tag (or direct discriminant)
for any given variant index.

@copybara-service copybara-service Bot force-pushed the test_925397284 branch 3 times, most recently from 1036e75 to b81ae20 Compare June 2, 2026 23:34
The `enums_test` (`TestQrError`) added in this CL was initially failing
because niche-optimized enums were being constructed in C++ using their
semantic discriminant (retrieved via `discriminant_for_variant`), which
evaluates to `0` for `QrError::DataTooLong` instead of the actual
in-memory niche tag value. This meant that `QrError::MakeDataTooLong()`
was constructing a value that on the Rust side was interpreted as
`Structured(AtLeast2Pieces)` instead of `DataTooLong`.

The fix is switching `generate_struct_and_union.rs` to use the
compiler's native layout helper `tcx.tag_for_variant(...)`, which
correctly returns the exact in-memory niche tag (or direct discriminant)
for any given variant index.

PiperOrigin-RevId: 925652439
@copybara-service copybara-service Bot merged commit 9c665a2 into main Jun 2, 2026
@copybara-service copybara-service Bot deleted the test_925397284 branch June 2, 2026 23:45
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