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 staging level is inferred when a variable is used in a HOAS pattern over splice/quote redex #18308

Open
zeptometer opened this issue Jul 29, 2023 · 1 comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@zeptometer
Copy link
Contributor

zeptometer commented Jul 29, 2023

Compiler version

3.3.0

Minimized code

import scala.quoted.*

def testExprImpl(body: Expr[Any])(using Quotes): Expr[String] =
  body match
    case '{ (a:Int) => (${'{1 + ($b(a) : Int)}} : Int) } =>
      '{ $b(1).toString }
    case _ => Expr("not matched"
)

Output

-- Warning: Macro.scala:6:24 ---------------------------------------------------
6 |    case '{ (a:Int) => (${'{1 + ($b(a) : Int)}} : Int) } =>
  |                        ^^^^^^^^^^^^^^^^^^^^^^^
  |Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.
-- Error: Macro.scala:6:36 -----------------------------------------------------
6 |    case '{ (a:Int) => (${'{1 + ($b(a) : Int)}} : Int) } =>
  |                                    ^
  |                           access to parameter a from wrong staging level:
  |                            - the definition is at level 0,
  |                            - but the access is at level 1.
1 warning found
1 error found

Expectation

  • If the working message is correct, it should compile as well as case '{ (a:Int) => 1 + ($b(a) : Int) } =>.
  • or, considering the behavior of (hoas) splice pattern, it is also reasonable to to conclude that $'{...} is not equivalent to ... as a pattern. In this case, we want to change the warning message.
    • The current error message informal about staging level, which seems not right. Instead, it should show an error message like "a is undefined in this scope".
@zeptometer zeptometer added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 29, 2023
@nicolasstucki nicolasstucki added the area:metaprogramming:quotes Issues related to quotes and splices label Jul 31, 2023
@nicolasstucki
Copy link
Contributor

This should be disallowed. The nested pattern cannot user outer variables in its hoas captures. We need give a better error message.

@nicolasstucki nicolasstucki added itype:enhancement itype:bug and removed stat:needs triage Every issue needs to have an "area" and "itype" label itype:bug itype:enhancement labels Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants