-
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
IDE0066 incorrectly handles nullable value types #37950
Comments
Fixed in #37052 |
note that this would be still an issue if there is no target-type until dotnet/csharplang#33 is implemented, but we wont suggest 0066 in those cases at the moment. |
perhaps I am. According to the feature spec dotnet/csharplang#2389 I expect this to work - there's no natural type but all arms are implicitly convertible to the target type. Edit: I do see why this happens though. The compiler yields bool as the best common type - excluding typeless expressions - but doesn't check if all expressions can be successfully converted. The same with (even different) reference types could work because best type algorithm would fail in the first place and if it's not, cc @gafter |
The fix for IDE is in review #38007, in case this is the intended behavior. |
Th problem is that there is a common type according to the current spec/implementation - because only two switch arms have expressions with types, and those are the same type. The spec and implementation should be amended to require that all arms are convertible to that common type in order for it to be considered the natural type of the switch. |
I'll note that dotnet/csharplang#33 will address this issue by taking |
Yes, but the switch expression must take lambdas, method groups, and other typeless things into account too. |
So if I understand correctly, "nullable-enhanced common type" would not help with the following (int?, int) t = b switch { true => (null, 1), false => (1, 2) }; but it should actually work because there's no natural type for the switch expression. |
I think that's right. However, it should work whether or not there is a natural type for the switch expression. |
I am going to aim to fix the compiler behavior in 16.4. |
Compiler part moved to #38226 |
Version Used:
3.3.0-beta3-19406-05+a1905991543bed104f7f7f0842aca2b65d263b87
Steps to Reproduce:
Expected Behavior:
IDE0066 refactors this into something that compiles, e.g.
Actual Behavior:
IDE0066 refactors this into the following code that doesn't compile:
This fails with the error "error CS0037: Cannot convert null to 'bool' because it is a non-nullable value type".
The text was updated successfully, but these errors were encountered: