Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "Fix uninitializing fields when evaluating a cached constructor call in global initialization checker" to 3.5.2 #21494

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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