From 78bd9c565a51bc2c5e2d2da3a2f4e89ef288b1b5 Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Fri, 29 Aug 2025 23:47:21 -0400 Subject: [PATCH] fix assigning to null --- .../src/dotty/tools/dotc/transform/init/Objects.scala | 2 +- tests/init-global/pos/assignment-to-null.scala | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/init-global/pos/assignment-to-null.scala diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 92262d528487..8b631f56c61e 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -905,7 +905,7 @@ class Objects(using Context @constructorOnly): // the typer might mistakenly set the receiver to be a package instead of package object. // See pos/packageObjectStringInterpolator.scala if packageModuleClass == klass || (klass.denot.isPackageObject && klass.owner == packageModuleClass) then a else Bottom - case v: SafeValue => if v.typeSymbol.asClass.isSubClass(klass) then a else Bottom + case v: SafeValue => if v.typeSymbol.asClass.isSubClass(klass) && v.typeSymbol.asClass != defn.NullClass then a else Bottom case ref: Ref => if ref.klass.isSubClass(klass) then ref else Bottom case ValueSet(values) => values.map(v => v.filterClass(klass)).join case fun: Fun => diff --git a/tests/init-global/pos/assignment-to-null.scala b/tests/init-global/pos/assignment-to-null.scala new file mode 100644 index 000000000000..1bbc66ae4d44 --- /dev/null +++ b/tests/init-global/pos/assignment-to-null.scala @@ -0,0 +1,9 @@ +class C { + var f: Int = 1 +} + +object O { + var c: C = null + c = new C + c.f = 2 +}