diff --git a/compiler/src/dotty/tools/dotc/core/NullOpsDecorator.scala b/compiler/src/dotty/tools/dotc/core/NullOpsDecorator.scala index f1ff60e4ea22..572c94070066 100644 --- a/compiler/src/dotty/tools/dotc/core/NullOpsDecorator.scala +++ b/compiler/src/dotty/tools/dotc/core/NullOpsDecorator.scala @@ -47,7 +47,7 @@ object NullOpsDecorator: /** Is self (after widening and dealiasing) a type of the form `T | Null`? */ def isNullableUnion(using Context): Boolean = { - val stripped = self.stripNull() + val stripped = self.stripNull(stripFlexibleTypes = false) stripped ne self } end extension diff --git a/tests/explicit-nulls/pos/flow-flexible.scala b/tests/explicit-nulls/pos/flow-flexible.scala new file mode 100644 index 000000000000..ea6d5fef2a95 --- /dev/null +++ b/tests/explicit-nulls/pos/flow-flexible.scala @@ -0,0 +1,9 @@ +// This test is based on sconfig/sconfig/shared/src/main/scala/org/ekrich/config/impl/SimpleConfig.scala +// In cases where a nullable variable is assigned a flexible type, we want +// later code to type the variable access as non-nullable +// Added in https://github.com/scala/scala3/pull/24278 + +def main() = + var result: String | Null = null + result = "".trim() + result.trim() \ No newline at end of file