-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Nullable analysis should understand XOR null tests #36927
Comments
Use |
Thanks. Several workarounds exist. This issue is tracking a gap in the product. It's not uncommon to see bitwise operators used on bools. |
Our nullable analysis just doesn't learn anything in the WhenFalse case of I feel that in a scenario like this we should ask the user to change |
To provide a little background, the compiler does what is usually referred to as a "path-independent flow analysis" for these situations. What this means practically is that conditional states are intersected at the end of the condition. So if (o1 == null && o2 == null) // cond 1
return "both null";
if ((o1 == null) ^ (o2 == null)) // cond 2
return "one null"; Is processed like if (o1 == null &&
^ split o1 is null and not null
1) o1 is null
o2 == null
^ split o2 is null and not null
1) o2 is null
return
2) goto cond 2
2) goto cond 2
// cond 2
state = (o1 NotNull && o2 MaybeNull) /\ (o1 MaybeNull && o2 NotNull) = o1 MaybeNull && o2 MaybeNull So because the states are intersected at One way to learn about the states would be to do what's called a "path-dependent" flow analysis, but that can be significantly more expensive and is not in the design of C# 8. |
Closing as out of scope of the design. |
Duplicate of #37344 (nullability analysis for single and-operator) |
Version Used: 3.3.0-beta1-19327-04
Steps to Reproduce:
Expected Behavior:
No diagnostics.
Actual Behavior:
Diagnostics.
The text was updated successfully, but these errors were encountered: