Skip to content

Semantic model incorrectly gives type for switch expression #45041

@gafter

Description

@gafter

In the following code

class C
{
    void M()
    {
        var i = 1;
        C x = i switch
        {
            0 => new A(),
            1 => new B(),
            _ => throw new ArgumentException(),
        };
    }
}
 
class A : C { }
 
class B : C { }

The switch expression has no "natural" type, as there is no common type among the expressions. It does, however, have a ConvertedType of C due to the target-typed switch expression conversion.

However, the semantic model currently reports that the switch expression has the type C. It should return null for the type.

/cc @allisonchou

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions