Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 14, 2025

Summary

This PR adds a regression test to verify that SyntaxFactory.ParseAttributeArgumentList properly handles invalid input without throwing a NullReferenceException.

Issue

Issue #8699 reported that calling SyntaxFactory.ParseAttributeArgumentList("somethingWithoutBrackets") would throw a NullReferenceException when given text without parentheses.

Resolution

Testing confirms that this issue is already fixed in the current codebase. The method now properly handles invalid input by:

  1. Returning a non-null AttributeArgumentListSyntax object
  2. Creating missing tokens for OpenParenToken and CloseParenToken
  3. Including appropriate diagnostics for the unexpected tokens

Changes

Added TestParseAttributeArgumentListWithInvalidString test that verifies:

  • The method returns a valid syntax node (not null)
  • Diagnostics are generated for the invalid input
  • The structure contains missing parenthesis tokens as expected
  • No exception is thrown

Example

// This no longer throws NullReferenceException
var result = SyntaxFactory.ParseAttributeArgumentList("somethingWithoutBrackets");

// Returns a valid node with diagnostics
Assert.NotNull(result);
Assert.True(result.GetDiagnostics().Any());
Assert.True(result.OpenParenToken.IsMissing);
Assert.True(result.CloseParenToken.IsMissing);

This test serves as documentation that the issue has been resolved and prevents future regression.

Original prompt

This section details on the original issue you should resolve

<issue_title>c# SyntaxFactory.ParseAttributeArgumentList throws NullReferenceException with invalid string</issue_title>
<issue_description>Version Used:
typeof(SyntaxFactory).Assembly.ImageRuntimeVersion = v4.0.30319

Steps to Reproduce:
in c# interactive:

  1. #r "Microsoft.CodeAnalysis.CSharp.dll"
  2. using Microsoft.CodeAnalysis.CSharp;
  3. SyntaxFactory.ParseAttributeArgumentList("somethingWithoutBrackets")
  4. Then this exception is being thrown:
    Object reference not set to an instance of an object.
  • Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxParser.AddError(TNode, Microsoft.CodeAnalysis.CSharp.ErrorCode, object[])
  • Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.LanguageParser.ConsumeUnexpectedTokens(TNode)
  • Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseAttributeArgumentList(string, int, Microsoft.CodeAnalysis.ParseOptions, bool)

Expected Behavior:
some kind of ParseException, e.g. UnexpectedTokenException
Actual Behavior:
NullReferenceException with stacktrace.
</issue_description>

<agent_instructions>Copilot, can you first add a test to see if htis issue still reproes. If so, fix. If not, we can just submit the test to demonstrate that this issue can be closed.</agent_instructions>

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

Fixes #23877


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Fix NullReferenceException in SyntaxFactory.ParseAttributeArgumentList Add regression test for ParseAttributeArgumentList with invalid input (fixes #8699) Oct 14, 2025
@Copilot Copilot AI requested a review from CyrusNajmabadi October 14, 2025 15:00
Copilot finished work on behalf of CyrusNajmabadi October 14, 2025 15:00
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review October 14, 2025 15:04
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 14, 2025 15:04
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.

c# SyntaxFactory.ParseAttributeArgumentList throws NullReferenceException with invalid string

5 participants