Skip to content
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