dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.88k stars 4.01k forks source link

Incorrect location for missing semicolon error after whitespace change #31161

Open svick opened 5 years ago

svick commented 5 years ago

Version Used: 2.9.0, 2.11.0-beta3-63513-06

Steps to Reproduce:

Run the following code:

using System;
using System.Globalization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

class Program
{

    static void Main()
    {
        CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

        string code = @"class C
{
    void M()
    {
        M()
    }
}";

        var node1 = SyntaxFactory.ParseCompilationUnit(code);

        Dump(node1);

        var node2 = node1.NormalizeWhitespace();

        Dump(node2);

        void Dump(SyntaxNode node)
        {
            Console.WriteLine(node);

            foreach (var diagnostic in node.GetDiagnostics())
            {
                Console.WriteLine(diagnostic);
            }
        }
    }
}

Expected Behavior:

Both syntax trees report the missing semicolon error just after the closing parenthesis on line 5. In other words, in both cases, the reported location should be (5,12).

Actual Behavior:

In the second syntax tree, the reported location is (5, 10), i.e. on the opening parenthesis:

class C
{
    void M()
    {
        M()
    }
}
(5,12): error CS1002: ; expected
class C
{
    void M()
    {
        M()}
}
(5,10): error CS1002: ; expected

I think this happens because the SyntaxDiagnosticInfo is created with offset of -2 when the tree is parsed, which works correctly for the original tree. But when the whitespace is updated, the offset causes the incorrect location.

jinujoseph commented 5 years ago

Possible duplicate of #23330

doivosevic commented 4 years ago

Hey! I'm trying to repro this bug on master Commit hash: f09c4b76a42616dd04ee9aef5510d1a3082e35ab and on my local Version 16.7.2 whichever Roslyn that is and it doesn't seem to repro

EDIT: My bad. Still repros