Resolve named struct fields in formula translation#118
Merged
Conversation
The formula translator's `ExprKind::Field` handler only accepted numeric tuple field names (e.g. `.0`). Structs are represented as tuples in the logic, so named field access like `result.x` panicked. Resolve a named field to its position in the ADT's variant, matching the tuple-projection form, so specifications can refer to struct fields by name. https://claude.ai/code/session_01WdLyxyy4ieAxrexj83X5MX
coord-e
pushed a commit
that referenced
this pull request
Jun 11, 2026
Replace the synthetic `Clause` used to render a predicate's translated formula with a `TermSortEnv` trait: anything that can resolve a term-level variable's sort (a `Clause` via its `vars`, or a bare `IndexVec<TermVarIdx, Sort>` for a define-fun signature) can drive term rendering. The smtlib2 Display wrappers now hold `&dyn TermSortEnv` instead of `&Clause`, so no fake clause is fabricated. Also revert the named-struct-field translator change and the trait predicate test conversions: that field resolution is now a separate PR (#118). The trait predicate tests stay on raw SMT2 here and can move to Rust syntax once the field PR lands; the scalar free-function predicate test exercises the Rust-syntax path. https://claude.ai/code/session_01WdLyxyy4ieAxrexj83X5MX
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes AnnotFnTranslator’s formula translation of ExprKind::Field so that named struct field accesses (e.g. result.x) are resolved to their positional tuple-projection index in the logical encoding, instead of panicking when the field name is not a numeric tuple index.
Changes:
- Extend
ExprKind::Fieldtranslation to map named ADT fields to their index in the struct’s (non-enum) variant field list. - Preserve the existing fast path for numeric tuple field names (
.0,.1, …). - Add UI pass/fail tests covering
ensuresformulas that reference struct fields by name.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/analyze/annot_fn.rs | Resolve named struct fields to tuple-projection indices during formula translation. |
| tests/ui/pass/formula_struct_field.rs | Adds a passing UI test verifying ensures can reference struct fields by name. |
| tests/ui/fail/formula_struct_field.rs | Adds a failing UI test where the named-field postcondition is intentionally incorrect (Unsat). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
coord-e
pushed a commit
that referenced
this pull request
Jun 12, 2026
Now that named struct-field resolution is on main (#118), flip the trait predicate tests (annot_preds_trait, annot_preds_trait_multi; pass and fail) from raw SMT-LIB2 bodies to Rust-expression bodies using `self.x`. https://claude.ai/code/session_01WdLyxyy4ieAxrexj83X5MX
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.
Summary
The formula translator (
AnnotFnTranslator, used byrequires/ensures/invariant/predicates) handledExprKind::Fieldonly for numeric tuple field names (.0,.1). Structs are represented as tuples in the logic, so accessing a named field — e.g.result.xin anensuresformula — panicked withtuple field index must be a non-negative integer.This resolves a named field to its position in the ADT's (single, non-enum) variant, producing the same tuple-projection term as the numeric path. Numeric tuple indices keep their fast path.
Change
src/analyze/annot_fn.rs—ExprKind::Field: when the field name isn't a numeric index, look up the field position viaexpr_ty().ty_adt_def()'s non-enum variant.Tests
tests/ui/{pass,fail}/formula_struct_field.rs— anensuresformula referring to struct fields by name (pass = correct postcondition forswap; fail = postcondition violated).Context
Extracted from the
thrust::predicateRust-syntax work (PR #114) as an independent, self-contained improvement to the formula translator. PR #114's trait/struct predicate tests depend on this.https://claude.ai/code/session_01WdLyxyy4ieAxrexj83X5MX
Generated by Claude Code