Skip to content

Commit

Permalink
Add reflect AppliedType constructor
Browse files Browse the repository at this point in the history
Fixes #14740
  • Loading branch information
nicolasstucki committed Apr 14, 2022
1 parent bdddd41 commit e6100e0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end AppliedTypeTypeTest

object AppliedType extends AppliedTypeModule:
def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType =
Types.AppliedType(tycon, args)
def unapply(x: AppliedType): (TypeRepr, List[TypeRepr]) =
(AppliedTypeMethods.tycon(x), AppliedTypeMethods.args(x))
end AppliedType
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 @@ -2812,6 +2812,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val AppliedType` */
trait AppliedTypeModule { this: AppliedType.type =>
/** Applied the type constructor `T` to a list of type arguments `T_1,..,T_n` to create `T[T_1,..,T_n]` */
@experimental
def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType
def unapply(x: AppliedType): (TypeRepr, List[TypeRepr])
}

Expand Down
9 changes: 6 additions & 3 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import com.typesafe.tools.mima.core._
object MiMaFilters {
val Library: Seq[ProblemFilter] = Seq(

// Those are OK because user code is not allowed to inherit from Quotes:
// Experimental APIs that can be added in 3.2.0
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#AppliedTypeModule.apply"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefModule.apply"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newClass"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeRef"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeTreeModule.ref"),

// Experimental APIs that still need some time before stabilization
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefModule.apply"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newClass"),
)
}
11 changes: 11 additions & 0 deletions tests/pos-macros/i14740/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

type Foo[T]

transparent inline def emptyList[T]: Any = ${ impl[T] }

private def impl[T: Type](using Quotes): Expr[Any] = {
import quotes.reflect._
val tpe = AppliedType(TypeRepr.of[Foo], List(TypeRepr.of[T])) // test AppliedType constructor
Typed('{???}.asTerm, Inferred(tpe)).asExpr // '{ ??? : Foo[T] }
}
3 changes: 3 additions & 0 deletions tests/pos-macros/i14740/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test1: Foo[Any] = emptyList[Any]
def test2: Foo[Int] = emptyList[Int]
def test3: Foo[String] = emptyList[String]

0 comments on commit e6100e0

Please sign in to comment.