-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix SyntaxFactory.Identifier to throw ArgumentNullException instead of NullReferenceException #80699
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 <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null)); | ||
| } | ||
|
|
||
| [Fact] |
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 These should have [WorkItem("...")] attributes identifying the github issue that prompted making these changes.
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 WorkItem attributes to all test methods in commit d9a0c5e.
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.
Filed #80736 to update our agent instructions
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| Assert.Same(parseOptions, typeName.SyntaxTree.Options); | ||
| } | ||
|
|
||
| [Fact, WorkItem(17637, "https://github.com/dotnet/roslyn/issues/17637")] |
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 you don't need the initial number argument with WorkItem(num, "..."). It's already in the url string. Please remove the first argument from all of these workitem attributes. #Closed
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.
Removed the redundant number argument from all WorkItem attributes in commit 1f8c4a2.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
|
@dotnet/roslyn-compiler ptal. |
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| SyntaxFactory.VerbatimIdentifier(SyntaxFactory.TriviaList(), "text", null, SyntaxFactory.TriviaList())); | ||
| } |
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 Consolidate all the added tests into one single test #Closed
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.
Consolidated all 6 tests into a single test method in commit 8f7b5d3.
| public void Identifier_WithTrivia_NullText_ThrowsArgumentNullException() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| Assert.Throws<ArgumentNullException>(() => |
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.
@CyrusNajmabadi Is this something we can automate or ask copilot to do with dotnet format?
Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com>
| { | ||
| Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null)); | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| SyntaxFactory.Identifier(SyntaxFactory.TriviaList(), null, SyntaxFactory.TriviaList())); |
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 Add named arguments to the null and "value" arguments in this test to improve readability. Other arguments don't need to be named #Closed
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 named arguments for text and valueText parameters in commit a4f2e8d.
Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com>
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 8)
|
@dotnet/roslyn-compiler ptal. |
333fred
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.
Signing off as I don't have a strong opinion on whether to polyfill or not, up to you @CyrusNajmabadi.
| /// <param name="text">The raw text of the identifier name, including any escapes or leading '@' character.</param> | ||
| public static SyntaxToken Identifier(string text) | ||
| { | ||
| if (text == null) |
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.
Nit: do we want to polyfill ArgumentNullException.ThrowIfNull?
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.
ok with not polyfilling.
Fix SyntaxFactory.Identifier(string) Null Reference Exception ✅
Issue
When calling
SyntaxFactory.XmlNameAttribute(null), the code threw aNullReferenceExceptiondeep in the internal syntax factory instead of the expectedArgumentNullException.Root Cause
The
SyntaxFactory.Identifiermethods did not validate that string parameters were not null before passing them to internal syntax factory methods.Changes Implemented
✅ Added null checks to all
SyntaxFactory.Identifieroverloads:Identifier(string text)Identifier(SyntaxTriviaList leading, string text, SyntaxTriviaList trailing)Identifier(SyntaxTriviaList leading, SyntaxKind contextualKind, string text, string valueText, SyntaxTriviaList trailing)VerbatimIdentifier(SyntaxTriviaList leading, string text, string valueText, SyntaxTriviaList trailing)✅ Added comprehensive test covering all overloads:
Identifier_Null_ThrowsArgumentNullException- consolidated test that validates all Identifier and VerbatimIdentifier overloads throwArgumentNullExceptionwhen passed null parameterstextandvalueTextparameters to improve readabilityTesting Results
✅ All 35 SyntaxFactoryTests pass
✅ All 9,735 syntax unit tests pass
✅ Manual verification confirms
XmlNameAttribute(null)now throwsArgumentNullExceptionwith parameter name "text"✅ Code review completed with no issues found
✅ All null checks follow the existing pattern used in the file
✅ Test includes WorkItem attribute referencing issue #17637
Impact
Fixes #17637
Original prompt
Fixes #17637
💡 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.