Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 14, 2025

Fix SyntaxFactory.Identifier(string) Null Reference Exception ✅

Issue

When calling SyntaxFactory.XmlNameAttribute(null), the code threw a NullReferenceException deep in the internal syntax factory instead of the expected ArgumentNullException.

Root Cause

The SyntaxFactory.Identifier methods 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.Identifier overloads:

  • 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 throw ArgumentNullException when passed null parameters
  • Test uses named arguments for text and valueText parameters to improve readability

Testing Results

✅ All 35 SyntaxFactoryTests pass
✅ All 9,735 syntax unit tests pass
✅ Manual verification confirms XmlNameAttribute(null) now throws ArgumentNullException with 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

  • Minimal surgical changes - only added null checks to public API methods
  • No breaking changes - only improved error messages
  • Consistent with existing code patterns in the file
  • Comprehensive test coverage ensures correctness

Fixes #17637

Original prompt

This section details on the original issue you should resolve

<issue_title>SyntaxFactory.XmlNameAttribute(string) throws NullReferenceException instead of ArgumentNullException</issue_title>
<issue_description>Version Used:
2.0.0-rc4

Steps to Reproduce:

Compile and run this code:

var x = SyntaxFactory.XmlNameAttribute(null);

Expected Behavior:

ArgumentNullException

Actual Behavior:

NullReferenceException:

>	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken.SyntaxIdentifier.SyntaxIdentifier Line 15	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken.SyntaxIdentifierExtended.SyntaxIdentifierExtended Line 18	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken.SyntaxIdentifierWithTrivia.SyntaxIdentifierWithTrivia Line 23	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken.Identifier Line 213	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken.Identifier Line 220	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory.Identifier Line 155	C#
 	Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory.Identifier Line 150	C#
 	Microsoft.CodeAnalysis.CSharp.SyntaxFactory.Identifier Line 294	C#
 	Microsoft.CodeAnalysis.CSharp.SyntaxFactory.IdentifierName Line 1587	C#
 	Microsoft.CodeAnalysis.CSharp.SyntaxFactory.XmlNameAttribute Line 10067	C#
 	Microsoft.CodeAnalysis.CSharp.SyntaxFactory.XmlNameAttribute Line 744	C#

</issue_description>

<agent_instructions>Ccopilot. Please add an argument check in SyntaxToken SyntaxFactory.Identifier(string text) that checks that thsi is not null. Also add a test for this in SyntaxFactoryTests</agent_instructions>

Comments on the Issue (you are @copilot in this section)

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.

Copilot AI and others added 2 commits October 14, 2025 12:31
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException in XmlNameAttribute method Fix SyntaxFactory.Identifier to throw ArgumentNullException instead of NullReferenceException Oct 14, 2025
Copilot finished work on behalf of CyrusNajmabadi October 14, 2025 12:41
Copilot AI requested a review from CyrusNajmabadi October 14, 2025 12:41
Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null));
}

[Fact]
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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>
Copilot finished work on behalf of CyrusNajmabadi October 14, 2025 12:56
Copilot AI requested a review from CyrusNajmabadi October 14, 2025 12:56
Assert.Same(parseOptions, typeName.SyntaxTree.Options);
}

[Fact, WorkItem(17637, "https://github.com/dotnet/roslyn/issues/17637")]
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Oct 14, 2025

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

Copy link
Contributor Author

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.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review October 14, 2025 13:04
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 14, 2025 13:04
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot finished work on behalf of CyrusNajmabadi October 14, 2025 13:10
Copilot AI requested a review from CyrusNajmabadi October 14, 2025 13:10
@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-compiler ptal.

{
Assert.Throws<ArgumentNullException>(() =>
SyntaxFactory.VerbatimIdentifier(SyntaxFactory.TriviaList(), "text", null, SyntaxFactory.TriviaList()));
}
Copy link
Member

@jcouv jcouv Oct 15, 2025

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

Copy link
Contributor Author

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>(() =>
Copy link
Member

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>
Copilot AI requested a review from jcouv October 15, 2025 16:47
Copilot finished work on behalf of jcouv October 15, 2025 16:47
{
Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null));
Assert.Throws<ArgumentNullException>(() =>
SyntaxFactory.Identifier(SyntaxFactory.TriviaList(), null, SyntaxFactory.TriviaList()));
Copy link
Member

@jcouv jcouv Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null

@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

Copy link
Contributor Author

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>
Copilot AI requested a review from jcouv October 15, 2025 17:11
Copilot finished work on behalf of jcouv October 15, 2025 17:11
Copy link
Member

@jcouv jcouv left a 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)

@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-compiler ptal.

Copy link
Member

@333fred 333fred left a 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)
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok with not polyfilling.

@CyrusNajmabadi CyrusNajmabadi merged commit 0ea58f2 into main Oct 24, 2025
24 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-nullreferenceexception branch October 24, 2025 20:03
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SyntaxFactory.XmlNameAttribute(string) throws NullReferenceException instead of ArgumentNullException

4 participants