Closed ghost closed 4 months ago
import macros
macro test*(): untyped =
for i, x in @[1, 2, 3]:
if false:
# this quote do is mandatory, removing it fixes the problem
result.add quote do:
discard
else:
echo "hi"
# removing this continue fixes the problem too
continue
test()
A bit shorter example
git bisect
showed https://github.com/nim-lang/Nim/commit/c79df2fb6abb5663505de13c0ae5995d72ae71a8 as the first bad commit, and by the looks of it that commit might be related to the issue.
quote do
when defined case2c:
import macros
proc fn(): NimNode =
var z: NimNode
for i in 0..2:
if false:
add(result, z)
else:
discard
# either break, return, continue would give error
break
Error: expression ' discard break' is of type 'NimNode' and has to be used (or discarded); start of expression here:
no error here; this is strage.
when defined case3:
import macros
type Foo = object
proc fn(): seq[Foo] {.compileTime.} =
for i in 0..2:
if false:
add(result, Foo())
else:
discard
break
indeed i think this is the same issue as #19150.
if you discard result.add z
the error disappears:
import macros
proc fn(): NimNode =
var z: NimNode
for i in 0..2:
if false:
discard add(result, z)
else:
discard
# either break, return, continue would give error
break
This is not specific to NimNode.
proc add2(a: var seq[int], b: int): int =
a.add(b)
b
proc fn(): seq[int] =
for i in 0..2:
if false:
add2(result, i)
else:
discard
break
Same error with or without discardable
, saying expression ' discard break' is of type 'int' and has to be used (or discarded);
, which is the wrong error. There might be another open issue for this (I know there is one for try/finally).
This code is mostly self-explanatory, I really don't understand why that error happens :)
Example
Current Output
Expected Output
No error
Additional information
Confirmed that it worked in 1.0.6 via the playground.