Skip to content

TypeRepr of constructor parameters unreliable - dependent on compilation order #19795

@OndrejSpanel

Description

@OndrejSpanel

The TypeRepr of class constructor parameters is sometimes wrong. Instead of an AppliedType sometimes a plain TypeRef is returned, missing type parameters.

Compiler version

3.0.0, 3.3.2, 3.4.1-RC1

Minimized code

https://github.com/OndrejSpanel/surface-sandbox

The repo contains two reproduction methods: default branch contains a minimized repro, branch surface-repro contains repro using aiframe-surface library, which is how the issue was originaly found.

  println(Surface.func[TypeUtils.Wrap])
import scala.quoted.*

object Surface:

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

    val t = TypeRepr.of(using tpe)
    println(s"func ${t.show}")

    val paramStrings = t.typeSymbol.primaryConstructor.paramSymss.flatten
      .map(s => s.tree.show)
    val output =  paramStrings.mkString(",")
    println(s"params $output")
    Expr(output)
  }


  inline def func[A]: String = ${ funcImpl[A] }
import scala.quoted.*

object TypeUtils {
  trait MyOption[T]

  class Wrap(val option: MyOption[Int])

  def macroImpl(using Quotes): Expr[Unit] = '{ }

  inline def myMacro(): Unit = ${ macroImpl }
}

Output

val localOpt: TypeUtils.MyOption[scala.Int]
val option: TypeUtils.MyOption

Expectation

Output for option should contain correct parameter type including type parameters. same way as localOpt does.

Note

The reproduction depends on compilation order. If you uncomment the line containing TypeUtils.myMacro(), the issue disappears and the output is as expected:

val localOpt: TypeUtils.MyOption[scala.Int]
val option: TypeUtils.MyOption[scala.Int]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions