Skip to content

Commit 6c329ba

Browse files
Fix ArgumentException in SeparatedSyntaxList.ReplaceSeparator to use proper message and paramName (#80690)
Fix ArgumentException in SeparatedSyntaxList.ReplaceSeparator Fixes the issue where `ReplaceSeparator` method throws `ArgumentException` with parameter names as messages instead of proper exception messages. ## Changes Made - [x] Fix the two ArgumentException calls in ReplaceSeparator method to use proper message and paramName parameters - [x] Break combined RawKind/Language check into two separate checks with dedicated resource strings - [x] Build and test the changes - [x] Code review ## Details - Fixed first exception (separator not found) to use `CodeAnalysisResources.MissingListItem` as message and `nameof(separatorToken)` as paramName - Split the combined RawKind/Language check into two separate checks: - RawKind mismatch: Uses `CodeAnalysisResources.SeparatorTokenMustHaveSameRawKind` - Language mismatch: Uses `CodeAnalysisResources.SeparatorTokenMustHaveSameLanguage` - Added two new resource strings to CodeAnalysisResources.resx - Updated .xlf files using dotnet msbuild /t:UpdateXlf - All tests pass (10 SeparatedSyntaxListTests) ## Verification Before fix: - Exception Message: 'separatorToken', ParamName: '' - Exception Message: 'newSeparator', ParamName: '' After fix: - Exception Message: 'The item specified is not the element of a list. (Parameter 'separatorToken')', ParamName: 'separatorToken' - Exception Message: 'New separator must have the same RawKind as the separator being replaced. (Parameter 'newSeparator')', ParamName: 'newSeparator' - Exception Message: 'New separator must have the same Language as the separator being replaced. (Parameter 'newSeparator')', ParamName: 'newSeparator' <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>SeparatedSyntaxList.ReplaceSeparator throws ArgumentException with bad message</issue_title> > <issue_description>The [ReplaceSeparator method throws ArgumentException passing in the parameter name](https://github.com/dotnet/roslyn/blob/8771786e5a80e9d27ffb40702f5cca558ca6fabb/src/Compilers/Core/Portable/Syntax/SeparatedSyntaxList.cs#L529) instead of a message as required by the exception constructor. This results in exceptions with odd one-word messages being thrown. > </issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> Fixes #2496 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 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](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
1 parent 6ad7a4f commit 6c329ba

15 files changed

+144
-4
lines changed

src/Compilers/Core/Portable/CodeAnalysisResources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,12 @@
438438
<data name="MissingListItem" xml:space="preserve">
439439
<value>The item specified is not the element of a list.</value>
440440
</data>
441+
<data name="SeparatorTokenMustHaveSameRawKind" xml:space="preserve">
442+
<value>New separator must have the same RawKind as the separator being replaced.</value>
443+
</data>
444+
<data name="SeparatorTokenMustHaveSameLanguage" xml:space="preserve">
445+
<value>New separator must have the same Language as the separator being replaced.</value>
446+
</data>
441447
<data name="InvalidPublicKey" xml:space="preserve">
442448
<value>Invalid public key.</value>
443449
</data>

src/Compilers/Core/Portable/Syntax/SeparatedSyntaxList.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,17 @@ public SeparatedSyntaxList<TNode> ReplaceSeparator(SyntaxToken separatorToken, S
573573
var index = nodesWithSeps.IndexOf(separatorToken);
574574
if (index < 0)
575575
{
576-
throw new ArgumentException("separatorToken");
576+
throw new ArgumentException(CodeAnalysisResources.MissingListItem, nameof(separatorToken));
577577
}
578578

579-
if (newSeparator.RawKind != nodesWithSeps[index].RawKind ||
580-
newSeparator.Language != nodesWithSeps[index].Language)
579+
if (newSeparator.RawKind != nodesWithSeps[index].RawKind)
581580
{
582-
throw new ArgumentException("newSeparator");
581+
throw new ArgumentException(CodeAnalysisResources.SeparatorTokenMustHaveSameRawKind, nameof(newSeparator));
582+
}
583+
584+
if (newSeparator.Language != nodesWithSeps[index].Language)
585+
{
586+
throw new ArgumentException(CodeAnalysisResources.SeparatorTokenMustHaveSameLanguage, nameof(newSeparator));
583587
}
584588

585589
return new SeparatedSyntaxList<TNode>(nodesWithSeps.Replace(separatorToken, newSeparator));

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)