Codegen single-non-ZST-field constants with name-keyed struct fields - #4724
Open
tautschnig wants to merge 1 commit into
Open
Codegen single-non-ZST-field constants with name-keyed struct fields#4724tautschnig wants to merge 1 commit into
tautschnig wants to merge 1 commit into
Conversation
try_codegen_constant built the field-value list in declaration order and used the positional struct constructor, but the goto struct type lists fields in LAYOUT order. For constants whose layout reorders fields (e.g. a (Wrap(u8), u16) pair placing the u16 first), this ICEd with 'value type does not match field type' (surfaced by yansi and bitvec in a crates.io autoharness sweep). Key the values by the variant's field names instead. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an ICE in try_codegen_constant when codegenning struct constants whose field layout order differs from declaration order by switching from positional struct initialization to name-keyed field initialization, aligning with the goto struct’s layout-based field ordering.
Changes:
- Build struct constant initializers as a name-keyed map of field values and use
Expr::struct_exprinstead ofstruct_expr_from_values. - Add a regression test intended to cover layout-vs-declaration field order mismatches in constant struct codegen.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| kani-compiler/src/codegen_cprover_gotoc/codegen/operand.rs | Switch constant struct expansion to name-keyed field initialization to match goto struct layout order. |
| tests/kani/Structs/const_struct_layout_order.rs | Add regression test for struct-constant codegen with layout-reordered fields. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+4
to
+24
| //! Constant structs whose layout order differs from declaration order must codegen with | ||
| //! name-keyed fields (regression: value/field type mismatch ICE in try_codegen_constant). | ||
| //! `Pair` declares (u8-wrapper, u16); layout places the u16 first. | ||
|
|
||
| #[derive(Clone, Copy, PartialEq)] | ||
| struct Wrap(u8); | ||
|
|
||
| #[derive(Clone, Copy, PartialEq)] | ||
| struct Pair { | ||
| w: Wrap, | ||
| n: u16, | ||
| } | ||
|
|
||
| const P: Pair = Pair { w: Wrap(3), n: 512 }; | ||
|
|
||
| #[kani::proof] | ||
| fn check_const_struct_layout_order() { | ||
| let p = P; | ||
| assert!(p.w == Wrap(3)); | ||
| assert!(p.n == 512); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
try_codegen_constantbuilt the field-value list in declaration order and used the positional struct constructor, but the goto struct type lists fields in layout order. For constants whose layout reorders fields (e.g. a(Wrap(u8), u16)pair placing theu16first), this ICEd with value type does not match field type.Surfaced by yansi and bitvec in a top-500 crates.io autoharness sweep (tracking #3832); the fix and regression test are independent of autoharness.
Changes
struct_expr) instead of position (struct_expr_from_values).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.