Skip to content

C# Parser Error: token.GetLocation().GetLineSpan() throws ArgumentOutOfRangeException for last token in SyntaxTree #79197

@martin-strecker-sonarsource

Description

Version Used:
Microsoft.CodeAnalysis.CSharp 4.14.0

Steps to Reproduce:

  1. Clone Add repo for community 141909 SonarSource/sonar-scanner-azdo-test#20
  2. Open ParseErrorReproducer.sln
  3. Run UT ParseErrorTests.LocationOfLastTokenIsInvalid() which parses ResourceColors.cs

The location of lastToken seems to be out of bounds of the document and the location mapping to the file does not work.
Minimal changes to the file fixes the error so it isn't clear to me what the root cause is (Make sure to checkout the exact same file for reproducers).

For example, adding a leading zero to the .241 literal (changing it to 0.241) fixes the location of the last token (the span of the last token doesn't change after the 0 was inserted, even though it should be offset by one). But adding the leading zero to .691 or .068 or both instead doesn't fix the problem.

See also https://community.sonarsource.com/t/sonaranalyzer-throwing-argumentoutofrangeexception-on-azure-devops/141909 for more changes to the file, that make the error disappear.

Maybe related to #78025

The stack trace looks slightly different, though:

Image

Note: The DebuggerDisplay of Location also throws, because it calls GetLineSpan

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions