Skip to content

Commit 1b05897

Browse files
committed
More fix for correct owner
1 parent d35d098 commit 1b05897

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

compiler/src/dotty/tools/dotc/transform/init/Checking.scala

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ object Checking {
4141
visited += eff
4242
copy(path = this.path :+ eff.source)
4343
}
44+
45+
def withOwner(sym: Symbol): State = copy(env = env.withOwner(sym))
4446
}
4547

4648
private implicit def theEnv(implicit state: State): Env = state.env
@@ -58,10 +60,10 @@ object Checking {
5860
// mark current class as initialized, required for linearization
5961
state.parentsInited += cls
6062

61-
def checkClassBodyStat(tree: Tree)(using Context): Unit = traceOp("checking " + tree.show, init) {
63+
def checkClassBodyStat(tree: Tree)(implicit state: State): Unit = traceOp("checking " + tree.show, init) {
6264
tree match {
6365
case vdef : ValDef =>
64-
val (pots, effs) = Summarization.analyze(vdef.rhs)(theEnv.withOwner(vdef.symbol))
66+
val (pots, effs) = Summarization.analyze(vdef.rhs)
6567
theEnv.summaryOf(cls).cacheFor(vdef.symbol, (pots, effs))
6668
if (!vdef.symbol.is(Flags.Lazy)) {
6769
checkEffectsIn(effs, cls)
@@ -79,17 +81,35 @@ object Checking {
7981
// see spec 5.1 about "Template Evaluation".
8082
// https://www.scala-lang.org/files/archive/spec/2.13/05-classes-and-objects.html
8183

82-
def checkCtor(ctor: Symbol, tp: Type, source: Tree)(using Context): Unit = {
84+
def checkCtor(ctor: Symbol, tp: Type, source: Tree)(implicit state: State): Unit = traceOp("checking " + ctor.show, init) {
8385
val cls = ctor.owner
8486
val classDef = cls.defTree
8587
if (!classDef.isEmpty) {
86-
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])
87-
else checkSecondaryConstructor(ctor)
88+
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])(state.withOwner(cls))
89+
else checkSecondaryConstructor(ctor)(state.withOwner(cls))
8890
}
8991
else if (!cls.isOneOf(Flags.EffectivelyOpenFlags))
9092
report.warning("Inheriting non-open class may cause initialization errors", source.srcPos)
9193
}
9294

95+
def checkSecondaryConstructor(ctor: Symbol)(implicit state: State): Unit = traceOp("checking " + ctor.show, init) {
96+
val Block(ctorCall :: stats, expr) = ctor.defTree.asInstanceOf[DefDef].rhs
97+
val cls = ctor.owner.asClass
98+
99+
traceOp("check ctor: " + ctorCall.show, init) {
100+
val ctor = ctorCall.symbol
101+
if (ctor.isPrimaryConstructor)
102+
checkClassBody(cls.defTree.asInstanceOf[TypeDef])
103+
else
104+
checkSecondaryConstructor(ctor)
105+
}
106+
107+
(stats :+ expr).foreach { stat =>
108+
val (_, effs) = Summarization.analyze(stat)(theEnv.withOwner(ctor))
109+
checkEffectsIn(effs, cls)
110+
}
111+
}
112+
93113
cls.paramAccessors.foreach { acc =>
94114
if (!acc.is(Flags.Method)) {
95115
traceIndented(acc.show + " initialized", init)
@@ -120,24 +140,6 @@ object Checking {
120140
tpl.body.foreach { checkClassBodyStat(_) }
121141
}
122142

123-
def checkSecondaryConstructor(ctor: Symbol)(implicit state: State): Unit = traceOp("checking " + ctor.show, init) {
124-
val Block(ctorCall :: stats, expr) = ctor.defTree.asInstanceOf[DefDef].rhs
125-
val cls = ctor.owner.asClass
126-
127-
traceOp("check ctor: " + ctorCall.show, init) {
128-
val ctor = ctorCall.symbol
129-
if (ctor.isPrimaryConstructor)
130-
checkClassBody(cls.defTree.asInstanceOf[TypeDef])
131-
else
132-
checkSecondaryConstructor(ctor)
133-
}
134-
135-
(stats :+ expr).foreach { stat =>
136-
val (_, effs) = Summarization.analyze(stat)(theEnv.withOwner(ctor))
137-
checkEffectsIn(effs, cls)
138-
}
139-
}
140-
141143
private def checkEffectsIn(effs: Effects, cls: ClassSymbol)(implicit state: State): Unit = traceOp("checking effects " + Effects.show(effs), init) {
142144
for {
143145
eff <- effs

0 commit comments

Comments
 (0)