-
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
Inconsistent behavior for bitwise operations with Nullable<bool> typed local variable and Nullable<bool> typed member binding expression #41760
Comments
Tweaking things to: public static void Process(SomeObject obj) // assume null passed
{
var valz = obj?.BoolValue;
var res1 = obj?.BoolValue | GetTrue();
var res2 = valz | GetTrue();
var res3 = obj?.BoolValue & GetFalse();
var res4 = valz & GetFalse();
Console.WriteLine($"res1: {res1}");
Console.WriteLine($"res2: {res2}");
Console.WriteLine($"res3: {res3}");
Console.WriteLine($"res4: {res4}");
}
public static bool GetTrue()
{
return true;
}
public static bool GetFalse()
{
return false;
} Spits out:
|
@aka-STInG and @Clockwork-Muse I think it has to do with the defined bool? bitwise operator truth table, it works differently for | and &. See below:
|
@brianpursley I'm a bit confused. How this table explain difference between |
Aren't you using | for res2 and & for res4? |
OK maybe I misunderstood the issue. Let me check again |
No you are right. This is seems wrong. It isn't even consistent with itself:
Output:
EDIT: Well I guess this can be in part due to short circuiting. But I think you two are right, something strange is happening here. |
Both generate interesting IL |
This is a language issue, not a codegen issue. @jaredpar can you move this over to dotnet/roslyn? |
Fixed in #43122 |
Here is some sample code. Checked it in console app targeting netcoreapp3.1.
Actual:
res1 != res2 and res3 != res4
Expected:
res1 == res2 and res3 == res4
The text was updated successfully, but these errors were encountered: