Skip to content

Commit 131ab5c

Browse files
committed
Avoid double context creation when modes change on a fresh context.
Move addMode and friends to two decorators, one for Context, the other for FreshContext. Implement behavior accordingly. This avoids creating two contexts in situations like: c.fresh.setxploreTyperState.addMode(...) Mow we can write c.fresh.addMode(...).setExploreTyperState Because addMode returns a fresh context when applied to a fresh context. Note that we specifically do not want virtual dispatch of addMode, that's why it was moved to a decorator. Also: removed mention of ".fresh: when just forllowed by an addMode, because that one is redundant.
1 parent 6f4b38c commit 131ab5c

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,6 @@ object Contexts {
386386
final def withOwner(owner: Symbol): Context =
387387
if (owner ne this.owner) fresh.setOwner(owner) else this
388388

389-
final def withMode(mode: Mode): Context =
390-
if (mode != this.mode) fresh.setMode(mode) else this
391-
392-
final def addMode(mode: Mode): Context = withMode(this.mode | mode)
393-
final def maskMode(mode: Mode): Context = withMode(this.mode & mode)
394-
final def retractMode(mode: Mode): Context = withMode(this.mode &~ mode)
395-
396389
override def toString =
397390
"Context(\n" +
398391
(outersIterator map ( ctx => s" owner = ${ctx.owner}, scope = ${ctx.scope}") mkString "\n")
@@ -444,6 +437,21 @@ object Contexts {
444437
def setDebug = setSetting(base.settings.debug, true)
445438
}
446439

440+
implicit class ModeChanges(val c: Context) extends AnyVal {
441+
final def withMode(mode: Mode): Context =
442+
if (mode != c.mode) c.fresh.setMode(mode) else c
443+
444+
final def addMode(mode: Mode): Context = withMode(c.mode | mode)
445+
final def maskMode(mode: Mode): Context = withMode(c.mode & mode)
446+
final def retractMode(mode: Mode): Context = withMode(c.mode &~ mode)
447+
}
448+
449+
implicit class FreshModeChanges(val c: FreshContext) extends AnyVal {
450+
final def addMode(mode: Mode): c.type = c.setMode(c.mode | mode)
451+
final def maskMode(mode: Mode): c.type = c.setMode(c.mode & mode)
452+
final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode)
453+
}
454+
447455
/** A class defining the initial context with given context base
448456
* and set of possible settings.
449457
*/

src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import typer.Mode
1212
import scala.annotation.switch
1313

1414
class PlainPrinter(_ctx: Context) extends Printer {
15-
protected[this] implicit def ctx: Context = _ctx.fresh.addMode(Mode.Printing)
15+
protected[this] implicit def ctx: Context = _ctx.addMode(Mode.Printing)
1616

1717
protected def maxToTextRecursions = 100
1818

src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ object Implicits {
9090
}
9191

9292
if (refs.isEmpty) refs
93-
else refs filter (refMatches(_)(ctx.fresh.setExploreTyperState.addMode(Mode.TypevarsMissContext))) // create a defensive copy of ctx to avoid constraint pollution
93+
else refs filter (refMatches(_)(ctx.fresh.addMode(Mode.TypevarsMissContext).setExploreTyperState)) // create a defensive copy of ctx to avoid constraint pollution
9494
}
9595
}
9696

@@ -480,7 +480,8 @@ trait Implicits { self: Typer =>
480480
pt)
481481
val generated1 = adapt(generated, pt)
482482
lazy val shadowing =
483-
typed(untpd.Ident(ref.name) withPos pos.toSynthetic, funProto)(nestedContext.setNewTyperState.addMode(Mode.ImplicitShadowing))
483+
typed(untpd.Ident(ref.name) withPos pos.toSynthetic, funProto)
484+
(nestedContext.addMode(Mode.ImplicitShadowing).setNewTyperState)
484485
def refMatches(shadowing: Tree): Boolean =
485486
ref.symbol == closureBody(shadowing).symbol || {
486487
shadowing match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ class Namer { typer: Typer =>
701701

702702
// println(s"final inherited for $sym: ${inherited.toString}") !!!
703703
// println(s"owner = ${sym.owner}, decls = ${sym.owner.info.decls.show}")
704-
val rhsCtx = ctx.fresh addMode Mode.InferringReturnType
704+
val rhsCtx = ctx.addMode(Mode.InferringReturnType)
705705
def rhsType = ctx.deskolemize(
706706
typedAheadExpr(mdef.rhs, rhsProto)(rhsCtx).tpe.widen.approximateUnion)
707707
def lhsType = fullyDefinedType(rhsType, "right-hand side", mdef.pos)

0 commit comments

Comments
 (0)