Skip to content

StagedTuple changes #5822

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 7 commits into from
Apr 15, 2019
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
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ object Splicer {
}
catch {
case ex: scala.quoted.QuoteError =>
ctx.error(ex.getMessage, pos)
val pos1 = ex.from match {
case None => pos
case Some(expr) =>
val reflect: scala.tasty.Reflection = ReflectionImpl(ctx)
import reflect._
expr.unseal.underlyingArgument.pos.asInstanceOf[SourcePosition]
}
ctx.error(ex.getMessage, pos1)
EmptyTree
case NonFatal(ex) =>
val msg =
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/reference/other-new-features/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def natConstImpl(x: Expr[Int])(implicit reflection: Reflection): Expr[Int] = {
xTree match {
case Term.Literal(Constant.Int(n)) =>
if (n <= 0)
throw new QuoteError("Parameter must be natural number")
QuoteError("Parameter must be natural number")
n.toExpr
case _ =>
throw new QuoteError("Parameter must be a known constant")
QuoteError("Parameter must be a known constant")
}
}
```
Expand Down
207 changes: 40 additions & 167 deletions library/src-bootstrapped/scala/Tuple.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package scala
import annotation.showAsInfix
import compiletime._
import scala.StagedTuple
import internal._

sealed trait Tuple extends Any {
import Tuple._

inline def toArray: Array[Object] =
if (stageIt) stagedToArray
if (stageIt) toArrayStaged
else inline constValueOpt[BoundedSize[this.type]] match {
case Some(0) =>
empty$Array
scala.runtime.DynamicTuple.empty$Array
case Some(1) =>
val t = asInstanceOf[Tuple1[Object]]
Array(t._1)
Expand All @@ -23,19 +23,19 @@ sealed trait Tuple extends Any {
case Some(4) =>
val t = asInstanceOf[Tuple4[Object, Object, Object, Object]]
Array(t._1, t._2, t._3, t._4)
case Some(n) if n <= MaxSpecialized =>
case Some(n) if n <= scala.runtime.DynamicTuple.MaxSpecialized =>
to$Array(this, n)
case Some(n) =>
asInstanceOf[TupleXXL].elems
case None =>
dynamicToArray(this)
runtime.DynamicTuple.dynamicToArray(this)
}

inline def stagedToArray: Array[Object] =
inline def toArrayStaged: Array[Object] =
${ StagedTuple.toArrayStaged('this, constValueOpt[BoundedSize[this.type]]) }

inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This =
if (stageIt) stagedCons[H](x)
if (stageIt) consStaged[H](x)
else {
type Result = H *: This
inline constValueOpt[BoundedSize[this.type]] match {
Expand All @@ -55,15 +55,15 @@ sealed trait Tuple extends Any {
case Some(n) =>
fromArray[H *: this.type](cons$Array(x, toArray))
case _ =>
dynamic_*:[This, H](this, x)
runtime.DynamicTuple.dynamic_*:[This, H](this, x)
}
}

inline def stagedCons[H] (x: H): H *: this.type =
${ StagedTuple.stagedCons('this, 'x, constValueOpt[BoundedSize[this.type]]) }
inline def consStaged[H] (x: H): H *: this.type =
${ StagedTuple.consStaged('this, 'x, constValueOpt[BoundedSize[this.type]]) }

inline def ++ [This >: this.type <: Tuple](that: Tuple): Concat[This, that.type] =
if (stageIt) stagedConcat(that).asInstanceOf
if (stageIt) concatStaged(that).asInstanceOf
else {
type Result = Concat[This, that.type]
inline constValueOpt[BoundedSize[this.type]] match {
Expand Down Expand Up @@ -99,35 +99,32 @@ sealed trait Tuple extends Any {
if (constValue[BoundedSize[that.type]] == 0) this.asInstanceOf[Result]
else genericConcat[Result](this, that).asInstanceOf[Result]
case None =>
dynamic_++[This, that.type](this, that)
runtime.DynamicTuple.dynamic_++[This, that.type](this, that)
}
}

inline def stagedConcat(that: Tuple): Concat[this.type, that.type] =
${ StagedTuple.stagedConcat('this, constValueOpt[BoundedSize[this.type]],
inline def concatStaged(that: Tuple): Concat[this.type, that.type] =
${ StagedTuple.concatStaged('this, constValueOpt[BoundedSize[this.type]],
'that, constValueOpt[BoundedSize[that.type]]) }

inline def genericConcat[T <: Tuple](xs: Tuple, ys: Tuple): Tuple =
fromArray[T](xs.toArray ++ ys.toArray)

inline def size[This >: this.type <: Tuple]: Size[This] =
if (stageIt) stagedSize.asInstanceOf
if (stageIt) sizeStaged.asInstanceOf
else {
type Result = Size[This]
inline constValueOpt[BoundedSize[this.type]] match {
case Some(n) => n.asInstanceOf[Result]
case _ => dynamicSize(this)
case _ => runtime.DynamicTuple.dynamicSize(this)
}
}

inline def stagedSize: Size[this.type] =
inline def sizeStaged: Size[this.type] =
${ StagedTuple.sizeStaged[Size[this.type]]('this, constValueOpt[BoundedSize[this.type]]) }
}

object Tuple {
inline val MaxSpecialized = 22
inline private val XXL = MaxSpecialized + 1

final val stageIt = false

type Head[X <: NonEmptyTuple] = X match {
Expand Down Expand Up @@ -167,7 +164,7 @@ object Tuple {

private[scala] type BoundedSize[X] = BoundedSizeRecur[X, 23]

val empty$Array = Array[Object]()
val $emptyArray = Array[Object]()

def to$Array(xs: Tuple, n: Int) = {
val arr = new Array[Object](n)
Expand All @@ -188,7 +185,7 @@ object Tuple {
}

inline def fromArray[T <: Tuple](xs: Array[Object]): T =
if (stageIt) stagedFromArray[T](xs)
if (stageIt) fromArrayStaged[T](xs)
else inline constValue[BoundedSize[T]] match {
case 0 => ().asInstanceOf[T]
case 1 => Tuple1(xs(0)).asInstanceOf[T]
Expand Down Expand Up @@ -216,103 +213,15 @@ object Tuple {
case _ => TupleXXL(xs).asInstanceOf[T]
}

inline def stagedFromArray[T <: Tuple](xs: Array[Object]): T =
${ StagedTuple.fromArrayStaged[T]('xs, constValueOpt[BoundedSize[this.type]]) }

def dynamicFromArray[T <: Tuple](xs: Array[Object]): T = xs.length match {
case 0 => ().asInstanceOf[T]
case 1 => Tuple1(xs(0)).asInstanceOf[T]
case 2 => Tuple2(xs(0), xs(1)).asInstanceOf[T]
case 3 => Tuple3(xs(0), xs(1), xs(2)).asInstanceOf[T]
case 4 => Tuple4(xs(0), xs(1), xs(2), xs(3)).asInstanceOf[T]
case 5 => Tuple5(xs(0), xs(1), xs(2), xs(3), xs(4)).asInstanceOf[T]
case 6 => Tuple6(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5)).asInstanceOf[T]
case 7 => Tuple7(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6)).asInstanceOf[T]
case 8 => Tuple8(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7)).asInstanceOf[T]
case 9 => Tuple9(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8)).asInstanceOf[T]
case 10 => Tuple10(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9)).asInstanceOf[T]
case 11 => Tuple11(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10)).asInstanceOf[T]
case 12 => Tuple12(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11)).asInstanceOf[T]
case 13 => Tuple13(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12)).asInstanceOf[T]
case 14 => Tuple14(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13)).asInstanceOf[T]
case 15 => Tuple15(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14)).asInstanceOf[T]
case 16 => Tuple16(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15)).asInstanceOf[T]
case 17 => Tuple17(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16)).asInstanceOf[T]
case 18 => Tuple18(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17)).asInstanceOf[T]
case 19 => Tuple19(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18)).asInstanceOf[T]
case 20 => Tuple20(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19)).asInstanceOf[T]
case 21 => Tuple21(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20)).asInstanceOf[T]
case 22 => Tuple22(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20), xs(21)).asInstanceOf[T]
case _ => TupleXXL(xs).asInstanceOf[T]
}

def dynamicToArray(self: Tuple): Array[Object] = (self: Any) match {
case self: Unit =>
empty$Array
case self: Tuple1[_] =>
val t = self.asInstanceOf[Tuple1[Object]]
Array(t._1)
case self: Tuple2[_, _] =>
val t = self.asInstanceOf[Tuple2[Object, Object]]
Array(t._1, t._2)
case self: Tuple3[_, _, _] =>
val t = self.asInstanceOf[Tuple3[Object, Object, Object]]
Array(t._1, t._2, t._3)
case self: Tuple4[_, _, _, _] =>
val t = self.asInstanceOf[Tuple4[Object, Object, Object, Object]]
Array(t._1, t._2, t._3, t._4)
case self: TupleXXL =>
self.elems
case self: Product =>
val arr = new Array[Object](self.productArity)
for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf[Object]
arr
}

def dynamic_*: [This <: Tuple, H] (self: Tuple, x: H): H *: This = {
type Result = H *: This
(self: Any) match {
case Unit =>
Tuple1(x).asInstanceOf[Result]
case self: Tuple1[_] =>
Tuple2(x, self._1).asInstanceOf[Result]
case self: Tuple2[_, _] =>
Tuple3(x, self._1, self._2).asInstanceOf[Result]
case self: Tuple3[_, _, _] =>
Tuple4(x, self._1, self._2, self._3).asInstanceOf[Result]
case self: Tuple4[_, _, _, _] =>
Tuple5(x, self._1, self._2, self._3, self._4).asInstanceOf[Result]
case _ =>
dynamicFromArray[Result](cons$Array(x, dynamicToArray(self)))
}
}

def dynamic_++[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = {
type Result = Concat[This, That]
(this: Any) match {
case self: Unit => return self.asInstanceOf[Result]
case _ =>
}
(that: Any) match {
case that: Unit => return self.asInstanceOf[Result]
case _ =>
}
dynamicFromArray[Result](dynamicToArray(self) ++ dynamicToArray(that))
}

def dynamicSize[This <: Tuple](self: This): Size[This] = (self: Any) match {
case self: Unit => 0.asInstanceOf[Size[This]]
case self: TupleXXL => self.elems.length.asInstanceOf[Size[This]]
case self: Product => self.productArity.asInstanceOf[Size[This]]
}
inline def fromArrayStaged[T <: Tuple](xs: Array[Object]): T =
${StagedTuple.fromArrayStaged[T]('xs, constValueOpt[BoundedSize[this.type]])}
}

sealed trait NonEmptyTuple extends Tuple {
import Tuple._
import NonEmptyTuple._

inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
if (stageIt) stagedHead.asInstanceOf
if (stageIt) headStaged.asInstanceOf
else {
type Result = Head[This]
val resVal = inline constValueOpt[BoundedSize[this.type]] match {
Expand All @@ -328,22 +237,22 @@ sealed trait NonEmptyTuple extends Tuple {
case Some(4) =>
val t = asInstanceOf[Tuple4[_, _, _, _]]
t._1
case Some(n) if n > 4 && n <= MaxSpecialized =>
case Some(n) if n > 4 && n <= scala.runtime.DynamicTuple.MaxSpecialized =>
asInstanceOf[Product].productElement(0)
case Some(n) if n > MaxSpecialized =>
case Some(n) if n > scala.runtime.DynamicTuple.MaxSpecialized =>
val t = asInstanceOf[TupleXXL]
t.elems(0)
case None =>
dynamicHead[this.type](this)
scala.runtime.DynamicTuple.dynamicHead[this.type](this)
}
resVal.asInstanceOf[Result]
}

inline def stagedHead: Head[this.type] =
inline def headStaged: Head[this.type] =
${ StagedTuple.headStaged[this.type]('this, constValueOpt[BoundedSize[this.type]]) }

inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
if (stageIt) stagedTail.asInstanceOf
if (stageIt) tailStaged.asInstanceOf
else {
type Result = Tail[This]
inline constValueOpt[BoundedSize[this.type]] match {
Expand All @@ -364,21 +273,21 @@ sealed trait NonEmptyTuple extends Tuple {
case Some(n) if n > 5 =>
fromArray[Result](toArray.tail)
case None =>
dynamicTail[This](this)
runtime.DynamicTuple.dynamicTail[This](this)
}
}

inline def stagedTail: Tail[this.type] =
inline def tailStaged: Tail[this.type] =
${ StagedTuple.tailStaged[this.type]('this, constValueOpt[BoundedSize[this.type]]) }

inline def fallbackApply(n: Int) =
inline constValueOpt[n.type] match {
case Some(n: Int) => error("index out of bounds: ", n)
case None => dynamicApply[this.type, n.type](this, n)
case None => runtime.DynamicTuple.dynamicApply[this.type, n.type](this, n)
}

inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
if (stageIt) stagedApply(n).asInstanceOf
if (stageIt) applyStaged(n).asInstanceOf
else {
type Result = Elem[This, n.type]
inline constValueOpt[Size[this.type]] match {
Expand Down Expand Up @@ -412,13 +321,13 @@ sealed trait NonEmptyTuple extends Tuple {
case Some(3) => t._4.asInstanceOf[Result]
case _ => fallbackApply(n).asInstanceOf[Result]
}
case Some(s) if s > 4 && s <= MaxSpecialized =>
case Some(s) if s > 4 && s <= scala.runtime.DynamicTuple.MaxSpecialized =>
val t = asInstanceOf[Product]
inline constValueOpt[n.type] match {
case Some(n) if n >= 0 && n < s => t.productElement(n).asInstanceOf[Result]
case _ => fallbackApply(n).asInstanceOf[Result]
}
case Some(s) if s > MaxSpecialized =>
case Some(s) if s > scala.runtime.DynamicTuple.MaxSpecialized =>
val t = asInstanceOf[TupleXXL]
inline constValueOpt[n.type] match {
case Some(n) if n >= 0 && n < s => t.elems(n).asInstanceOf[Result]
Expand All @@ -428,53 +337,17 @@ sealed trait NonEmptyTuple extends Tuple {
}
}

inline def stagedApply(n: Int): Elem[this.type, n.type] =
${ StagedTuple.applyStaged[this.type, n.type](
'this, constValueOpt[Size[this.type]],
'n, constValueOpt[n.type]) }
}

object NonEmptyTuple {
import Tuple._

def dynamicHead[This <: NonEmptyTuple] (self: This): Head[This] = {
type Result = Head[This]
val res = (self: Any) match {
case self: Tuple1[_] => self._1
case self: Tuple2[_, _] => self._1
case self: Tuple3[_, _, _] => self._1
case self: Tuple4[_, _, _, _] => self._1
case self: TupleXXL => self.elems(0)
case self: Product => self.productElement(0)
}
res.asInstanceOf[Result]
}

def dynamicTail[This <: NonEmptyTuple] (self: This): Tail[This] = {
type Result = Tail[This]
val res = (self: Any) match {
case self: Tuple1[_] => ()
case self: Tuple2[_, _] => Tuple1(self._2)
case self: Tuple3[_, _, _] => Tuple2(self._2, self._3)
case self: Tuple4[_, _, _, _] => Tuple3(self._2, self._3, self._4)
case _ => dynamicFromArray[Result](dynamicToArray(self).tail)
}
res.asInstanceOf[Result]
}

def dynamicApply[This <: NonEmptyTuple, N <: Int] (self: This, n: N): Elem[This, N] = {
type Result = Elem[This, N]
val res = (self: Any) match {
case self: TupleXXL => self.elems(n)
case self: Product => self.productElement(n)
}
res.asInstanceOf[Result]
}
inline def applyStaged(n: Int): Elem[this.type, n.type] =
${StagedTuple.applyStaged[this.type, n.type](
'this, constValueOpt[Size[this.type]], 'n, constValueOpt[n.type])}
}

@showAsInfix
sealed class *:[+H, +T <: Tuple] extends NonEmptyTuple

object *: {
inline def unapply[H, T <: Tuple](x: H *: T) = (x.head, x.tail)
inline def unapply[H, T <: Tuple](x: H *: T) =
// With stageIt on we cannot expand x.head in the same run and fails
if (Tuple.stageIt) (scala.runtime.DynamicTuple.dynamicHead(x), scala.runtime.DynamicTuple.dynamicTail(x))
else (x.head, x.tail)
}
Loading