Skip to content

Quoted pattern matching of poly functions does not work with quoted code #23589

@jchyb

Description

@jchyb

Compiler version

main, any after 3.6.0

Minimized code

@scala.annotation.experimental @main def main() =
  testMacro([B] => (a : B, b : B) => (a, b))
import scala.quoted.*
import scala.language.experimental.quotedPatternsWithPolymorphicFunctions

inline def testMacro(inline body: Any) = ${test('body)}
def test(outsideBody: Expr[Any])(using Quotes): Expr[Unit] =
  val insideBody = '{[B] => (a : B, b : B) => (a, b)}
  insideBody match // outsideBody works
    case '{ [A] => (x : A, y : A) => $b[A](x, y): (A, A) } => println("ok")
  '{()}

Output

-- Error: a2.scala:3:11 --------------------------------------------------------
3 |  testMacro([B] => (a : B, b : B) => (a, b))
  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |Exception occurred while executing macro expansion.
  |scala.MatchError: scala.quoted.runtime.impl.ExprImpl@14ba7f15 (of class scala.quoted.runtime.impl.ExprImpl)
  |     at a$package$.test(a.scala:10)
  |
  |-----------------------------------------------------------------------------
  |Inline stack trace
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |This location contains code that was inlined from a.scala:4
4 |inline def testMacro(inline body: Any) = ${test('body)}
  |                                         ^^^^^^^^^^^^^^
   -----------------------------------------------------------------------------

Expectation

Should compile, as both insideBody and outsideBody have the exact same shape when printed out:

{
  def $anonfun[B](a: B, b: B): scala.Tuple2[B, B] = scala.Tuple2.apply[B, B](a, b)
  $anonfun
}

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