Skip to content
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

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

Closed
alaviss opened this issue Jul 11, 2024 · 1 comment · Fixed by #1389
Closed

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

alaviss opened this issue Jul 11, 2024 · 1 comment · Fixed by #1389
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@alaviss
Copy link
Contributor

alaviss commented Jul 11, 2024

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

  • This breaks CPS when dealing with AST originating from a template, as they contains `gensym in their repr.
  • It seems that the ` character is key to triggering this bug. Other special characters does not seem to cause any problems.
  • Bisect found fix(sem): issues with method-call syntax in templates #1298 to be the culprit.

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`"
@alaviss alaviss added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Jul 11, 2024
@alaviss alaviss changed the title macro generated type with field containing '`' cannot be accessed macro generated objects with field name containing '`' cannot be accessed Jul 11, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 27, 2024
## Summary

Fix a regression where field names with backticks in them couldn't be
accessed with a field access.

Fixes #1379.

## Details

* identifier nodes resulting from gensyms are now tagged with a new
  node flag (`nfWasGensym`)
* `originalName` only strips the `gensym` suffix from identifiers
  marked with the flag
* the node flag is persistent, so that it stays on the node across tree
  copies

Looking for the full "`gensym" suffix wouldn't work, because it would
also trigger the stripping for user-created names containing the
suffix.
@zerbina
Copy link
Collaborator

zerbina commented Jul 27, 2024

Fixed by #1389.

@zerbina zerbina closed this as completed Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants