nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
280 stars 39 forks source link

macro generated objects with field name containing '`' cannot be accessed #1379

Closed alaviss closed 4 months ago

alaviss commented 4 months ago

Example

import macros

macro emit(): untyped =
  result = newStmtList()

  let field = ident("x`")
  let obj = nnkObjectTy.newTree(newEmptyNode(), newEmptyNode()):
    nnkRecList.newTree:
      newIdentDefs(field, ident"int")

  let env = ident"env"
  result.add:
    nnkTypeSection.newTree:
      nnkTypeDef.newTree(env, newEmptyNode(), obj)

  result.add(
    newLetStmt(ident"e", newCall(env)),
    nnkDiscardStmt.newTree(nnkDotExpr.newTree(ident"e", field))
  )

emit()

Actual Output

test.nim(6, 20) Error: undeclared field: 'x`' for type test.env [type declared in test.nim(11, 18)]

Expected Output

This should compile normally

Additional Information

Generated code:

type
  env = object
    x`: int

let e = env()
discard e.x`
AST form: ``` StmtList TypeSection TypeDef Ident "env" Empty ObjectTy Empty Empty RecList IdentDefs Ident "x`" Ident "int" Empty LetSection IdentDefs Ident "e" Empty Call Ident "env" DiscardStmt DotExpr Ident "e" Ident "x`" ```
zerbina commented 4 months ago

Fixed by #1389.