Skip to content

Commit 1f1b495

Browse files
Merge pull request #14088 from dotty-staging/fix-#13947
Add Reflect TypeRepr.typeArgs
2 parents 6184122 + 2105653 commit 1f1b495

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
17491749
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
17501750
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr =
17511751
self.subst(from, to)
1752+
1753+
def typeArgs: List[TypeRepr] = self match
1754+
case AppliedType(_, args) => args
1755+
case _ => List.empty
17521756
end extension
17531757
end TypeReprMethods
17541758

@@ -2480,6 +2484,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24802484

24812485
def name: String = self.denot.name.toString
24822486
def fullName: String = self.denot.fullName.toString
2487+
24832488
def pos: Option[Position] =
24842489
if self.exists then Some(self.sourcePos) else None
24852490

library/src/scala/quoted/Quotes.scala

+3
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
25712571
@experimental
25722572
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr
25732573

2574+
/** The applied type arguments (empty if there is no such arguments) */
2575+
@experimental
2576+
def typeArgs: List[TypeRepr]
25742577
end extension
25752578
}
25762579

project/MiMaFilters.scala

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ object MiMaFilters {
99
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
1010
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
1111
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
12+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"),
13+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"),
1214
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double"),
1315
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double$"),
1416
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.float"),

tests/run-macros/i13947.check

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[]
2+
[scala.Predef.String]
3+
[scala.Int, scala.Float, scala.Long]

tests/run-macros/i13947/Macro_1.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted.*
2+
3+
inline def printTypeParams[A]: Unit = ${ printTypeParamsImpl[A] }
4+
5+
def printTypeParamsImpl[A: Type](using Quotes): Expr[Unit] = {
6+
import quotes.reflect.*
7+
8+
val targs: List[TypeRepr] = TypeRepr.of[A].typeArgs
9+
val debug = targs.map(_.show).mkString("[", ", ", "]")
10+
11+
'{ println(${Expr(debug)}) }
12+
}

tests/run-macros/i13947/Test_2.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@main def Test: Unit =
2+
printTypeParams[scala.util.Random]
3+
printTypeParams[Option[String]]
4+
printTypeParams[Function2[Int, Float, Long]]

0 commit comments

Comments
 (0)