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

Better diagnostics wanted for use of nullable type in switch pattern #16708

Open
gafter opened this issue Jan 23, 2017 · 2 comments
Open

Better diagnostics wanted for use of nullable type in switch pattern #16708

gafter opened this issue Jan 23, 2017 · 2 comments
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@gafter
Copy link
Member

gafter commented Jan 23, 2017

We currently produce suboptimal parse errors for an attempt to use a nullable type in a switch pattern. We interpret it as a ?: expression, producing syntax errors, rather than letting the user know that a nullable type may not be used in a type pattern. For example:

        [Fact, WorkItem(16559, "https://github.com/dotnet/roslyn/issues/16559")]
        public void CasePatternVariableUsedInCaseExpression()
        {
            var program = @"
public class Program5815
{
    public static void Main(object o)
    {
        switch (o)
        {
            case Color Color:
            case Color? Color2:
                break;
        }
    }
    private static object M() => null;
}";
            CreateCompilationWithMscorlib45(program).VerifyDiagnostics(
                // (9,32): error CS1525: Invalid expression term 'break'
                //             case Color? Color2:
                Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments("break").WithLocation(9, 32),
                // (9,32): error CS1003: Syntax error, ':' expected
                //             case Color? Color2:
                Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments(":", "break").WithLocation(9, 32),
                // (8,18): error CS0118: 'Color' is a variable but is used like a type
                //             case Color Color:
                Diagnostic(ErrorCode.ERR_BadSKknown, "Color").WithArguments("Color", "variable", "type").WithLocation(8, 18),
                // (9,25): error CS0103: The name 'Color2' does not exist in the current context
                //             case Color? Color2:
                Diagnostic(ErrorCode.ERR_NameNotInContext, "Color2").WithArguments("Color2").WithLocation(9, 25)
                );
        }
@gafter gafter added Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels Jan 23, 2017
@gafter gafter modified the milestones: Unknown, 3.0 Jan 25, 2017
@svick
Copy link
Contributor

svick commented May 24, 2017

Using a nullable type in an is pattern has a very similar issue. The code if (o is int? i) produces:

error CS1003: Syntax error, ':' expected
error CS1525: Invalid expression term ')'
error CS0103: The name 'i' does not exist in the current context

I think it should behave the same as if (o is Nullable<int> i):

error CS8116: It is not legal to use nullable type 'int?' in a pattern; use the underlying type 'int' instead.

@jaredpar jaredpar added the Bug label Jul 7, 2017
@jaredpar jaredpar modified the milestones: Unknown, 16.0 Jul 7, 2017
@lindexi
Copy link

lindexi commented Jun 1, 2019

In the code o is int? means (o is int) ? a : b not if (o is Nullable<int> i) See #20156

@gafter gafter added this to Backlog in Compiler: Pattern-Matching Jun 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
No open projects
Development

No branches or pull requests

4 participants