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

Is T expressions result in CS8121 error if sealed class, implementing a generic interface, is used for T #16993

Closed
DavidArno opened this issue Feb 7, 2017 · 7 comments
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue

Comments

@DavidArno
Copy link

DavidArno commented Feb 7, 2017

Visual C# 2017 RC 00369-50000-00000-AA146
Visual Studio/15.0.0-RC.3+26127.3:

Steps to Reproduce:

Attempt to compile the following code:

namespace NS1
{
    public interface ITest<T> {}

    internal class Test1 : ITest<int> { }
    internal sealed class Test2 : ITest<int> { }

    internal class C<T>
    {
        public void F(ITest<T> x)
        {
            if (x is Test1 z) { }
            if (x is Test2 y) { }
        }
    }
}

Expected Behavior:
All compiles OK.

Actual Behavior:
The if (x is Test2 y) { } line results in the error:
CS8121 An expression of type ITest<T> cannot be handled by a pattern of type Test2

@DavidArno DavidArno changed the title Is T expressions result in CS8121 error if sealed class is used for T Is T expressions result in CS8121 error if sealed class, implementing a generic interface, is used for T Feb 7, 2017
@controlflow
Copy link

Hm, ordinary x is Test2 compiles fine

@DavidArno
Copy link
Author

DavidArno commented Feb 7, 2017

@controlflow,

Indeed, it's only when the new x is T y pattern match is used that the problem occurs and only when the base type is generic and the type in the pattern is sealed.

@DavidArno
Copy link
Author

@Pilchie,

I notice you have only tagged this as "Area-Language-Design", rather than as a bug. Does this mean that the x is T y pattern deliberately doesn't support sealed classes? Is there anything documented on why this is the case, if so?

@Pilchie
Copy link
Member

Pilchie commented Feb 7, 2017

Just trying to get it closer to the right people looking at it :)

@aelij
Copy link
Contributor

aelij commented Apr 22, 2017

Also note that the "use pattern matching" analyzer produces an incorrect code fix (IDE0019) that results in non-compilable code in these cases.

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Apr 22, 2017

tagging @gafter I believe this is a manifestation of dotnet/csharplang#154, which is something he's aware of and which we're taking a fix for for 7.1. However, i'ts not 100% clear to me this is the same issue. So i'd like him to weigh in.

@gafter
Copy link
Member

gafter commented Apr 23, 2017

The simple way to demonstrate that this is the same issue is to see if you get an error when the body of F is rewritten this way:

            //if (x is Test1 z) { }
            var z = (Test1)x;

            //if (x is Test2 y) { }
            var y = (Test2)x;

Indeed, the second cast results in an error.

So this is a dup of dotnet/csharplang#154

@gafter gafter closed this as completed Apr 23, 2017
@gafter gafter added Feature Request Resolution-Duplicate The described behavior is tracked in another issue labels Apr 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants