Skip to content

Commit 4fa9403

Browse files
Retract InTypeOf correctly during unpickling
1 parent 3bf80c9 commit 4fa9403

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ object Trees {
12141214
case TypeTree() =>
12151215
tree
12161216
case SingletonTypeTree(ref) =>
1217-
cpy.SingletonTypeTree(tree)(transform(ref)(ctx.fresh.addMode(Mode.InTypeOf)))
1217+
cpy.SingletonTypeTree(tree)(transform(ref)(ctx.enterTypeOf()))
12181218
case AndTypeTree(left, right) =>
12191219
cpy.AndTypeTree(tree)(transform(left), transform(right))
12201220
case OrTypeTree(left, right) =>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ object Contexts {
116116
protected def owner_=(owner: Symbol) = _owner = owner
117117
def owner: Symbol = _owner
118118

119+
/** The owner at the point of entering TypeOf (for SingletonTypeTrees) */
120+
private[this] var _inTypeOfOwner: Symbol = _
121+
protected def inTypeOfOwner_=(owner: Symbol) = _inTypeOfOwner = owner
122+
def inTypeOfOwner: Symbol = _inTypeOfOwner
123+
119124
/** The current tree */
120125
private[this] var _tree: Tree[_ >: Untyped]= _
121126
protected def tree_=(tree: Tree[_ >: Untyped]) = _tree = tree
@@ -179,12 +184,15 @@ object Contexts {
179184
* dependent, we finally also compute `_dependent` based on this context.
180185
*/
181186
if (!_dependentInit) {
182-
_dependent = this.mode.is(Mode.InTypeOf) || isDepOwner(this.owner)
187+
_dependent = this.isInTypeOf || isDepOwner(this.owner)
183188
_dependentInit = true
184189
}
185190
_dependent
186191
}
187192

193+
final def isInTypeOf: Boolean =
194+
this.inTypeOfOwner == this.owner
195+
188196
/** A map in which more contextual properties can be stored
189197
* Typically used for attributes that are read and written only in special situations.
190198
*/
@@ -497,6 +505,7 @@ object Contexts {
497505
def setPeriod(period: Period): this.type = { this.period = period; this }
498506
def setMode(mode: Mode): this.type = { this.mode = mode; this }
499507
def setOwner(owner: Symbol): this.type = { assert(owner != NoSymbol); this.owner = owner; this }
508+
def enterTypeOf(): this.type = { assert(this.owner != NoSymbol); this.inTypeOfOwner = this.owner; this }
500509
def setTree(tree: Tree[_ >: Untyped]): this.type = { this.tree = tree; this }
501510
def setScope(scope: Scope): this.type = { this.scope = scope; this }
502511
def setNewScope: this.type = { this.scope = newScope; this }
@@ -561,6 +570,8 @@ object Contexts {
561570
final def addMode(mode: Mode): Context = withModeBits(c.mode | mode)
562571
final def maskMode(mode: Mode): Context = withModeBits(c.mode & mode)
563572
final def retractMode(mode: Mode): Context = withModeBits(c.mode &~ mode)
573+
574+
final def enterTypeOf(): Context = if (c.isInTypeOf) c else c.fresh.enterTypeOf()
564575
}
565576

566577
implicit class FreshModeChanges(val c: FreshContext) extends AnyVal {

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,9 +4084,6 @@ object Types {
40844084
} else
40854085
tp
40864086

4087-
private def dependently(implicit ctx: Context): Context =
4088-
ctx.addMode(Mode.InTypeOf)
4089-
40904087
object If {
40914088
def apply(underlyingTp: Type, condTp: Type, thenTp: Type, elseTp: Type)(implicit ctx: Context): TypeOf =
40924089
TypeOf(underlyingTp, untpd.If(
@@ -4107,7 +4104,7 @@ object Types {
41074104
treeWithTpe(cond, condTp),
41084105
treeWithTpe(thenp, thenTp),
41094106
treeWithTpe(elsep, elseTp)
4110-
)(dependently)
4107+
)(ctx.enterTypeOf())
41114108
})
41124109
}
41134110

@@ -4126,7 +4123,7 @@ object Types {
41264123
def derived(to: TypeOf)(selectorTp: Type, caseTps: List[Type])(implicit ctx: Context): Type =
41274124
finalizeDerived(to, to.tree match {
41284125
case Trees.Match(selector, cases) =>
4129-
cpy.Match(to.tree)(treeWithTpe(selector, selectorTp), treesWithTpes(cases, caseTps))(dependently)
4126+
cpy.Match(to.tree)(treeWithTpe(selector, selectorTp), treesWithTpes(cases, caseTps))(ctx.enterTypeOf())
41304127
})
41314128
}
41324129

@@ -4145,7 +4142,7 @@ object Types {
41454142
def derived(to: TypeOf)(funTp: Type, argTps: List[Type])(implicit ctx: Context): Type =
41464143
finalizeDerived(to, to.tree match {
41474144
case Trees.Apply(fun, args) =>
4148-
cpy.Apply(to.tree)(treeWithTpe(fun, funTp), treesWithTpes(args, argTps))(dependently)
4145+
cpy.Apply(to.tree)(treeWithTpe(fun, funTp), treesWithTpes(args, argTps))(ctx.enterTypeOf())
41494146
})
41504147
}
41514148

@@ -4164,7 +4161,7 @@ object Types {
41644161
def derived(to: TypeOf)(funTp: Type, argTps: List[Type])(implicit ctx: Context): Type =
41654162
finalizeDerived(to, to.tree match {
41664163
case Trees.TypeApply(fun, args) =>
4167-
cpy.TypeApply(to.tree)(treeWithTpe(fun, funTp), treesWithTpes(args, argTps))(dependently)
4164+
cpy.TypeApply(to.tree)(treeWithTpe(fun, funTp), treesWithTpes(args, argTps))(ctx.enterTypeOf())
41684165
})
41694166
}
41704167

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ class TreeUnpickler(reader: TastyReader,
10401040
case THROW =>
10411041
Throw(readTerm())
10421042
case SINGLETONtpt =>
1043-
SingletonTypeTree(readTerm()(ctx.fresh.addMode(Mode.InTypeOf)))
1043+
SingletonTypeTree(readTerm()(ctx.enterTypeOf()))
10441044
case BYNAMEtpt =>
10451045
ByNameTypeTree(readTpt())
10461046
case NAMEDARG =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ class Typer extends Namer
12031203
}
12041204

12051205
def typedSingletonTypeTree(tree: untpd.SingletonTypeTree)(implicit ctx: Context): SingletonTypeTree = track("typedSingletonTypeTree") {
1206-
val ref1 = typedExpr(tree.ref)(ctx.fresh.addMode(Mode.InTypeOf))
1206+
val ref1 = typedExpr(tree.ref)(ctx.enterTypeOf())
12071207
// TODO: Discuss stability requirements of singleton type trees and potentially reenable check
12081208
// checkStable(ref1.tpe, tree.pos)
12091209
assignType(cpy.SingletonTypeTree(tree)(ref1), ref1)

0 commit comments

Comments
 (0)