Skip to content

Commit 9363540

Browse files
Merge pull request #7681 from dotty-staging/remove-QuoteContext-macro-context
Fix #7618: Remove QuoteContext.macroContext
2 parents 5a695a5 + 309a1bc commit 9363540

File tree

9 files changed

+75
-29
lines changed

9 files changed

+75
-29
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

-2
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,6 @@ class Definitions {
645645
@tu lazy val QuotedExprModule_unitExpr: Symbol = QuotedExprModule.requiredMethod(nme.unitExpr)
646646

647647
@tu lazy val QuoteContextClass: ClassSymbol = ctx.requiredClass("scala.quoted.QuoteContext")
648-
@tu lazy val QuoteContextModule: Symbol = QuoteContextClass.companionModule
649-
@tu lazy val QuoteContext_macroContext: Symbol = QuoteContextModule.requiredMethod("macroContext")
650648

651649
@tu lazy val LiftableModule: Symbol = ctx.requiredModule("scala.quoted.Liftable")
652650
@tu lazy val LiftableModule_BooleanIsLiftable: Symbol = LiftableModule.requiredMethod("BooleanIsLiftable")

compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala

-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
192192
case Some(l) =>
193193
l == level ||
194194
level == -1 && (
195-
sym == defn.QuoteContext_macroContext ||
196195
// here we assume that Splicer.canBeSpliced was true before going to level -1,
197196
// this implies that all non-inline arguments are quoted and that the following two cases are checked
198197
// on inline parameters or type parameters.

compiler/src/dotty/tools/dotc/transform/Splicer.scala

-9
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ object Splicer {
9898
case Literal(Constant(value)) =>
9999
// OK
100100

101-
case _ if tree.symbol == defn.QuoteContext_macroContext =>
102-
// OK
103-
104101
case Call(fn, args)
105102
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package)) ||
106103
fn.symbol.is(Module) || fn.symbol.isStatic ||
@@ -191,9 +188,6 @@ object Splicer {
191188
case Literal(Constant(value)) =>
192189
interpretLiteral(value)
193190

194-
case _ if tree.symbol == defn.QuoteContext_macroContext =>
195-
interpretQuoteContext()
196-
197191
// TODO disallow interpreted method calls as arguments
198192
case Call(fn, args) =>
199193
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package))
@@ -263,9 +257,6 @@ object Splicer {
263257
private def interpretVarargs(args: List[Object])(implicit env: Env): Object =
264258
args.toSeq
265259

266-
private def interpretQuoteContext()(implicit env: Env): Object =
267-
QuoteContext()
268-
269260
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol)(implicit env: Env): List[Object] => Object = {
270261
val (inst, clazz) =
271262
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE))

compiler/src/dotty/tools/dotc/typer/Implicits.scala

-7
Original file line numberDiff line numberDiff line change
@@ -732,11 +732,6 @@ trait Implicits { self: Typer =>
732732
}
733733
}
734734

