Skip to content

Commit 8af6f21

Browse files
committed
Don't trigger object access effect after super constructor call
1 parent 06e577f commit 8af6f21

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Checker extends Phase {
8181
parentsInited = mutable.Set.empty,
8282
safePromoted = mutable.Set.empty,
8383
dependencies = mutable.Set.empty,
84+
superConstrCalled = false,
8485
env = Env(ctx.withOwner(cls), cache)
8586
)
8687

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ object Checking {
3838
parentsInited: mutable.Set[ClassSymbol],
3939
safePromoted: mutable.Set[Potential], // Potentials that can be safely promoted
4040
dependencies: mutable.Set[Dependency], // dependencies collected for checking global objects
41+
var superConstrCalled: Boolean, // Wether super constructor has been called for the current object
4142
env: Env,
4243
init: Boolean = false // whether the object is initialized, used in CycleChecker
4344
) {
@@ -188,6 +189,10 @@ object Checking {
188189
checkConstructor(cls.primaryConstructor, ref.tpe, ref)
189190
}
190191

192+
// Global objects can be safely accessed after super constructor is called
193+
if cls == state.thisClass then
194+
state.superConstrCalled = true
195+
191196
// check class body
192197
tpl.body.foreach { checkClassBodyStat(_) }
193198
}
@@ -385,7 +390,9 @@ object Checking {
385390

386391
private def checkAccessGlobal(eff: AccessGlobal)(using state: State): Errors =
387392
val obj = eff.potential
388-
if obj.enclosingClass != obj.moduleClass then
393+
if obj.moduleClass != state.thisClass
394+
|| obj.enclosingClass != state.thisClass && !state.superConstrCalled
395+
then
389396
state.dependencies += ObjectAccess(obj.symbol)(state.path)
390397
Errors.empty
391398

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ class CycleChecker(cache: Cache) {
217217
parentsInited = mutable.Set.empty,
218218
safePromoted = mutable.Set(ThisRef()(dep.cls.defTree)),
219219
dependencies = mutable.Set.empty,
220+
superConstrCalled = true,
220221
env = env,
221222
init = true
222223
)
@@ -353,6 +354,7 @@ class CycleChecker(cache: Cache) {
353354
parentsInited = mutable.Set.empty,
354355
safePromoted = mutable.Set(ThisRef()(dep.cls.defTree)),
355356
dependencies = mutable.Set.empty,
357+
superConstrCalled = true,
356358
env = env,
357359
init = true
358360
)

0 commit comments

Comments
 (0)