Skip to content

Commit 838a454

Browse files
committed
Add reflect asSeenFrom
Fixes #14957
1 parent 4c24473 commit 838a454

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
17651765
def typeArgs: List[TypeRepr] = self match
17661766
case AppliedType(_, args) => args
17671767
case _ => List.empty
1768+
def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr =
1769+
self.asSeenFrom(pre, cls)
17681770
end extension
17691771
end TypeReprMethods
17701772

library/src/scala/quoted/Quotes.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,6 +2663,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
26632663
/** The applied type arguments (empty if there is no such arguments) */
26642664
@experimental
26652665
def typeArgs: List[TypeRepr]
2666+
2667+
/** This type seen as if it were the type of a member of prefix type `pre`
2668+
* declared in class `cls`.
2669+
*/
2670+
@experimental
2671+
def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr
2672+
26662673
end extension
26672674
}
26682675

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ val experimentalDefinitionInLibrary = Set(
7070
"scala.quoted.Quotes.reflectModule.TypeReprMethods.typeArgs",
7171
"scala.quoted.Quotes.reflectModule.TypeTreeModule.ref",
7272
"scala.quoted.Quotes.reflectModule.SymbolMethods.info",
73+
"scala.quoted.Quotes.reflectModule.TypeReprMethods.asSeenFrom",
7374
// Can be stabilized in 3.2.0 (unsure) or later
7475
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
7576
// Cant be stabilized yet.

tests/run-macros/i14957.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
=> D[scala.Int]

tests/run-macros/i14957/Macro_1.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
inline def asSeenFromTest: String = ${ impl }
4+
5+
private def impl(using Quotes): Expr[String] =
6+
import quotes.reflect.*
7+
val aSym = Symbol.requiredClass("A")
8+
val xSym = aSym.methodMember("x").head
9+
// =>D[B] as seen from C with owner A
10+
Expr(xSym.info.asSeenFrom(TypeRepr.of[C], aSym).show)
11+
12+
13+
trait D[E]
14+
15+
class A[B] {
16+
def x: D[B] = ???
17+
}
18+
19+
class C extends A[Int]:
20+
override def x: D[Int] = ???

tests/run-macros/i14957/Test_2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main
2+
def Test: Unit =
3+
println(asSeenFromTest)

0 commit comments

Comments
 (0)