Skip to content

Commit f88921b

Browse files
committed
Switch to (using Context) for Typer
1 parent 4335575 commit f88921b

File tree

9 files changed

+266
-262
lines changed

9 files changed

+266
-262
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Erasure extends Phase with DenotTransformer {
118118

119119
def run(implicit ctx: Context): Unit = {
120120
val unit = ctx.compilationUnit
121-
unit.tpdTree = eraser.typedExpr(unit.tpdTree)(ctx.fresh.setTyper(eraser).setPhase(this.next))
121+
unit.tpdTree = eraser.typedExpr(unit.tpdTree)(using ctx.fresh.setTyper(eraser).setPhase(this.next))
122122
}
123123

124124
override def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = {

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

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ class TreeChecker extends Phase with SymTransformer {
4949

5050
val NoSuperClassFlags: FlagSet = Trait | Package
5151

52-
def testDuplicate(sym: Symbol, registry: mutable.Map[String, Symbol], typ: String)(implicit ctx: Context): Unit = {
52+
def testDuplicate(sym: Symbol, registry: mutable.Map[String, Symbol], typ: String)(using Context): Unit = {
5353
val name = sym.fullName.mangledString
5454
val isDuplicate = this.flatClasses && registry.contains(name)
5555
assert(!isDuplicate, s"$typ defined twice $sym ${sym.id} ${registry(name).id}")
5656
registry(name) = sym
5757
}
5858

59-
def checkCompanion(symd: SymDenotation)(implicit ctx: Context): Unit = {
59+
def checkCompanion(symd: SymDenotation)(using Context): Unit = {
6060
val cur = symd.linkedClass
6161
val prev = ctx.atPhase(ctx.phase.prev) {
6262
symd.symbol.linkedClass
@@ -66,7 +66,7 @@ class TreeChecker extends Phase with SymTransformer {
6666
assert(cur.exists, i"companion disappeared from $symd")
6767
}
6868

69-
def transformSym(symd: SymDenotation)(implicit ctx: Context): SymDenotation = {
69+
def transformSym(symd: SymDenotation)(using Context): SymDenotation = {
7070
val sym = symd.symbol
7171

7272
if (sym.isClass && !sym.isAbsent()) {
@@ -92,13 +92,13 @@ class TreeChecker extends Phase with SymTransformer {
9292

9393
def phaseName: String = "Ycheck"
9494

95-
def run(implicit ctx: Context): Unit =
95+
def run(using Context): Unit =
9696
if (ctx.settings.YtestPickler.value && ctx.phase.prev.isInstanceOf[Pickler])
9797
ctx.echo("Skipping Ycheck after pickling with -Ytest-pickler, the returned tree contains stale symbols")
9898
else if (ctx.phase.prev.isCheckable)
9999
check(ctx.base.allPhases.toIndexedSeq, ctx)
100100

101-
private def previousPhases(phases: List[Phase])(implicit ctx: Context): List[Phase] = phases match {
101+
private def previousPhases(phases: List[Phase])(using Context): List[Phase] = phases match {
102102
case (phase: MegaPhase) :: phases1 =>
103103
val subPhases = phase.miniPhases
104104
val previousSubPhases = previousPhases(subPhases.toList)
@@ -115,15 +115,15 @@ class TreeChecker extends Phase with SymTransformer {
115115
val squahsedPhase = ctx.base.squashed(prevPhase)
116116
ctx.echo(s"checking ${ctx.compilationUnit} after phase ${squahsedPhase}")
117117

118-
assertSelectWrapsNew(ctx.compilationUnit.tpdTree)(ctx)
118+
assertSelectWrapsNew(ctx.compilationUnit.tpdTree)(using ctx)
119119

120120
val checkingCtx = ctx
121121
.fresh
122122
.setMode(Mode.ImplicitsEnabled)
123123
.setReporter(new ThrowingReporter(ctx.reporter))
124124

125-
val checker = new Checker(previousPhases(phasesToRun.toList)(ctx))
126-
try checker.typedExpr(ctx.compilationUnit.tpdTree)(checkingCtx)
125+
val checker = new Checker(previousPhases(phasesToRun.toList)(using ctx))
126+
try checker.typedExpr(ctx.compilationUnit.tpdTree)(using checkingCtx)
127127
catch {
128128
case NonFatal(ex) => //TODO CHECK. Check that we are bootstrapped
129129
implicit val ctx = checkingCtx
@@ -139,9 +139,9 @@ class TreeChecker extends Phase with SymTransformer {
139139
private val everDefinedSyms = newMutableSymbolMap[untpd.Tree]
140140

141141
// don't check value classes after typer, as the constraint about constructors doesn't hold after transform
142-
override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context): Unit = ()
142+
override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(using Context): Unit = ()
143143

144-
def withDefinedSyms[T](trees: List[untpd.Tree])(op: => T)(implicit ctx: Context): T = {
144+
def withDefinedSyms[T](trees: List[untpd.Tree])(op: => T)(using Context): T = {
145145
var locally = List.empty[Symbol]
146146
for (tree <- trees) {
147147
val sym = tree.symbol
@@ -179,7 +179,7 @@ class TreeChecker extends Phase with SymTransformer {
179179
*
180180
* patBoundSyms.contains(sym) <=> sym.isPatternBound
181181
*/
182-
def withPatSyms[T](syms: List[Symbol])(op: => T)(implicit ctx: Context): T = {
182+
def withPatSyms[T](syms: List[Symbol])(op: => T)(using Context): T = {
183183
syms.foreach { sym =>
184184
assert(
185185
sym.isPatternBound,
@@ -203,7 +203,7 @@ class TreeChecker extends Phase with SymTransformer {
203203
res
204204
}
205205

206-
def assertDefined(tree: untpd.Tree)(implicit ctx: Context): Unit =
206+
def assertDefined(tree: untpd.Tree)(using Context): Unit =
207207
if (tree.symbol.maybeOwner.isTerm) {
208208
val sym = tree.symbol
209209
assert(
@@ -219,14 +219,14 @@ class TreeChecker extends Phase with SymTransformer {
219219
}
220220

221221
/** assert Java classes are not used as objects */
222-
def assertIdentNotJavaClass(tree: Tree)(implicit ctx: Context): Unit = tree match {
222+
def assertIdentNotJavaClass(tree: Tree)(using Context): Unit = tree match {
223223
case _ : untpd.Ident =>
224224
assert(!tree.symbol.isAllOf(JavaModule), "Java class can't be used as value: " + tree)
225225
case _ =>
226226
}
227227

228228
/** check Java classes are not used as objects */
229-
def checkIdentNotJavaClass(tree: Tree)(implicit ctx: Context): Unit = tree match {
229+
def checkIdentNotJavaClass(tree: Tree)(using Context): Unit = tree match {
230230
// case tree: untpd.Ident =>
231231
// case tree: untpd.Select =>
232232
// case tree: untpd.Bind =>
@@ -284,10 +284,10 @@ class TreeChecker extends Phase with SymTransformer {
284284
* made `private` in phase `UnlinkErasedDecls`. These symbols will be removed
285285
* completely in phase `Erasure` if they are defined in a currently compiled unit.
286286
*/
287-
override def excludeFromDoubleDeclCheck(sym: Symbol)(implicit ctx: Context): Boolean =
287+
override def excludeFromDoubleDeclCheck(sym: Symbol)(using Context): Boolean =
288288
sym.isEffectivelyErased && sym.is(Private) && !sym.initial.is(Private)
289289

290-
override def typed(tree: untpd.Tree, pt: Type = WildcardType)(implicit ctx: Context): Tree = {
290+
override def typed(tree: untpd.Tree, pt: Type = WildcardType)(using Context): Tree = {
291291
val tpdTree = super.typed(tree, pt)
292292
Typer.assertPositioned(tree)
293293
if (ctx.erasedTypes)
@@ -297,7 +297,7 @@ class TreeChecker extends Phase with SymTransformer {
297297
tpdTree
298298
}
299299

300-
override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(implicit ctx: Context): Tree = {
300+
override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree = {
301301
val res = tree match {
302302
case _: untpd.TypedSplice | _: untpd.Thicket | _: EmptyValDef[?] =>
303303
super.typedUnadapted(tree, pt, locked)
@@ -325,21 +325,21 @@ class TreeChecker extends Phase with SymTransformer {
325325
res
326326
}
327327

328-
def checkNotRepeated(tree: Tree)(implicit ctx: Context): tree.type = {
328+
def checkNotRepeated(tree: Tree)(using Context): tree.type = {
329329
def allowedRepeated = tree.tpe.widen.isRepeatedParam
330330

331331
assert(!tree.tpe.widen.isRepeatedParam || allowedRepeated, i"repeated parameter type not allowed here: $tree")
332332
tree
333333
}
334334

335335
/** Check that all methods have MethodicType */
336-
def isMethodType(pt: Type)(implicit ctx: Context): Boolean = pt match {
336+
def isMethodType(pt: Type)(using Context): Boolean = pt match {
337337
case at: AnnotatedType => isMethodType(at.parent)
338338
case _: MethodicType => true // MethodType, ExprType, PolyType
339339
case _ => false
340340
}
341341

342-
override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context): Tree = {
342+
override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree = {
343343
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
344344
assert(tree.isType || ctx.mode.is(Mode.Pattern) && untpd.isWildcardArg(tree) || !needsSelect(tree.tpe), i"bad type ${tree.tpe} for $tree # ${tree.uniqueId}")
345345
assertDefined(tree)
@@ -351,7 +351,7 @@ class TreeChecker extends Phase with SymTransformer {
351351
* calling `member` on the qualifier type.
352352
* Approximately means: The two symbols might be different but one still overrides the other.
353353
*/
354-
override def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = {
354+
override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
355355
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
356356
val tpe = tree.typeOpt
357357
val sym = tree.symbol
@@ -380,14 +380,14 @@ class TreeChecker extends Phase with SymTransformer {
380380
checkNotRepeated(super.typedSelect(tree, pt))
381381
}
382382

383-
override def typedThis(tree: untpd.This)(implicit ctx: Context): Tree = {
383+
override def typedThis(tree: untpd.This)(using Context): Tree = {
384384
val res = super.typedThis(tree)
385385
val cls = res.symbol
386386
assert(cls.isStaticOwner || ctx.owner.isContainedIn(cls), i"error while typing $tree, ${ctx.owner} is not contained in $cls")
387387
res
388388
}
389389

390-
private def checkOwner(tree: untpd.Tree)(implicit ctx: Context): Unit = {
390+
private def checkOwner(tree: untpd.Tree)(using Context): Unit = {
391391
def ownerMatches(symOwner: Symbol, ctxOwner: Symbol): Boolean =
392392
symOwner == ctxOwner ||
393393
ctxOwner.isWeakOwner && ownerMatches(symOwner, ctxOwner.owner)
@@ -396,7 +396,7 @@ class TreeChecker extends Phase with SymTransformer {
396396
i"owner chain = ${tree.symbol.ownersIterator.toList}%, %, ctxOwners = ${ctx.outersIterator.map(_.owner).toList}%, %")
397397
}
398398

399-
override def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context): Tree = {
399+
override def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(using Context): Tree = {
400400
val TypeDef(_, impl @ Template(constr, _, _, _)) = cdef
401401
assert(cdef.symbol == cls)
402402
assert(impl.symbol.owner == cls)
@@ -421,7 +421,7 @@ class TreeChecker extends Phase with SymTransformer {
421421
super.typedClassDef(cdef, cls)
422422
}
423423

424-
override def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context): Tree =
424+
override def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(using Context): Tree =
425425
withDefinedSyms(ddef.tparams) {
426426
withDefinedSyms(ddef.vparamss.flatten) {
427427
if (!sym.isClassConstructor && !(sym.name eq nme.STATIC_CONSTRUCTOR))
@@ -440,12 +440,12 @@ class TreeChecker extends Phase with SymTransformer {
440440
}
441441
}
442442

443-
override def typedCase(tree: untpd.CaseDef, sel: Tree, selType: Type, pt: Type)(implicit ctx: Context): CaseDef =
443+
override def typedCase(tree: untpd.CaseDef, sel: Tree, selType: Type, pt: Type)(using Context): CaseDef =
444444
withPatSyms(tpd.patVars(tree.pat.asInstanceOf[tpd.Tree])) {
445445
super.typedCase(tree, sel, selType, pt)
446446
}
447447

448-
override def typedClosure(tree: untpd.Closure, pt: Type)(implicit ctx: Context): Tree = {
448+
override def typedClosure(tree: untpd.Closure, pt: Type)(using Context): Tree = {
449449
if (!ctx.phase.lambdaLifted) nestingBlock match {
450450
case block @ Block((meth : DefDef) :: Nil, closure: Closure) =>
451451
assert(meth.symbol == closure.meth.symbol, "closure.meth symbol not equal to method symbol. Block: " + block.show)
@@ -459,10 +459,10 @@ class TreeChecker extends Phase with SymTransformer {
459459
super.typedClosure(tree, pt)
460460
}
461461

462-
override def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context): Tree =
462+
override def typedBlock(tree: untpd.Block, pt: Type)(using Context): Tree =
463463
withBlock(tree) { withDefinedSyms(tree.stats) { super.typedBlock(tree, pt) } }
464464

465-
override def typedInlined(tree: untpd.Inlined, pt: Type)(implicit ctx: Context): Tree =
465+
override def typedInlined(tree: untpd.Inlined, pt: Type)(using Context): Tree =
466466
withDefinedSyms(tree.bindings) { super.typedInlined(tree, pt) }
467467

468468
/** Check that all defined symbols have legal owners.
@@ -472,7 +472,7 @@ class TreeChecker extends Phase with SymTransformer {
472472
* is that we should be able to pull out an expression as an initializer
473473
* of a helper value without having to do a change owner traversal of the expression.
474474
*/
475-
override def typedStats(trees: List[untpd.Tree], exprOwner: Symbol)(implicit ctx: Context): (List[Tree], Context) = {
475+
override def typedStats(trees: List[untpd.Tree], exprOwner: Symbol)(using Context): (List[Tree], Context) = {
476476
for (tree <- trees) tree match {
477477
case tree: untpd.DefTree => checkOwner(tree)
478478
case _: untpd.Thicket => assert(false, i"unexpanded thicket $tree in statement sequence $trees%\n%")
@@ -481,12 +481,12 @@ class TreeChecker extends Phase with SymTransformer {
481481
super.typedStats(trees, exprOwner)
482482
}
483483

484-
override def typedLabeled(tree: untpd.Labeled)(implicit ctx: Context): Labeled = {
484+
override def typedLabeled(tree: untpd.Labeled)(using Context): Labeled = {
485485
checkOwner(tree.bind)
486486
withDefinedSyms(tree.bind :: Nil) { super.typedLabeled(tree) }
487487
}
488488

489-
override def typedReturn(tree: untpd.Return)(implicit ctx: Context): Return = {
489+
override def typedReturn(tree: untpd.Return)(using Context): Return = {
490490
val tree1 = super.typedReturn(tree)
491491
val from = tree1.from
492492
val fromSym = from.symbol
@@ -495,15 +495,15 @@ class TreeChecker extends Phase with SymTransformer {
495495
tree1
496496
}
497497

498-
override def typedWhileDo(tree: untpd.WhileDo)(implicit ctx: Context): Tree = {
498+
override def typedWhileDo(tree: untpd.WhileDo)(using Context): Tree = {
499499
assert((tree.cond ne EmptyTree) || ctx.phase.refChecked, i"invalid empty condition in while at $tree")
500500
super.typedWhileDo(tree)
501501
}
502502

503-
override def ensureNoLocalRefs(tree: Tree, pt: Type, localSyms: => List[Symbol])(implicit ctx: Context): Tree =
503+
override def ensureNoLocalRefs(tree: Tree, pt: Type, localSyms: => List[Symbol])(using Context): Tree =
504504
tree
505505

506-
override def adapt(tree: Tree, pt: Type, locked: TypeVars)(implicit ctx: Context): Tree = {
506+
override def adapt(tree: Tree, pt: Type, locked: TypeVars)(using Context): Tree = {
507507
def isPrimaryConstructorReturn =
508508
ctx.owner.isPrimaryConstructor && pt.isRef(ctx.owner.owner) && tree.tpe.isRef(defn.UnitClass)
509509
def infoStr(tp: Type) = tp match {
@@ -527,15 +527,15 @@ class TreeChecker extends Phase with SymTransformer {
527527
tree
528528
}
529529

530-
override def simplify(tree: Tree, pt: Type, locked: TypeVars)(implicit ctx: Context): tree.type = tree
530+
override def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type = tree
531531
}
532532

533533
/**
534534
* Checks that `New` nodes are always wrapped inside `Select` nodes.
535535
*/
536-
def assertSelectWrapsNew(tree: Tree)(implicit ctx: Context): Unit =
536+
def assertSelectWrapsNew(tree: Tree)(using Context): Unit =
537537
(new TreeAccumulator[tpd.Tree] {
538-
override def apply(parent: Tree, tree: Tree)(implicit ctx: Context): Tree = {
538+
override def apply(parent: Tree, tree: Tree)(using Context): Tree = {
539539
tree match {
540540
case tree: New if !parent.isInstanceOf[tpd.Select] =>
541541
assert(assertion = false, i"`New` node must be wrapped in a `Select`:\n parent = ${parent.show}\n child = ${tree.show}")
@@ -554,7 +554,7 @@ object TreeChecker {
554554
/** - Check that TypeParamRefs and MethodParams refer to an enclosing type.
555555
* - Check that all type variables are instantiated.
556556
*/
557-
def checkNoOrphans(tp0: Type, tree: untpd.Tree = untpd.EmptyTree)(implicit ctx: Context): Type = new TypeMap() {
557+
def checkNoOrphans(tp0: Type, tree: untpd.Tree = untpd.EmptyTree)(using Context): Type = new TypeMap() {
558558
val definedBinders = new java.util.IdentityHashMap[Type, Any]
559559
def apply(tp: Type): Type = {
560560
tp match {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ trait Applications extends Compatibility {
11041104
case tree: untpd.RefTree =>
11051105
val nestedCtx = ctx.fresh.setNewTyperState()
11061106
val ttree =
1107-
typedType(untpd.rename(tree, tree.name.toTypeName))(nestedCtx)
1107+
typedType(untpd.rename(tree, tree.name.toTypeName))(using nestedCtx)
11081108
ttree.tpe match {
11091109
case alias: TypeRef if alias.info.isTypeAlias && !nestedCtx.reporter.hasErrors =>
11101110
companionRef(alias) match {
@@ -2030,7 +2030,7 @@ trait Applications extends Compatibility {
20302030
val (core, pt1) = integrateTypeArgs(pt)
20312031
val app =
20322032
typed(untpd.Apply(core, untpd.TypedSplice(receiver) :: Nil), pt1, ctx.typerState.ownedVars)(
2033-
ctx.addMode(Mode.SynthesizeExtMethodReceiver))
2033+
using ctx.addMode(Mode.SynthesizeExtMethodReceiver))
20342034
def isExtension(tree: Tree): Boolean = methPart(tree) match {
20352035
case Inlined(call, _, _) => isExtension(call)
20362036
case tree @ Select(qual, nme.apply) => tree.symbol.is(Extension) || isExtension(qual)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ trait Checking {
11041104
// would have produced the same symbol without errors
11051105
def allowAccess(name: Name, sym: Symbol): Boolean = {
11061106
val testCtx = caseCtx.fresh.setNewTyperState()
1107-
val ref = ctx.typer.typedIdent(untpd.Ident(name), WildcardType)(testCtx)
1107+
val ref = ctx.typer.typedIdent(untpd.Ident(name), WildcardType)(using testCtx)
11081108
ref.symbol == sym && !testCtx.reporter.hasErrors
11091109
}
11101110
checkRefsLegal(tree, cdef.symbol, allowAccess, "enum case")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ object Inliner {
283283
val parseErrors = ctx2.reporter.allErrors.toList
284284
res ++= parseErrors.map(e => ErrorKind.Parser -> e)
285285
if !stopAfterParser || res.isEmpty
286-
ctx2.typer.typed(tree2)(ctx2)
286+
ctx2.typer.typed(tree2)(using ctx2)
287287
val typerErrors = ctx2.reporter.allErrors.filterNot(parseErrors.contains)
288288
res ++= typerErrors.map(e => ErrorKind.Typer -> e)
289289
res.toList
@@ -684,7 +684,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
684684
}
685685

686686
// Run a typing pass over the inlined tree. See InlineTyper for details.
687-
val expansion1 = inlineTyper.typed(expansion)(inlineCtx)
687+
val expansion1 = inlineTyper.typed(expansion)(using inlineCtx)
688688

689689
if (ctx.settings.verbose.value) {
690690
inlining.println(i"to inline = $rhsToInline")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,10 +1322,10 @@ class Namer { typer: Typer =>
13221322
}
13231323

13241324
def typedAheadType(tree: Tree, pt: Type = WildcardType)(implicit ctx: Context): tpd.Tree =
1325-
typedAhead(tree, typer.typed(_, pt)(ctx retractMode Mode.PatternOrTypeBits addMode Mode.Type))
1325+
typedAhead(tree, typer.typed(_, pt)(using ctx.retractMode(Mode.PatternOrTypeBits).addMode(Mode.Type)))
13261326

13271327
def typedAheadExpr(tree: Tree, pt: Type = WildcardType)(implicit ctx: Context): tpd.Tree =
1328-
typedAhead(tree, typer.typed(_, pt)(ctx retractMode Mode.PatternOrTypeBits))
1328+
typedAhead(tree, typer.typed(_, pt)(using ctx.retractMode(Mode.PatternOrTypeBits)))
13291329

13301330
def typedAheadAnnotation(tree: Tree)(implicit ctx: Context): tpd.Tree =
13311331
typedAheadExpr(tree, defn.AnnotationClass.typeRef)

0 commit comments

Comments
 (0)