Skip to content

Commit f44c5a2

Browse files
committed
Add reflect TypeRef.underlying
Fixes #15799
1 parent d6cc101 commit f44c5a2

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18411841
extension (self: TypeRef)
18421842
def isOpaqueAlias: Boolean = self.symbol.isOpaqueAlias
18431843
def translucentSuperType: TypeRepr = self.translucentSuperType
1844+
def underlying: TypeRepr = self.underlying
18441845
end extension
18451846
end TypeRefMethods
18461847

library/src/scala/quoted/Quotes.scala

+3
Original file line numberDiff line numberDiff line change
@@ -2766,6 +2766,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
27662766
extension (self: TypeRef)
27672767
def isOpaqueAlias: Boolean
27682768
def translucentSuperType: TypeRepr
2769+
/** The type bounds of the referenced type. */
2770+
@experimental
2771+
def underlying: TypeRepr
27692772
end extension
27702773
end TypeRefMethods
27712774

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ val experimentalDefinitionInLibrary = Set(
6565
//// New feature: Macro annotations
6666
"scala.annotation.MacroAnnotation",
6767

68-
//// New APIs: Quotes
69-
// Can be stabilized in 3.3.0 (unsure) or later
68+
//// New APIs: Quotes
69+
// Can be stabilized in 3.4.0 or later
70+
"scala.quoted.Quotes.reflectModule.TypeRefMethods.underlying",
71+
// Can be stabilized in 3.4.0 (unsure) or later
7072
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
7173
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",
7274
// Cant be stabilized yet.

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)