Skip to content

Experimental . operator confuses the dot call syntax notation #7777

@PMunch

Description

@PMunch

I've been struggling with a hard to track down bug in my protobuf library for quite a while now. I couldn't for the life of me figure out what the error message I got meant. The problem was a simple missing export marker on a template, but the error pointed to something completely different. The below example shows what I'm talking about:

{.experimental.}
import macros

type
  TestType = ref object
    private_field: string

# This is the analogue to my procedure that was missing an export marker
#template getField(obj, field: untyped): untyped =
#  obj.field

# This experimental will match all unmatched field access to this macro before failing.
macro `.`(obj: TestType, field: untyped): untyped =
  # This is probably not strictly necessary for this example, but it shows what I'm
  # trying to achieve with the `.` macro.
  let private = newIdentNode("private_" & $field)
  # This is where the problem lies. The first commented statement will create error 1,
  # while the second statement would create error 2 below.
  result = quote do:
    #getField(`obj`, `private`)
    #`obj`.getField(`private`)

var tt = new TestType
tt.private_field = "Hello world"

echo tt.field

Error 1 (this is what we would normally see):
Error: undeclared identifier: 'getField'
Error 2 (this is what confused me):
Error: undeclared identifier: 'private_field'
If we remove the comments on the getField proc both examples work

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions