Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Strange error message for template when a type mismatch occurs

Open dwhall opened this issue 1 year ago • 1 comments

Description

On a 64-bit platform, this code

template declareFoo(fooName: untyped, value: uint) =
  const `fooName Value`* {.inject.} = value

declareFoo(FOO, 0x70000000)
declareFoo(BAR, 0x80000000)

produces the following error:

/usercode/in.nim(5, 12) Error: undeclared identifier: 'BAR'

No error is produced as long as the value argument is less than 0x8000_0000. A workaround has been provided by @Araq in the forum.

Nim Version

Reproducible on the playground v2.0.2 and my desktop:

Nim Compiler Version 2.0.0 [MacOSX: arm64]
Compiled at 2023-08-01
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release -d:nimUseLinenoise

Current Output

nim c -r "/hide/my/path/file.nim"
Hint: used config file '/opt/homebrew/Cellar/nim/2.0.0_1/nim/config/nim.cfg' [Conf]
Hint: used config file '/opt/homebrew/Cellar/nim/2.0.0_1/nim/config/config.nims' [Conf]
......................................................................
/hide/my/path/file.nim(5, 12) Error: undeclared identifier: 'BAR'

Expected Output

I expect to be able to use the constants `FOOValue` and `BARValue`.

Possible Solution

No response

Additional Information

I understand I must apply the 'u32 type annotation to satisfy the compiler and {.inject.} to gain access to the constants. However, it is the error message that seems misleading.

dwhall avatar Jan 29 '24 15:01 dwhall

Side effect of

proc foo(value: uint) =
  echo value

foo(0x80000000)
(4, 4) Error: type mismatch
Expression: foo(0x0000000080000000'i64)
  [1] 0x0000000080000000'i64: int64

Expected one of (first mismatch at [position]):
[1] proc foo(value: uint)

metagn avatar Jan 30 '24 05:01 metagn