fixes transf cannot handle bare sym for nim ic (#25664)

```nim
template compute(body: untyped): int =
  block:
    body
let x = compute:
  var sum = 0
  for i in 1..10: sum += i
  sum
echo x
```

supersedes https://github.com/nim-lang/Nim/pull/25653

which in
02893e2f4c

```nim
of nkSym:
    genSingleVar(p, it.sym, newSymNode(it.sym), it.sym.astdef)
```

A new branch for `nkSym` is added, though more changes might be needed
if `nkSym` is handled specifically
This commit is contained in:
ringabout
2026-03-31 15:49:55 +08:00
committed by GitHub
parent 7a82c5920c
commit 8076fb40b8
2 changed files with 19 additions and 0 deletions

View File

@@ -1190,6 +1190,13 @@ proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
# no need to transform type sections:
return n
of nkVarSection, nkLetSection:
# NIF loads let/var sections with bare nkSym children instead of nkIdentDefs.
# Expand them so transformSons reaches the value expression (e.g. for-loop).
for i in 0 ..< n.len:
if n[i].kind == nkSym:
let impl = n[i].sym.ast # triggers lazy load if Partial
if impl != nil and impl.kind == nkIdentDefs:
n[i] = impl
if c.inlining > 0:
# we need to copy the variables for multiple yield statements:
result = transformVarSection(c, n)

View File

@@ -6,6 +6,7 @@ discard """
2
1.0
2.0
55
'''
"""
@@ -67,3 +68,14 @@ m[0, 0] = 1.0
m[1, 1] = 2.0
echo m[0, 0]
echo m[1, 1]
template compute(body: untyped): int =
block:
body
let x = compute:
var sum = 0
for i in 1..10: sum += i
sum
echo x