Skip to content

Macro matching with anonymous variable fails #9570

Closed
@hmf

Description

@hmf

Minimized code

In a Macros.scala file we have

import scala.quoted._

object Macros {

  object HList {
    sealed trait HList
    case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList
    case object HNil extends HList

    private def sizeImpl(e: Expr[HList], n:Int)(using qctx:QuoteContext): Expr[Int] = {
      import qctx.tasty._
      e match {
        case '{HCons(_,$t)} =>
        //case '{HCons($a,$t)} =>
        //case '{HCons(($a,$b),$t)} =>
          sizeImpl(t,n+1)
        case '{HNil} => Expr(n)
      }
    }

    inline def size(inline expr: HList ): Int = {
      ${sizeImpl('expr,0)}
    }

  }
}

In a Main.scala file we have

object Main {

  def main(args: Array[String]): Unit = {
    import HList._

    val hl0n = size( HCons(("1",1), HCons(("2",2), HCons(("3",3),HNil))) )
    println(s"size(?) = $hl0n")
  }
}

Output

[error] -- Error: /IdeaProjects/snol/tutorial/src/ad/Main.scala:130:19 --------
[error] 130 |    val hl0n = size( HCons(("1",2), HCons(("2",2), HCons(("3",3),HNil))) )
[error]     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]     |      Exception occurred while executing macro expansion.
[error]     |      scala.MatchError: '{ ... } (of class scala.internal.quoted.Expr)
[error]     |
[error]     | This location contains code that was inlined from Main.scala:130
[error] one error found

Expectation

I expected a correct match. If we use:

        case '{HCons($a,$t)} =>

or

        case '{HCons(($a,$b),$t)} =>

matching works correctly.

Tested on 0.26.0-RC1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions