Skip to content

Commit 65c51a5

Browse files
committed
Switch to (using Context) in more files in typer
1 parent 889556e commit 65c51a5

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
@@ -71,8 +71,8 @@ trait NamerContextOps {
7171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -760,16 +760,16 @@ class Namer { typer: Typer =>
760760

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

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

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

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

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

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

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

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

11911191
index(constr)
1192-
index(rest)(localCtx)
1192+
index(rest)(using localCtx)
11931193

11941194
symbolOfTree(constr).info.stripPoly match // Completes constr symbol as a side effect
11951195
case mt: MethodType if cls.is(Case) && mt.isParamDependent =>
@@ -1225,7 +1225,7 @@ class Namer { typer: Typer =>
12251225
if (ptype.typeParams.isEmpty) ptype
12261226
else {
12271227
if (denot.is(ModuleClass) && denot.sourceModule.isOneOf(GivenOrImplicit))
1228-
missingType(denot.symbol, "parent ")(creationContext)
1228+
missingType(denot.symbol, "parent ")(using creationContext)
12291229
fullyDefinedType(typedAheadExpr(parent).tpe, "class parent", parent.span)
12301230
}
12311231
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)