Skip to content

Commit 10f2f48

Browse files
committed
Workaround TreeChecker & specialised tuples
1 parent 5cdfb1c commit 10f2f48

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,8 @@ class Definitions {
13291329

13301330
@tu lazy val TupleType: Array[TypeRef | Null] = mkArityArray("scala.Tuple", MaxTupleArity, 1)
13311331

1332+
def isSpecializedTuple(cls: Symbol): Boolean = specializedTupleArgs(cls).nonEmpty
1333+
13321334
def specializedTupleArgs(cls: Symbol): List[Type] = cls.name.toString match
13331335
case "Tuple1$mcD$sp" => List(defn.DoubleType)
13341336
case "Tuple1$mcI$sp" => List(defn.IntType)

compiler/src/dotty/tools/dotc/transform/SpecializeTuples.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class SpecializeTuples extends MiniPhase:
2424
override def transformApply(tree: Apply)(using Context): Tree = tree match
2525
case Apply(TypeApply(fun: NameTree, targs), args)
2626
if fun.name == nme.apply && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)) =>
27-
Apply(Select(New(defn.SpecialisedTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args)
27+
Apply(Select(New(defn.SpecialisedTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
2828
case Apply(TypeApply(fun: NameTree, targs), args)
2929
if fun.name == nme.CONSTRUCTOR && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner, targs.map(_.tpe)) =>
30-
Apply(Select(New(defn.SpecialisedTuple(fun.symbol.owner, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args)
30+
Apply(Select(New(defn.SpecialisedTuple(fun.symbol.owner, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
3131
case _ => tree
3232
end transformApply
3333

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@ class TreeChecker extends Phase with SymTransformer {
421421
assert(tree.qual.typeOpt.isInstanceOf[ThisType], i"expect prefix of Super to be This, actual = ${tree.qual}")
422422
super.typedSuper(tree, pt)
423423

424+
override def typedApply(tree: untpd.Apply, pt: Type)(using Context): Tree = tree match
425+
case Apply(Select(qual, nme.CONSTRUCTOR), _)
426+
if !ctx.phase.erasedTypes
427+
&& defn.isSpecializedTuple(qual.typeOpt.typeSymbol)
428+
&& pt.forallParts(tp => tp != WildcardType && tp != TypeBounds.empty) =>
429+
tree.withType(pt) // e.g. `new Tuple2$mcII$sp(7, 8)` should keep its `(7, 8)` type instead of `Tuple2$mcII$sp`
430+
case _ => super.typedApply(tree, pt)
431+
424432
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree =
425433
val tpt1 = checkSimpleKinded(typedType(tree.tpt))
426434
val expr1 = tree.expr match

0 commit comments

Comments
 (0)