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

wrong error messages (undeclared identifier) on type mismatch of another error when untyped followed by typed params #9620

Closed
mratsim opened this issue Nov 4, 2018 · 1 comment · Fixed by #23984

Comments

@mratsim
Copy link
Collaborator

mratsim commented Nov 4, 2018

In the same vein as #9414, using a parameter constraint like {lvalue} does not work with identifier injection.

Test case:

template forLoop*(index: untyped, length: int{lvalue}, body: untyped) =
  for `index`{.inject.} in 0 ..< length:
      body

var x = newSeq[int](10)

forLoop(i, x.len):
  x[i] = i

Error: undeclared identifier: 'i'

Removing the lvalue constraint makes the code compile.

@timotheecour
Copy link
Member

timotheecour commented Nov 13, 2020

x.len is not an lvalue so the code isn't valid; the problem here is that the error message is wrong.

reduced:

when true:
  # Error: undeclared identifier: 'i'
  template fn(index: untyped, length: int{lvalue}, body: untyped) = discard
  var x = @[1]
  fn(i, len(x)): discard

further reduced, which shows title is wrong:

when true:
  template fn(a: untyped, b: int) = discard
  fn(nonexistant, 1.0)

this is a consequence of current sigmatch implementation for untyped params, which trigger semcheck for untyped params when not all arguments are untyped (IIUC this used to be what immediate templates were).

see also: https://nim-lang.github.io/Nim/manual.html#overloading-resolution-lazy-type-resolution-for-untyped
and https://nim-lang.github.io/Nim/manual.html#templates-typed-vs-untyped-parameters

this is related to #14827 but definitely different

@timotheecour timotheecour changed the title Parameter constraint + identifier injection leads to undeclared identifier wrong error messages on sigmatch error when untyped followed by typed params Nov 13, 2020
@timotheecour timotheecour changed the title wrong error messages on sigmatch error when untyped followed by typed params wrong error messages (undeclared identifier) on type mismatch of another error when untyped followed by typed params Nov 13, 2020
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