Skip to content

Commit e54f734

Browse files
committed
Switch to (using Context) in more files in typer
1 parent c06b024 commit e54f734

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ object Inliner {
130130
* @param unapp The tree of the pattern to inline
131131
* @return An `Unapply` with a `fun` containing the inlined call to the unapply
132132
*/
133-
def inlinedUnapply(unapp: tpd.UnApply)(using ctx: Context): Tree = {
133+
def inlinedUnapply(unapp: tpd.UnApply)(using Context): Tree = {
134134
// We cannot inline the unapply directly, since the pattern matcher relies on unapply applications
135135
// as signposts what to do. On the other hand, we can do the inlining only in typer, not afterwards.
136136
// So the trick is to create a "wrapper" unapply in an anonymous class that has the inlined unapply
@@ -193,14 +193,14 @@ object Inliner {
193193
.reporting(i"retainer for $meth: $result", inlining)
194194

195195
/** Replace `Inlined` node by a block that contains its bindings and expansion */
196-
def dropInlined(inlined: Inlined)(implicit ctx: Context): Tree =
196+
def dropInlined(inlined: Inlined)(using Context): Tree =
197197
val tree1 =
198198
if inlined.bindings.isEmpty then inlined.expansion
199199
else cpy.Block(inlined)(inlined.bindings, inlined.expansion)
200200
// Reposition in the outer most inlined call
201201
if (enclosingInlineds.nonEmpty) tree1 else reposition(tree1, inlined.span)
202202

203-
def reposition(tree: Tree, callSpan: Span)(implicit ctx: Context): Tree = {
203+
def reposition(tree: Tree, callSpan: Span)(using Context): Tree = {
204204
// Reference test tests/run/i4947b
205205

206206
val curSource = ctx.compilationUnit.source
@@ -222,23 +222,22 @@ object Inliner {
222222
*/
223223
class Reposition extends TreeMap(cpyWithNewSource) {
224224

225-
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
225+
override def transform(tree: Tree)(using Context): Tree = {
226226
def finalize(copied: untpd.Tree) =
227227
val span = if tree.source == curSource then tree.span else callSpan
228228
copied.withSpan(span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
229229

230-
given as Context = ctx.withSource(curSource)
231-
232-
tree match {
233-
case tree: Ident => finalize(untpd.Ident(tree.name)(curSource))
234-
case tree: Literal => finalize(untpd.Literal(tree.const)(curSource))
235-
case tree: This => finalize(untpd.This(tree.qual)(curSource))
236-
case tree: JavaSeqLiteral => finalize(untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
237-
case tree: SeqLiteral => finalize(untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
238-
case tree: Bind => finalize(untpd.Bind(tree.name, transform(tree.body))(curSource))
239-
case tree: TypeTree => finalize(tpd.TypeTree(tree.tpe))
240-
case tree: DefTree => super.transform(tree).setDefTree
241-
case _ => super.transform(tree)
230+
inContext(ctx.withSource(curSource)) {
231+
tree match
232+
case tree: Ident => finalize(untpd.Ident(tree.name)(curSource))
233+
case tree: Literal => finalize(untpd.Literal(tree.const)(curSource))
234+
case tree: This => finalize(untpd.This(tree.qual)(curSource))
235+
case tree: JavaSeqLiteral => finalize(untpd.JavaSeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
236+
case tree: SeqLiteral => finalize(untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
237+
case tree: Bind => finalize(untpd.Bind(tree.name, transform(tree.body))(curSource))
238+
case tree: TypeTree => finalize(tpd.TypeTree(tree.tpe))
239+
case tree: DefTree => super.transform(tree).setDefTree
240+
case _ => super.transform(tree)
242241
}
243242
}
244243
}

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ trait NamerContextOps {
7070

7171
/** The symbol (stored in some typer's symTree) of an enclosing context definition */
7272
def symOfContextTree(tree: untpd.Tree): Symbol = {
73-
def go(ctx: Context): Symbol =
74-
ctx.typeAssigner match {
73+
def go(contxt: Context): Symbol =
74+
contxt.typeAssigner match {
7575
case typer: Typer =>
7676
tree.getAttachment(typer.SymOfTree) match {
7777
case Some(sym) => sym
@@ -131,7 +131,7 @@ trait NamerContextOps {
131131
termParamss
132132

133133
/** The method type corresponding to given parameters and result type */
134-
def methodType(typeParams: List[Symbol], valueParamss: List[List[Symbol]], resultType: Type, isJava: Boolean = false)(implicit ctx: Context): Type = {
134+
def methodType(typeParams: List[Symbol], valueParamss: List[List[Symbol]], resultType: Type, isJava: Boolean = false)(using Context): Type = {
135135
val monotpe =
136136
valueParamss.foldRight(resultType) { (params, resultType) =>
137137
val (isContextual, isImplicit, isErased) =
@@ -162,7 +162,7 @@ trait NamerContextOps {
162162

163163
object NamerContextOps {
164164
/** Find moduleClass/sourceModule in effective scope */
165-
private def findModuleBuddy(name: Name, scope: Scope)(implicit ctx: Context) = {
165+
private def findModuleBuddy(name: Name, scope: Scope)(using Context) = {
166166
val it = scope.lookupAll(name).filter(_.is(Module))
167167
if (it.hasNext) it.next()
168168
else NoSymbol.assertingErrorsReported(s"no companion $name in $scope")
@@ -238,7 +238,7 @@ class Namer { typer: Typer =>
238238
private var lateCompile = false
239239

240240
/** The symbol of the given expanded tree. */
241-
def symbolOfTree(tree: Tree)(implicit ctx: Context): Symbol = {
241+
def symbolOfTree(tree: Tree)(using Context): Symbol = {
242242
val xtree = expanded(tree)
243243
xtree.getAttachment(TypedAhead) match {
244244
case Some(ttree) => ttree.symbol
@@ -247,7 +247,7 @@ class Namer { typer: Typer =>
247247
}
248248

249249
/** The enclosing class with given name; error if none exists */
250-
def enclosingClassNamed(name: TypeName, span: Span)(implicit ctx: Context): Symbol =
250+
def enclosingClassNamed(name: TypeName, span: Span)(using Context): Symbol =
251251
if (name.isEmpty) NoSymbol
252252
else {
253253
val cls = ctx.owner.enclosingClassNamed(name)
@@ -257,7 +257,7 @@ class Namer { typer: Typer =>
257257
}
258258

259259
/** Record `sym` as the symbol defined by `tree` */
260-
def recordSym(sym: Symbol, tree: Tree)(implicit ctx: Context): Symbol = {
260+
def recordSym(sym: Symbol, tree: Tree)(using Context): Symbol = {
261261
for (refs <- tree.removeAttachment(References); ref <- refs) ref.watching(sym)
262262
tree.pushAttachment(SymOfTree, sym)
263263
sym
@@ -309,7 +309,7 @@ class Namer { typer: Typer =>
309309
/** If this tree is a member def or an import, create a symbol of it
310310
* and store in symOfTree map.
311311
*/
312-
def createSymbol(tree: Tree)(implicit ctx: Context): Symbol = {
312+
def createSymbol(tree: Tree)(using Context): Symbol = {
313313

314314
def privateWithinClass(mods: Modifiers) =
315315
enclosingClassNamed(mods.privateWithin, tree.span)
@@ -422,7 +422,7 @@ class Namer { typer: Typer =>
422422
createOrRefine[Symbol](tree, name, flags, ctx.owner, _ => info,
423423
(fs, _, pwithin) => ctx.newSymbol(ctx.owner, name, fs, info, pwithin, tree.nameSpan))
424424
case tree: Import =>
425-
recordSym(ctx.newImportSymbol(ctx.owner, new Completer(tree), tree.span), tree)
425+
recordSym(ctx.newImportSymbol(ctx.owner, Completer(tree)(ctx), tree.span), tree)
426426
case _ =>
427427
NoSymbol
428428
}
@@ -431,7 +431,7 @@ class Namer { typer: Typer =>
431431
/** If `sym` exists, enter it in effective scope. Check that
432432
* package members are not entered twice in the same run.
433433
*/
434-
def enterSymbol(sym: Symbol)(implicit ctx: Context): Symbol = {
434+
def enterSymbol(sym: Symbol)(using Context): Symbol = {
435435
if (sym.exists) {
436436
typr.println(s"entered: $sym in ${ctx.owner}")
437437
ctx.enter(sym)
@@ -440,7 +440,7 @@ class Namer { typer: Typer =>
440440
}
441441

442442
/** Create package if it does not yet exist. */
443-
private def createPackageSymbol(pid: RefTree)(implicit ctx: Context): Symbol = {
443+
private def createPackageSymbol(pid: RefTree)(using Context): Symbol = {
444444
val pkgOwner = pid match {
445445
case Ident(_) => if (ctx.owner eq defn.EmptyPackageClass) defn.RootClass else ctx.owner
446446
case Select(qual: RefTree, _) => createPackageSymbol(qual).moduleClass
@@ -469,7 +469,7 @@ class Namer { typer: Typer =>
469469
}
470470

471471
/** Expand tree and store in `expandedTree` */
472-
def expand(tree: Tree)(implicit ctx: Context): Unit = {
472+
def expand(tree: Tree)(using Context): Unit = {
473473
def record(expanded: Tree) =
474474
if (expanded `ne` tree) {
475475
typr.println(i"Expansion: $tree expands to $expanded")
@@ -483,7 +483,7 @@ class Namer { typer: Typer =>
483483
}
484484

485485
/** The expanded version of this tree, or tree itself if not expanded */
486-
def expanded(tree: Tree)(implicit ctx: Context): Tree = tree match {
486+
def expanded(tree: Tree)(using Context): Tree = tree match {
487487
case _: DefTree | _: PackageDef => tree.attachmentOrElse(ExpandedTree, tree)
488488
case _ => tree
489489
}
@@ -492,7 +492,7 @@ class Namer { typer: Typer =>
492492
* not also defined in `xstats`, invalidate it by setting its info to
493493
* NoType.
494494
*/
495-
def invalidateCompanions(pkg: Symbol, xstats: List[untpd.Tree])(implicit ctx: Context): Unit = {
495+
def invalidateCompanions(pkg: Symbol, xstats: List[untpd.Tree])(using Context): Unit = {
496496
val definedNames = xstats collect { case stat: NameTree => stat.name }
497497
def invalidate(name: TypeName) =
498498
if (!(definedNames contains name)) {
@@ -507,19 +507,19 @@ class Namer { typer: Typer =>
507507
}
508508

509509
/** Expand tree and create top-level symbols for statement and enter them into symbol table */
510-
def index(stat: Tree)(implicit ctx: Context): Context = {
510+
def index(stat: Tree)(using Context): Context = {
511511
expand(stat)
512512
indexExpanded(stat)
513513
}
514514

515515
/** Create top-level symbols for all statements in the expansion of this statement and
516516
* enter them into symbol table
517517
*/
518-
def indexExpanded(origStat: Tree)(implicit ctx: Context): Context = {
518+
def indexExpanded(origStat: Tree)(using Context): Context = {
519519
def recur(stat: Tree): Context = stat match {
520520
case pcl: PackageDef =>
521521
val pkg = createPackageSymbol(pcl.pid)
522-
index(pcl.stats)(ctx.fresh.setOwner(pkg.moduleClass))
522+
index(pcl.stats)(using ctx.fresh.setOwner(pkg.moduleClass))
523523
invalidateCompanions(pkg, Trees.flatten(pcl.stats map expanded))
524524
setDocstring(pkg, stat)
525525
ctx
@@ -540,15 +540,15 @@ class Namer { typer: Typer =>
540540
}
541541

542542
/** Determines whether this field holds an enum constant. */
543-
def isEnumConstant(vd: ValDef)(implicit ctx: Context): Boolean =
543+
def isEnumConstant(vd: ValDef)(using Context): Boolean =
544544
vd.mods.isAllOf(JavaEnumValue)
545545

546546
/** Add child annotation for `child` to annotations of `cls`. The annotation
547547
* is added at the correct insertion point, so that Child annotations appear
548548
* in reverse order of their start positions.
549549
* @pre `child` must have a position.
550550
*/
551-
final def addChild(cls: Symbol, child: Symbol)(implicit ctx: Context): Unit = {
551+
final def addChild(cls: Symbol, child: Symbol)(using Context): Unit = {
552552
val childStart = if (child.span.exists) child.span.start else -1
553553
def insertInto(annots: List[Annotation]): List[Annotation] =
554554
annots.find(_.symbol == defn.ChildAnnot) match {
@@ -567,15 +567,15 @@ class Namer { typer: Typer =>
567567
}
568568

569569
/** Add java enum constants */
570-
def addEnumConstants(mdef: DefTree, sym: Symbol)(implicit ctx: Context): Unit = mdef match {
570+
def addEnumConstants(mdef: DefTree, sym: Symbol)(using Context): Unit = mdef match {
571571
case vdef: ValDef if (isEnumConstant(vdef)) =>
572572
val enumClass = sym.owner.linkedClass
573573
if (!enumClass.is(Sealed)) enumClass.setFlag(Flags.AbstractSealed)
574574
addChild(enumClass, sym)
575575
case _ =>
576576
}
577577

578-
def setDocstring(sym: Symbol, tree: Tree)(implicit ctx: Context): Unit = tree match {
578+
def setDocstring(sym: Symbol, tree: Tree)(using Context): Unit = tree match {
579579
case t: MemberDef if t.rawComment.isDefined =>
580580
ctx.docCtx.foreach(_.addDocstring(sym, t.rawComment))
581581
case _ => ()
@@ -584,7 +584,7 @@ class Namer { typer: Typer =>
584584
/** Create top-level symbols for statements and enter them into symbol table
585585
* @return A context that reflects all imports in `stats`.
586586
*/
587-
def index(stats: List[Tree])(implicit ctx: Context): Context = {
587+
def index(stats: List[Tree])(using Context): Context = {
588588

589589
// module name -> (stat, moduleCls | moduleVal)
590590
val moduleClsDef = mutable.Map[TypeName, (Tree, TypeDef)]()
@@ -692,14 +692,14 @@ class Namer { typer: Typer =>
692692
}
693693

694694
/** Create links between companion object and companion class */
695-
def createLinks(classTree: TypeDef, moduleTree: TypeDef)(implicit ctx: Context) = {
695+
def createLinks(classTree: TypeDef, moduleTree: TypeDef)(using Context) = {
696696
val claz = ctx.effectiveScope.lookup(classTree.name)
697697
val modl = ctx.effectiveScope.lookup(moduleTree.name)
698698
modl.registerCompanion(claz)
699699
claz.registerCompanion(modl)
700700
}
701701

702-
def createCompanionLinks(implicit ctx: Context): Unit = {
702+
def createCompanionLinks(using Context): Unit = {
703703
val classDef = mutable.Map[TypeName, TypeDef]()
704704
val moduleDef = mutable.Map[TypeName, TypeDef]()
705705

@@ -759,16 +759,16 @@ class Namer { typer: Typer =>
759759

760760
stats.foreach(expand)
761761
mergeCompanionDefs()
762-
val ctxWithStats = stats.foldLeft(ctx)((ctx, stat) => indexExpanded(stat)(ctx))
763-
createCompanionLinks(ctxWithStats)
762+
val ctxWithStats = stats.foldLeft(ctx)((ctx, stat) => indexExpanded(stat)(using ctx))
763+
createCompanionLinks(using ctxWithStats)
764764
ctxWithStats
765765
}
766766

767767
/** Index symbols in `tree` while asserting the `lateCompile` flag.
768768
* This will cause any old top-level symbol with the same fully qualified
769769
* name as a newly created symbol to be replaced.
770770
*/
771-
def lateEnter(tree: Tree)(implicit ctx: Context): Context = {
771+
def lateEnter(tree: Tree)(using Context): Context = {
772772
val saved = lateCompile
773773
lateCompile = true
774774
try index(tree :: Nil) finally lateCompile = saved
@@ -778,27 +778,27 @@ class Namer { typer: Typer =>
778778
* Nothing if no wildcard imports of this kind exist
779779
* Any if there are unbounded wildcard imports of this kind
780780
*/
781-
def importBound(sels: List[untpd.ImportSelector], isGiven: Boolean)(implicit ctx: Context): Type =
781+
def importBound(sels: List[untpd.ImportSelector], isGiven: Boolean)(using Context): Type =
782782
sels.foldLeft(defn.NothingType: Type) { (bound, sel) =>
783783
if sel.isWildcard && sel.isGiven == isGiven then
784784
if sel.bound.isEmpty then defn.AnyType
785785
else bound | typedAheadType(sel.bound).tpe
786786
else bound
787787
}
788788

789-
def missingType(sym: Symbol, modifier: String)(implicit ctx: Context): Unit = {
789+
def missingType(sym: Symbol, modifier: String)(using Context): Unit = {
790790
ctx.error(s"${modifier}type of implicit definition needs to be given explicitly", sym.sourcePos)
791791
sym.resetFlag(GivenOrImplicit)
792792
}
793793

794794
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */
795-
class Completer(val original: Tree)(implicit ctx: Context) extends LazyType with SymbolLoaders.SecondCompleter {
795+
class Completer(val original: Tree)(ictx: Context) extends LazyType with SymbolLoaders.SecondCompleter {
796796

797797
protected def localContext(owner: Symbol): FreshContext = ctx.fresh.setOwner(owner).setTree(original)
798798

799799
/** The context with which this completer was created */
800-
def creationContext: Context = ctx
801-
ctx.typerState.markShared()
800+
given creationContext as Context = ictx
801+
ictx.typerState.markShared()
802802

803803
protected def typeSig(sym: Symbol): Type = original match {
804804
case original: ValDef =>
@@ -820,10 +820,10 @@ class Namer { typer: Typer =>
820820
}
821821
}
822822

823-
final override def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
824-
if (Config.showCompletions && ctx.typerState != this.ctx.typerState) {
823+
final override def complete(denot: SymDenotation)(using Context): Unit = {
824+
if (Config.showCompletions && ctx.typerState != creationContext.typerState) {
825825
def levels(c: Context): Int =
826-
if (c.typerState eq this.ctx.typerState) 0
826+
if (c.typerState eq creationContext.typerState) 0
827827
else if (c.typerState == null) -1
828828
else if (c.outer.typerState == c.typerState) levels(c.outer)
829829
else levels(c.outer) + 1
@@ -1188,7 +1188,7 @@ class Namer { typer: Typer =>
11881188
localCtx = completerCtx.inClassContext(selfInfo)
11891189

11901190
index(constr)
1191-
index(rest)(localCtx)
1191+
index(rest)(using localCtx)
11921192

11931193
symbolOfTree(constr).info.stripPoly match // Completes constr symbol as a side effect
11941194
case mt: MethodType if cls.is(Case) && mt.isParamDependent =>
@@ -1224,7 +1224,7 @@ class Namer { typer: Typer =>
12241224
if (ptype.typeParams.isEmpty) ptype
12251225
else {
12261226
if (denot.is(ModuleClass) && denot.sourceModule.isOneOf(GivenOrImplicit))
1227-
missingType(denot.symbol, "parent ")(creationContext)
1227+
missingType(denot.symbol, "parent ")(using creationContext)
12281228
fullyDefinedType(typedAheadExpr(parent).tpe, "class parent", parent.span)
12291229
}
12301230
case _ =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@ class Typer extends Namer
24382438
// in preceding statements (unless the DefTree is completed ahead of time,
24392439
// then it is impossible).
24402440
sym.info = Completer(completer.original)(
2441-
using completer.creationContext.withNotNullInfos(ctx.notNullInfos))
2441+
completer.creationContext.withNotNullInfos(ctx.notNullInfos))
24422442
true
24432443
case _ =>
24442444
// If it has been completed, then it must be because there is a forward reference

0 commit comments

Comments
 (0)