scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Trees quoted macros doesn't resolve the constructor rhs

Open cchantep opened this issue 3 years ago • 1 comments

Compiler version

3.2.1-RC1-bin-20220705-9bb3108-NIGHTLY-git-9bb3108

Minimized code

package test

class Foo(val name: String)

object Bar extends Foo("bar")

object Macros {
  import scala.quoted.{Expr, Quotes, Type}

  inline def show[A]: String = ${ showImpl[A] }

  def showImpl[A](using tpe: Type[A], q: Quotes): Expr[String] = {
    import q.reflect.*

    val repr = TypeRepr.of[A](using tpe)

    val tpeSym = repr.typeSymbol

    Expr(tpeSym.tree.show)
  }
}

Output

> _root_.test.Macros.show[_root_.test.Bar.type]

val res0: String = @scala.annotation.internal.SourceFile("macros/src/main/scala-3/enumeratum/Foo.scala") object Bar extends test.Foo { this: test.Bar.type =>

}

Expectation

Following Tree should be resolved from the super call.

Literal(Constant("bar"))

Alternatives

Having the macro from the reproducer in a separate file doesn't help (same result).

Using repr.typeSymbol.companionModule instead of repr.typeSymbol resolves as bellow:

"lazy val Bar: test.Bar.type"

Try repr.typeSymbol.primaryConstructor doesn't help much:

"def this()"

Even trying to patter-match on it:

tpeSym.tree match {
  case DefDef(_, _, _, rhs) =>
    println(s"Constructor rhs = $rhs")

  case _ =>
}

// => "Constructor rhs = None"

cchantep avatar Aug 10 '22 13:08 cchantep

Maybe related to #15799

cchantep avatar Aug 10 '22 13:08 cchantep

Cause may be same as for https://github.com/lampepfl/dotty/issues/14195 but none of the workaround is sufficient

cchantep avatar Aug 27 '22 13:08 cchantep

That's quite a blocker. I'm trying to debug it, but there is no test that can be easily executed while developing quotes impl in this code base.

cchantep avatar Aug 31 '22 09:08 cchantep

Confirmed to be related to -Yretain-trees

cchantep avatar Sep 02 '22 21:09 cchantep