Skip to content

Unhelpful "Tree had an unexpected owner" error message #16363

Closed
@kubukoz

Description

@kubukoz

Compiler version

3.2.1

Minimized code

//> using scala "3.2.1"
//> using option "-Ycheck:all"
//> using option "-Xcheck-macros"
import scala.annotation.experimental

import scala.quoted.*

object testmacros {
  inline def example = ${ exampleImpl }

  @experimental
  def exampleImpl(using Quotes): Expr[Any] = {
    import quotes.reflect._

    val methodType = MethodType(List("arg"))(_ => List(TypeRepr.of[String]), _ => TypeRepr.of[Unit])
    def method(
      owner: Symbol
    ) = Symbol.newMethod(
      owner,
      "test",
      methodType,
    )

    val clazz = Symbol.newClass(
      Symbol.spliceOwner,
      "$anon",
      List(TypeRepr.of[Object]),
      owner => method(owner) :: Nil,
      None,
    )

    ClassDef(
      clazz,
      Nil,
      List(
        DefDef(
          method(clazz),
          argss =>
            Some(
              Lambda(method(clazz), methodType, (ls, _) => '{ ??? }.asTerm)
            ),
        )
      ),
    ).asExpr
  }

}

//in another file

val f = testmacros.example

Output

...
[error] Expected: method test (demo$._$_$$anon.test)
[error] But was: method test (demo$._$_$$anon.test)
...
Full output
[error] ./main.scala:46:3: Exception occurred while executing macro expansion.
[error] java.lang.AssertionError: assertion failed: Tree had an unexpected owner for method $anonfun
[error] Expected: method test (demo$._$_$$anon.test)
[error] But was: method test (demo$._$_$$anon.test)
[error] 
[error] 
[error] The code of the definition of method $anonfun is
[error] def $anonfun(arg: java.lang.String): scala.Unit = scala.Predef.???
[error] 
[error] which was found in the code
[error] ((arg: java.lang.String) => scala.Predef.???)
[error] 
[error] which has the AST representation
[error] Block(List(DefDef("$anonfun", List(TermParamClause(List(ValDef("arg", Inferred(), None)))), Inferred(), Some(Inlined(Some(TypeIdent("macros$")), Nil, Ident("???"))))), Closure(Ident("$anonfun"), None))
[error] 
[error] 
[error] 
[error] Tip: The owner of a tree can be changed using method `Tree.changeOwner`.
[error] Tip: The default owner of definitions created in quotes can be changed using method `Symbol.asQuotes`.
[error] 
[error]         at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[error]         at scala.quoted.runtime.impl.QuotesImpl$$anon$9.traverse(QuotesImpl.scala:2949)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.apply(Trees.scala:1657)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.apply(Trees.scala:1657)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeAccumulator.fold$1(Trees.scala:1529)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeAccumulator.apply(Trees.scala:1531)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeAccumulator.foldOver(Trees.scala:1562)
[error]         at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.traverseChildren(Trees.scala:1658)
[error]         at scala.quoted.runtime.impl.QuotesImpl$$anon$9.traverse(QuotesImpl.scala:2956)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$.xCheckMacroOwners(QuotesImpl.scala:2957)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$xCheckMacroedOwners(QuotesImpl.scala:2915)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$DefDef$.apply$$anonfun$3$$anonfun$1(QuotesImpl.scala:272)
[error]         at dotty.tools.dotc.ast.tpd$.DefDef(tpd.scala:289)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$DefDef$.apply$$anonfun$3(QuotesImpl.scala:273)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$withDefaultPos(QuotesImpl.scala:2906)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$DefDef$.apply(QuotesImpl.scala:273)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$DefDef$.apply(QuotesImpl.scala:269)
[error]         at macros$.exampleImpl(macros.scala:182)
[error] 
[error]   macros.example
[error]   ^^^^^^^^^^^^^^
Error compiling project (Scala 3.2.1, JVM)

Expectation

Something that distinguishes the actual owner from the expected one.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions