Suppress CS0119 when CS0075 is reported for negative cast#81283
Suppress CS0119 when CS0075 is reported for negative cast#81283CyrusNajmabadi merged 14 commits intomainfrom
Conversation
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| // 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.
@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.
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 <4564579+CyrusNajmabadi@users.noreply.github.com>
| 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)) |
| } | ||
|
|
||
| private static bool IsInPossibleBadNegCastContext(SyntaxNode syntax) | ||
| { |
There was a problem hiding this comment.
@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.
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 <4564579+CyrusNajmabadi@users.noreply.github.com>
…CheckNotNamespaceOrType Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| [Fact] | ||
| public void PossibleBadNegCastNestedParentheses() | ||
| { | ||
| var source = @"using System; |
There was a problem hiding this comment.
@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.
Updated in a34f2a8. Changed to use raw string literal, added WorkItem attribute, and removed the comment.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
|
@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.
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.