Skip to content

Commit

Permalink
semexprs: fix argument hoisting producing ill-typed expressions
Browse files Browse the repository at this point in the history
Arguments to `var` parameters weren't hoisted correctly, resulting in
arguments of `var` type that are not `HiddenAddr` expressions.
  • Loading branch information
zerbina committed Dec 25, 2024
1 parent 2fe1d70 commit 4c8d983
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3491,10 +3491,22 @@ proc hoistParamsUsedInDefault(c: PContext, call, letSection, defExpr: var PNode)
newNodeI(nkEmpty, letSection.info),
call[paramPos])

call[paramPos] = newSymNode(hoistedVarSym) # Refer the original arg to its hoisted sym
if hoistedVarSym.typ.kind == tyVar:
# only ``HiddenAddr`` expressions may be of ``var`` type in argument
# positions
call[paramPos] =
newTreeIT(nkHiddenAddr, letSection.info, hoistedVarSym.typ,
newDeref(newSymNode(hoistedVarSym)))
else:
# Refer the original arg to its hoisted sym
call[paramPos] = newSymNode(hoistedVarSym)

# arg we refer to is a sym, wether introduced by hoisting or not doesn't matter, we simply reuse it
defExpr = call[paramPos]
# arg is either a sym, wether introduced by hoisting or not doesn't
# matter, or a ``(HiddenAddr (HiddenDeref sym))`` introduced by hoisting
if call[paramPos].kind == nkHiddenAddr:
defExpr = call[paramPos][0][0] # retrieve the symbol
else:
defExpr = call[paramPos] # must be a symbol
else:
for i in 0..<defExpr.safeLen:
hoistParamsUsedInDefault(c, call, letSection, defExpr[i])
Expand Down

0 comments on commit 4c8d983

Please sign in to comment.