From eae6a33adac08b0ee105d9c5ea7a3c40bf249e34 Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Tue, 20 Aug 2024 12:52:07 -0400 Subject: [PATCH] Fix uninitializing fields when evaluating a cached constructor call [Cherry-picked a722358a0944e788c8aef15dac0af19e811d4761] --- .../src/dotty/tools/dotc/transform/init/Objects.scala | 1 - tests/init-global/pos/cache-constructor.scala | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/init-global/pos/cache-constructor.scala diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 1ceb8d4472a3..892cd3ce8b06 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -950,7 +950,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 diff --git a/tests/init-global/pos/cache-constructor.scala b/tests/init-global/pos/cache-constructor.scala new file mode 100644 index 000000000000..f7af30f32516 --- /dev/null +++ b/tests/init-global/pos/cache-constructor.scala @@ -0,0 +1,9 @@ +class Bar: + var f: Int = 0 + +object A: + val b1 = new Bar() + val b2 = new Bar() + val b3 = new Bar() + b3.f = 1 + \ No newline at end of file