Skip to content

Commit 4c8d983

Browse files
committed
semexprs: fix argument hoisting producing ill-typed expressions
Arguments to `var` parameters weren't hoisted correctly, resulting in arguments of `var` type that are not `HiddenAddr` expressions.
1 parent 2fe1d70 commit 4c8d983

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

compiler/sem/semexprs.nim

+15-3
Original file line numberDiff line numberDiff line change
@@ -3491,10 +3491,22 @@ proc hoistParamsUsedInDefault(c: PContext, call, letSection, defExpr: var PNode)
34913491
newNodeI(nkEmpty, letSection.info),
34923492
call[paramPos])
34933493

3494-
call[paramPos] = newSymNode(hoistedVarSym) # Refer the original arg to its hoisted sym
3494+
if hoistedVarSym.typ.kind == tyVar:
3495+
# only ``HiddenAddr`` expressions may be of ``var`` type in argument
3496+
# positions
3497+
call[paramPos] =
3498+
newTreeIT(nkHiddenAddr, letSection.info, hoistedVarSym.typ,
3499+
newDeref(newSymNode(hoistedVarSym)))
3500+
else:
3501+
# Refer the original arg to its hoisted sym
3502+
call[paramPos] = newSymNode(hoistedVarSym)
34953503

3496-
# arg we refer to is a sym, wether introduced by hoisting or not doesn't matter, we simply reuse it
3497-
defExpr = call[paramPos]
3504+
# arg is either a sym, wether introduced by hoisting or not doesn't
3505+
# matter, or a ``(HiddenAddr (HiddenDeref sym))`` introduced by hoisting
3506+
if call[paramPos].kind == nkHiddenAddr:
3507+
defExpr = call[paramPos][0][0] # retrieve the symbol
3508+
else:
3509+
defExpr = call[paramPos] # must be a symbol
34983510
else:
34993511
for i in 0..<defExpr.safeLen:
35003512
hoistParamsUsedInDefault(c, call, letSection, defExpr[i])

0 commit comments

Comments
 (0)