Skip to content

Add reflect Symbol.info and TypeRepr.asSeenFrom #15024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions tests/run-macros/i14957.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=> D[scala.Int]
20 changes: 20 additions & 0 deletions tests/run-macros/i14957/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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] = ???
3 changes: 3 additions & 0 deletions tests/run-macros/i14957/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@main
def Test: Unit =
println(asSeenFromTest)