Skip to content
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

Unexpected nullability warning is reported for erroneous conditional assignment #72912

Open
AlekseyTs opened this issue Apr 6, 2024 · 1 comment

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void Test()
        {
            string source = @"
#nullable enable

public class C
{
    public static void Main()
    {
        C2? c = null;
        var a = c ??= ""2"";
        Print(a);        
    }

    static void Print(dynamic b) 
    {
    }
}

class C2 {}
";

            var comp = CreateCompilation(source, options: TestOptions.DebugExe, targetFramework: TargetFramework.StandardAndCSharp);

            comp.VerifyDiagnostics(
                // (9,17): error CS0019: Operator '??=' cannot be applied to operands of type 'C2' and 'string'
                //         var a = c ??= "2";
                Diagnostic(ErrorCode.ERR_BadBinaryOps, @"c ??= ""2""").WithArguments("??=", "C2", "string").WithLocation(9, 17),
                // (9,23): warning CS8619: Nullability of reference types in value of type 'string' doesn't match target type 'C2'.
                //         var a = c ??= "2";
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, @"""2""").WithArguments("string", "C2").WithLocation(9, 23)
                );
        }

The nullability warning doesn't really make sense. The nullability actually matches, the types are different though.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 6, 2024
@AlekseyTs
Copy link
Contributor Author

CC @333fred

AlekseyTs added a commit that referenced this issue Apr 11, 2024
…c` arguments (#72964)

Fixes #72750.

For C# 12 language version: behavior of the compiler in regards to deciding between whether binding should be static or dynamic is the same as behavior of C# 12 compiler. As a result, for affected scenarios, what used to have `dynamic` type in C# 12 compiler will have `dynamic` type when C# 12 language version is targeted.

For newer language versions: invocations statically bound in presence of dynamic arguments should have dynamic result if their dynamic binding succeeded in C# 12. Corresponding spec update - dotnet/csharplang#8027 at commit 8.

Related to #33011, #72906, #72912, #72913, #72914, #72916, #72931.
AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue Apr 16, 2024
…c` arguments (dotnet#72964)

Fixes dotnet#72750.

For C# 12 language version: behavior of the compiler in regards to deciding between whether binding should be static or dynamic is the same as behavior of C# 12 compiler. As a result, for affected scenarios, what used to have `dynamic` type in C# 12 compiler will have `dynamic` type when C# 12 language version is targeted.

For newer language versions: invocations statically bound in presence of dynamic arguments should have dynamic result if their dynamic binding succeeded in C# 12. Corresponding spec update - dotnet/csharplang#8027 at commit 8.

Related to dotnet#33011, dotnet#72906, dotnet#72912, dotnet#72913, dotnet#72914, dotnet#72916, dotnet#72931.
AlekseyTs added a commit that referenced this issue Apr 17, 2024
Restore dynamic as result type of some operations involving dynamic arguments (#72964)

Fixes #72750.

For C# 12 language version: behavior of the compiler in regards to deciding between whether binding should be static or dynamic is the same as behavior of C# 12 compiler. As a result, for affected scenarios, what used to have dynamic type in C# 12 compiler will have dynamic type when C# 12 language version is targeted.

For newer language versions: invocations statically bound in presence of dynamic arguments should have dynamic result if their dynamic binding succeeded in C# 12. Corresponding spec update - dotnet/csharplang#8027 at commit 8.

Related to #33011, #72906, #72912, #72913, #72914, #72916, #72931.
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 22, 2024
@jaredpar jaredpar added this to the Backlog milestone Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants