Expression of type T cannot be handled by a pattern of type X

Alex Wiese picture Alex Wiese · Jan 3, 2017 · Viewed 8.6k times · Source

I have upgraded my project to target C# 7 and used Visual Studio 2017 RC to implement pattern matching across my solution. After doing this some errors were introduced relating to pattern matching with generic parameters.

Consider the following code:

public class Packet
{
}

public class KeepalivePacket : Packet
{
}

public void Send<T>(T packet)
    where T : Packet
{
    if (packet is KeepalivePacket keepalive)
    {
        // Do stuff with keepalive
    }

    switch (packet)
    {
        case KeepalivePacket keepalivePacket:
            // Do stuff with keepalivePacket
            break;
    }
}

Both the if statement and the case statement produce a compilation error.

An expression of type T cannot be handled by a pattern of type KeepalivePacket

If I first cast the parameter to type object the pattern matching works as expected. Roslyn then marks the cast to object as redundant.

if ((object)packet is KeepalivePacket keepalive)
{
    // This works
}

This error only appears to apply to generic parameters and variables. Roslyn appears to not be aware of this issue as it recommends changing the code to use pattern matching via an analyzer and allows me to apply the "code fix" resulting in the broken code.

Answer

Alex Wiese picture Alex Wiese · Jan 5, 2017

As explained by Neal Gafter from Microsoft:

The reason it doesn’t work is that there is no conversion (explicit or implicit) defined from T to KeepalivePacket. Pattern matching requires such a conversion to exist, as it is defined in terms of the cast operator, which requires a conversion exist. The language specification and compiler agree that no conversion exists. It seems strange to me that the language specification is defined such that no (explicit) conversion exists here. We'll look at what we can do about that.

We're not going to do anything about this in C# 7. You'll have to add a cast to your code to work around it. Once we have recursive patterns, this may be more difficult to work around. Moreover, the awkward language rule that underlies this issue (i.e. that there is no conversion from T to KeepalivePacket) doesn't make a lot of sense.

Update

This is now working in C# 7.1