Feature Request: tracking boolean logic to compute nullability #2388
Replies: 5 comments
-
Why is this a language issue vs just a compiler issue? The language says "you must not use an unassigned variable"; does it actually say that the following snippet should not compile? (or throw a null ref warning if s is assigned null):
dotnet/roslyn#31893 says "we don't trace values"; which implies that if the compiler did do some logic tracing then it could be happy to compile it. I don't think a langauge feature that states "a compiler must consider all permutations of boolean conditions before warning/erroring a potential use of a null/unassigned variable" could be written, as that opens the door to forcing the compiler to do arbitrarily complicated calculations (what if you have 4 if clauses in the function, with 4 variables; is the compiler expected to search all 32 conditional code branches for accidential miss-use if a potential Null warning or unassigned use error is detected? I think that reduced statement like "an object may be safely used if it used under the same conditions it is assigned" -- it doesn't solve the larger problem posed in the original question, but it would allow a subset of these to compile without issues. -- (this comment may be edited if i've missed something and the language itself says that my fragment must not compile) |
Beta Was this translation helpful? Give feedback.
-
This is with regards to flow analysis for C# 8.0's Nullable Reference Types feature, not unassigned variables. |
Beta Was this translation helpful? Give feedback.
-
Neither the language nor the compiler have a concept of "the same condition". |
Beta Was this translation helpful? Give feedback.
-
In the given example, would it still avoid the warning if first and second were fields, not locals? I.e. Would the theorem prover err on the side of being conservative, or would it make the assumption that the code is well behaved? Currently nullability analysis makes the assumption code is well behaved, whereas definite assignment analysis is conservative. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt The hypothetical theorem prover hasn't been designed, so we don't know. |
Beta Was this translation helpful? Give feedback.
-
See also dotnet/roslyn#34665 reported by @stephentoub
Consider the following program
This warns on the last line that
second
might be null ("Possible dereference of a null reference"), but it is possible to infer that's not possible, given the Assert that at least one of first or second is non-null and then that first is null.Implementing a compiler that is smart enough to avoid the warning may require some theorem-prover-style analysis, which might be useful for many other things.
See also dotnet/roslyn#31893 dotnet/roslyn#34976
Beta Was this translation helpful? Give feedback.
All reactions