Skip to content

Quoted pattern matching does not work when quote is in a val. #16543

Open
@smarter

Description

@smarter

Compiler version

3.3.0-RC1-bin-20221215-ef653b6-NIGHTLY

Minimized code

After inlining, pat(3) is replaced by "ok" as expected in the following code:

import scala.quoted.*

inline def pat(inline e: Int): String = ${patImpl('e)}

private def patImpl(e: Expr[Int])(using Quotes): Expr[String] = {
  e match
    case '{ 3 } => '{"ok"}
    case _ => '{"other"}
}
def test = pat(3)

But if I put the quote in a pattern, then I get "other" instead:

private def patImpl(e: Expr[Int])(using Quotes): Expr[String] = {
  val Expr = '{ 3 }
  e match
    case Expr => '{"ok"}
    case _ => '{"other"}
}

Expectation

I think ideally both code snippets should behave the same. My usecase is that I'd like to check if some expression is already present in the input, and if not output what code should be generated (cf https://contributors.scala-lang.org/t/scala-3-macro-annotations-and-code-generation/6035), so I'd like to write the Expr once and use it both for pattern matching and as an output.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions