Open
Description
Compiler version
3.1.1-RC1-bin-SNAPSHOT. (master at May. 9.2023)
Minimized code
Macro.scala:
package x
import scala.quoted.*
import scala.collection.*
object Macro {
def myIndexWhere[A](arrOps: ArrayOps[A])(p: A=>Boolean, from:Int):Int =
???
inline def changeIndexWhere[A](inline expr:A):A = ${
changeIndexWhereImpl('expr)
}
def changeIndexWhereImpl[A:Type](expr:Expr[A])(using Quotes):Expr[A] = {
import quotes.reflect.*
val r0 = expr.asTerm
val checker = new TreeMap() {}
val r = checker.transformTerm(r0)(Symbol.spliceOwner)
r.asExprOf[A]
}
}
X.scala:
package x
object X {
def complileMe:Int = {
Macro.changeIndexWhere{
val arr = Array(1,2,3)
val result = arr.indexWhere(_==2)
result
}
}
}
Output
When compiling with -Xcheck-macros
info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 2 Scala sources to /Users/rssh/tests/dotty/fail-defparam/target/scala-3.3.1-RC1-bin-SNAPSHOT/classes ...
[error] -- Error: /Users/rssh/tests/dotty/fail-defparam/src/main/scala/x/X.scala:8:26 --
[error] 8 | Macro.changeIndexWhere{
[error] | ^
[error] |Malformed tree was found while expanding macro with -Xcheck-macros.
[error] | |The tree does not conform to the compiler's tree invariants.
[error] | |
[error] | |Macro was:
[error] | |scala.quoted.runtime.Expr.splice[scala.Int](((evidence$2: scala.quoted.Quotes) ?=> x.Macro.changeIndexWhereImpl[scala.Int](scala.quoted.runtime.Expr.quote[scala.Int]({
[error] | val arr: scala.Array[scala.Int] = scala.Array.apply(1, 2, 3)
[error] | val result: scala.Int = {
[error] | val $2$: scala.collection.ArrayOps[scala.Int] = scala.Predef.intArrayOps(arr)
[error] | $2$.indexWhere(((_$2: scala.Int) => _$2.==(2)), $2$.indexWhere$default$2)
[error] | }
[error] |
[error] | (result: scala.Int)
[error] |}).apply(using evidence$2))(scala.quoted.Type.of[scala.Int](evidence$2), evidence$2)))
[error] | |
[error] | |The macro returned:
[error] | |{
[error] | val arr: scala.Array[scala.Int] = scala.Array.apply(1, 2, 3)
[error] | val result: scala.Int = {
[error] | val $2$: scala.collection.ArrayOps[scala.Int] = scala.Predef.intArrayOps(arr)
[error] | $2$.indexWhere(((_$2: scala.Int) => _$2.==(2)), $2$.indexWhere$default$2)
[error] | }
[error] |
[error] | (result: scala.Int)
[error] |}
[error] | |
[error] | |Error:
[error] | |assertion failed: symbols differ for $2$.indexWhere$default$2
[error] |was : method indexWhere$default$2
[error] |alternatives by type: of types
[error] |qualifier type : ($2$ : scala.collection.ArrayOps[Int])
[error] |tree type : ($2$.indexWhere$default$2 : => Int @uncheckedVariance) of class class dotty.tools.dotc.core.Types$CachedTermRe
[error] | |
[error] |stacktrace available when compiling with `-Ydebug`
[error] | |
[error] 9 | val arr = Array(1,2,3)
[error] 10 | val result = arr.indexWhere(_==2)
[error] 11 | result
[error] 12 | }
[error] |----------------------------------------------------------------------------
[error] |Inline stack trace
[error] |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error] |This location contains code that was inlined from Macro.scala:12
[error] 12 | inline def changeIndexWhere[A](inline expr:A):A = ${
[error] | ^
[error] 13 | changeIndexWhereImpl('expr)
[error] 14 | }
[error] ----------------------------------------------------------------------------
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 5 s, completed 9 May 2023, 18:12:50
Expectation
It should be compiled.
After a short investigation, I have discovered that in TreeChecker, we seek for existence of SimpleName for indexWhere$default$2
when decl.members
of the appropriate symbol contains DerivedName