Skip to content

Adding a genSym-ed field with the same name as a pre-existing field causes redefinition error in an object definition #17851

Open
@alaviss

Description

@alaviss

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:

Nim/compiler/semtypes.nim

Lines 805 to 806 in 9e6f2d7

if containsOrIncl(check, f.name.id):
localError(c.config, info, "attempt to redefine: '" & f.name.s & "'")

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CPSbugs/pulls related to the cps projectMacros

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions