Open
Description
Example
import std/macros
macro typeMacro(td: untyped): untyped =
expectKind td, nnkTypeDef
expectKind td[2], nnkObjectTy
td[2][2].add newIdentDefs(genSym(nskField, "foo"), ident"string", newEmptyNode())
result = td
echo repr result
type
O {.typeMacro.} = object
foo: int
Current Output
O {..} = object
foo: int
foo_369098762: string
test.nim(6, 35) Error: attempt to redefine: 'foo'
Expected Output
Compiles successfully.
Possible Solution
The record field redefinition check within the compiler matches against the node name:
Lines 805 to 806 in 9e6f2d7
This doesn't work with genSym-ed symbols.
Additional Information
- Found while developing cps.
- From what I can infer from the code, this issue would extend to proc types and tuples.
$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-04-25
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 22e06ee95aeb32929a5b912096dfaa662165c162
active boot switches: -d:release -d:nimUseLinenoise -d:nativeStackTrace