Skip to content

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

Open
tautschnig wants to merge 1 commit into
model-checking:mainfrom
tautschnig:autoharness-backend-fixes
Open

Codegen single-non-ZST-field constants with name-keyed struct fields#4724
tautschnig wants to merge 1 commit 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 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);
}
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.

2 participants