Skip to content

Commit 832c509

Browse files
committed
Initialize constraints on the QuoteContext
1 parent 57d2804 commit 832c509

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ 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+
59+
def Constraints_context[T]: scala.quoted.QuoteContext =
60+
val ctx = rootContext.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
61+
dotty.tools.dotc.quoted.QuoteContext()(using ctx)
6062

6163
def Constraints_add(self: Context)(syms: List[Symbol]): Boolean =
6264
self.gadt.addToConstraint(syms)

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: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ 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 =>
@@ -162,18 +161,14 @@ object Matcher {
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 =>
@@ -183,9 +178,6 @@ object Matcher {
183178
})
184179
}
185180
}
186-
else {
187-
scrutineeTypeTree =?= patternTypeTree
188-
}
189181
}
190182

191183
private def hasPatternTypeAnnotation(sym: Symbol) = sym.annots.exists(isPatternTypeAnnotation)

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
3333
// Constraints //
3434
/////////////////
3535

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

library/src/scala/tasty/Reflection.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,22 +1363,22 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
13631363

13641364
object Type:
13651365

1366-
def apply(clazz: Class[_])(using ctx: Context): Type =
1366+
def apply(clazz: Class[_]): Type =
13671367
reflectSelf.Type_apply(clazz)
13681368

13691369
extension (self: Type):
13701370

13711371
/** Convert `Type` to an `quoted.Type[_]` */
1372-
def seal(using ctx: Context): scala.quoted.Type[_] =
1372+
def seal: scala.quoted.Type[_] =
13731373
new scala.internal.quoted.Type(Inferred(self), reflectSelf.compilerId)
13741374

13751375
/** Is `self` type the same as `that` type?
13761376
* This is the case iff `self <:< that` and `that <:< self`.
13771377
*/
1378-
def =:=(that: Type)(using ctx: Context): Boolean = reflectSelf.Type_isTypeEq(self)(that)
1378+
def =:=(that: Type): Boolean = reflectSelf.Type_isTypeEq(self)(that)
13791379

13801380
/** Is this type a subtype of that type? */
1381-
def <:<(that: Type)(using ctx: Context): Boolean = reflectSelf.Type_isSubType(self)(that)
1381+
def <:<(that: Type): Boolean = reflectSelf.Type_isSubType(self)(that)
13821382

13831383
/** Widen from singleton type to its underlying non-singleton
13841384
* base type by applying one or more `underlying` dereferences,
@@ -1992,6 +1992,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
19921992
/** The position of this symbol */
19931993
def pos: Position = reflectSelf.Symbol_pos(sym)
19941994

1995+
// TODO replace with different abstraction
19951996
def localContext(using ctx: Context): Context = ctx // reflectSelf.Symbol_localContext(sym)
19961997

19971998
/** The comment for this symbol, if any */

0 commit comments

Comments
 (0)