Skip to content

Commit 04ebb28

Browse files
authored
Fix uninitializing fields when evaluating a cached constructor call in global initialization checker (#21403)
This PR fixes the accessing uninitialized field errors given by the global initialization checker, where fields are uninitialized when evaluating a cached constructor call during the analysis.
2 parents 355910d + 36b4949 commit 04ebb28

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,10 @@ class Objects(using Context @constructorOnly):
908908
Bottom
909909
}
910910

911-
/** Handle new expression `new p.C(args)`.
911+
/**
912+
* Handle new expression `new p.C(args)`.
913+
* The actual instance might be cached without running the constructor.
914+
* See tests/init-global/pos/cache-constructor.scala
912915
*
913916
* @param outer The value for `p`.
914917
* @param klass The symbol of the class `C`.
@@ -950,7 +953,6 @@ class Objects(using Context @constructorOnly):
950953

951954
val instance = OfClass(klass, outerWidened, ctor, args.map(_.value), envWidened)
952955
callConstructor(instance, ctor, args)
953-
instance
954956

955957
case ValueSet(values) =>
956958
values.map(ref => instantiate(ref, klass, ctor, args)).join
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Bar:
2+
var f: Int = 0
3+
4+
object A:
5+
val b1 = new Bar()
6+
val b2 = new Bar()
7+
val b3 = new Bar()
8+
b3.f = 1

0 commit comments

Comments
 (0)