nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

Confusing error messages with macro genSym-ed types overload mismatch #18340

Open alaviss opened 3 years ago

alaviss commented 3 years ago

Example

import macros

macro foo(): untyped =
  let env = nskType.genSym"Env"
  result = quote:
    type `env` = ref object of RootObj

    proc foobar(e: `env`) = discard

macro bar(): untyped =
  let env = nskType.genSym"Env"
  result = quote:
    type `env` = ref object of RootObj

    let e = `env`()
    foobar(e)

foo()
bar()

Current Output

test.nim(12, 12) Error: type mismatch: got <test.Env>
but expected one of:
proc foobar(e`gensym0: Env_419430421)
  first type mismatch at position: 1
  required type for e`gensym0: Env
  but expression 'e`gensym1' is of type: Env

expression: foobar(e`gensym1)

Expected Output

test.nim(12, 12) Error: type mismatch: got <test.Env>
but expected one of:
proc foobar(e`gensym0: Env_419430421)
  first type mismatch at position: 1
  required type for e`gensym0: Env_419430421
  but expression 'e`gensym1' is of type: Env_<the other number>

expression: foobar(e`gensym1)

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-06-24
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 0f91b67f5c15328330f74a8769aed9961940aab2
active boot switches: -d:release -d:nimUseLinenoise
hkl615 commented 3 years ago

import macros

macro foo(): untyped = let env = nskType.genSym"Env" result = quote: type env = ref object of RootObj

proc foobar(e: env) = discard

macro bar(): untyped = let env = nskType.genSym"Env" result = quote: type env = ref object of RootObj

let e = `env`()
foobar(e)

foo() bar() #