Open TessenR opened 8 months ago
@jcouv I seem to recall a decision that nullability attributes didn't factor into constraint checking, or something like that. Is it possible that a similar decision was made here, for nullability attributes to not factor in to delegate compatibility?
@jcouv ?
Nullability attributes do factor in delegate conversion. Below is an example with conversion of lambda to delegate type.
So the example in OP seems a bug.
FWIW, the logic lives in CheckValidNullableMethodOverride
.
#nullable enable
using System.Diagnostics.CodeAnalysis;
public class C
{
void M()
{
MyDel2<string> d5 = (t) => t; // warning CS8621: Nullability of reference types in return type of 'lambda expression' doesn't match the target delegate 'C<T>.MyDel2<string>' (possibly because of nullability attributes).
MyDel2<string> d6 = [return: MaybeNull] string (string t) => t;
}
[return: MaybeNull] delegate T2 MyDel2<T2>(T2 t);
}
Version Used:
Steps to Reproduce:
Compile the following code:
sharplab.io
Diagnostic Id:
CS8621, CS8603
Expected Behavior: No warnings in the code above. All the delegate conversions are safe and equivalent to each other
Actual Behavior: False
CS8603: Possible null reference return.
andCS8621: Nullability of reference types in return type of 'lambda expression' doesn't match the target delegate 'C<T>.MyDel' (possibly because of nullability attributes).
are reported for non-method group conversions