Skip to content

Commit 8106cc9

Browse files
committed
Remove constraints abstraction from Context
The constraints API is available durring the execution of the Matcher and initialized before the matching strats independently of the local contexts in the Matcher.
1 parent 68d5d68 commit 8106cc9

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
5555
// Constraints //
5656
/////////////////
5757

58-
def Constraints_init(self: Context): Context =
59-
self.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
58+
def Constraints_context[T]: scala.quoted.QuoteContext =
59+
val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
60+
dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
6061

61-
def Constraints_add(self: Context)(syms: List[Symbol]): Boolean =
62-
self.gadt.addToConstraint(syms)
62+
def Constraints_add(syms: List[Symbol]): Boolean =
63+
ctx.gadt.addToConstraint(syms)
6364

64-
def Constraints_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
65-
self.gadt.approximation(sym, fromBelow)
65+
def Constraints_approximation(sym: Symbol, fromBelow: Boolean): Type =
66+
ctx.gadt.approximation(sym, fromBelow)
6667

6768
////////////
6869
// Source //

library/src-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ object Expr {
5353
*/
5454
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[Any])(using patternExpr: scala.quoted.Expr[Any],
5555
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
56-
new Matcher.QuoteMatcher[qctx.type](qctx).termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
56+
val qctx1 = quoteContextWithCompilerInterface(qctx)
57+
val qctx2 = if hasTypeSplices then qctx1.tasty.Constraints_context else qctx1
58+
given qctx2.type = qctx2
59+
new Matcher.QuoteMatcher[qctx2.type](qctx2).termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
5760
}
5861

5962
/** Returns a null expresssion equivalent to `'{null}` */

library/src-bootstrapped/scala/internal/quoted/Matcher.scala

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ object Matcher {
128128

129129
// TODO improve performance
130130

131-
// TODO use flag from qctx.tasty.rootContext. Maybe -debug or add -debug-macros
131+
// TODO use flag from qctx.tasty. Maybe -debug or add -debug-macros
132132
private final val debug = false
133133

134134
import qctx.tasty._
@@ -149,43 +149,35 @@ object Matcher {
149149

150150
def termMatch(scrutineeTerm: Term, patternTerm: Term, hasTypeSplices: Boolean): Option[Tuple] = {
151151
given Env = Map.empty
152-
if (hasTypeSplices) {
153-
val ctx: Context = qctx.tasty.Constraints_init(rootContext)
154-
given Context = ctx
155-
val matchings = scrutineeTerm =?= patternTerm
152+
val matchings = scrutineeTerm =?= patternTerm
153+
if !hasTypeSplices then matchings
154+
else {
156155
// After matching and doing all subtype checks, we have to approximate all the type bindings
157156
// that we have found and seal them in a quoted.Type
158157
matchings.asOptionOfTuple.map { tup =>
159158
Tuple.fromArray(tup.toArray.map { // TODO improve performance
160-
case x: SymBinding => qctx.tasty.Constraints_approximation(summon[Context])(x.sym, !x.fromAbove).seal
159+
case x: SymBinding => qctx.tasty.Constraints_approximation(x.sym, !x.fromAbove).seal
161160
case x => x
162161
})
163162
}
164163
}
165-
else {
166-
scrutineeTerm =?= patternTerm
167-
}
168164
}
169165

170166
// TODO factor out common logic with `termMatch`
171167
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree, hasTypeSplices: Boolean): Option[Tuple] = {
172168
given Env = Map.empty
173-
if (hasTypeSplices) {
174-
val ctx: Context = qctx.tasty.Constraints_init(rootContext)
175-
given Context = ctx
176-
val matchings = scrutineeTypeTree =?= patternTypeTree
169+
val matchings = scrutineeTypeTree =?= patternTypeTree
170+
if !hasTypeSplices then matchings
171+
else {
177172
// After matching and doing all subtype checks, we have to approximate all the type bindings
178173
// that we have found and seal them in a quoted.Type
179174
matchings.asOptionOfTuple.map { tup =>
180175
Tuple.fromArray(tup.toArray.map { // TODO improve performance
181-
case x: SymBinding => qctx.tasty.Constraints_approximation(summon[Context])(x.sym, !x.fromAbove).seal
176+
case x: SymBinding => qctx.tasty.Constraints_approximation(x.sym, !x.fromAbove).seal
182177
case x => x
183178
})
184179
}
185180
}
186-
else {
187-
scrutineeTypeTree =?= patternTypeTree
188-
}
189181
}
190182

191183
private def hasPatternTypeAnnotation(sym: Symbol) = sym.annots.exists(isPatternTypeAnnotation)
@@ -326,7 +318,7 @@ object Matcher {
326318
fn1 =?= fn2 &&& args1 =?= args2
327319

328320
case (Block(stats1, expr1), Block(binding :: stats2, expr2)) if isTypeBinding(binding) =>
329-
qctx.tasty.Constraints_add(summon[Context])(binding.symbol :: Nil)
321+
qctx.tasty.Constraints_add(binding.symbol :: Nil)
330322
matched(new SymBinding(binding.symbol, hasFromAboveAnnotation(binding.symbol))) &&& Block(stats1, expr1) =?= Block(stats2, expr2)
331323

332324
/* Match block */
@@ -343,7 +335,7 @@ object Matcher {
343335

344336
case (scrutinee, Block(typeBindings, expr2)) if typeBindings.forall(isTypeBinding) =>
345337
val bindingSymbols = typeBindings.map(_.symbol)
346-
qctx.tasty.Constraints_add(summon[Context])(bindingSymbols)
338+
qctx.tasty.Constraints_add(bindingSymbols)
347339
bindingSymbols.foldRight(scrutinee =?= expr2)((x, acc) => matched(new SymBinding(x, hasFromAboveAnnotation(x))) &&& acc)
348340

349341
/* Match if */

library/src-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ object Type {
4040
*/
4141
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: scala.quoted.Type[_])(using patternType: scala.quoted.Type[_],
4242
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
43-
new Matcher.QuoteMatcher[qctx.type](qctx).typeTreeMatch(scrutineeType.unseal, patternType.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
43+
val qctx1 = quoteContextWithCompilerInterface(qctx)
44+
val qctx2 = if hasTypeSplices then qctx1.tasty.Constraints_context else qctx1
45+
given qctx2.type = qctx2
46+
new Matcher.QuoteMatcher[qctx2.type](qctx2).typeTreeMatch(scrutineeType.unseal, patternType.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
4447
}
4548

4649
def Unit: QuoteContext ?=> quoted.Type[Unit] =

library/src/scala/internal/tasty/CompilerInterface.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
3333
// Constraints //
3434
/////////////////
3535

36-
def Constraints_init(self: Context): Context
37-
def Constraints_add(self: Context)(syms: List[Symbol]): Boolean
38-
def Constraints_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type
36+
def Constraints_context[T]: scala.quoted.QuoteContext
37+
def Constraints_add(syms: List[Symbol]): Boolean
38+
def Constraints_approximation(sym: Symbol, fromBelow: Boolean): Type
3939

4040
////////////
4141
// Source //

0 commit comments

Comments
 (0)