Skip to content

Commit 7006a8e

Browse files
committed
Move let to Block and fix signature
Rather than remove it as originally planed, we keep it under Block as it creates a block with vals. Both signatures take an return an Ident now.
1 parent 2e220d5 commit 7006a8e

File tree

12 files changed

+61
-63
lines changed

12 files changed

+61
-63
lines changed

library/src/scala/tasty/Reflection.scala

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,22 @@ trait Reflection { reflection =>
818818
/** Creates a block `{ <statements: List[Statement]>; <expr: Term> }` */
819819
def apply(stats: List[Statement], expr: Term): Block
820820

821+
/** 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+
}
826+
827+
/** 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+
}
836+
821837
def copy(original: Tree)(stats: List[Statement], expr: Term): Block
822838

823839
/** Matches a block `{ <statements: List[Statement]>; <expr: Term> }` */
@@ -3329,22 +3345,4 @@ trait Reflection { reflection =>
33293345
val reflect: reflection.type = reflection
33303346
}
33313347

3332-
// TODO: extract from Reflection
3333-
3334-
/** Bind the `rhs` to a `val` and use it in `body` */
3335-
def let(rhs: Term)(body: Ident => Term): Term = {
3336-
val sym = Symbol.newVal(Symbol.currentOwner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
3337-
Block(List(ValDef(sym, Some(rhs))), body(Ref(sym).asInstanceOf[Ident]))
3338-
}
3339-
3340-
/** Bind the given `terms` to names and use them in the `body` */
3341-
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
3342-
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
3343-
case Nil => body(acc)
3344-
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
3345-
}
3346-
rec(terms, Nil)
3347-
}
3348-
3349-
33503348
}

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ object scalatest {
1010

1111
cond.unseal.underlyingArgument match {
1212
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
13-
let(lhs) { left =>
14-
let(rhs) { right =>
13+
Block.let(lhs) { left =>
14+
Block.let(rhs) { right =>
1515
val app = Select.overloaded(left, op, Nil, right :: Nil)
16-
let(app) { result =>
16+
Block.let(app) { result =>
1717
val l = left.seal
1818
val r = right.seal
1919
val b = result.seal.cast[Boolean]

tests/pos-macros/i8866/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Macro {
1414
def impl(using qctx: QuoteContext): Expr[Int] = {
1515
import qctx.reflect._
1616

17-
let(
17+
Block.let(
1818
Select.unique(
1919
'{ OtherMacro }.unseal,
2020
"apply"

tests/pos-macros/i8866b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
def impl(using qctx: QuoteContext): Expr[Int] = {
1010
import qctx.reflect._
1111

12-
let(
12+
Block.let(
1313
Select.unique(
1414
'{ Other }.unseal,
1515
"apply"

tests/run-macros/i6171/Macro_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
Block.let(app) { result =>
2121
val l = left.seal
2222
val r = right.seal
2323
val b = result.seal.cast[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.seal.cast[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
let(lhs) { left =>
32-
let(rhs) { right =>
31+
Block.let(lhs) { left =>
32+
Block.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
Block.let(Apply(app, implicits)) { result =>
3535
val l = left.seal
3636
val r = right.seal
3737
val b = result.seal.cast[Boolean]

tests/run-macros/reflect-dsl/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
1919
val app = left.select(sel.symbol).appliedTo(right)
20-
let(app) { result =>
20+
Block.let(app) { result =>
2121
val l = left.seal
2222
val r = right.seal
2323
val b = result.seal.cast[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.seal.cast[Unit]
2929
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
let(lhs) { left =>
32-
let(rhs) { right =>
31+
Block.let(lhs) { left =>
32+
Block.let(rhs) { right =>
3333
val app = qual.appliedTo(left).select(sel.symbol).appliedTo(right)
34-
let(Apply(app, implicits)) { result =>
34+
Block.let(Apply(app, implicits)) { result =>
3535
val l = left.seal
3636
val r = right.seal
3737
val b = result.seal.cast[Boolean]

tests/run-macros/reflect-pos-fun/assert_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object scalatest {
1010

1111
cond.unseal.underlyingArgument match {
1212
case t @ Apply(TypeApply(Select(lhs, op), targs), rhs) =>
13-
let(lhs) { left =>
14-
lets(rhs) { rs =>
13+
Block.let(lhs) { left =>
14+
Block.let(rhs) { rs =>
1515
val app = Select.overloaded(left, op, targs.map(_.tpe), rs)
1616
val b = app.seal.cast[Boolean]
1717
'{ scala.Predef.assert($b) }.unseal

tests/run-macros/reflect-select-constructor/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
Block.let(app) { result =>
2121
val l = left.seal
2222
val r = right.seal
2323
val b = result.seal.cast[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.seal.cast[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
let(lhs) { left =>
32-
let(rhs) { right =>
31+
Block.let(lhs) { left =>
32+
Block.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
Block.let(Apply(app, implicits)) { result =>
3535
val l = left.seal
3636
val r = right.seal
3737
val b = result.seal.cast[Boolean]

tests/run-macros/reflect-select-copy-2/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
19-
let(Apply(Select.copy(sel)(left, op), right :: Nil)) { result =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
19+
Block.let(Apply(Select.copy(sel)(left, op), right :: Nil)) { result =>
2020
val l = left.seal
2121
val r = right.seal
2222
val b = result.seal.cast[Boolean]
@@ -27,9 +27,9 @@ object scalatest {
2727
}.seal.cast[Unit]
2828
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
2929
if isImplicitMethodType(f.tpe) =>
30-
let(lhs) { left =>
31-
let(rhs) { right =>
32-
let(Apply(Apply(Select.copy(sel)(Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
30+
Block.let(lhs) { left =>
31+
Block.let(rhs) { right =>
32+
Block.let(Apply(Apply(Select.copy(sel)(Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
3333
val l = left.seal
3434
val r = right.seal
3535
val b = result.seal.cast[Boolean]

tests/run-macros/reflect-select-symbol-constructor/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
1919
val app = Apply(Select(left, sel.symbol), right :: Nil)
20-
let(app) { result =>
20+
Block.let(app) { result =>
2121
val l = left.seal
2222
val r = right.seal
2323
val b = result.seal.cast[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.seal.cast[Unit]
2929
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
let(lhs) { left =>
32-
let(rhs) { right =>
31+
Block.let(lhs) { left =>
32+
Block.let(rhs) { right =>
3333
val app = Apply(Select(Apply(qual, left :: Nil), sel.symbol), right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
Block.let(Apply(app, implicits)) { result =>
3535
val l = left.seal
3636
val r = right.seal
3737
val b = result.seal.cast[Boolean]

tests/run-macros/reflect-select-value-class/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
cond.unseal.underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
let(lhs) { left =>
18-
let(rhs) { right =>
17+
Block.let(lhs) { left =>
18+
Block.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
Block.let(app) { result =>
2121
val l = left.seal
2222
val r = right.seal
2323
val b = result.seal.cast[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.seal.cast[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
let(lhs) { left =>
32-
let(rhs) { right =>
31+
Block.let(lhs) { left =>
32+
Block.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
Block.let(Apply(app, implicits)) { result =>
3535
val l = left.seal
3636
val r = right.seal
3737
val b = result.seal.cast[Boolean]

tests/run-macros/tasty-unsafe-let/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object Macros {
1010

1111
val rhsTerm = rhs.unseal
1212

13-
import qctx.reflect.{let => letTerm}
14-
letTerm(rhsTerm) { rhsId =>
13+
import qctx.reflect._
14+
Block.let(rhsTerm) { rhsId =>
1515
Expr.betaReduce('{$body(${rhsId.seal.asInstanceOf[Expr[T]]})}).unseal // Dangerous uncheked cast!
1616
}.seal.cast[Unit]
1717
}

0 commit comments

Comments
 (0)