diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 4d08e0582d1d..e288a8ca89e7 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -1774,6 +1774,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def typeArgs: List[TypeRepr] = self match case AppliedType(_, args) => args case _ => List.empty + def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr = + self.asSeenFrom(pre, cls) end extension end TypeReprMethods diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 22ec107aeae8..cd4e5a886438 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -2684,6 +2684,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** The applied type arguments (empty if there is no such arguments) */ def typeArgs: List[TypeRepr] + + /** This type seen as if it were the type of a member of prefix type `pre` + * declared in class `cls`. + */ + @experimental + def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr + end extension } diff --git a/tests/run-macros/i14957.check b/tests/run-macros/i14957.check new file mode 100644 index 000000000000..ad70f5f8da8b --- /dev/null +++ b/tests/run-macros/i14957.check @@ -0,0 +1 @@ + => D[scala.Int] diff --git a/tests/run-macros/i14957/Macro_1.scala b/tests/run-macros/i14957/Macro_1.scala new file mode 100644 index 000000000000..90d2966f8c16 --- /dev/null +++ b/tests/run-macros/i14957/Macro_1.scala @@ -0,0 +1,20 @@ +import scala.quoted.* + +inline def asSeenFromTest: String = ${ impl } + +private def impl(using Quotes): Expr[String] = + import quotes.reflect.* + val aSym = Symbol.requiredClass("A") + val xSym = aSym.methodMember("x").head + // =>D[B] as seen from C with owner A + Expr(xSym.info.asSeenFrom(TypeRepr.of[C], aSym).show) + + +trait D[E] + +class A[B] { + def x: D[B] = ??? +} + +class C extends A[Int]: + override def x: D[Int] = ??? diff --git a/tests/run-macros/i14957/Test_2.scala b/tests/run-macros/i14957/Test_2.scala new file mode 100644 index 000000000000..5c7fe9e0ec1b --- /dev/null +++ b/tests/run-macros/i14957/Test_2.scala @@ -0,0 +1,3 @@ +@main +def Test: Unit = + println(asSeenFromTest)