Skip to content

Can't link type parameters to fields in generic classes for TASTY reflect #8879

Closed
@yilinwei

Description

@yilinwei

Minimized example

case class Foo[A](a: A)

object Test {

  import scala.quoted._

  def impl[T](t: T)(using qctx: QuoteContext, tt: Type[T]): Expr[Any] = {

    import qctx.tasty._
    import util._

    val symbol = typeOf[Foo[String]].typeSymbol.field("a")
    val ValDef(field, typeTree, _) = symbol.tree
    qctx.error(s"${typeTree}")

    '{???}
  }


  inline def apply[T](inline t: T) = ${ Test.impl('t )}

}

object Run {
  def test(): Unit = {
    Test[Foo[String]](Foo("moo"))
  }
}

Output

[error] 7 |    Test[Foo[String]](Foo("moo"))
[error]   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]   |TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class Foo)),type A)]
[error]   | This location contains code that was inlined from Run.scala:7

Expectation

typeOf[Foo[String], has the following type constructor information. I can't work out a way of linking the applied type to type A. This is problematic in generic methods such as copy because it's hard to work out what arguments should be passed in TypeApply when using TASTY reflect and in which order. I expect there's some part of the API I'm missing which would allow me to map A to String in this case.

[error]   |AppliedType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),Foo),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),module lang),String)))
[error]   | This location contains code that was inlined from Run.scala:7

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions