Skip to content

Commit b8c4ee7

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. Also add variant of let with custom name
1 parent 7b5cdba commit b8c4ee7

File tree

14 files changed

+63
-61
lines changed

14 files changed

+63
-61
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
242242
tpd.cpy.ValDef(original)(name.toTermName, tpt, rhs.getOrElse(tpd.EmptyTree))
243243
def unapply(vdef: ValDef): Option[(String, TypeTree, Option[Term])] =
244244
Some((vdef.name.toString, vdef.tpt, optional(vdef.rhs)))
245+
246+
def let(name: String, rhs: Term)(body: Ident => Term): Term =
247+
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)
248+
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ident]
249+
Block(List(vdef), body(ref))
250+
251+
def let(terms: List[Term])(body: List[Ident] => Term): Term =
252+
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term))
253+
val refs = vdefs.map(vdef => tpd.ref(vdef.symbol).asInstanceOf[Ident])
254+
Block(vdefs, body(refs))
245255
end ValDef
246256

247257
object ValDefMethodsImpl extends ValDefMethods:

library/src/scala/tasty/Reflection.scala

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ trait Reflection { reflection =>
311311
def apply(symbol: Symbol, rhs: Option[Term]): ValDef
312312
def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef
313313
def unapply(vdef: ValDef): Option[(String, TypeTree, Option[Term])]
314+
315+
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }` */
316+
def let(name: String, rhs: Term)(body: Ident => Term): Term
317+
318+
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
319+
def let(rhs: Term)(body: Ident => Term): Term = let("x", rhs)(body)
320+
321+
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
322+
def let(terms: List[Term])(body: List[Ident] => Term): Term
314323
}
315324

316325
given ValDefMethods as ValDefMethods = ValDefMethodsImpl
@@ -3603,22 +3612,4 @@ trait Reflection { reflection =>
36033612

36043613
end TreeMap
36053614

3606-
// TODO: extract from Reflection
3607-
3608-
/** Bind the `rhs` to a `val` and use it in `body` */
3609-
def let(rhs: Term)(body: Ident => Term): Term = {
3610-
val sym = Symbol.newVal(Symbol.currentOwner, "x", rhs.tpe.widen, Flags.EmptyFlags, Symbol.noSymbol)
3611-
Block(List(ValDef(sym, Some(rhs))), body(Ref(sym).asInstanceOf[Ident]))
3612-
}
3613-
3614-
/** Bind the given `terms` to names and use them in the `body` */
3615-
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
3616-
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
3617-
case Nil => body(acc)
3618-
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
3619-
}
3620-
rec(terms, Nil)
3621-
}
3622-
3623-
36243615
}

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ object scalatest {
77
def assertImpl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
88
import qctx.reflect._
99
import util._
10+
import ValDef.let
1011

1112
cond.unseal.underlyingArgument match {
1213
case t @ Apply(Select(lhs, op), rhs :: Nil) =>

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+
ValDef.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+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
ValDef.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+
ValDef.let(lhs) { left =>
32+
ValDef.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
1919
val app = left.select(sel.symbol).appliedTo(right)
20-
let(app) { result =>
20+
ValDef.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+
ValDef.let(lhs) { left =>
32+
ValDef.let(rhs) { right =>
3333
val app = qual.appliedTo(left).select(sel.symbol).appliedTo(right)
34-
let(Apply(app, implicits)) { result =>
34+
ValDef.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+
ValDef.let(lhs) { left =>
14+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
ValDef.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+
ValDef.let(lhs) { left =>
32+
ValDef.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
19+
ValDef.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+
ValDef.let(lhs) { left =>
31+
ValDef.let(rhs) { right =>
32+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
1919
val app = Apply(Select(left, sel.symbol), right :: Nil)
20-
let(app) { result =>
20+
ValDef.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+
ValDef.let(lhs) { left =>
32+
ValDef.let(rhs) { right =>
3333
val app = Apply(Select(Apply(qual, left :: Nil), sel.symbol), right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
ValDef.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+
ValDef.let(lhs) { left =>
18+
ValDef.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
let(app) { result =>
20+
ValDef.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+
ValDef.let(lhs) { left =>
32+
ValDef.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
let(Apply(app, implicits)) { result =>
34+
ValDef.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+
ValDef.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)