Closed jesvedberg closed 1 year ago
"Simpler" case:
import macros
macro foo() =
let symName = genSym(nskLet, "tmp")
result = newLetStmt(symName, newLit(123))
template myTemplate(x: typed): untyped =
x
x
proc main =
myTemplate(foo())
main()
Workaround is wrap result of macro with block
, which #21761 does for strformat and should close this issue.
The general case I posted above is still an issue but it's a separate priority and might not have an easy fix (again a workaround exists).
What happened?
If I pass a string generated using the
fmt""
macro fromstrformat
to a template that accepts a string, and then use that string twice in the template, I get a C compiler error. Here's a minimal example:When trying to compile this, I get an error message from the C compiler step shown below. If I don't use the
fmt""
macro, this compiles as expected, and if I remove the themain()
function and just callmyTemplate
globally it also compiles. Furthermore, the issue remains if I change the template argument type totyped
, but it disappears if I change it tountyped
.Nim Version
Tested with 1.6.6 and 1.2.0 with the same result.
Current Standard Output Logs
Expected Standard Output Logs
Possible Solution
I have no solution, but the issue can be worked around by either using specifying the template argument as
untyped
or by using a temporary variable in the template:Additional Information
As far as I can tell, this is a different issue than the limitation with
strformat
discussed here: https://nim-lang.org/docs/strformat.html#limitations