Skip to content

Commit 2aa9079

Browse files
Add missing version of ValDef.let which accepts flags
1 parent 1b5138d commit 2aa9079

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
369369
def unapply(vdef: ValDef): (String, TypeTree, Option[Term]) =
370370
(vdef.name.toString, vdef.tpt, optional(vdef.rhs))
371371

372+
def let(owner: Symbol, name: String, rhs: Term, flags: Flags)(body: Ref => Term): Term =
373+
Symbol.checkValidFlags(flags.toTermFlags, Flags.validValFlags)
374+
val vdef = tpd.SyntheticValDef(name.toTermName, rhs, flags)(using ctx.withOwner(owner))
375+
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ref]
376+
Block(List(vdef), body(ref))
377+
372378
def let(owner: Symbol, name: String, rhs: Term)(body: Ref => Term): Term =
373379
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)(using ctx.withOwner(owner))
374380
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ref]
@@ -2863,7 +2869,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28632869

28642870
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol
28652871

2866-
private inline def checkValidFlags(inline flags: Flags, inline valid: Flags): Unit =
2872+
private[QuotesImpl] inline def checkValidFlags(inline flags: Flags, inline valid: Flags): Unit =
28672873
xCheckMacroAssert(
28682874
flags <= valid,
28692875
s"Received invalid flags. Expected flags ${flags.show} to only contain a subset of ${valid.show}."

library/src/scala/quoted/Quotes.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
682682
def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef
683683
def unapply(vdef: ValDef): (String, TypeTree, Option[Term])
684684

685+
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
686+
*
687+
* Usage:
688+
* ```
689+
* ValDef.let(owner, "x", rhs1, Flags.Lazy) { x =>
690+
* ValDef.let(x.symbol.owner, "y", rhs2, Flags.Mutable) { y =>
691+
* // use `x` and `y`
692+
* }
693+
* }
694+
* ```
695+
*
696+
* @param flags extra flags to with which the symbol should be constructed. Can be `Private | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum | JavaStatic | Synthetic | Artifact`
697+
*/
698+
// Keep: `flags` doc aligned with QuotesImpl's `validValFlags`
699+
def let(owner: Symbol, name: String, rhs: Term, flags: Flags)(body: Ref => Term): Term
700+
685701
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
686702
*
687703
* Usage:

project/MiMaFilters.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ object MiMaFilters {
1919

2020
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.Conversion.underlying"),
2121
ProblemFilters.exclude[MissingClassProblem]("scala.Conversion$"),
22+
23+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ValDefModule.let"),
24+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ValDefModule.let"),
2225
),
2326

2427
// Additions since last LTS

0 commit comments

Comments
 (0)