Description
Compiler version
3.1.3 RC1 with -Yexplicit-nulls
Minimized example
When reviewing #14032, I observed some pain points for explicit null checking that we might be able to fix. In decreasing order of importance:
-
Flow typing does not extend to variables of enclosing methods or fields of enclosing classes. It's clearly more difficult to know when a nullability info needs to be invalidated in these cases. But there is one situation, which happens to be the most common one, where I think we should be able to check nullability: It's when we know that a variable is initially null and every assignment to the variable is a non-null value. In that case, there's nothing to invalidate: once we know a variable is non-null at some point, it will stay non-null forever. If we could exploit that knowledge, the vast majority of caches in dotty could be null-checked without needing
.nn
.The tricky bit is sequencing. To know that all assignments are non-null we need to type check the program. But that means we might have to type check dereferences to a nullable variable first. We could try to find a better phase ordering. Maybe delay flow-typing until after typer. During type checking, always proceed under the
unsafeNulls
assumption. During subsequent flow typing, remove inserted.nn
calls if they are redundant according to flow typing rules. If any inserted.nn
calls remain, issue errors unlessunsafeNulls
is imported. -
Calls to Java methods. Super annoying to have to write
System.err.nn.println(...)
orstr.substring(a, b).nn
. We should consider erring more towards unsoundness here. -
eq
, andne
should also work for nullable types. We create a lot of complexity in comparisons since that's currently not the case.