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

Type pattern in switch expression involving generic type with non-keyword type parameters causes syntax error #47614

Closed
PathogenDavid opened this issue Sep 11, 2020 · 3 comments · Fixed by #47756
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug New Language Feature - Pattern Matching Pattern Matching
Milestone

Comments

@PathogenDavid
Copy link
Contributor

Version Used: Compiler version: '3.8.0-4.20460.4 (64f2392)'. Language version: preview.

Steps to Reproduce:

Compile this code: (SharpLab)

using System;

public class Test
{
    public string M(object o)
        => o switch
        {
            string => "String",
            Action => "Action",
            Action<int> => "Aciton<int>",
            Action<object> => "Action<object>",
            Action<Int16> => "Action<Int16>", // <-- Syntax Errors
            Action<Test> => "Action<Test>", // <-- Syntax Errors
            _ => "Unknown"
        };
}

Expected Behavior: Compiles without errors

Actual Behavior: The lines marked have errors associated with them:

Line 12: error CS1001: Identifier expected
Line 12: error CS1003: Syntax error, '=>' expected
Line 12: error CS1525: Invalid expression term ','
Line 13: error CS1001: Identifier expected
Line 13: error CS1003: Syntax error, '=>' expected
Line 13: error CS1525: Invalid expression term ','

This does not happen with the equivalent switch statement: (SharpLab)

using System;

public class Test
{
    public string M(object o)
    {
        switch (o)
        {
            case string:
                return "String";
            case Action:
                return "Action";
            case Action<int>:
                return "Aciton<int>";
            case Action<object>:
                return "Aciton<object>";
            case Action<Int16>:
                return "Aciton<Int16>";
            case Action<Test>:
                return "Aciton<Test>";
            default:
                return "Unknown";
        }
    }
}
@PathogenDavid
Copy link
Contributor Author

I almost forgot to mention, this does not happen with declaration patterns with/without a discard: (SharpLab)

using System;

public class Test
{
    public string M(object o)
        => o switch
        {
            string => "String",
            Action => "Action",
            Action<int> => "Aciton<int>",
            Action<object> => "Action<object>",
            Action<Int16> _ => "Action<Int16>",
            Action<Test> actionTest => "Action<Test>",
            _ => "Unknown"
        };
}

Although a well-meaning but unhelpful (and somewhat misleadingly labeled) code fix offers to remove the variable declaration:

image

@PathogenDavid PathogenDavid changed the title Type pattern in switch expression involving generic type with non-keyword types causes syntax error Type pattern in switch expression involving generic type with non-keyword type parameters causes syntax error Sep 11, 2020
@gafter gafter self-assigned this Sep 16, 2020
@gafter gafter added this to the 16.8 milestone Sep 16, 2020
@gafter
Copy link
Member

gafter commented Sep 16, 2020

See https://github.com/dotnet/csharplang/blob/master/proposals/csharp-9.0/patterns3.md#change-to-7542-grammar-ambiguities for the spec change needed to support this.

@PathogenDavid
Copy link
Contributor Author

Thanks for the fix @gafter, works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug New Language Feature - Pattern Matching Pattern Matching
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants