Skip to content

Commit d48b91e

Browse files
committed
Add reflect asSeenFrom
Fixes #14957
1 parent e4fd917 commit d48b91e

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
@@ -2660,6 +2660,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
26602660

26612661
/** The applied type arguments (empty if there is no such arguments) */
26622662
def typeArgs: List[TypeRepr]
2663+
2664+
/** This type seen as if it were the type of a member of prefix type `pre`
2665+
* declared in class `cls`.
2666+
*/
2667+
@experimental
2668+
def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr
2669+
26632670
end extension
26642671
}
26652672

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ val experimentalDefinitionInLibrary = Set(
7272
"scala.quoted.Quotes.reflectModule.ClassDefModule.apply",
7373
"scala.quoted.Quotes.reflectModule.SymbolModule.newClass",
7474
"scala.quoted.Quotes.reflectModule.SymbolMethods.info",
75+
"scala.quoted.Quotes.reflectModule.TypeReprMethods.asSeenFrom",
7576

7677
// New APIs: Lightweight lazy vals. Can be stabilized in 3.3.0
7778
"scala.runtime.LazyVals$.Evaluating",

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)