fix(codegen): path-specific drops for struct return from nested scopes#284
Merged
fix(codegen): path-specific drops for struct return from nested scopes#284
Conversation
Fixes struct return from nested scopes (if/for/while) returning the
wrong value and leaking the non-returned variable.
`return StructType{...}` from inside nested control flow returned the
trailing expression value instead of the early return value, because
initReturnFlagAndSlot excluded aggregate types from returnSlot creation.
Additionally, non-returned variables were never dropped.
The fix introduces lazy returnSlot creation with path-specific drop
semantics:
- ensureReturnSlot(): creates return slot lazily for aggregate types
at the function entry block when a nested return is encountered
- earlyReturnFlag: distinguishes explicit return statements from
trailing expressions for drop path selection
- Pre-scan with scanReturns/hasNestedReturn: detects nested returns
before body generation to enable return guards
- Path-specific drops in popDropScope: scf.if(earlyReturnFlag) with
separate exclusion sets per branch — early-return path excludes
return-expression vars, normal path excludes trailing-expression vars
- Struct alloca promotion: extends declareVariable promotion to struct
types when returnSlotIsLazy, with zeroinitializer
- Struct null-guard: checks first pointer field before calling
user-defined Drop on potentially uninitialized structs
- collectExcludeVarsFromBlock: extended to recurse into nested control
flow (if/for/while/match) to find return statements
- FunctionGenerationScope: updated to save/restore returnSlotIsLazy
and earlyReturnFlag alongside existing return state
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.
Supersedes #276 (rebased onto current main after 6 major merges).
Problem
return StructType{...}from inside nested control flow (if/for/while) returned the trailing expression value instead of the early return value, becauseinitReturnFlagAndSlotexcluded aggregate types from returnSlot creation. Additionally, non-returned variables were never dropped — a memory leak.Solution
Lazy returnSlot creation with path-specific drop semantics:
scf.if(earlyReturnFlag)with separate exclusion sets per branchTests
3 new E2E tests covering struct return from if-body, for-loop, and outer-scope variable return.
Closes #276