Skip to content

Commit 98e10d8

Browse files
committed
Add reflect asSeenFrom
Fixes #14957
1 parent 7e2f80b commit 98e10d8

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-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
@@ -1774,6 +1774,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
17741774
def typeArgs: List[TypeRepr] = self match
17751775
case AppliedType(_, args) => args
17761776
case _ => List.empty
1777+
def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr =
1778+
self.asSeenFrom(pre, cls)
17771779
end extension
17781780
end TypeReprMethods
17791781

library/src/scala/quoted/Quotes.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
26842684

26852685
/** The applied type arguments (empty if there is no such arguments) */
26862686
def typeArgs: List[TypeRepr]
2687+
2688+
/** This type seen as if it were the type of a member of prefix type `pre`
2689+
* declared in class `cls`.
2690+
*/
2691+
@experimental
2692+
def asSeenFrom(pre: TypeRepr, cls: Symbol): TypeRepr
2693+
26872694
end extension
26882695
}
26892696

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)