Skip to content

Commit

Permalink
Workaround ciclic reference issues
Browse files Browse the repository at this point in the history
And avoid recomputing tuple sizes
  • Loading branch information
nicolasstucki committed Jan 23, 2020
1 parent f6bc23f commit 07f104e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions library/src/scala/quoted/unsafe/UnsafeExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ object UnsafeExpr {
*/
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(given qctx: QuoteContext): X = {
import qctx.tasty.{given, _}
val (params, bodyExpr) = paramsAndBody(f)
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
}

def open[T1, T2, R, X](f: Expr[(T1, T2) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2]) => Expr[t]) => X)(given qctx: QuoteContext)(given DummyImplicit): X = {
import qctx.tasty.{given, _}
val (params, bodyExpr) = paramsAndBody(f)
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal)).seal.asInstanceOf[Expr[t]])
}

def open[T1, T2, T3, R, X](f: Expr[(T1, T2, T3) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2], Expr[T3]) => Expr[t]) => X)(given qctx: QuoteContext)(given DummyImplicit, DummyImplicit): X = {
import qctx.tasty.{given, _}
val (params, bodyExpr) = paramsAndBody(f)
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2], v3: Expr[T3]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal, v3.unseal)).seal.asInstanceOf[Expr[t]])
}

private def paramsAndBody[R](given qctx: QuoteContext)(f: Expr[Any]) = {
private def paramsAndBody[R](given qctx: QuoteContext)(f: Expr[Any]): (List[qctx.tasty.ValDef], Expr[R]) = {
import qctx.tasty.{given, _}
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
Expand Down
27 changes: 14 additions & 13 deletions library/src/scala/runtime/DynamicTuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,16 @@ object DynamicTuple {
def dynamicConcat[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = {
type Result = Concat[This, That]

val selfSize: Int = self.size
// If one of the tuples is empty, we can leave early
(self: Any) match {
case self: Unit => return that.asInstanceOf[Result]
case _ =>
}
if selfSize == 0 then
return that.asInstanceOf[Result]

(that: Any) match {
case that: Unit => return self.asInstanceOf[Result]
case _ =>
}
val thatSize: Int = that.size
if thatSize == 0 then
return self.asInstanceOf[Result]

val arr = new Array[Object](self.size + that.size)
val arr = new Array[Object](selfSize + thatSize)

// Copies the tuple to an array, at the given offset
inline def copyToArray[T <: Tuple](tuple: T, array: Array[Object], offset: Int): Unit = (tuple: Any) match {
Expand Down Expand Up @@ -401,9 +399,11 @@ object DynamicTuple {
}

def dynamicZip[This <: Tuple, T2 <: Tuple](t1: This, t2: T2): Zip[This, T2] = {
if (t1.size == 0 || t2.size == 0) return ().asInstanceOf[Zip[This, T2]]
val size = Math.min(t1.size, t2.size)
Tuple.fromIArray(
val t1Size: Int = t1.size
val t2Size: Int = t2.size
val size = Math.min(t1Size, t2Size)
if size == 0 then ().asInstanceOf[Zip[This, T2]]
else Tuple.fromIArray(
zipIterators(
t1.asInstanceOf[Product].productIterator,
t2.asInstanceOf[Product].productIterator,
Expand Down Expand Up @@ -475,7 +475,8 @@ object DynamicTuple {

def dynamicTake[This <: Tuple, N <: Int](self: This, n: N): Take[This, N] = {
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
val actualN = Math.min(n, self.size)
val selfSize: Int = self.size
val actualN = Math.min(n, selfSize)

type Result = Take[This, N]

Expand Down

0 comments on commit 07f104e

Please sign in to comment.