bugfix(return-opt): Fixed issue with identical variants on return building. - #10056
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
PR SummaryMedium Risk Overview
Adds a Reviewed by Cursor Bugbot for commit 0089e45. Bugbot is set up for automated code reviews on this repo. Configure here. |
eytan-starkware
left a comment
There was a problem hiding this comment.
@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
TomerStarkware
left a comment
There was a problem hiding this comment.
@TomerStarkware reviewed all commit messages and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on orizi).

Summary
Replaces the
(TypeId, Vec<VariableId>)tuple key used inEarlyReturnContext::constructedwith a dedicatedConstructionenum that has distinctStructandEnumvariants. TheEnumvariant stores asemantic::ConcreteVariantand a singleVariableId, rather than aTypeIdand aVec<VariableId>, ensuring that different enum variants with the same input variable cannot collide in the memoization map.A regression test is added covering the case where two different variants of the same enum (
MyEnum::A(x)andMyEnum::B(x)) are constructed from the same input variable, verifying they produce distinct output variables.Type of change
Please check one:
Why is this change needed?
The previous key
(TypeId, Vec<VariableId>)for enum constructions used the enum's type ID and the input variable wrapped in aVec. Because two different variants of the same enum share the sameTypeId, constructingMyEnum::A(x)andMyEnum::B(x)would produce identical keys, causing the second construction to incorrectly reuse the variable allocated for the first. This led to wrong lowered IR when both variants appeared in the same early-return path.What was the behavior or documentation before?
Constructing two different variants of the same enum from the same input variable during return optimization would collide in the
constructedmap, causing the second enum construct statement to reuse the output variable of the first variant instead of allocating a new one.What is the behavior or documentation after?
Each enum construction is keyed by its
ConcreteVariant(which encodes the specific variant, not just the enum type) and its inputVariableId. Struct constructions continue to be keyed byTypeIdand the full list of inputVariableIds. The two cases are now structurally separate and cannot collide.Related issue or discussion (if any)
None.
Additional context
The new
Constructionenum derivesDebug,Clone,PartialEq,Eq, andHash, making it a well-typed, self-documenting replacement for the ad-hoc tuple key.