735-
lazy val synthesizedQuoteContext: SpecialHandler =
736-
(formal, span) => implicit ctx =>
737-
if (ctx.inMacro || enclosingInlineds.nonEmpty) ref(defn.QuoteContext_macroContext)
738-
else EmptyTree
739-
740735
lazy val synthesizedTupleFunction: SpecialHandler =
741736
(formal, span) => implicit ctx => formal match {
742737
case AppliedType(_, funArgs @ fun :: tupled :: Nil) =>
@@ -1082,7 +1077,6 @@ trait Implicits { self: Typer =>
10821077
mySpecialHandlers = List(
10831078
defn.ClassTagClass -> synthesizedClassTag,
10841079
defn.QuotedTypeClass -> synthesizedTypeTag,
1085-
defn.QuoteContextClass -> synthesizedQuoteContext,
10861080
defn.EqlClass -> synthesizedEq,
10871081
defn.TupledFunctionClass -> synthesizedTupleFunction,
10881082
defn.ValueOfClass -> synthesizedValueOf,
@@ -1464,7 +1458,6 @@ trait Implicits { self: Typer =>
14641458
case alt1: SearchSuccess =>
14651459
var diff = compareCandidate(alt1, alt2.ref, alt2.level)
14661460
assert(diff <= 0) // diff > 0 candidates should already have been eliminated in `rank`
1467-
14681461
if diff == 0 then
14691462
// Fall back: if both results are extension method applications,
14701463
// compare the extension methods instead of their wrappers.

library/src/scala/quoted/QuoteContext.scala

-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,3 @@ class QuoteContext(val tasty: scala.tasty.Reflection) {
4747
}
4848

4949
}
50-
51-
object QuoteContext {
52-
def macroContext: QuoteContext = throw new Exception("Not in inline macro.")
53-
}

tests/neg/i7618.scala

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package macros
2+
3+
import scala.quoted.{given, _}
4+
5+
enum Exp {
6+
case Num(n: Int)
7+
case Plus(e1: Exp, e2: Exp)
8+
case Var(x: String)
9+
case Let(x: String, e: Exp, in: Exp)
10+
}
11+
12+
object Compiler {
13+
import Exp._
14+
15+
inline def compile(e: Exp, env: Map[String, Expr[Int]])(given ctx: QuoteContext): Expr[Int] = inline e match { // error
16+
case Num(n) =>
17+
Expr(n)
18+
case Plus(e1, e2) =>
19+
'{ ${ compile(e1, env) } + ${ compile(e2, env) } }
20+
case Var(x) =>
21+
env(x)
22+
case Let(x, e, body) =>
23+
'{ val y = ${ compile(e, env) }; ${ compile(body, env + (x -> 'y)) } }
24+
}
25+
}
26+
27+
object Example {
28+
def run(): Unit = {
29+
import Exp._
30+
31+
val exp = Plus(Plus(Num(2), Var("x")), Num(4))
32+
val letExp = Let("x", Num(3), exp)
33+
34+
Compiler.compile(letExp, Map.empty)(given QuoteContext.macroContext) // error
35+
}
36+
}

tests/neg/i7618b.scala

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package macros
2+
3+
import scala.quoted.{given, _}
4+
5+
enum Exp {
6+
case Num(n: Int)
7+
case Plus(e1: Exp, e2: Exp)
8+
case Var(x: String)
9+
case Let(x: String, e: Exp, in: Exp)
10+
}
11+
12+
object Compiler {
13+
import Exp._
14+
15+
inline def compile(e: Exp, env: Map[String, Expr[Int]])(given ctx: QuoteContext): Expr[Int] = inline e match { // error
16+
case Num(n) =>
17+
Expr(n)
18+
case Plus(e1, e2) =>
19+
'{ ${ compile(e1, env) } + ${ compile(e2, env) } }
20+
case Var(x) =>
21+
env(x)
22+
case Let(x, e, body) =>
23+
'{ val y = ${ compile(e, env) }; ${ compile(body, env + (x -> 'y)) } }
24+
}
25+
}
26+
27+
object Example {
28+
def run(): Unit = {
29+
import Exp._
30+
31+
val exp = Plus(Plus(Num(2), Var("x")), Num(4))
32+
val letExp = Let("x", Num(3), exp)
33+
34+
Compiler.compile(letExp, Map.empty)(given (??? : QuoteContext))
35+
}
36+
}

tests/pos/i7358.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package test
33
import scala.quoted._
44
import scala.compiletime._
55

6-
inline def summonT[Tp <: Tuple] <: Tuple = inline erasedValue[Tp] match {
6+
inline def summonT[Tp <: Tuple](given QuoteContext) <: Tuple = inline erasedValue[Tp] match {
77
case _ : Unit => ()
88
case _ : (hd *: tl) => {
99
type H = hd
@@ -13,4 +13,4 @@ inline def summonT[Tp <: Tuple] <: Tuple = inline erasedValue[Tp] match {
1313
}
1414
}
1515

16-
def test[T : Type] = summonT[Tuple1[List[T]]]
16+
def test[T : Type](given QuoteContext) = summonT[Tuple1[List[T]]]

tests/run-staging/quote-macro-in-splice/quoted_2.scala

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ object Test {
88
val x = '{
99
val y = 1
1010
${
11-
// FIXME remove context when $ will provide one
12-
// Currently we would accidentally capture the one from withQuoteContext
13-
inline def a(z: Int): Int = ${ impl('z)(given QuoteContext.macroContext) }
11+
inline def a(z: Int): Int = ${ impl('z) }
1412
val b = Expr(a(7))
1513
'{ y + $b }
1614
}
1715
}
1816
println(x.show)
1917
}
20-
2118
}

0 commit comments

Comments
 (0)