Skip to content

Quote pattern not matching on *: when elements are tuples #20113

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

Open
hmf opened this issue Apr 6, 2024 · 0 comments
Open

Quote pattern not matching on *: when elements are tuples #20113

hmf opened this issue Apr 6, 2024 · 0 comments
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@hmf
Copy link

hmf commented Apr 6, 2024

Compiler version

3.4.2-RC1

Minimized code

The code below tries to match a Tuple of Tuple2. The only match that works is:

 '{ (1,"1") *: EmptyTuple }

matched with

case '{ type t1 <: Tuple; type t2 <: Tuple; $hd: t2 *: EmptyTuple } 

Test code:

  transparent inline def filter(inline from: Tuple): Any = 
    ${ filterImpl('from) }

  // see https://github.com/scala/scala3/issues/19941
  def filterImpl[T <: Tuple](from: Expr[T])(using Quotes): Expr[?] =
    // '{ (1,"1") *: EmptyTuple } match
    '{ (1,"1") *: (2, "2") *: EmptyTuple } match
      // Ok: '{ (1,"1") *: EmptyTuple }
      // case '{ type t1 <: Tuple; type t2 <: Tuple; $hd: t2 *: EmptyTuple } =>
      // fails match with: '{ (1,"1") *: EmptyTuple }
      // case '{ type t1 <: Tuple; type t2 <: Tuple; ($hd: t2) *: EmptyTuple } =>
      // Compile fails: Found:    EmptyTuple.type ; Required: t1
      // case '{ type t1 <: Tuple; type t2 <: Tuple; ($hd: t2) *: (EmptyTuple: t1) } =>
      // fails match: '{ (1,"1") *: EmptyTuple }
      // fails match: '{ (1,"1") *: (2, "2") *: EmptyTuple }
      // case '{ type t1 <: Tuple; type t2 <: Tuple; ($hd: t2) *: ($tl: t1) } =>
      // fails match with: '{ (1,"1") *: (2, "2") *: EmptyTuple }
      // case '{ type t1 <: Tuple; type t2 <: Tuple2[?,?]; ($hd: t2) *: ($tl: t1) } =>
      case '{ type t1 <: Tuple; type t2 <: Tuple2[?,?]; ($hd:t2).*: ($tl:t1) } =>
        println("matched")
        Expr(true)
      case x => 
        throw new MatchError(x.show)

Output

[error] 17 |  val r1 = Tuples.filter(List("v1", "v2"), c1)
[error]    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |Exception occurred while executing macro expansion.
[error]    |scala.MatchError: {
[error]    |  val x$2: scala.Tuple2[scala.Int, java.lang.String] = scala.Tuple2.apply[scala.Int, java.lang.String](1, "1")
[error]    |
[error]    |  {
[error]    |    val x$1: scala.Tuple2[scala.Int, java.lang.String] = scala.Tuple2.apply[scala.Int, java.lang.String](2, "2")
[error]    |    scala.Tuple$package.EmptyTuple.*:[scala.Tuple2[scala.Int, java.lang.String], scala.Tuple$package.EmptyTuple.type](x$1)
[error]    |  }.*:[scala.Tuple2[scala.Int, java.lang.String], scala.*:[scala.Tuple2[scala.Int, java.lang.String], scala.Tuple$package.EmptyTuple]](x$2)
[error]    |} (of class java.lang.String)
[error]    |    at examples.project.Tuples$.filterImpl(MProject.scala:74)
[error]    |
[error]    |----------------------------------------------------------------------------
[error]    |Inline stack trace
[error]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]    |This location contains code that was inlined from MProject.scala:51
[error] 51 |    ${ filterImpl('from) }
[error]    |    ^^^^^^^^^^^^^^^^^^^^^^
[error]     ----------------------------------------------------------------------------
[error] one error found
1 targets failed

Expectation

I would expect to be able to match on any type of element, not just singletons. I suspect the parenthesis may be the issue. Note that this is a follow-up of #19947.

@hmf hmf added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 6, 2024
@Gedochao Gedochao added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 8, 2024
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