dotnet/roslyn

Nullability analysis doesn't quite work for user-defined conversions involved into tuple value conversions

Open

#81675 opened on Dec 12, 2025

View on GitHub
 (0 comments) (0 reactions) (1 assignee)C# (20,414 stars) (4,257 forks)batch import
Area-CompilersFeature - Nullable Reference Typeshelp wanted

Description

The unit-test below reflects the current behavior. Expected no warnings for Test3, note no warnings for Test4

        [Fact]
        public void Test()
        {
            var src = @"
#nullable enable

struct S1
{
    public static implicit operator S1([System.Diagnostics.CodeAnalysis.NotNull] string? x) => throw null!;
}

class Program
{
    static void Test3((string?, int) x)
    {
        (S1, int) s = x;
        x.Item1.ToString();
    } 

    static void Test4(string? x)
    {
        S1 s = x;
        x.ToString();
    } 
}
";
            var comp = CreateCompilation([src, NotNullAttributeDefinition]);
            comp.VerifyDiagnostics(
                // (13,23): warning CS8619: Nullability of reference types in value of type '(string?, int)' doesn't match target type '(S1, int)'.
                //         (S1, int) s = x;
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "x").WithArguments("(string?, int)", "(S1, int)").WithLocation(13, 23),
                // (14,9): warning CS8602: Dereference of a possibly null reference.
                //         x.Item1.ToString();
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x.Item1").WithLocation(14, 9)
                );
        }

Contributor guide