fixes #18104; tranform one liner var decl before templates expansion (#23294)

fixes #18104

(cherry picked from commit 1e9a3c438b)
This commit is contained in:
ringabout
2024-02-13 15:10:28 +08:00
committed by narimiran
parent 3a334e09ea
commit 4e1b5ee702
2 changed files with 25 additions and 0 deletions

View File

@@ -780,6 +780,24 @@ proc makeVarTupleSection(c: PContext, n, a, def: PNode, typ: PType, symkind: TSy
proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
var b: PNode
result = copyNode(n)
# transform var x, y = 12 into var x = 12; var y = 12
# bug #18104; transformation should be finished before templates expansion
# TODO: move warnings for tuple here
var transformed = copyNode(n)
for i in 0..<n.len:
var a = n[i]
if a.kind == nkIdentDefs and a.len > 3 and a[^1].kind != nkEmpty:
for j in 0..<a.len-2:
var b = newNodeI(nkIdentDefs, a.info)
b.add a[j]
b.add a[^2]
b.add copyTree(a[^1])
transformed.add b
else:
transformed.add a
let n = transformed
for i in 0..<n.len:
var a = n[i]
if c.config.cmd == cmdIdeTools: suggestStmt(c, a)

View File

@@ -2,6 +2,7 @@ discard """
output: "44"
"""
# Test the new variable declaration syntax
import std/sequtils
var
x = 0
@@ -10,3 +11,9 @@ var
write(stdout, a)
writeLine(stdout, b) #OUT 44
proc p() = # bug #18104
var x, y = newSeqWith(10, newString(3))
discard (x, y)
p()