Skip to content

Conversation

Copy link
Contributor

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 AI and others added 3 commits October 14, 2025 14:52
…ithout NullReferenceException

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
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 SyntaxFactory.ParseAttributeArgumentList Add regression test for ParseAttributeArgumentList with invalid input (fixes #8699) Oct 14, 2025
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
@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-compiler for a second pair of eyes.

@CyrusNajmabadi CyrusNajmabadi merged commit 0b1739b into main Oct 23, 2025
24 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-nullreferenceexception-parseattributeargumentl branch October 23, 2025 13:31
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 23, 2025
333fred added a commit to 333fred/roslyn that referenced this pull request Oct 24, 2025
* upstream/main: (332 commits)
  Cache lambdas in analyzer driver (dotnet#80759)
  Add information for NuGet package version 4.14 (dotnet#80870)
  Add missing search keywords to VB Advanced options page
  Fix IDE0031 false positive when preprocessor directives are used in if statements (dotnet#80878)
  Use core compiler on netfx hosts with toolset package (dotnet#80631)
  Make string concat assert more precise (dotnet#80619)
  Extensions: address some diagnostic quality issues (dotnet#80827)
  Add note on traversal order for bound nodes (dotnet#80872)
  Ensure that locals at the top level of a constructor have the same safe-context as parameters (dotnet#80807)
  Fix handling of SymbolDisplayCompilerInternalOptions.UseArityForGenericTypes option for non-native symbol implementations (dotnet#80826)
  Update src/Analyzers/CSharp/Tests/UseCollectionInitializer/UseCollectionInitializerTests.cs
  Add IsValidContainingStatement check to prevent collection initializers in using declarations
  Add back old DocumentSpan constructor (dotnet#80864)
  Add tests verifying pointer types in type parameters require unsafe context (dotnet#80776)
  Add regression test for Interlocked.Exchange with nullable types (dotnet#80796)
  Add regression test for ParseAttributeArgumentList with invalid input (fixes dotnet#8699) (dotnet#80705)
  Add regression test for compiler crash with syntax error in indexer declaration (dotnet#80772)
  Add runtime NullReferenceException validation to foreach null iteration tests (dotnet#80839)
  Update MicrosoftBuildTasksCoreVersionForMetrics to 17.11.48 (dotnet#80812)
  Mark CS4009 error as a "build only" error. (dotnet#80698)
  ...
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