diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-02-13 15:10:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-13 08:10:28 +0100 |
commit | 1e9a3c438bed693bc95d5b1a9501e1386163e1cc (patch) | |
tree | 5a76af89ffb13ba120698151b3026b9a1aef1f7e | |
parent | a8c168c1688f64e8bd3acba9afee9d02bb03c649 (diff) | |
download | Nim-1e9a3c438bed693bc95d5b1a9501e1386163e1cc.tar.gz |
fixes #18104; tranform one liner var decl before templates expansion (#23294)
fixes #18104
-rw-r--r-- | compiler/semstmts.nim | 18 | ||||
-rw-r--r-- | tests/varstmt/tvardecl.nim | 7 |
2 files changed, 25 insertions, 0 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 59dc94f36..14b7fc931 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -669,6 +669,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) diff --git a/tests/varstmt/tvardecl.nim b/tests/varstmt/tvardecl.nim index 37bc4bad7..d5ccfafb7 100644 --- a/tests/varstmt/tvardecl.nim +++ b/tests/varstmt/tvardecl.nim @@ -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() |