fix(codegen): path-specific drops for struct return from nested scopes#276
Closed
fix(codegen): path-specific drops for struct return from nested scopes#276
Conversation
Return of aggregate types (structs, tuples) from inside if/for/while previously returned the wrong value — the trailing expression instead of the early return value — because initReturnFlagAndSlot excluded aggregate types from returnSlot creation. This introduces lazy returnSlot creation via ensureReturnSlot() and path-specific drop semantics that correctly handle both early-return and normal-flow paths: - ensureReturnSlot(): lazily creates the return slot at the function entry block when a return statement is encountered inside a nested SCF region, or eagerly when a pre-scan detects nested returns - earlyReturnFlag: a new per-function flag set ONLY by explicit return statements (not trailing expressions), enabling popDropScope to distinguish early return from normal flow - funcLevelEarlyReturnVarNames: variables referenced only by return statements, separate from trailing-expression vars, populated by a recursive AST pre-scan that handles else-if chains - Path-specific drops in popDropScope: when returnSlotIsLazy, emits scf.if(earlyReturnFlag) with different exclusion sets per branch - Struct alloca promotion in declareVariable: extends alloca promotion to LLVMStructType when returnSlotIsLazy, with zeroinitializer for safe null-guard detection - Struct null-guard in emitDropEntry: checks first pointer field for null (or first integer field for zero in scalar-only structs) before calling user-defined Drop
Contributor
Author
auto-merge was automatically disabled
March 21, 2026 19:47
Pull request was closed
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.
Fixes struct return from nested scopes (if/for/while) returning the wrong value and leaking the non-returned variable.
Problem
return StructType{...}from inside nested control flow 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. All 530 E2E tests pass.