Open TessenR opened 8 months ago
@cston was nullability on return one of the items we considered when doing lambda natural types?
Nullable analysis is not re-inferring the function type, so nullable analysis in the method uses the unannotated delegate type. This looks like an oversight.
Version Used:
Steps to Reproduce:
Compile and run the following code (.net8 target framework):
sharplab.io
Expected Behavior: A warning is reported for
x().ToString()
call since the return type ofx
should be inferred to bestring?
Actual Behavior: No warnings at all in the code above. It crashes with a
NullReferenceException
at runtimeNotes: I've discovered a couple of related issues that all seems to have the same root cause - a lambda expression inferes its return type to be not nullable despite having nullable returns. Please let me know if I need to create separate issues for each case.
incorrect type inferecne with the same lambda expression:
incorrect cascading diagnostics:
This one is a bit different because it does report type constraint violation
However, the second diagnostic is very confusing:
According to the warning the lambda expression isn't compatible with
Func<string?>
which is the type inferred from the lambda expression itself. What could even be the return type of the lambda expression to warrant this warning?It seems that Roslyn incorrectly considers the
.Invoke
method of the lambda expression to be not nullable (which might be the root caues of all the issues above) while correctly inferring the type argument to be nullable, resulting in the following "logically impossible" type infernce which gives the same set of warnings: