Unions: a generic union instantiated with case types that differ only by nullable annotations is accepted
Version Used: .NET SDK 11.0.100-rc.1.26425.128, compiler 5.11.0-1.26425.128 (3551975be08744f0418857c5bed8ab1545c5dd47), <Nullable>enable</Nullable>.
Steps to Reproduce
IList<string?> failureList = ["oops", null];
Result<IList<string>, IList<string?>> a = failureList; // compiles
Result<IList<string>, IList<string?>> b = new(failureList); // error CS0121: ambiguous
Result<string, string?> c = "x"; // error CS0457: ambiguous
Console.WriteLine(a switch { IList<string> s => "success", IList<string?> f => "failure" }); // error CS8510 on the second arm
public readonly union Result<TSuccess, TFailure>(TSuccess, TFailure);The non-generic equivalent is rejected at the declaration:
public readonly union Lists(IList<string>, IList<string?>); // error CS0111: 'Lists' already defines a member called 'Lists' with the same parameter typesExpected Behavior
Result<IList<string>, IList<string?>> is rejected like Lists: IList<string> and IList<string?> are the same type at run time, and the union can't tell which case it holds.
Actual Behavior
The instantiation is accepted, and the first line compiles without a diagnostic; the value ends up in the first case, and with the switch a matches IList<string>. The compiler treats the two case types as identical for the constructor, for overload resolution and for pattern matching, but not for the implicit union conversion.
Source: dotnet/roslyn