Sitelet https://github.com/starkware-libs/cairo/pull/10136
Skip to content

bug fix(semantic): Support struct-update (..base) syntax in const expressions. - #10136

Merged
orizi merged 1 commit into
mainfrom
orizi/const-struct-update-eval
Jun 22, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/const-struct-update-eval

Conversation

@orizi

@orizi orizi commented Jun 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds support for struct update syntax (Foo { field: value, ..base }) in constant expressions. Previously, ConstantEvaluateContext only handled struct constructors with no base struct (base_struct: None), causing struct update syntax to be unsupported in const contexts. Now, the base struct expression is validated and evaluated, and its member values are used as fallbacks for any fields not explicitly specified in the constructor.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

Struct update syntax (Point { y: 20, ..BASE }) was not usable in const declarations. The constant evaluator would silently skip or fail on any struct constructor that included a base struct, leaving users unable to derive one constant struct from another using this syntax.


What was the behavior or documentation before?

Struct update syntax in const expressions was not supported. The evaluator only matched ExprStructCtor with base_struct: None, so any use of ..base in a constant struct constructor would not be evaluated correctly.


What is the behavior or documentation after?

Struct update syntax is now valid in const expressions. The base struct is validated and evaluated, and its member values fill in any fields not explicitly provided. Chained updates (e.g., deriving a constant from another already-updated constant) are also supported, as demonstrated by the new test:

const BASE: Point = Point { x: 1, y: 2, z: 3 };
const UPDATED: Point = Point { y: 20, ..BASE };
assert_eq!(UPDATED, Point { x: 1, y: 20, z: 3 });
const FROM_UPDATED: Point = Point { x: 10, z: 30, ..UPDATED };
assert_eq!(FROM_UPDATED, Point { x: 10, y: 20, z: 30 });

Related issue or discussion (if any)


Additional context

The base struct's evaluated ConstValue::Struct values are indexed by member order position and used as fallbacks via .or_else(|| Some(base_members?[index])) when a field is not explicitly listed in the constructor.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi marked this pull request as ready for review June 21, 2026 08:28
@cursor

cursor Bot commented Jun 21, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Scoped change to const struct evaluation in the semantic layer; no runtime, auth, or I/O paths. Wrong-type bases fail evaluation without affecting non-const code.

Overview
Const struct update syntax (Point { y: 20, ..BASE }) now works in const declarations. The constant evaluator in constant.rs no longer ignores base_struct on struct constructors.

Validation walks the base expression like other const subexpressions. Evaluation evaluates the base to a ConstValue::Struct (same type as the ctor), then builds the result in member order: explicit fields win, otherwise values come from the base by index. Chained updates are covered by a new test in const_test.cairo.

Reviewed by Cursor Bugbot for commit d98cfb7. Bugbot is set up for automated code reviews on this repo. Configure here.

@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from 6831725 to 342ba4a Compare June 21, 2026 08:30

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6831725608

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/cairo-lang-semantic/src/items/constant.rs Outdated

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and orizi).

@orizi
orizi changed the base branch from main to graphite-base/10136 June 21, 2026 11:36
@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from 342ba4a to c78a1d7 Compare June 21, 2026 11:36
@orizi
orizi changed the base branch from graphite-base/10136 to orizi/const-eval-substitute-generic-result June 21, 2026 11:36
@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from c78a1d7 to 0389587 Compare June 21, 2026 12:56
@orizi
orizi force-pushed the orizi/const-eval-substitute-generic-result branch from fd4fc03 to f241872 Compare June 21, 2026 12:56
@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from 0389587 to fa8c243 Compare June 21, 2026 13:00

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@orizi
orizi force-pushed the orizi/const-eval-substitute-generic-result branch from f241872 to fafd3c2 Compare June 22, 2026 07:22
@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from fa8c243 to 3251d29 Compare June 22, 2026 07:22
@orizi
orizi changed the base branch from orizi/const-eval-substitute-generic-result to graphite-base/10136 June 22, 2026 07:51
@orizi
orizi force-pushed the graphite-base/10136 branch from fafd3c2 to 7ff5282 Compare June 22, 2026 07:51
@orizi
orizi force-pushed the orizi/const-struct-update-eval branch from 3251d29 to d98cfb7 Compare June 22, 2026 07:51
@orizi
orizi changed the base branch from graphite-base/10136 to main June 22, 2026 07:51
@orizi
orizi enabled auto-merge June 22, 2026 07:54

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi reviewed 1 file and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@orizi
orizi added this pull request to the merge queue Jun 22, 2026
Merged via the queue into main with commit 6296ede Jun 22, 2026
106 checks passed
@orizi
orizi deleted the orizi/const-struct-update-eval branch June 23, 2026 07: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.

3 participants