Skip to content

Add reflect TypeRef.underlying #16747

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
wants to merge 1 commit into from
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
1 change: 1 addition & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
extension (self: TypeRef)
def isOpaqueAlias: Boolean = self.symbol.isOpaqueAlias
def translucentSuperType: TypeRepr = self.translucentSuperType
def underlying: TypeRepr = self.underlying
end extension
end TypeRefMethods

Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2775,6 +2775,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
extension (self: TypeRef)
def isOpaqueAlias: Boolean
def translucentSuperType: TypeRepr
/** The type bounds of the referenced type. */
Copy link
Member

Choose a reason for hiding this comment

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

If I have a TypeRef to a class type, then its underlying will be a ClassInfo which I believe we don't want to expose in the reflection API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not expose those.

@experimental
def underlying: TypeRepr
end extension
end TypeRefMethods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ val experimentalDefinitionInLibrary = Set(
//// New feature: Macro annotations
"scala.annotation.MacroAnnotation",

//// New APIs: Quotes
//// New APIs: Quotes
// Should be stabilized in 3.4.0
"scala.quoted.Quotes.reflectModule.TypeRefMethods.underlying",
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
// Can be stabilized in 3.4.0 (unsure) or later
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
Expand Down
7 changes: 7 additions & 0 deletions tests/run-macros/i15799.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Alias: (X.Alias,_ >: scala.Predef.String <: scala.Predef.String)
Opaque: (X.Opaque,_ >: scala.Nothing <: scala.Any)
Bound: (X.Bound,_ >: scala.Nothing <: scala.Predef.String)
OpaqueBound: (X.OpaqueBound,_ >: scala.Nothing <: scala.Predef.String)
Param T: (T,_ >: scala.Nothing <: scala.Any)
Param U: (U,_ >: scala.Nothing <: scala.Predef.String)
Param V: (V,_ >: scala.Int <: scala.Any)
14 changes: 14 additions & 0 deletions tests/run-macros/i15799/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.quoted.*

object exampleMacro {

inline def typeDefRhs[T <: AnyKind]: (String, String) = ${ typeDefRhsImpl[T] }

def typeDefRhsImpl[T <: AnyKind: Type](using Quotes): Expr[(String, String)] = {
import quotes.reflect.*
val underlyingTpe =
TypeRepr.of[T] match
case tpe: TypeRef => tpe.underlying
Expr((Type.show[T], underlyingTpe.show))
}
}
21 changes: 21 additions & 0 deletions tests/run-macros/i15799/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@main def Test = {
object X {
type Alias = String
opaque type Opaque = String
type Bound <: String
opaque type OpaqueBound <: String = String
}
import X.*

println(s"Alias: ${exampleMacro.typeDefRhs[Alias]}")
println(s"Opaque: ${exampleMacro.typeDefRhs[Opaque]}")
println(s"Bound: ${exampleMacro.typeDefRhs[Bound]}")
println(s"OpaqueBound: ${exampleMacro.typeDefRhs[OpaqueBound]}")

def testParams[T, U <: String, V >: Int](x: Int): Unit =
println(s"Param T: ${exampleMacro.typeDefRhs[T]}")
println(s"Param U: ${exampleMacro.typeDefRhs[U]}")
println(s"Param V: ${exampleMacro.typeDefRhs[V]}")

testParams(3)
}