Skip to content

Codegen single-non-ZST-field constants with name-keyed struct fields - #4724

Merged
feliperodri merged 2 commits into
model-checking:mainfrom
tautschnig:autoharness-backend-fixes
Aug 14, 2026
Merged

Codegen single-non-ZST-field constants with name-keyed struct fields#4724
feliperodri merged 2 commits into
model-checking:mainfrom
tautschnig:autoharness-backend-fixes

Conversation

@tautschnig

Copy link
Copy Markdown
Member

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 top-500 crates.io autoharness sweep (tracking #3832); the fix and regression test are independent of autoharness.

Changes

  • Key the constant's field values by the variant's field names (struct_expr) instead of position (struct_expr_from_values).
  • Regression test with a layout-reordered constant struct.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

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>
@tautschnig
tautschnig requested a review from a team as a code owner August 7, 2026 17:40
Copilot AI lite review requested due to automatic review settings August 7, 2026 17:40
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Aug 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_expr instead of struct_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 thread tests/kani/Structs/const_struct_layout_order.rs Outdated
The `try_codegen_constant` fix only applies to constant structs with a
single non-ZST field (`non_zst_types.len() == 1`). The original test used
a `Pair` with two non-ZST fields (`Wrap(u8)` and `u16`), which takes the
separate allocation branch and never reaches the name-keyed `struct_expr`
path, so it did not cover the regression.

Replace it with a struct that has exactly one non-ZST field: a zero-sized
`Marker` declared before a higher-aligned `u64`, so layout order (`u64`
first) differs from declaration order. Verified that this reproduces the
"value type does not match field type" ICE on the pre-fix compiler and
passes with the fix.

Signed-off-by: Felipe Monteiro <felisous@amazon.com>
@feliperodri
feliperodri enabled auto-merge August 14, 2026 00:50
@feliperodri
feliperodri added this pull request to the merge queue Aug 14, 2026
Merged via the queue into model-checking:main with commit 2bf550f Aug 14, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants