Skip to content

Add Reflect TypeRepr.typeArgs #14088

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

Merged
merged 1 commit into from
Dec 13, 2021
Merged
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
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]]