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

Missing nullability warning for an explicit conversion to a type parameter #30925

Closed
AlekseyTs opened this issue Nov 2, 2018 · 1 comment
Closed
Assignees
Labels
Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void Test()
        {
            var source = @"
class Outer
{
    void M1<T>(T x1, object? y1) where T : class?
    {
        x1 = (T)y1;
    }

    void M2<T>(T x2, object? y2) where T : class?
    {
        x2 = y2;
    }

    void M3(string x3, object? y3)
    {
        x3 = (string)y3;
    }

    void M4(string x4, object? y4)
    {
        x4 = y4;
    }
}
";

            CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics(
                // (11,14): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?)
                //         x2 = y2;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y2").WithArguments("object", "T").WithLocation(11, 14),
                // (11,14): warning CS8619: Nullability of reference types in value of type 'object' doesn't match target type 'T'.
                //         x2 = y2;
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y2").WithArguments("object", "T").WithLocation(11, 14),
                // (16,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //         x3 = (string)y3;
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(string)y3").WithLocation(16, 14),
                // (16,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //         x3 = (string)y3;
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(string)y3").WithLocation(16, 14),
                // (21,14): error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
                //         x4 = y4;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y4").WithArguments("object", "string").WithLocation(21, 14),
                // (21,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //         x4 = y4;
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y4").WithLocation(21, 14),
                // (21,14): warning CS8619: Nullability of reference types in value of type 'object' doesn't match target type 'string'.
                //         x4 = y4;
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y4").WithArguments("object", "string").WithLocation(21, 14)
                );
        }

Observed:
No warnings for x1 = (T)y1;.

Expected :
A warning because T could be a not nullable reference type and y1 is object?.

@jcouv
Copy link
Member

jcouv commented Jan 30, 2019

I think this was fixed by #32850
I'll add a test to verify

@jcouv jcouv self-assigned this Jan 30, 2019
@sharwell sharwell added Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented and removed 3 - Working labels Feb 1, 2019
@sharwell sharwell modified the milestones: 16.1, 16.0.P4 Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

5 participants