Skip to content

Commit 3b01a9f

Browse files
committed
Provide more efficient implemetation of Block.let
1 parent 7006a8e commit 3b01a9f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
706706
tpd.cpy.Block(original)(stats, expr)
707707
def unapply(x: Block): Option[(List[Statement], Term)] =
708708
Some((x.statements, x.expr))
709+
710+
def let(rhs: Term)(body: Ident => Term): Term =
711+
let(List(rhs))(x => body(x.head))
712+
713+
def let(terms: List[Term])(body: List[Ident] => Term): Term =
714+
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term))
715+
val refs = vdefs.map(vdef => tpd.ref(vdef.symbol).asInstanceOf[Ident])
716+
Block(vdefs, body(refs))
717+
709718
end Block
710719

711720
object BlockMethodsImpl extends BlockMethods:

library/src/scala/tasty/Reflection.scala

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -819,20 +819,10 @@ trait Reflection { reflection =>
819819
def apply(stats: List[Statement], expr: Term): Block
820820

821821
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
822-
def let(rhs: Term)(body: Ident => Term): Term = {
823-
val sym = Symbol.newVal(Symbol.currentOwner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
824-
Block(List(ValDef(sym, Some(rhs))), body(Ref(sym).asInstanceOf[Ident]))
825-
}
822+
def let(rhs: Term)(body: Ident => Term): Term
826823

827824
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
828-
def let(terms: List[Term])(body: List[Ident] => Term): Term = {
829-
// TODO implement efficitent flat version
830-
def rec(xs: List[Term], acc: List[Ident]): Term = xs match {
831-
case Nil => body(acc)
832-
case x :: xs => let(x) { (x: Ident) => rec(xs, x :: acc) }
833-
}
834-
rec(terms, Nil)
835-
}
825+
def let(terms: List[Term])(body: List[Ident] => Term): Term
836826

837827
def copy(original: Tree)(stats: List[Statement], expr: Term): Block
838828

0 commit comments

Comments
 (0)