Skip to content

Add TypeRepr.isTupleN to reflection API #13384

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 8 commits into from
Aug 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ object CheckTrees {
optionArg.argTypesHi match {
case Nil =>
optionArg :: Nil
case tupleArgs if defn.isTupleType(optionArg) =>
case tupleArgs if defn.isTupleNType(optionArg) =>
tupleArgs
}
case _ =>
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,11 @@ class Definitions {
def isPolymorphicAfterErasure(sym: Symbol): Boolean =
(sym eq Any_isInstanceOf) || (sym eq Any_asInstanceOf) || (sym eq Object_synchronized)

def isTupleType(tp: Type)(using Context): Boolean = {
/** Is this type a `TupleN` type?
*
* @return true if the dealiased type of `tp` is `TupleN[T1, T2, ..., Tn]`
*/
def isTupleNType(tp: Type)(using Context): Boolean = {
val arity = tp.dealias.argInfos.length
arity <= MaxTupleArity && TupleType(arity) != null && tp.isRef(TupleType(arity).symbol)
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
compareLower(info2, tyconIsTypeRef = true)
case info2: ClassInfo =>
tycon2.name.startsWith("Tuple") &&
defn.isTupleType(tp2) && recur(tp1, tp2.toNestedPairs) ||
defn.isTupleNType(tp2) && recur(tp1, tp2.toNestedPairs) ||
tryBaseType(info2.cls)
case _ =>
fourthTry
Expand Down Expand Up @@ -2620,9 +2620,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
fullyInstantiated(tp2) && !tp1.classSymbols.exists(_.derivesFrom(tp2.symbol))
case (tp1: TypeRef, tp2: TermRef) if isEnumValue(tp2) =>
fullyInstantiated(tp1) && !tp2.classSymbols.exists(_.derivesFrom(tp1.symbol))
case (tp1: Type, tp2: Type) if defn.isTupleType(tp1) =>
case (tp1: Type, tp2: Type) if defn.isTupleNType(tp1) =>
provablyDisjoint(tp1.toNestedPairs, tp2)
case (tp1: Type, tp2: Type) if defn.isTupleType(tp2) =>
case (tp1: Type, tp2: Type) if defn.isTupleNType(tp2) =>
provablyDisjoint(tp1, tp2.toNestedPairs)
case (tp1: TypeProxy, tp2: TypeProxy) =>
provablyDisjoint(tp1.superType, tp2) || provablyDisjoint(tp1, tp2.superType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
changePrec(GlobalPrec) {
val argStr: Text =
if args.length == 2
&& !defn.isTupleType(args.head)
&& !defn.isTupleNType(args.head)
&& !isGiven && !isErased
then
atPrec(InfixPrec) { argText(args.head) }
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ class SpaceEngine(using Context) extends SpaceLogic {

val sym = tp.classSymbol

if (ctx.definitions.isTupleType(tp))
if (ctx.definitions.isTupleNType(tp))
params(tp).map(_ => "_").mkString("(", ", ", ")")
else if (scalaListType.isRef(sym))
if (flattenList) "_*" else "_: List"
Expand All @@ -782,7 +782,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
else if (decomposed) "_: " + showType(tp, showTypeArgs = true)
else "_"
case Prod(tp, fun, params) =>
if (ctx.definitions.isTupleType(tp))
if (ctx.definitions.isTupleNType(tp))
"(" + params.map(doShow(_)).mkString(", ") + ")"
else if (tp.isRef(scalaConsType.symbol))
if (flattenList) params.map(doShow(_, flattenList)).filter(_.nonEmpty).mkString(", ")
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ trait Applications extends Compatibility {
for (argType <- argTypes) assert(!isBounds(argType), unapplyApp.tpe.show)
val bunchedArgs = argTypes match {
case argType :: Nil =>
if (args.lengthCompare(1) > 0 && Feature.autoTuplingEnabled && defn.isTupleType(argType)) untpd.Tuple(args) :: Nil
if (args.lengthCompare(1) > 0 && Feature.autoTuplingEnabled && defn.isTupleNType(argType)) untpd.Tuple(args) :: Nil
else args
case _ => args
}
Expand Down
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 @@ -1738,6 +1738,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
val tpNoRefinement = self.dropDependentRefinement
tpNoRefinement != self
&& dotc.core.Symbols.defn.isNonRefinedFunction(tpNoRefinement)
def isTupleN: Boolean =
dotc.core.Symbols.defn.isTupleNType(self)
def select(sym: Symbol): TypeRepr = self.select(sym)
def appliedTo(targ: TypeRepr): TypeRepr =
dotc.core.Types.decorateTypeApplications(self).appliedTo(targ)
Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
def isDependentFunctionType: Boolean

/** Is this type a `TupleN` type?
*
* @return true if the dealiased type of `self` is `TupleN[T1, T2, ..., Tn]`
*/
def isTupleN: Boolean

/** The type <this . sym>, reduced if possible */
def select(sym: Symbol): TypeRepr

Expand Down
1 change: 1 addition & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ object MiMaFilters {
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.TypedOrTestTypeTest"),
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.TypedOrTest"),
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.TypedOrTestMethods"),
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.isTupleN"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ trait InkuireSupport:
params = typeList.init.map(p => Inkuire.Contravariance(inner(p, vars))) :+ Inkuire.Covariance(inner(typeList.last, vars)),
itid = Some(Inkuire.ITID(s"${name}scala.${name}//[]", isParsed = false))
)
else if t.isTupleType then
else if t.isTupleN then
val name = s"Tuple${typeList.size}"
Inkuire.Type(
name = Inkuire.TypeName(name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ import scala.quoted._
object SyntheticsSupport:

extension (using Quotes)(t: reflect.TypeRepr)
def isTupleType: Boolean = t.hackIsTupleType(t)

def isCompiletimeAppliedType: Boolean = t.hackIsCompiletimeAppliedType(t)

private def hackIsTupleType(rtpe: reflect.TypeRepr): Boolean =
import dotty.tools.dotc
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
ctx.definitions.isTupleType(tpe)

private def hackIsCompiletimeAppliedType(rtpe: reflect.TypeRepr): Boolean =
import dotty.tools.dotc
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
Expand Down
2 changes: 1 addition & 1 deletion scaladoc/src/dotty/tools/scaladoc/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ trait TypesSupport:
partOfSignature ++ texts(" => ") ++ inner(rtpe)
case args =>
texts("(") ++ commas(args.init.map(inner)) ++ texts(") => ") ++ inner(args.last)
else if t.isTupleType then
else if t.isTupleN then
typeList match
case Nil =>
Nil
Expand Down
8 changes: 8 additions & 0 deletions tests/run-macros/TypeRepr-isTupleN/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

inline def isTupleN[T]: Boolean = ${ isTupleNImpl[T] }

private def isTupleNImpl[T: Type](using Quotes): Expr[Boolean] = {
import quotes.reflect.*
Expr(TypeRepr.of[T].isTupleN)
}
38 changes: 38 additions & 0 deletions tests/run-macros/TypeRepr-isTupleN/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@main def Test = {
assert(isTupleN[Tuple1[Int]])
assert(isTupleN[(Int, Int)])
assert(isTupleN[(Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])
assert(isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)])

type Tup = (Int, Int)
assert(isTupleN[Tup])

assert(!isTupleN[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)]) // No tuple 23
assert(!isTupleN[Tuple])
assert(!isTupleN[EmptyTuple])
assert(!isTupleN[NonEmptyTuple])
assert(!isTupleN[Int *: Tuple])

assert(!isTupleN[Any])
assert(!isTupleN[Int])
assert(!isTupleN[Object])
assert(!isTupleN[Nothing])
}