Skip to content

Support custom val symbol in ValDef.let #13936

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term)(using ctx1))
val refs = vdefs.map(vdef => tpd.ref(vdef.symbol).asInstanceOf[Ref])
Block(vdefs, body(refs))

def let(owner: Symbol, symbol: Symbol, rhs: Term)(body: Ref => Term): Term =
val vdef = apply(symbol, Some(rhs)).changeOwner(owner)
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ref]
Block(List(vdef), body(ref))
end ValDef

given ValDefMethods: ValDefMethods with
Expand Down
35 changes: 35 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,41 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
def let(owner: Symbol, terms: List[Term])(body: List[Ref] => Term): Term

/**
* Creates a block `{ val x = <rhs: Term>; <body(x): Term> }`
* with the given symbol for the `val` (allowing to specify `Flags`).
*
* Usage:
* ```scala sc:nocompile
* val tpe = TypeRepr.of[String]
*
* val valSym = Symbol.newVal(
* Symbol.spliceOwner,
* "myValName",
* tpe,
* Flags.Lazy,
* Symbol.noSymbol
* )
*
* ValDef.let(Symbol.spliceOwner, valSym, Expr("foo")) { v =>
* '{ println(v) }.asTerm
* }
* ```
*
* In this way, it's possible to create an `Ref` to the `val`
* before its definition (required for recursive/lazy definitions).
*
* ```scala sc:nocompile
* // After `tpe` and `valSym` but before `let` call
* val earlyRef = Typed(Ref(valSym), Inferred(tpe))
* ```
*
* @param symbol the `val` symbol
* @see `Symbol.newVal`
*/
@experimental
def let(owner: Symbol, symbol: Symbol, rhs: Term)(body: Ref => Term): Term
}

/** Makes extension methods on `ValDef` available without any imports */
Expand Down
2 changes: 2 additions & 0 deletions tests/run-macros/i13929.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
early=i13929
vref=i13929
26 changes: 26 additions & 0 deletions tests/run-macros/i13929/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.quoted.*

inline def testSymLet[T](f: T) = ${ testSymLetImpl[T]('f) }

def testSymLetImpl[T: Type](f: Expr[T])(using Quotes): Expr[Unit] = {
import quotes.reflect.*

val tpe = TypeRepr.of[T]

val valSym = Symbol.newVal(
Symbol.spliceOwner,
"myVal",
tpe,
Flags.Lazy,
Symbol.noSymbol
)

val earlyRef = Typed(Ref(valSym), Inferred(tpe))

ValDef.let(Symbol.spliceOwner, valSym, f) { vref =>
'{
println("early=" + ${earlyRef.asExpr}.toString())
println("vref=" + ${vref.asExpr}.toString())
}.asTerm
}.asExprOf[Unit]
}
1 change: 1 addition & 0 deletions tests/run-macros/i13929/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = testSymLet("i13929")