Skip to content

Add typeRef and termRef to Reflection #9707

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
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,9 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte

def Symbol_noSymbol(using Context): Symbol = core.Symbols.NoSymbol

def Symbol_typeRef(self: Symbol)(using Context): TypeRef = self.typeRef

def Symbol_termRef(self: Symbol)(using Context): TermRef = self.termRef

///////////
// FLAGS //
Expand Down
6 changes: 1 addition & 5 deletions library/src-bootstrapped/scala/quoted/util/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ trait ExprMap {
val tp = tpt.tpe match
// TODO improve code
case AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "<repeated>"), List(tp0: Type)) =>
// TODO rewrite without using quotes
type T
val qtp: quoted.Type[T] = tp0.seal.asInstanceOf[quoted.Type[T]]
given qtp.type = qtp
'[Seq[T]].unseal.tpe
Symbol.requiredClass("scala.collection.immutable.Seq").typeRef.appliedTo(tp0)
Copy link
Contributor

@liufengyun liufengyun Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems for this typical usage, an API like def fromPath(path: String): Type is more friendly:

Type.fromPath("scala.collection.immutable.Seq").appliedTo(tp0)
// or
Type.staticRef("scala.collection.immutable.Seq").appliedTo(tp0)

The compiler API can keep the same, the user API can be a little different. This way, we also defend against compiler changes from meta-programmers.

case tp => tp
Typed.copy(tree)(transformTerm(expr, tp), transformTypeTree(tpt))
case tree: NamedArg =>
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/internal/tasty/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,11 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
/** Case class or case object children of a sealed trait */
def Symbol_children(self: Symbol)(using ctx: Context): List[Symbol]

/** Type referene to the symbol */
def Symbol_typeRef(self: Symbol)(using Context): TypeRef

/** Term referene to the symbol */
def Symbol_termRef(self: Symbol)(using Context): TermRef
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two methods are rarely used in the compiler. I'd suggest only expose them when there is a clear use case.


///////////
// FLAGS //
Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,14 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
def companionModule(using ctx: Context): Symbol =
reflectSelf.Symbol_companionModule(sym)

/** Type referene to the symbol */
def typeRef(using Context): TypeRef =
reflectSelf.Symbol_typeRef(sym)

/** Term referene to the symbol */
def termRef(using Context): TermRef =
reflectSelf.Symbol_termRef(sym)

/** Shows the tree as extractors */
def showExtractors(using ctx: Context): String =
new ExtractorsPrinter[reflectSelf.type](reflectSelf).showSymbol(sym)
Expand Down