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.9k stars 4.01k forks source link

Nonsense identifier expected error depending on how you edit unrelated code #74456

Open hamarb123 opened 1 month ago

hamarb123 commented 1 month ago

Version Used:

VS Info Microsoft Visual Studio Community 2022 Version 17.10.4 VisualStudio.17.Release/17.10.4+35027.167 Microsoft .NET Framework Version 4.8.09037 Installed Version: Community Visual C++ 2022 00482-90000-00000-AA873 Microsoft Visual C++ 2022 ASP.NET and Web Tools 17.10.344.38934 ASP.NET and Web Tools Azure App Service Tools v3.0.0 17.10.344.38934 Azure App Service Tools v3.0.0 Azure Functions and Web Jobs Tools 17.10.344.38934 Azure Functions and Web Jobs Tools C# Tools 4.10.0-3.24324.8+04fb39164c99c519832109f21b22630bc9246fce C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Cookiecutter 17.0.24064.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines NuGet Package Manager 6.10.1 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Office Developer Tools for Visual Studio 17.10.34627.00 Microsoft Office Developer Tools for Visual Studio Python - Profiling support 17.0.24064.1 Profiling support for Python projects. Python with Pylance 17.0.24064.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers. Razor (ASP.NET Core) 17.10.3.2427201+4f57d1de251e654812adde201c0265a8ca7ca31d Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 17.10.178.1 Microsoft SQL Server Data Tools Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory. Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory. TypeScript Tools 17.0.30327.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.10.0-3.24324.8+04fb39164c99c519832109f21b22630bc9246fce Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual C++ for Cross Platform Mobile Development (Android) 15.0.35004.147 Visual C++ for Cross Platform Mobile Development (Android) Visual F# Tools 17.10.0-beta.24228.1+dd749058c91585e9b5dae62b0f8df892429ee28f Microsoft Visual F# Tools Visual Studio Color Theme Designer 1.0 Allows users to create and edit Visual Studio themes. Themes can customize colors used for menus, toolbars, tabs, titlebars, the text editor, and other environment colors. Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager.

Also reproducible in VS Code on macOS.

Steps to Reproduce:

  1. Create a file TestProject.csproj with the following (I don't think it's specific to any settings in here(?). tabs & crlf - not sure if this is important):

    
    <Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>12</LangVersion>
        <Nullable>enable</Nullable>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>


2. Create a file `Class1.cs` with the following code (tabs & crlf - not sure if this is important):
```csharp
using System;
using System.Linq;

namespace TestProject1
{
    public sealed class Class1
    {
        public void M2(bool bool1, int int1, int[] array1)
        {
            if (bool1)
            {
                for (int i = 0, j = 0; i < int1; i++)
                {
                    if ((Math.Abs(i) & 1) == 0)
                    {
                        array1[j++] = i;
                    }
                }
            }
            if (bool1)
            {
                array1 = [.. array1.GroupBy((x) => Math.Abs((long)x)).SelectMany((x) => x)];
            }
        }
    }
}
  1. Re-open VS & open the project on its own to ensure edits are done in the correct order.

  2. Select the following selection of text & replace with a single space:

    image
  3. After waiting to see that the errors/warnings throughout the document stops changing, select the following selection of text & press backspace:

    image
  4. Note the incorrect error on the unrelated line:

    image
  5. To see that the error is nonsense, put a space at the end of the broken line and see that the error goes away:

    image

Diagnostic Id:

CS1001: Identifier expected. There was a different set of errors/warnings on my previous VS version (~17.9.0), but I didn't think to capture them before updating VS.

Expected Behavior:

No error

Actual Behavior:

Error exists depending on how you change unrelated lines

Rekkonnect commented 1 month ago

This is probably a bug in the LSP or the incremental adjustment of the syntax tree. I could verify the bug in SharpLab, which uses the LSP for that matter. However, in a program I've got where I've recently added displaying diagnostics I only saw errors in the actually erroneous code. In my program I don't use the LSP and instead I directly call the Roslyn APIs to get semantic info and diagnostics, by replacing the syntax tree in the compilation.