Skip to content

Commit 6b68e24

Browse files
Backport "Fix uninitializing fields when evaluating a cached constructor call in global initialization checker" to 3.5.2 (#21494)
Backports #21403 to the 3.5.2 branch. PR submitted by the release tooling. [skip ci]
2 parents 4535dff + f8659a9 commit 6b68e24

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Diff for: 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

Diff for: tests/init-global/pos/cache-constructor.scala

+8
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)