-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
non-hygenic behavior for gensymmed name in template #24260
Comments
Is this a regression? |
not sure, I haven't tested earlier versions. the code is "recent" in the sense that it wasn't tested with a conflicting name in the same scope. it was discovered by accident due to random unexpected values for a variable named |
Templates don't work with macro-generated gensym names at all, because import std/macros
macro fooTempl(name: untyped): untyped =
result = newProc(
name = genSym(nskTemplate, $name),
params = [ident"untyped"],
body = newLit(123),
procType = nnkTemplateDef,
)
fooTempl(bar)
echo bar() # 123
macro fooProc(name: untyped): untyped =
result = newProc(
name = genSym(nskProc, $name),
params = [ident"auto"],
body = newLit(123),
procType = nnkProcDef,
)
fooProc(bar2)
echo bar2() # undeclared identifier: bar2 The example in the issue still wouldn't work though because the |
Not sure I follow .. in the example |
The compiler doesn't consider when template names are marked If this was fixed, i.e. templates did gensym their names like other routines, the generated sym would be inaccessible from outside of the macro, so the macro wouldn't work as expected. So the macro actually needs to not gensym the name of the template, i.e. it depends on this bug. This would mean the code in the issue will still fail though, because of the limitation that the template macro data*(bParam: ArrayBuf): openArray =
let b = ident repr genSym(nskTemplate, "b")
result = quote do:
`bParam`.evalOnceAs(`b`)
`b`.buf.toOpenArray(0, `b`.n - 1) |
I look at it in a slightly different way: If |
there's a similar macro in stdlib btw: Nim/lib/pure/collections/sequtils.nim Line 112 in d72b848
it's also buggy: import std/sequtils
var iter2 = 10
iterator xxx(): int {.closure.} =
yield 5
proc f() =
let x = xxx
var v = toSeq(x)
echo iter2, " ", v
f() prints |
Description
the name b is local to the template and not dirty - it should neither conflict with the other b nor leak out of the template
A variation, when
b
is in a different scope:Here,
(buf: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], n: 0)
gets printed.Nim Version
2.0.8
Current Output
Expected Output
No response
Known Workarounds
No response
Additional Information
No response
The text was updated successfully, but these errors were encountered: