Skip to content

Commit

Permalink
Fix uninitializing fields when evaluating a cached constructor call i…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
olhotak authored Aug 20, 2024
2 parents 355910d + 36b4949 commit 04ebb28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,10 @@ class Objects(using Context @constructorOnly):
Bottom
}

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

val instance = OfClass(klass, outerWidened, ctor, args.map(_.value), envWidened)
callConstructor(instance, ctor, args)
instance

case ValueSet(values) =>
values.map(ref => instantiate(ref, klass, ctor, args)).join
Expand Down
8 changes: 8 additions & 0 deletions tests/init-global/pos/cache-constructor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Bar:
var f: Int = 0

object A:
val b1 = new Bar()
val b2 = new Bar()
val b3 = new Bar()
b3.f = 1

0 comments on commit 04ebb28

Please sign in to comment.