Skip to content

Commit dda7386

Browse files
committed
Verify that flow inference for blocks only works inside methods
There's no principled reason for this. It's just that our approach with the new type of Completer seems brittle, so I'd like to guarantee that is only used for vals that appear inside method bodies, since those must be forced when the corresponding block is typed (so it seems safe to use the completion context, as opposed to the creation one).
1 parent d918ca5 commit dda7386

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,8 @@ class Typer extends Namer
21192119
import untpd.modsDeco
21202120
val ctx1 = if (ctx.settings.YexplicitNulls.value) {
21212121
mdef match {
2122-
case mdef: untpd.ValDef if !mdef.mods.is(Lazy) && !mdef.mods.is(Implicit) => ctx.fresh.addNonNullFacts(facts)
2122+
case mdef: untpd.ValDef if ctx.owner.is(Method) && !mdef.mods.is(Lazy) && !mdef.mods.is(Implicit) =>
2123+
ctx.fresh.addNonNullFacts(facts)
21232124
case _ => ctx
21242125
}
21252126
} else {

tests/neg/explicit-null-flow5.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Foo(x: String|Null) {
55

66
// Test within constructor
77
if (x == null) throw new NullPointerException()
8-
val x2: String = x // ok: x == null case handled by throwing
8+
val x2: String = x // error: flow inference for blocks doesn't work inside constructors
99

1010
def foo(): Unit = {
1111
val y: String|Null = ???

0 commit comments

Comments
 (0)