Skip to content

Commit

Permalink
Merge pull request scala#14088 from dotty-staging/fix-#13947
Browse files Browse the repository at this point in the history
Add Reflect TypeRepr.typeArgs
  • Loading branch information
anatoliykmetyuk authored Dec 13, 2021
2 parents 6184122 + 2105653 commit 1f1b495
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr =
self.subst(from, to)

def typeArgs: List[TypeRepr] = self match
case AppliedType(_, args) => args
case _ => List.empty
end extension
end TypeReprMethods

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

def name: String = self.denot.name.toString
def fullName: String = self.denot.fullName.toString

def pos: Option[Position] =
if self.exists then Some(self.sourcePos) else None

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 @@ -2571,6 +2571,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
@experimental
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr

/** The applied type arguments (empty if there is no such arguments) */
@experimental
def typeArgs: List[TypeRepr]
end extension
}

Expand Down
2 changes: 2 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ object MiMaFilters {
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"),
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double"),
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.double$"),
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.float"),
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/i13947.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[]
[scala.Predef.String]
[scala.Int, scala.Float, scala.Long]
12 changes: 12 additions & 0 deletions tests/run-macros/i13947/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted.*

inline def printTypeParams[A]: Unit = ${ printTypeParamsImpl[A] }

def printTypeParamsImpl[A: Type](using Quotes): Expr[Unit] = {
import quotes.reflect.*

val targs: List[TypeRepr] = TypeRepr.of[A].typeArgs
val debug = targs.map(_.show).mkString("[", ", ", "]")

'{ println(${Expr(debug)}) }
}
4 changes: 4 additions & 0 deletions tests/run-macros/i13947/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main def Test: Unit =
printTypeParams[scala.util.Random]
printTypeParams[Option[String]]
printTypeParams[Function2[Int, Float, Long]]

0 comments on commit 1f1b495

Please sign in to comment.