Classify parameter discards earlier, syntactically instead of semantically, resolving classification conflict - #85225
Conversation
…cally, resolving classification conflict
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟢 Approval recommended
The change is small, aligns with compiler discard rules for lambda parameters, and includes updated/added test coverage for both syntactic and total classification behavior.
Pull request overview
This PR adjusts C# syntax classification so lambda parameter discards (_ used as a discard when duplicated in the same anonymous function parameter list) are classified as Keyword syntactically (instead of relying on semantic classification), avoiding classification-merge precedence issues where ParameterName would previously win.
Changes:
- Removed semantic classification of discard lambda parameters from
DiscardSyntaxClassifier. - Added syntactic classification logic to classify
_asKeywordfor anonymous-function parameter lists where_appears more than once. - Updated/added classification tests to validate syntactic + total classification behavior for lambda discards.
File summaries
| File | Description |
|---|---|
| src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs | Removes the semantic classification path for ParameterSyntax discards so it no longer conflicts with syntactic parameter-name classification. |
| src/Workspaces/CSharp/Portable/Classification/ClassificationHelpers.cs | Implements syntactic detection of lambda parameter discards by checking for another _ in the same anonymous-function parameter list and returning Keyword. |
| src/EditorFeatures/CSharpTest/Classification/TotalClassifierTests.cs | Adds regression coverage to ensure the merged/total classifier produces Keyword for discard _ parameters and Parameter for non-discard _. |
| src/EditorFeatures/CSharpTest/Classification/SyntacticClassifierTests_Preprocessor.cs | Updates syntactic expected classifications so (_, _) => ... now yields Keyword for both _ parameters. |
| src/EditorFeatures/CSharpTest/Classification/SemanticClassifierTests.cs | Updates semantic-classifier expectations to no longer require keyword classification for lambda discard parameters (now handled syntactically). |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
The new discard detection uses Text comparisons for _ instead of ValueText, which can misclassify unicode-escaped underscore identifiers that are semantically equivalent to _.
Review details
Suppressed comments (2)
Previously missed (2) — in code that hasn't changed since the last review.
src/Workspaces/CSharp/Portable/Classification/ClassificationHelpers.cs:263
- Discard detection should use
ValueTextinstead ofTextso escaped underscore identifiers (e.g.\u005f) are treated consistently as_discards. UsingTextcan cause incorrect classification for syntactically-equivalent identifiers.
src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs:36 - Discard classification for identifier names should use
ValueTextrather thanTextso unicode-escaped underscore identifiers are classified the same as_.
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Lite
|
Fixes #51553.
Roslyn classifies discards as keywords for syntax highlighting which shows whether or not
_is referring to a symbol.However, it's not working for lambda parameter discards:

There was an attempt to implement it in #40396 for lambda parameter discards:
roslyn/src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs
Lines 36 to 44 in 6de0973
But this was insufficient because it was implemented in the semantic classifier. The syntactic classifier already classified it as ParameterName, and the two are merged with ordering rules which cause ParameterName to win over Keyword.
roslyn/src/EditorFeatures/Core/Classification/ClassificationTypeFormatDefinitions.cs
Lines 520 to 523 in 6de0973
This PR fixes the issue by implementing the proper classification in the syntactic classifier instead of the semantic classifier. This is preferable because the semantic classifier may run after more of a delay, so this is less visual flicker in the IDE.
Microsoft Reviewers: Open in CodeFlow