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

bug fix(semantic): Substitute result of generic const function calls. - #10143

Merged
orizi merged 1 commit into
mainfrom
orizi/const-eval-substitute-generic-result
Jun 22, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/const-eval-substitute-generic-result

Conversation

@orizi

@orizi orizi commented Jun 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When a generic const fn returns an aggregate value (struct, tuple, or enum), the returned ConstValue was tagged with the generic types from the function body rather than the concrete types at the call site. This caused type mismatches when using generic const functions that construct and return aggregate types.

The fix applies substitute to the type of each aggregate ConstValue (struct, tuple, and enum variant) during constant evaluation, replacing generic type parameters with the concrete types from the call site.


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?

Generic const fns that return structs, tuples, or enums were producing ConstValues tagged with the generic type parameters from the function body. At the call site, the expected type is the concrete substituted type, so the mismatch caused incorrect constant evaluation results.


What was the behavior or documentation before?

A generic const fn returning an aggregate (e.g., Pair<T>, (T, T), or Either<T>) would produce a ConstValue whose inner type was still the generic T rather than the concrete type (e.g., felt252) supplied at the call site.


What is the behavior or documentation after?

After evaluating each aggregate expression inside a generic const fn, the type attached to the resulting ConstValue is passed through self.substitute(...) so that structs, tuples, and enum variants are tagged with the concrete types from the call site. Three new tests cover this behavior:

  • test_const_generic_struct_ctor — generic const fn returning a struct
  • test_const_generic_tuple_return — generic const fn returning a tuple
  • test_const_generic_enum_return — generic const fn returning an enum variant

Related issue or discussion (if any)


Additional context

For enum variants, substitute is applied to the entire ConcreteVariant (not just its type) to ensure all generic parameters embedded in the variant descriptor are also resolved to their concrete forms.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi changed the base branch from main to graphite-base/10143 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 changed the base branch from graphite-base/10143 to orizi/const-eval-or-return-refactor June 21, 2026 12:56
@orizi
orizi marked this pull request as ready for review June 21, 2026 12:57
@cursor

cursor Bot commented Jun 21, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core constant-evaluation paths in the semantic compiler; scope is narrow but incorrect substitution could affect any generic const aggregate at compile time.

Overview
Fixes generic const fn calls that return structs, tuples, enums, or fixed-size arrays: constant evaluation now runs substitute on the aggregate’s type (and on enum variants) so ConstValue is tagged with call-site concrete types instead of unresolved generics from the function body.

constant.rs applies this in ConstantEvaluateContext::evaluate for tuple/Struct/StructCtor/EnumVariantCtor/FixedSizeArray paths. const_test.cairo adds three tests (make_pair, make_tuple, make_left) that assert const results match expected felt252-typed aggregates.

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

@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 changed the base branch from orizi/const-eval-or-return-refactor to graphite-base/10143 June 22, 2026 07:22
@orizi
orizi force-pushed the orizi/const-eval-substitute-generic-result branch from f241872 to fafd3c2 Compare June 22, 2026 07:22
@orizi
orizi changed the base branch from graphite-base/10143 to main June 22, 2026 07:22
@orizi
orizi enabled auto-merge June 22, 2026 07:25

@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 7ff5282 Jun 22, 2026
55 checks passed
@orizi
orizi deleted the orizi/const-eval-substitute-generic-result 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