Skip to content

Commit f677b4c

Browse files
committed
Implement Tuple.head as Tuple.apply(0)
1 parent ad71df8 commit f677b4c

File tree

5 files changed

+6
-28
lines changed

5 files changed

+6
-28
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,6 @@ class Definitions {
809809
lazy val DynamicTuple_concatIterator: Symbol = DynamicTupleModule.requiredMethod("concatIterator")
810810
lazy val DynamicTuple_dynamicApply: Symbol = DynamicTupleModule.requiredMethod("dynamicApply")
811811
lazy val DynamicTuple_dynamicCons: Symbol = DynamicTupleModule.requiredMethod("dynamicCons")
812-
lazy val DynamicTuple_dynamicHead: Symbol = DynamicTupleModule.requiredMethod("dynamicHead")
813812
lazy val DynamicTuple_dynamicSize: Symbol = DynamicTupleModule.requiredMethod("dynamicSize")
814813
lazy val DynamicTuple_dynamicTail: Symbol = DynamicTupleModule.requiredMethod("dynamicTail")
815814
lazy val DynamicTuple_dynamicConcat: Symbol = DynamicTupleModule.requiredMethod("dynamicConcat")

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
2626

2727
override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree = {
2828
if (tree.symbol == defn.DynamicTuple_dynamicCons) transformTupleCons(tree)
29-
else if (tree.symbol == defn.DynamicTuple_dynamicHead) transformTupleHead(tree)
3029
else if (tree.symbol == defn.DynamicTuple_dynamicTail) transformTupleTail(tree)
3130
else if (tree.symbol == defn.DynamicTuple_dynamicSize) transformTupleSize(tree)
3231
else if (tree.symbol == defn.DynamicTuple_dynamicConcat) transformTupleConcat(tree)
@@ -96,23 +95,6 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
9695
}
9796
}
9897

99-
private def transformTupleHead(tree: tpd.Apply)(implicit ctx: Context): Tree = {
100-
val Apply(TypeApply(_, tpt :: Nil), tup :: Nil) = tree
101-
tupleTypes(tpt.tpe) match { // TODO tupleBoundedTypes
102-
case Some(tpes) =>
103-
if (tpes.size <= Definitions.MaxTupleArity) {
104-
// tup._1
105-
Typed(tup, TypeTree(defn.tupleType(tpes))).select(nme.selectorName(0))
106-
} else {
107-
// tup.asInstanceOf[TupleXXL].productElement(0)
108-
tup.asInstance(defn.TupleXXLType).select(nme.productElement).appliedTo(Literal(Constant(0)))
109-
}
110-
case None =>
111-
// DynamicTuple.dynamicHead(tup)
112-
tree
113-
}
114-
}
115-
11698
private def transformTupleSize(tree: tpd.Apply)(implicit ctx: Context): Tree = {
11799
tree.tpe.tryNormalize match {
118100
case tp: ConstantType => Literal(tp.value)

library/src-3.x/scala/Tuple.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ object Tuple {
6666
sealed trait NonEmptyTuple extends Tuple {
6767
import Tuple._
6868

69+
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
70+
DynamicTuple.dynamicApply[This, n.type](this, n)
71+
6972
inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
70-
DynamicTuple.dynamicHead[This](this)
73+
DynamicTuple.dynamicApply[This, 0](this, 0)
7174

7275
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
7376
DynamicTuple.dynamicTail[This](this)
7477

75-
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
76-
DynamicTuple.dynamicApply[This, n.type](this, n)
7778
}
7879

7980
@showAsInfix

library/src-3.x/scala/runtime/DynamicTuple.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,6 @@ object DynamicTuple {
226226
case self: Product => self.productArity.asInstanceOf[Size[This]]
227227
}
228228

229-
def dynamicHead[This <: NonEmptyTuple] (self: This): Head[This] =
230-
self.asInstanceOf[Product].productElement(0).asInstanceOf[Head[This]]
231-
232229
def dynamicTail[This <: NonEmptyTuple] (self: This): Tail[This] = {
233230
type Result = Tail[This]
234231
val res = (self: Any) match {
@@ -245,7 +242,6 @@ object DynamicTuple {
245242
type Result = Elem[This, N]
246243
val res = (self: Any) match {
247244
case self: Unit => throw new IndexOutOfBoundsException(n.toString)
248-
case self: TupleXXL => self.elems(n)
249245
case self: Product => self.productElement(n)
250246
}
251247
res.asInstanceOf[Result]

tests/run-with-compiler/staged-tuples/StagedTuple.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ object StagedTuple {
7979
}
8080

8181
def headStaged[Tup <: NonEmptyTuple : Type](tup: Expr[Tup], size: Option[Int]): Expr[Head[Tup]] = {
82-
if (!specialize) '{dynamicHead($tup)}
82+
if (!specialize) '{dynamicApply($tup, 0)}
8383
else {
8484
val resVal = size match {
8585
case Some(1) =>
@@ -95,7 +95,7 @@ object StagedTuple {
9595
case Some(n) if n > MaxSpecialized =>
9696
'{${tup.as[TupleXXL] }.elems(0)}
9797
case None =>
98-
'{dynamicHead($tup)}
98+
'{dynamicApply($tup, 0)}
9999
}
100100
resVal.as[Head[Tup]]
101101
}

0 commit comments

Comments
 (0)