Skip to content

Commit c93da1b

Browse files
committed
Add ensureApplied to the quotes reflect API
1 parent 903cdf9 commit c93da1b

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
484484
argss.foldLeft(self: Term)(Apply(_, _))
485485
def appliedToNone: Apply =
486486
self.appliedToArgs(Nil)
487+
def ensureApplied: Term =
488+
if (self.tpe.widen.isParameterless) self else self.appliedToNone
487489
def appliedToType(targ: TypeRepr): Term =
488490
self.appliedToTypes(targ :: Nil)
489491
def appliedToTypes(targs: List[TypeRepr]): Term =

library/src/scala/quoted/Quotes.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
890890
/** The current tree applied to (): `tree()` */
891891
def appliedToNone: Apply
892892

893+
/** The current tree applied to `()` unless the tree's widened type is parameterless */
894+
def ensureApplied: Term
895+
893896
/** The current tree applied to given type argument: `tree[targ]` */
894897
def appliedToType(targ: TypeRepr): Term
895898

project/MiMaFilters.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ object MiMaFilters {
140140
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ValDefModule.let"),
141141
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newClass"),
142142
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newModule"),
143+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TermMethods.ensureApplied"),
143144

144145
// Change `experimental` annotation to a final class
145146
ProblemFilters.exclude[FinalClassProblem]("scala.annotation.experimental"),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import scala.quoted._
3+
4+
object TestMethods:
5+
def a1 = ()
6+
def a2() = ()
7+
8+
transparent inline def runMacro() = ${runMacroImpl}
9+
def runMacroImpl(using Quotes): Expr[Any] =
10+
import quotes.reflect._
11+
12+
// ensureApplied test
13+
Select.unique('{TestMethods}.asTerm, "a1").ensureApplied match
14+
case Select(_, _) =>
15+
case _ => assert(false)
16+
Select.unique('{TestMethods}.asTerm, "a2").ensureApplied match
17+
case Apply(_, _) =>
18+
case _ => assert(false)
19+
20+
// regression test
21+
val Inlined(_, _, generated) = '{BigDecimal(0).toString()}.asTerm : @unchecked
22+
val Inlined(_, _, bigDecimal) = '{BigDecimal(0)}.asTerm : @unchecked
23+
val custom = Select.unique(bigDecimal, "toString").ensureApplied
24+
// ensure both have the same shape
25+
assert(custom.toString == generated.toString)
26+
custom.asExpr

tests/pos-macros/i23969/Main.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test() = runMacro()

0 commit comments

Comments
 (0)