-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise NullableWalker to use a two-state solution #33648
Conversation
The NullableWalker is revised so that the inferred state of an expression is either `NotNull` or `MaybeNull` (represented by the new type `NullableFlowState`). There is no longer such as thing as an oblivious rvalue. There are four kinds of lvalues: - oblivious lvalues read as NotNull but null can be written to them - annotated lvalues read as MaybeNull and null can be written to them - unannotated lvalues read as NotNull and null may not be written to them - unconstrained type parameters read as MaybeNull but null may not be written to them In order to preserve the safety in the face of such unconstrained type parameters, we warn immediately when a null value of such a type is introduced. This is a safety warning. The contexts in which such a warning are given are - `default` and `default(T)` - `null` conversion to `T` (when `T` is known to be ref type) - `e?.M()` when the return type is `T` - dynamic conversion or cast to `T` when the dynamic might be null - explicit conversion to `T` - `e as T` when there is not an implicit conversion from the type of `e` to `T` - a call to a method like `FirstOrDefault()` that is annotated with `[MaybeNull] (not yet implemented) We remove the hidden diagnostics previously computed by the NullableWalker. We add debugger display support for a number of types used by the NullableWalker. The display for `NullableWalker` summarizes the computed nullability of variables in a nice compact form. We remove support for definite assignment in the NullableWalker. Some scenarios involving the use of not-definitely-assigned variables may cause cascaded diagnostics. We might remove them in the future by initializing all vaiables to the `NotNull` state when they enter scope. We overhaul and simplify the inplementation of conversions, the null-conditional operator, and result inference for lifted operators. We simplify the Meet and Join operations so that they form a lattice on both the NullableAnnotation and NullableFlowState. Catch variables are now initialized to a non-null state on entry to the catch block. In unreachable code, every expression produces a non-null rvalue. An erroneous expression produces a non-null rvalue (to suppress cascaded diagnostics).
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
All the properties that dereference Refers to: src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs:599 in 6c329e3. [](commit_id = 6c329e3, deletion_comment = False) |
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
Outdated
Show resolved
Hide resolved
Tracked by #33743 (issue with In reply to: 468053080 [](ancestors = 468053080) Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:67259 in eb68fb8. [](commit_id = eb68fb8, deletion_comment = True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (iteration 9)
I'm going to merge latest bits from master into this branch to resolve conflicts and investigate the failing test (stack overflow on long fluent invocation)
Then this is incorrect for boolean logical operators: https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#boolean-logical-operators In reply to: 468145643 [](ancestors = 468145643,468073628) Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:1717 in eb68fb8. [](commit_id = eb68fb8, deletion_comment = False) |
var otherAnnotation = other.NullableAnnotation; | ||
if (!HasType) | ||
{ | ||
return thisAnnotation == otherAnnotation; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is guaranteed to be true, as we'd have already returned above it wasn't.
if (other.IsNull || !TypeSymbolEquals(other, comparison)) | ||
if (!HasType) | ||
{ | ||
if (other.HasType || NullableAnnotation != other.NullableAnnotation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just return !other.HasType && NullableAnnotation == other.NullableAnnotation
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (iteration 9) with filled followup issues.
…lablewalker-to-use-a-two-state-solution
@@ -33,7 +33,7 @@ public void OverflowOnFluentCall() | |||
numberFluentCalls = 460; | |||
break; | |||
case 32 when !isDebug: | |||
numberFluentCalls = 1300; | |||
numberFluentCalls = 1100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1100 [](start = 40, length = 4)
Filed #33775 to investigate further and restore our expectations
The NullableWalker is revised so that the inferred state of an expression is either
NotNull
orMaybeNull
(represented by the new typeNullableFlowState
). There is no longer such as thing as an oblivious rvalue. There are four kinds of lvalues:In order to preserve the safety in the face of such unconstrained type parameters, we warn immediately when a null value of such a type is introduced. This is a safety warning. The contexts in which such a warning are given are
default
anddefault(T)
null
conversion toT
(whenT
is known to be ref type)e?.M()
when the return type isT
T
when the dynamic might be nullT
e as T
when there is not an implicit conversion from the type ofe
toT
FirstOrDefault()
that is annotated with `[MaybeNull] (not yet implemented)We remove the hidden diagnostics previously computed by the NullableWalker.
We add debugger display support for a number of types used by the NullableWalker. The display for
NullableWalker
summarizes the computed nullability of variables in a nice compact form.We remove support for definite assignment in the NullableWalker. Some scenarios involving the use of not-definitely-assigned variables may cause cascaded diagnostics. We might remove them in the future by initializing all vaiables to the
NotNull
state when they enter scope.We overhaul and simplify the inplementation of conversions, the null-conditional operator, and result inference for lifted operators.
We simplify the Meet and Join operations so that they form a lattice on both the NullableAnnotation and NullableFlowState.
Catch variables are now initialized to a non-null state on entry to the catch block.
In unreachable code, every expression produces a non-null rvalue. An erroneous expression produces a non-null rvalue (to suppress cascaded diagnostics).