-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Suppress CS0119 when CS0075 is reported for negative cast #81283
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
Conversation
Co-authored-by: CyrusNajmabadi <[email protected]>
| // For negative cast detection, we need to check if we're going to report ERR_PossibleBadNegCast | ||
| // before binding the left operand. If we will report it, we should suppress the ERR_BadSKunknown | ||
| // error that would be reported when binding a type name in a value context. | ||
| bool mightReportPossibleBadNegCast = node.IsKind(SyntaxKind.SubtractExpression) |
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.
@copilot instead of this, where you're adding code to BindSimpleBinaryOperator where you try to undo the error added deeper in, let's instead NOT add the ERR_BadSKunknown if we can see that we'd add it in a (T)-X syntactic cases we check for here where we end up reporting ERR_PossibleBadNegCast.
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.
Changed to check for the (T)-X pattern in CheckNotNamespaceOrType before reporting ERR_BadSKunknown, rather than filtering after. Simpler and cleaner approach. Fixed in c093621.
…known Co-authored-by: CyrusNajmabadi <[email protected]>
| Error(diagnostics, ErrorCode.ERR_BadSKunknown, expr.Syntax, expr.Type, MessageID.IDS_SK_TYPE.Localize()); | ||
| // Don't report ERR_BadSKunknown if we're in a (T)-X pattern, as we'll report | ||
| // ERR_PossibleBadNegCast instead which is more specific and helpful. | ||
| if (!IsInPossibleBadNegCastContext(expr.Syntax)) |
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.
@copilot mention that this needs to be kept in sync with BindSimpleBinaryOperator
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 comment in 3730db3.
| } | ||
|
|
||
| private static bool IsInPossibleBadNegCastContext(SyntaxNode syntax) | ||
| { |
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.
@copilot it would also be good if BindSimpleBinaryOperator and this code shared the same helper function. That way we can be absolutely sure that if this isn't reporting ERR_BadSKunknown that BindSimpleBinaryOperator will report ERR_PossibleBadNegCast in the same case.
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.
Refactored to share IsParenthesizedExpressionInPossibleBadNegCastContext between both places. This ensures they check for the exact same pattern consistently. Fixed in 6bbab47.
…BindSimpleBinaryOperator Co-authored-by: CyrusNajmabadi <[email protected]>
…CheckNotNamespaceOrType Co-authored-by: CyrusNajmabadi <[email protected]>
…ression, not dynamic The nested parentheses check should only apply to the TypeExpression case, not to the dynamic identifier case. This preserves the original behavior where ((dynamic))-1 would not report CS0075 because the expression inside is a ParenthesizedExpression, not an IdentifierName. Added test PossibleBadNegCastNestedParentheses to demonstrate this behavior. Co-authored-by: CyrusNajmabadi <[email protected]>
| [Fact] | ||
| public void PossibleBadNegCastNestedParentheses() | ||
| { | ||
| var source = @"using System; |
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.
@copilot use a raw string literal. make the attribute [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/32057")] Also, remove the comment on the test.
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.
Updated in a34f2a8. Changed to use raw string literal, added WorkItem attribute, and removed the comment.
|
@dotnet/roslyn-compiler this is ready for review. |
|
@jjonescz @jcouv @RikkiGibson ptal. |
| private static bool IsParenthesizedExpressionInPossibleBadNegCastContext(ParenthesizedExpressionSyntax parenthesized) | ||
| { | ||
| // Check if it's the left side of a subtraction: (T) - X | ||
| return parenthesized.Parent is Syntax.BinaryExpressionSyntax { RawKind: (int)SyntaxKind.SubtractExpression } binary && |
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.
optional suggestion.
| return parenthesized.Parent is Syntax.BinaryExpressionSyntax { RawKind: (int)SyntaxKind.SubtractExpression } binary && | |
| return parenthesized.Parent is BinaryExpressionSyntax { RawKind: (int)SyntaxKind.SubtractExpression } binary && |
Plan to Improve Error Reporting for Negative Cast to Enum ✅
(ConsoleColor)-1, the compiler reports both CS0119 ("ConsoleColor is a type, which is not valid") and CS0075 ("To cast a negative value, you must enclose the value in parentheses")Binder_Operators.csline 912Binder.ValueChecks.csviaCheckNotNamespaceOrTypeCheckNotNamespaceOrTypeto check for the(T)-Xsyntactic pattern before reporting ERR_BadSKunknownIsParenthesizedExpressionInPossibleBadNegCastContextused by bothCheckNotNamespaceOrTypeandBindSimpleBinaryOperatorBinder_Operators.csBindSimpleBinaryOperatorPossibleBadNegCasttest to expect only CS0075 errorsPossibleBadNegCastNestedParenthesestest to demonstrate behavior with nested parenthesesSummary
Successfully improved error reporting for negative cast to enum with a cleaner approach that prevents the error from being added rather than filtering it out afterward. Both places that need to detect the pattern now share the same helper method to ensure consistency.
Example
Before:
After:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.