scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

TypeTest of TypeBlock of Quotes API matches every Block

Open felher opened this issue 4 months ago • 0 comments

Compiler version

3.5.1 (and way before, as far as I can see)

Minimized code

Test.scala

object Test:
  // give a Block(...) to the macro
  Macro.impl:
    val a = 3
    a

Macro.scala

import quoted.*

object Macro:
  inline def impl(inline expr: Any): Any =
    ${implImpl('expr)}

  def implImpl(expr: Expr[Any])(using q: Quotes): Expr[Any] =
    import q.reflect.*
    expr.asTerm.asInstanceOf[Inlined].body match
      // this should not fail with a MatchError
      // but it does because `TypeBlock(_, _)` matches the block
      // of the `expr`, but of course `val a = ...` is not a `TypeDef`,
      // which the `unapply` of `TypeBlock` assumes.
      case TypeBlock(_, _) => '{ "TypeBlock" }
      case _ => '{ "Nothing" }

Output

[error] -- Error: /home/felher/tmp/macrotest/src/main/scala/Main.scala:3:4 -------------
[error] 2 |  Macro.impl:
[error] 3 |    val a = 3
[error]   |  ^
[error]   |Exception occurred while executing macro expansion.
[error]   |scala.MatchError: ValDef(a,TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Int)],Literal(Constant(3))) (of class dotty.tools.dotc.ast.Trees$ValDef)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl.scala$quoted$runtime$impl$QuotesImpl$reflect$TypeBlockMethods$$$_$aliases$$anonfun$1(QuotesImpl.scala:1431)
[error]   |	at scala.collection.immutable.List.map(List.scala:247)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeBlockMethods$.aliases(QuotesImpl.scala:1431)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeBlock$.unapply(QuotesImpl.scala:1426)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeBlock$.unapply(QuotesImpl.scala:1425)
[error]   |	at Macro$.implImpl(Impl.scala:10)
[error]   |
[error] 4 |    a 
[error]   |-----------------------------------------------------------------------------
[error]   |Inline stack trace
[error]   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]   |This location contains code that was inlined from Impl.scala:5
[error] 5 |    ${implImpl('expr)}
[error]   |    ^^^^^^^^^^^^^^^^^^
[error]    -----------------------------------------------------------------------------
[error] one error found

Expectation

There should be no match error here. It should just compile fine.

PR

PR attempt for a fix here: #21722

felher avatar Oct 07 '24 19:10 felher