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 message with quote do + template injection + VM bool/enum #9625

Closed
mratsim opened this issue Nov 5, 2018 · 2 comments
Closed

Comments

@mratsim
Copy link
Collaborator

mratsim commented Nov 5, 2018

This is a followup to #7375 and #9520:

Summary of both issues: bool and enum in the VM are changed to int when passed via quote do or to other macros.

It also triggers a very misleading error if they are passed to a template that uses identifier injection.
Instead of saying that one of the parameter is of incorrect type, for example got static int instead of static bool we get undeclared identifier.

Due to the many other early symbol resolution issues with macro this wrong message led me to try lots of unrelated changes that didn't solve the actual issue.

Test case:

import macros

template foo(index: untyped, condition: static bool): untyped =
  for `index`{.inject.} in 0 ..< 1:
    if condition:
      echo "Success"

macro fails(): untyped =
  let macro_idx = newIdentNode"my_index"
  let success = true

  result = quote do:
    foo(`macro_idx`, `success`)

macro works(): untyped =
  let macro_idx = newIdentNode"my_index"
  let success = true

  let success_node = newLit success

  result = quote do:
    foo(`macro_idx`, `success_node`)

# fails() # Error: undeclared identifier: 'my_index'
works()

##########################################
# Trying without static

template foo2(index: untyped, condition: bool): untyped =
  for `index`{.inject.} in 0 ..< 1:
    if condition:
      echo "Success"

macro fails2(): untyped =
  let macro_idx = newIdentNode"my_index"
  let success = true

  result = quote do:
    foo2(`macro_idx`, `success`)

# fails2() # Error: undeclared identifier: 'my_index'

##########################################

macro works3(): untyped =
  let macro_idx = newIdentNode"my_index"
  let success = true

  result = quote do:
    foo2(`macro_idx`, bool(`success`))

works3()
@krux02
Copy link
Contributor

krux02 commented Nov 5, 2018

binding to symbols with backticks that are not of type NimNode is not supported, and it can't reliably be supported. It is a bug though the error message is not correct.

Use let success = newLit(true).

@krux02
Copy link
Contributor

krux02 commented Nov 5, 2018

this is a duplicate of #9607

@krux02 krux02 closed this as completed Nov 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants