Skip to content

Commit 984a9c4

Browse files
committed
Add reflect TypeRef.underlying
Fixes #15799
1 parent c466fa0 commit 984a9c4

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+1
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18441844
extension (self: TypeRef)
18451845
def isOpaqueAlias: Boolean = self.symbol.isOpaqueAlias
18461846
def translucentSuperType: TypeRepr = self.translucentSuperType
1847+
def underlying: TypeRepr = self.underlying
18471848
end extension
18481849
end TypeRefMethods
18491850

library/src/scala/quoted/Quotes.scala

+3
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
27752775
extension (self: TypeRef)
27762776
def isOpaqueAlias: Boolean
27772777
def translucentSuperType: TypeRepr
2778+
/** The type bounds of the referenced type. */
2779+
@experimental
2780+
def underlying: TypeRepr
27782781
end extension
27792782
end TypeRefMethods
27802783

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ val experimentalDefinitionInLibrary = Set(
6666
//// New feature: Macro annotations
6767
"scala.annotation.MacroAnnotation",
6868

69-
//// New APIs: Quotes
69+
//// New APIs: Quotes
7070
// Should be stabilized in 3.4.0
71+
"scala.quoted.Quotes.reflectModule.TypeRefMethods.underlying",
7172
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
7273
// Can be stabilized in 3.4.0 (unsure) or later
7374
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",

tests/run-macros/i15799.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Alias: (X.Alias,_ >: scala.Predef.String <: scala.Predef.String)
2+
Opaque: (X.Opaque,_ >: scala.Nothing <: scala.Any)
3+
Bound: (X.Bound,_ >: scala.Nothing <: scala.Predef.String)
4+
OpaqueBound: (X.OpaqueBound,_ >: scala.Nothing <: scala.Predef.String)
5+
Param T: (T,_ >: scala.Nothing <: scala.Any)
6+
Param U: (U,_ >: scala.Nothing <: scala.Predef.String)
7+
Param V: (V,_ >: scala.Int <: scala.Any)

tests/run-macros/i15799/Macro_1.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
object exampleMacro {
4+
5+
inline def typeDefRhs[T <: AnyKind]: (String, String) = ${ typeDefRhsImpl[T] }
6+
7+
def typeDefRhsImpl[T <: AnyKind: Type](using Quotes): Expr[(String, String)] = {
8+
import quotes.reflect.*
9+
val underlyingTpe =
10+
TypeRepr.of[T] match
11+
case tpe: TypeRef => tpe.underlying
12+
Expr((Type.show[T], underlyingTpe.show))
13+
}
14+
}

tests/run-macros/i15799/Test_2.scala

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@main def Test = {
2+
object X {
3+
type Alias = String
4+
opaque type Opaque = String
5+
type Bound <: String
6+
opaque type OpaqueBound <: String = String
7+
}
8+
import X.*
9+
10+
println(s"Alias: ${exampleMacro.typeDefRhs[Alias]}")
11+
println(s"Opaque: ${exampleMacro.typeDefRhs[Opaque]}")
12+
println(s"Bound: ${exampleMacro.typeDefRhs[Bound]}")
13+
println(s"OpaqueBound: ${exampleMacro.typeDefRhs[OpaqueBound]}")
14+
15+
def testParams[T, U <: String, V >: Int](x: Int): Unit =
16+
println(s"Param T: ${exampleMacro.typeDefRhs[T]}")
17+
println(s"Param U: ${exampleMacro.typeDefRhs[U]}")
18+
println(s"Param V: ${exampleMacro.typeDefRhs[V]}")
19+
20+
testParams(3)
21+
}

0 commit comments

Comments
 (0)