dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19k stars 4.03k forks source link

Type mismatch error not reported on inconvertible expression in object initializer with incomplete member assignment #74954

Open Rekkonnect opened 1 month ago

Rekkonnect commented 1 month ago

Version Used

VS 2022 Version 17.11.2 Roslyn 4.11.0 (5e3a11e2e7)

Project info C# 12.0 .NET 8.0

Steps to Reproduce

public class C
{
    public Make M()
    {
        return new()
        {
            Name = 123,
            Value =,
        }
    }
}

public class Make
{
    public string Name { get; set; }
    public int Value { get; set; }
}

In SharpLab

In the above example, 123 cannot be assigned to Name because int cannot be converted to string.

Expected Behavior

An error is reported on 123.

Actual Behavior

No error is reported, and instead only a nullability warning may appear (if nullable is enabled). Despite the nullability annotations, the warning is reported.

Notes

This does not happen if the actual name of the type is provided, like Make. So, based on the above example, if we write

        return new Make()
        {
            Name = 123,
            Value =,
        }

the error would be correctly reported on 123.

CyrusNajmabadi commented 1 month ago

If there are no parse errors, is the semantic error reported?

Rekkonnect commented 1 month ago

Yes

CyrusNajmabadi commented 1 month ago

Seems fine to me then. You fix the error and find out about the other one.