Skip to content

Commit fe79288

Browse files
committed
Add inMode, withMode, withoutMode operations to Contexts
1 parent 4fda583 commit fe79288

19 files changed

+72
-50
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ object Contexts {
8888
/** Does current phase use an erased types interpretation? */
8989
def currentlyAfterErasure(using Context): Boolean = currentPhase.erasedTypes
9090

91+
inline def inMode[T](mode: Mode)(inline op: Context ?=> T)(using ctx: Context): T =
92+
op(using if mode != ctx.mode then ctx.fresh.setMode(mode) else ctx)
93+
94+
inline def withMode[T](mode: Mode)(inline op: Context ?=> T)(using ctx: Context): T =
95+
inMode(ctx.mode | mode)(op)
96+
97+
inline def withoutMode[T](mode: Mode)(inline op: Context ?=> T)(using ctx: Context): T =
98+
inMode(ctx.mode &~ mode)(op)
99+
91100
/** A context is passed basically everywhere in dotc.
92101
* This is convenient but carries the risk of captured contexts in
93102
* objects that turn into space leaks. To combat this risk, here are some
@@ -638,13 +647,11 @@ object Contexts {
638647
if (mode != c.mode) c.fresh.setMode(mode) else c
639648

640649
final def addMode(mode: Mode): Context = withModeBits(c.mode | mode)
641-
final def maskMode(mode: Mode): Context = withModeBits(c.mode & mode)
642650
final def retractMode(mode: Mode): Context = withModeBits(c.mode &~ mode)
643651
}
644652

645653
implicit class FreshModeChanges(val c: FreshContext) extends AnyVal {
646654
final def addMode(mode: Mode): c.type = c.setMode(c.mode | mode)
647-
final def maskMode(mode: Mode): c.type = c.setMode(c.mode & mode)
648655
final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode)
649656
}
650657

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import config.Printers.cyclicErrors
1717
class TypeError(msg: String) extends Exception(msg) {
1818
def this() = this("")
1919
final def toMessage(using Context): Message =
20-
produceMessage(using ctx.addMode(Mode.Printing))
20+
withMode(Mode.Printing)(produceMessage)
2121
def produceMessage(using Context): Message = super.getMessage
2222
override def getMessage: String = super.getMessage
2323
}

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ class ClassfileParser(
752752
}
753753

754754
val unpickler = new unpickleScala2.Scala2Unpickler(bytes, classRoot, moduleRoot)(ctx)
755-
unpickler.run()(using ctx.addMode(Scala2UnpicklingMode))
755+
withMode(Scala2UnpicklingMode)(unpickler.run())
756756
Some(unpickler)
757757
}
758758

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ object PickledQuotes {
5353
/** Unpickle the tree contained in the TastyExpr */
5454
def unpickleExpr(tasty: PickledQuote, splices: PickledArgs)(using Context): Tree = {
5555
val tastyBytes = TastyString.unpickle(tasty)
56-
val unpickled = unpickle(tastyBytes, splices, isType = false)(using ctx.addMode(Mode.ReadPositions))
56+
val unpickled = withMode(Mode.ReadPositions)(
57+
unpickle(tastyBytes, splices, isType = false))
5758
val Inlined(call, Nil, expnasion) = unpickled
5859
val inlineCtx = inlineContext(call)
5960
val expansion1 = spliceTypes(expnasion, splices)(using inlineCtx)
@@ -64,7 +65,8 @@ object PickledQuotes {
6465
/** Unpickle the tree contained in the TastyType */
6566
def unpickleType(tasty: PickledQuote, args: PickledArgs)(using Context): Tree = {
6667
val tastyBytes = TastyString.unpickle(tasty)
67-
val unpickled = unpickle(tastyBytes, args, isType = true)(using ctx.addMode(Mode.ReadPositions))
68+
val unpickled = withMode(Mode.ReadPositions)(
69+
unpickle(tastyBytes, args, isType = true))
6870
spliceTypes(unpickled, args)
6971
}
7072

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,11 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
606606
// println(s"unpickled ${denot.debugString}, info = ${denot.info}") !!! DEBUG
607607
}
608608
atReadPos(startCoord(denot).toIndex,
609-
() => parseToCompletion(denot)(
610-
using ctx.addMode(Mode.Scala2Unpickling).withPhaseNoLater(picklerPhase)))
609+
() => withMode(Mode.Scala2Unpickling) {
610+
atPhaseNotLaterThan(picklerPhase) {
611+
parseToCompletion(denot)
612+
}
613+
})
611614
}
612615
catch {
613616
case ex: RuntimeException => handleRuntimeException(ex)

compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.tastyreflect.ReflectionImpl
1414
class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
1515

1616
private val myInitCtx: Context = {
17-
val rootCtx = initCtx.fresh.addMode(Mode.Interactive).addMode(Mode.ReadPositions).addMode(Mode.ReadComments)
17+
val rootCtx = initCtx.fresh.addMode(Mode.Interactive | Mode.ReadPositions | Mode.ReadComments)
1818
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
1919
rootCtx.setSetting(rootCtx.settings.fromTasty, true)
2020
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx)._2

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package fromtasty
44

55
import core._
66
import Decorators._
7-
import Contexts.{Context, ctx}
7+
import Contexts._
88
import Symbols.{Symbol, ClassSymbol}
99
import SymDenotations.ClassDenotation
1010
import Denotations.staticRef
@@ -22,7 +22,7 @@ class ReadTasty extends Phase {
2222
ctx.settings.fromTasty.value
2323

2424
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
25-
units.flatMap(readTASTY(_)(using ctx.addMode(Mode.ReadPositions)))
25+
withMode(Mode.ReadPositions)(units.flatMap(readTASTY(_)))
2626

2727
def readTASTY(unit: CompilationUnit)(using Context): Option[CompilationUnit] = unit match {
2828
case unit: TASTYCompilationUnit =>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package printing
33

44
import core._
55
import Texts._, Types._, Flags._, Names._, Symbols._, NameOps._, Constants._, Denotations._
6-
import Contexts.{Context, ctx}
6+
import Contexts._
77
import Scopes.Scope, Denotations.Denotation, Annotations.Annotation
88
import StdNames.nme
99
import ast.Trees._
@@ -215,8 +215,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
215215
else {
216216
val constr = ctx.typerState.constraint
217217
val bounds =
218-
if (constr.contains(tp)) ctx.addMode(Mode.Printing).typeComparer.fullBounds(tp.origin)
219-
else TypeBounds.empty
218+
if constr.contains(tp) then
219+
withMode(Mode.Printing)(ctx.typeComparer.fullBounds(tp.origin))
220+
else
221+
TypeBounds.empty
220222
if (bounds.isTypeAlias) toText(bounds.lo) ~ (Str("^") provided printDebug)
221223
else if (ctx.settings.YshowVarBounds.value) "(" ~ toText(tp.origin) ~ "?" ~ toText(bounds) ~ ")"
222224
else toText(tp.origin)

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ abstract class Reporter extends interfaces.ReporterResult {
143143

144144
def report(dia: Diagnostic)(using Context): Unit =
145145
if (!isHidden(dia)) {
146-
doReport(dia)(using ctx.addMode(Mode.Printing))
146+
withMode(Mode.Printing)(doReport(dia))
147147
dia match {
148148
case dia: ConditionalWarning if !dia.enablingOption.value =>
149149
val key = dia.enablingOption.name

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
308308
goTyped(cpy.Typed(tree)(expr, tpt), start)
309309
case tree: CaseDef =>
310310
given Context = prepCaseDef(tree, start)(using outerCtx)
311-
val pat = transformTree(tree.pat, start)(using ctx.addMode(Mode.Pattern))
311+
val pat = withMode(Mode.Pattern)(transformTree(tree.pat, start))
312312
val guard = transformTree(tree.guard, start)
313313
val body = transformTree(tree.body, start)
314314
goCaseDef(cpy.CaseDef(tree)(pat, guard, body), start)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
236236
case tree @ Select(qual, name) =>
237237
if (name.isTypeName) {
238238
Checking.checkRealizable(qual.tpe, qual.posd)
239-
super.transform(tree)(using ctx.addMode(Mode.Type))
239+
withMode(Mode.Type)(super.transform(tree))
240240
}
241241
else
242242
transformSelect(tree, Nil)
@@ -361,7 +361,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
361361
if !sel.isWildcard then checkIdent(sel)
362362
super.transform(tree)
363363
case Typed(Ident(nme.WILDCARD), _) =>
364-
super.transform(tree)(using ctx.addMode(Mode.Pattern))
364+
withMode(Mode.Pattern)(super.transform(tree))
365365
// The added mode signals that bounds in a pattern need not
366366
// conform to selector bounds. I.e. assume
367367
// type Tree[T >: Null <: Type]
@@ -372,7 +372,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
372372
case m @ MatchTypeTree(bounds, selector, cases) =>
373373
// Analog to the case above for match types
374374
def tranformIgnoringBoundsCheck(x: CaseDef): CaseDef =
375-
super.transform(x)(using ctx.addMode(Mode.Pattern)).asInstanceOf[CaseDef]
375+
withMode(Mode.Pattern)(super.transform(x)).asInstanceOf[CaseDef]
376376
cpy.MatchTypeTree(tree)(
377377
super.transform(bounds),
378378
super.transform(selector),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ trait Applications extends Compatibility {
12421242
// We ignore whether constraining the pattern succeeded.
12431243
// Constraining only fails if the pattern cannot possibly match,
12441244
// but useless pattern checks detect more such cases, so we simply rely on them instead.
1245-
ctx.addMode(Mode.GadtConstraintInference).typeComparer.constrainPatternType(unapplyArgType, selType)
1245+
withMode(Mode.GadtConstraintInference)(ctx.typeComparer.constrainPatternType(unapplyArgType, selType))
12461246
val patternBound = maximizeType(unapplyArgType, tree.span, fromScala2x)
12471247
if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
12481248
unapp.println(i"case 2 $unapplyArgType ${ctx.typerState.constraint}")
@@ -1682,7 +1682,7 @@ trait Applications extends Compatibility {
16821682
return resolveMapped(alts, alt => stripImplicit(alt.widen), pt)
16831683
case _ =>
16841684

1685-
var found = resolveOverloaded1(alts, pt)(using ctx.retractMode(Mode.ImplicitsEnabled))
1685+
var found = withoutMode(Mode.ImplicitsEnabled)(resolveOverloaded1(alts, pt))
16861686
if found.isEmpty && ctx.mode.is(Mode.ImplicitsEnabled) then
16871687
found = resolveOverloaded1(alts, pt)
16881688
found match
@@ -1997,7 +1997,7 @@ trait Applications extends Compatibility {
19971997
else defn.FunctionOf(commonParamTypes, WildcardType)
19981998
overload.println(i"pretype arg $arg with expected type $commonFormal")
19991999
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
2000-
pt.typedArg(arg, commonFormal)(using ctx.addMode(Mode.ImplicitsEnabled))
2000+
withMode(Mode.ImplicitsEnabled)(pt.typedArg(arg, commonFormal))
20012001
}
20022002
case None =>
20032003
}
@@ -2100,9 +2100,9 @@ trait Applications extends Compatibility {
21002100
(methodRef, pt)
21012101
}
21022102
val (core, pt1) = integrateTypeArgs(pt)
2103-
val app =
2104-
typed(untpd.Apply(core, untpd.TypedSplice(receiver) :: Nil), pt1, ctx.typerState.ownedVars)(
2105-
using ctx.addMode(Mode.SynthesizeExtMethodReceiver))
2103+
val app = withMode(Mode.SynthesizeExtMethodReceiver) {
2104+
typed(untpd.Apply(core, untpd.TypedSplice(receiver) :: Nil), pt1, ctx.typerState.ownedVars)
2105+
}
21062106
def isExtension(tree: Tree): Boolean = methPart(tree) match {
21072107
case Inlined(call, _, _) => isExtension(call)
21082108
case tree @ Select(qual, nme.apply) => tree.symbol.is(Extension) || isExtension(qual)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ object Checking {
120120
}
121121
def checkValidIfApply(using Context): Unit =
122122
checkWildcardApply(tycon.tpe.appliedTo(args.map(_.tpe)))
123-
checkValidIfApply(using ctx.addMode(Mode.AllowLambdaWildcardApply))
123+
withMode(Mode.AllowLambdaWildcardApply)(checkValidIfApply)
124124
}
125125

126126
/** Check all applied type trees in inferred type `tpt` for well-formedness */
@@ -330,7 +330,7 @@ object Checking {
330330
* by a `LazyRef`, or `ErrorType` if a cycle was detected and reported.
331331
*/
332332
def checkNonCyclic(sym: Symbol, info: Type, reportErrors: Boolean)(using Context): Type = {
333-
val checker = new CheckNonCyclicMap(sym, reportErrors)(using ctx.addMode(Mode.CheckCyclic))
333+
val checker = withMode(Mode.CheckCyclic)(new CheckNonCyclicMap(sym, reportErrors))
334334
try checker.checkInfo(info)
335335
catch {
336336
case ex: CyclicReference =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ trait Implicits { self: Typer =>
994994
val deepPt = pt.deepenProto
995995
if (deepPt ne pt) inferImplicit(deepPt, argument, span)
996996
else if (migrateTo3 && !ctx.mode.is(Mode.OldOverloadingResolution))
997-
inferImplicit(pt, argument, span)(using ctx.addMode(Mode.OldOverloadingResolution)) match {
997+
withMode(Mode.OldOverloadingResolution)(inferImplicit(pt, argument, span)) match {
998998
case altResult: SearchSuccess =>
999999
report.migrationWarning(
10001000
s"According to new implicit resolution rules, this will be ambiguous:\n${result.reason.explanation}",

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,16 @@ class Namer { typer: Typer =>
12111211
}
12121212

12131213
def typedAheadType(tree: Tree, pt: Type = WildcardType)(using Context): tpd.Tree =
1214-
typedAhead(tree, typer.typed(_, pt)(using ctx.retractMode(Mode.PatternOrTypeBits).addMode(Mode.Type)))
1214+
withoutMode(Mode.PatternOrTypeBits) {
1215+
withMode(Mode.Type) {
1216+
typedAhead(tree, typer.typed(_, pt))
1217+
}
1218+
}
12151219

12161220
def typedAheadExpr(tree: Tree, pt: Type = WildcardType)(using Context): tpd.Tree =
1217-
typedAhead(tree, typer.typed(_, pt)(using ctx.retractMode(Mode.PatternOrTypeBits)))
1221+
withoutMode(Mode.PatternOrTypeBits) {
1222+
typedAhead(tree, typer.typed(_, pt))
1223+
}
12181224

12191225
def typedAheadAnnotation(tree: Tree)(using Context): tpd.Tree =
12201226
typedAheadExpr(tree, defn.AnnotationClass.typeRef)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object ProtoTypes {
6767
* fits the given expected result type.
6868
*/
6969
def constrainResult(mt: Type, pt: Type)(using Context): Boolean =
70-
inContext(ctx.addMode(Mode.ConstrainResult)) {
70+
withMode(Mode.ConstrainResult) {
7171
val savedConstraint = ctx.typerState.constraint
7272
val res = pt.widenExpr match {
7373
case pt: FunProto =>

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ReTyper extends Typer with ReChecking {
3838

3939
override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
4040
assertTyped(tree)
41-
val qual1 = typed(tree.qualifier, AnySelectionProto)(using ctx.retractMode(Mode.Pattern))
41+
val qual1 = withoutMode(Mode.Pattern)(typed(tree.qualifier, AnySelectionProto))
4242
untpd.cpy.Select(tree)(qual1, tree.name).withType(tree.typeOpt)
4343
}
4444

@@ -78,11 +78,9 @@ class ReTyper extends Typer with ReChecking {
7878
}
7979

8080
override def typedUnApply(tree: untpd.UnApply, selType: Type)(using Context): UnApply = {
81-
val fun1 = {
81+
val fun1 =
8282
// retract PatternOrTypeBits like in typedExpr
83-
val ctx1 = ctx.retractMode(Mode.PatternOrTypeBits)
84-
typedUnadapted(tree.fun, AnyFunctionProto)(using ctx1)
85-
}
83+
withoutMode(Mode.PatternOrTypeBits)(typedUnadapted(tree.fun, AnyFunctionProto))
8684
val implicits1 = tree.implicits.map(typedExpr(_))
8785
val patterns1 = tree.patterns.mapconserve(pat => typed(pat, pat.tpe))
8886
untpd.cpy.UnApply(tree)(fun1, implicits1, patterns1).withType(tree.tpe)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
129129
*/
130130
def validEqAnyArgs(tp1: Type, tp2: Type)(using Context) =
131131
typer.assumedCanEqual(tp1, tp2)
132-
|| inContext(ctx.addMode(Mode.StrictEquality)) {
132+
|| withMode(Mode.StrictEquality) {
133133
!hasEq(tp1) && !hasEq(tp2)
134134
}
135135

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ class Typer extends Namer
767767
def handlePattern: Tree = {
768768
val tpt1 = typedTpt
769769
if (!currentlyAfterTyper && pt != defn.ImplicitScrutineeTypeRef)
770-
ctx.addMode(Mode.GadtConstraintInference).typeComparer.constrainPatternType(tpt1.tpe, pt)
770+
withMode(Mode.GadtConstraintInference)(ctx.typeComparer.constrainPatternType(tpt1.tpe, pt))
771771
// special case for an abstract type that comes with a class tag
772772
tryWithClassTag(ascription(tpt1, isWildcard = true), pt)
773773
}
@@ -785,8 +785,9 @@ class Typer extends Namer
785785
def tryWithClassTag(tree: Typed, pt: Type)(using Context): Tree = tree.tpt.tpe.dealias match {
786786
case tref: TypeRef if !tref.symbol.isClass && !currentlyAfterTyper && !(tref =:= pt) =>
787787
require(ctx.mode.is(Mode.Pattern))
788-
inferImplicit(defn.ClassTagClass.typeRef.appliedTo(tref),
789-
EmptyTree, tree.tpt.span)(using ctx.retractMode(Mode.Pattern)) match {
788+
withoutMode(Mode.Pattern)(
789+
inferImplicit(defn.ClassTagClass.typeRef.appliedTo(tref), EmptyTree, tree.tpt.span)
790+
) match {
790791
case SearchSuccess(clsTag, _, _) =>
791792
typed(untpd.Apply(untpd.TypedSplice(clsTag), untpd.TypedSplice(tree.expr)), pt)
792793
case _ =>
@@ -864,8 +865,9 @@ class Typer extends Namer
864865
typedStats(stats, ctx.owner)
865866

866867
def typedBlock(tree: untpd.Block, pt: Type)(using Context): Tree = {
867-
val localCtx = ctx.retractMode(Mode.Pattern)
868-
val (stats1, exprCtx) = typedBlockStats(tree.stats)(using localCtx)
868+
val (stats1, exprCtx) = withoutMode(Mode.Pattern) {
869+
typedBlockStats(tree.stats)
870+
}
869871
val expr1 = typedExpr(tree.expr, pt.dropIfProto)(using exprCtx)
870872
ensureNoLocalRefs(
871873
cpy.Block(tree)(stats1, expr1)
@@ -1444,7 +1446,7 @@ class Typer extends Namer
14441446
/** Type a case of a type match */
14451447
def typedTypeCase(cdef: untpd.CaseDef, selType: Type, pt: Type)(using Context): CaseDef = {
14461448
def caseRest(using Context) = {
1447-
val pat1 = checkSimpleKinded(typedType(cdef.pat)(using ctx.addMode(Mode.Pattern)))
1449+
val pat1 = withMode(Mode.Pattern)(checkSimpleKinded(typedType(cdef.pat)))
14481450
val pat2 = indexPattern(cdef).transform(pat1)
14491451
var body1 = typedType(cdef.body, pt)
14501452
if !body1.isType then
@@ -1654,7 +1656,9 @@ class Typer extends Namer
16541656
return errorTree(tree, dependentStr)
16551657
case _ =>
16561658

1657-
val tpt1 = typed(tree.tpt, AnyTypeConstructorProto)(using ctx.retractMode(Mode.Pattern))
1659+
val tpt1 = withoutMode(Mode.Pattern) {
1660+
typed(tree.tpt, AnyTypeConstructorProto)
1661+
}
16581662
val tparams = tpt1.tpe.typeParams
16591663
if (tparams.isEmpty) {
16601664
report.error(TypeDoesNotTakeParameters(tpt1.tpe, tree.args), tree.sourcePos)
@@ -2195,7 +2199,7 @@ class Typer extends Namer
21952199
}
21962200

21972201
def typedPackageDef(tree: untpd.PackageDef)(using Context): Tree =
2198-
val pid1 = typedExpr(tree.pid, AnySelectionProto)(using ctx.addMode(Mode.InPackageClauseName))
2202+
val pid1 = withMode(Mode.InPackageClauseName)(typedExpr(tree.pid, AnySelectionProto))
21992203
val pkg = pid1.symbol
22002204
pid1 match
22012205
case pid1: RefTree if pkg.is(Package) =>
@@ -2664,11 +2668,11 @@ class Typer extends Namer
26642668
:: (if mdef.symbol.isRetainedInlineMethod then Inliner.bodyRetainer(mdef) :: Nil else Nil)
26652669

26662670
def typedExpr(tree: untpd.Tree, pt: Type = WildcardType)(using Context): Tree =
2667-
typed(tree, pt)(using ctx.retractMode(Mode.PatternOrTypeBits))
2671+
withoutMode(Mode.PatternOrTypeBits)(typed(tree, pt))
26682672
def typedType(tree: untpd.Tree, pt: Type = WildcardType)(using Context): Tree = // todo: retract mode between Type and Pattern?
2669-
typed(tree, pt)(using ctx.addMode(Mode.Type))
2673+
withMode(Mode.Type)(typed(tree, pt))
26702674
def typedPattern(tree: untpd.Tree, selType: Type = WildcardType)(using Context): Tree =
2671-
typed(tree, selType)(using ctx.addMode(Mode.Pattern))
2675+
withMode(Mode.Pattern)(typed(tree, selType))
26722676

26732677
def tryEither[T](op: Context ?=> T)(fallBack: (T, TyperState) => T)(using Context): T = {
26742678
val nestedCtx = ctx.fresh.setNewTyperState()
@@ -3468,7 +3472,7 @@ class Typer extends Namer
34683472
found // nothing to check or adapt for extension method applications
34693473
case SearchSuccess(found, _, _) =>
34703474
checkImplicitConversionUseOK(found.symbol, tree.posd)
3471-
readapt(found)(using ctx.retractMode(Mode.ImplicitsEnabled))
3475+
withoutMode(Mode.ImplicitsEnabled)(readapt(found))
34723476
case failure: SearchFailure =>
34733477
if (pt.isInstanceOf[ProtoType] && !failure.isAmbiguous) {
34743478
// don't report the failure but return the tree unchanged. This
@@ -3586,7 +3590,7 @@ class Typer extends Namer
35863590
case _: RefTree | _: Literal
35873591
if !isVarPattern(tree) &&
35883592
!(pt <:< tree.tpe) &&
3589-
!ctx.addMode(Mode.GadtConstraintInference).typeComparer.constrainPatternType(tree.tpe, pt) =>
3593+
!withMode(Mode.GadtConstraintInference)(ctx.typeComparer.constrainPatternType(tree.tpe, pt)) =>
35903594
val cmp =
35913595
untpd.Apply(
35923596
untpd.Select(untpd.TypedSplice(tree), nme.EQ),

0 commit comments

Comments
 (0)