-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Improve diagnostic for != operator in patterns #81294
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
Improve diagnostic for != operator in patterns #81294
Conversation
Co-authored-by: CyrusNajmabadi <[email protected]>
Co-authored-by: CyrusNajmabadi <[email protected]>
Co-authored-by: CyrusNajmabadi <[email protected]>
src/Compilers/CSharp/Test/Syntax/Parsing/PatternParsingTests.cs
Outdated
Show resolved
Hide resolved
|
@dotnet/roslyn-compiler ptal |
|
@copilot two tests are failing: Relational_10 and Pointer_Pattern_Equality. Investigate and upddate PR as appropriate. |
Updated tests to expect new error ERR_EqualityOperatorInPatternNotSupported for != operator and updated operation tree to show INegatedPatternOperation instead of IRelationalPatternOperation. Co-authored-by: CyrusNajmabadi <[email protected]>
Fixed both failing tests in commit 8750ad1. For For Both tests now pass along with all their combinatorial variations. |
…ality-operator-diagnostics
| Diagnostic(ErrorCode.ERR_EqualityOperatorInPatternNotSupported, "!=").WithLocation(1, 18) | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Diagnostic(ErrorCode.ERR_EqualityOperatorInPatternNotSupported, "!=").WithLocation(1, 18) | |
| ); | |
| Diagnostic(ErrorCode.ERR_EqualityOperatorInPatternNotSupported, "!=").WithLocation(1, 18)); |
src/Compilers/CSharp/Test/Syntax/Parsing/PatternParsingTests.cs
Outdated
Show resolved
Hide resolved
| void M(bool x, int a, object b) | ||
| { | ||
| var c = !(a <= 10 || a is not == 20); | ||
| var c = !(a <= 10 || a is not== 20); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why has this changed? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's basically a syntax error here now (since the parser runs into this, skips the token, and proceeds. The formatting engine doesn't like skipped tokens. And this test was really just ensuring we don't crash in these sorts of error scenarios. So this behavior just says what we'll do, which is completely fine here.
src/Compilers/CSharp/Portable/Parser/LanguageParser_Patterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Parser/LanguageParser_Patterns.cs
Outdated
Show resolved
Hide resolved
|
@jcouv @RikkiGibson @333fred ptal |
|
@jcouv ptal |
| ConvertToKeyword(this.EatToken()), | ||
| ParseNegatedPattern(precedence, afterIs, inSwitchArmPattern)); | ||
| } | ||
| else if (this.CurrentToken.Kind == SyntaxKind.EqualsEqualsToken) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this recovery should be in ParsePrimaryPattern, and that we may want to treat the inner expression as a top-level pattern. Do we have tests for other patterns being involved? For example, == > 10 and < 15? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't feel like we needed anything that involved. Basically, it seemed like what we were addressing was a user who saw that >= x was a pattern and then intuited that they could then use != or == in a similar fashion (like in a normal expression).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't feel like we needed anything that involved. Basically, it seemed like what we were addressing was a user who saw that >= x was a pattern and then intuited that they could then use != or == in a similar fashion (like in a normal expression).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more tests, including one for your use case. It shows the behavior is good :)
…ality-operator-diagnostics
|
@333fred anything else you need here? |
|
Fixes #58010 ? |
jcouv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (commit 15)
Fixes #58010
Plan: Improve diagnostic when using equality operators for relational patterns
Understanding
foo is { Value: != 5 }, they get an unhelpful error "Invalid expression term '!='"foo is { Value: not 5 }!=during pattern parsing and provide a better diagnostic messageImplementation Checklist
!=and handle it specially!=as a not-pattern with missingnottoken and!=as skipped triviaSummary
Successfully implemented improved diagnostics for the
!=operator in patterns. The parser now:!=when parsing patternsnotinsteadAll pattern parsing tests now pass, including the fixed Relational_10 and Pointer_Pattern_Equality tests!
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.