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

Strange error message for template when a type mismatch occurs #23265

Closed
dwhall opened this issue Jan 29, 2024 · 1 comment · Fixed by #23984
Closed

Strange error message for template when a type mismatch occurs #23265

dwhall opened this issue Jan 29, 2024 · 1 comment · Fixed by #23984

Comments

@dwhall
Copy link

dwhall commented Jan 29, 2024

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.

@metagn
Copy link
Collaborator

metagn commented Jan 30, 2024

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 added a commit to metagn/Nim that referenced this issue Aug 20, 2024
@Araq Araq closed this as completed in 34719ca Aug 20, 2024
narimiran pushed a commit that referenced this issue Sep 13, 2024
…or (#23984)

fixes #8697, fixes #9620, fixes #23265

When matching a `template` with an `untyped` argument fails because of a
mismatching typed argument, `presentFailedCandidates` tries to sem every
single argument to show their types, but trying to type the `untyped`
argument can fail if it's supposed to use an injected symbol, so we get
an unrelated error message like "undeclared identifier".

Instead we use `tryExpr` as the comment suggests, setting the type to
`untyped` if it fails to compile. We could also maybe check if an
`untyped` argument is expected in its place and not try to compile the
expression if it is but this would require a bit of reorganizing the
code here and IMO it's better to have the information of what type it
would be if it can be typed.

(cherry picked from commit 34719ca)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants