bug fix(semantic): Substitute result of generic const function calls. - #10143
Conversation
fd4fc03 to
f241872
Compare
PR SummaryMedium Risk Overview
Reviewed by Cursor Bugbot for commit fafd3c2. Bugbot is set up for automated code reviews on this repo. Configure here. |
TomerStarkware
left a comment
There was a problem hiding this comment.
@TomerStarkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).
f241872 to
fafd3c2
Compare
orizi
left a comment
There was a problem hiding this comment.
@orizi reviewed 1 file and all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

Summary
When a generic
const fnreturns an aggregate value (struct, tuple, or enum), the returnedConstValuewas 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
substituteto the type of each aggregateConstValue(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:
Why is this change needed?
Generic
const fns that return structs, tuples, or enums were producingConstValues 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 fnreturning an aggregate (e.g.,Pair<T>,(T, T), orEither<T>) would produce aConstValuewhose inner type was still the genericTrather 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 resultingConstValueis passed throughself.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 structtest_const_generic_tuple_return— generic const fn returning a tupletest_const_generic_enum_return— generic const fn returning an enum variantRelated issue or discussion (if any)
Additional context
For enum variants,
substituteis applied to the entireConcreteVariant(not just its type) to ensure all generic parameters embedded in the variant descriptor are also resolved to their concrete forms.