Skip to content

Commit 536149b

Browse files
committed
cleanup: simplify ARC fix code in exprs.go and stmts.go
- genPipeExpr: deduplicate ARC release+return across fat-fn and plain-fn branches - callFatFn: remove unreachable bounds check (llArgsPreCoerce always has 1+len(argNodes) entries) - emitDefers: remove redundant i<len(pendingDeferEnvs) and env!=nil guards (pendingDefers and pendingDeferEnvs are always kept in sync; buildEnv never returns nil, only constant.Null for the no-captures case) - lambda params: inline one-use isRC variable https://claude.ai/code/session_016ZZaZLVcvtzTmYVaM1pmAB
1 parent 2d97eb9 commit 536149b

2 files changed

Lines changed: 11 additions & 27 deletions

File tree

codegen/exprs.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,24 +1312,17 @@ func (cg *CodeGen) genPipeExpr(block *ir.Block, e *ast.PipeExpr) (value.Value, e
13121312
if rightFn == nil {
13131313
return leftVal, nil
13141314
}
1315-
// If rightFn is a closure fat pointer {fn*, i8*}, call through it.
1315+
// Call through the function (fat-pointer or plain).
1316+
var result value.Value
13161317
if isFatFnPtr(rightFn.Type()) {
13171318
fnPtr := block.NewExtractValue(rightFn, 0)
13181319
envPtr := block.NewExtractValue(rightFn, 1)
13191320
fnType := fnPtr.Type().(*irtypes.PointerType).ElemType.(*irtypes.FuncType)
13201321
llArgs := cg.adaptArgs(block, []value.Value{envPtr, leftVal}, fnType)
1321-
result := block.NewCall(fnPtr, llArgs...)
1322-
// ARC: release the left-hand value if it is a temporary RC allocation.
1323-
if isRCTrackedType(leftVal.Type()) && !isCopyExpr(e.Left) {
1324-
cg.emitRelease(block, leftVal)
1325-
}
1326-
if irtypes.IsVoid(result.Type()) {
1327-
return nil, nil
1328-
}
1329-
return result, nil
1322+
result = block.NewCall(fnPtr, llArgs...)
1323+
} else {
1324+
result = block.NewCall(rightFn, leftVal)
13301325
}
1331-
// Plain function pointer.
1332-
result := block.NewCall(rightFn, leftVal)
13331326
// ARC: release the left-hand value if it is a temporary RC allocation.
13341327
if isRCTrackedType(leftVal.Type()) && !isCopyExpr(e.Left) {
13351328
cg.emitRelease(block, leftVal)
@@ -1842,9 +1835,6 @@ func (cg *CodeGen) callFatFn(block *ir.Block, fatPtr value.Value, argNodes []ast
18421835
// ARC: release temporary RC-tracked arguments (skip index 0 = env).
18431836
for i, astArg := range argNodes {
18441837
argIdx := i + 1 // offset by 1 for the env slot
1845-
if argIdx >= len(llArgsPreCoerce) {
1846-
break
1847-
}
18481838
preCoerce := llArgsPreCoerce[argIdx]
18491839
postCoerce := llArgs[argIdx]
18501840

@@ -1947,8 +1937,7 @@ func (cg *CodeGen) genLambdaExpr(block *ir.Block, e *ast.LambdaExpr) (value.Valu
19471937
// ARC: retain RC-tracked params so scope-exit release is balanced.
19481938
// Same convention as genFuncDeclAs: callee owns a reference.
19491939
cg.emitRetain(entry, param)
1950-
isRC := isRCTrackedType(pt)
1951-
cg.curScope.set(p.Name, &scopeEntry{val: alloca, isAlloc: true, isRC: isRC})
1940+
cg.curScope.set(p.Name, &scopeEntry{val: alloca, isAlloc: true, isRC: isRCTrackedType(pt)})
19521941
}
19531942

19541943
// For where-list bodies, the match subject is the first parameter so that

codegen/stmts.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,11 @@ func (cg *CodeGen) emitDefers(block *ir.Block) error {
485485
if _, err := cg.genExpr(block, cg.pendingDefers[i]); err != nil {
486486
return err
487487
}
488-
// Free the env that was malloc'd for the thunk/panic path.
489-
// The env is an i8* (may be null if there were no captures).
490-
if i < len(cg.pendingDeferEnvs) {
491-
env := cg.pendingDeferEnvs[i]
492-
if env != nil {
493-
// Only free non-null envs (captures present case).
494-
if _, isNull := env.(*constant.Null); !isNull {
495-
block.NewCall(cg.ensureFree(), env)
496-
}
497-
}
488+
// Free the heap env that was malloc'd for the thunk/panic path.
489+
// Skip the null sentinel emitted when there were no captures.
490+
env := cg.pendingDeferEnvs[i]
491+
if _, isNull := env.(*constant.Null); !isNull {
492+
block.NewCall(cg.ensureFree(), env)
498493
}
499494
}
500495
return nil

0 commit comments

Comments
 (0)