File tree Expand file tree Collapse file tree 3 files changed +11
-1
lines changed
compiler/src/dotty/tools/dotc/transform/init Expand file tree Collapse file tree 3 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ class Checker extends Phase {
81
81
parentsInited = mutable.Set .empty,
82
82
safePromoted = mutable.Set .empty,
83
83
dependencies = mutable.Set .empty,
84
+ superConstrCalled = false ,
84
85
env = Env (ctx.withOwner(cls), cache)
85
86
)
86
87
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ object Checking {
38
38
parentsInited : mutable.Set [ClassSymbol ],
39
39
safePromoted : mutable.Set [Potential ], // Potentials that can be safely promoted
40
40
dependencies : mutable.Set [Dependency ], // dependencies collected for checking global objects
41
+ var superConstrCalled : Boolean , // Wether super constructor has been called for the current object
41
42
env : Env ,
42
43
init : Boolean = false // whether the object is initialized, used in CycleChecker
43
44
) {
@@ -188,6 +189,10 @@ object Checking {
188
189
checkConstructor(cls.primaryConstructor, ref.tpe, ref)
189
190
}
190
191
192
+ // Global objects can be safely accessed after super constructor is called
193
+ if cls == state.thisClass then
194
+ state.superConstrCalled = true
195
+
191
196
// check class body
192
197
tpl.body.foreach { checkClassBodyStat(_) }
193
198
}
@@ -385,7 +390,9 @@ object Checking {
385
390
386
391
private def checkAccessGlobal (eff : AccessGlobal )(using state : State ): Errors =
387
392
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
389
396
state.dependencies += ObjectAccess (obj.symbol)(state.path)
390
397
Errors .empty
391
398
Original file line number Diff line number Diff line change @@ -217,6 +217,7 @@ class CycleChecker(cache: Cache) {
217
217
parentsInited = mutable.Set .empty,
218
218
safePromoted = mutable.Set (ThisRef ()(dep.cls.defTree)),
219
219
dependencies = mutable.Set .empty,
220
+ superConstrCalled = true ,
220
221
env = env,
221
222
init = true
222
223
)
@@ -353,6 +354,7 @@ class CycleChecker(cache: Cache) {
353
354
parentsInited = mutable.Set .empty,
354
355
safePromoted = mutable.Set (ThisRef ()(dep.cls.defTree)),
355
356
dependencies = mutable.Set .empty,
357
+ superConstrCalled = true ,
356
358
env = env,
357
359
init = true
358
360
)
You can’t perform that action at this time.
0 commit comments