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.92k stars 4.02k forks source link

IDE should not suggest "using declaration" feature unavailable in language settings #55485

Closed Zannick closed 3 years ago

Zannick commented 3 years ago

Version Used: 16.10.4

Full Versions ``` Microsoft Visual Studio Community 2019 Version 16.10.4 VisualStudio.16.Release/16.10.4+31515.178 Microsoft .NET Framework Version 4.8.04084 Installed Version: Community ASP.NET and Web Tools 2019 16.10.526.50910 ASP.NET Web Frameworks and Tools 2019 16.10.526.50910 Azure App Service Tools v3.0.0 16.10.526.50910 Azure Functions and Web Jobs Tools 16.10.526.50910 C# Tools 3.10.0-4.21329.37+246ce641f04b67ef017655275d850bf902a8e40f Common Azure Tools 1.10 CreateActionClasses Extension 1.0 IntelliCode Extension 1.0 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.40423.1 Razor (ASP.NET Core) 16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725 SQL Server Data Tools 16.0.62106.24090 ```

Steps to Reproduce:

Write code using a using-statement such as with TransactionScope:

var tso = new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead };
using (var trans = new TransactionScope(TransactionScopeOption.Required, tso))
{
  [...]
}

VS reports IDE0063: 'using' statement can be simplified, which produces:

var tso = new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead };
using var trans = new TransactionScope(TransactionScopeOption.Required, tso);
[...]

Expected Behavior:

The suggestion is not offered OR the code compiles after accepting the suggestion.

Actual Behavior:

After accepting the suggestion, the code fails to compile with error CS8652: The feature 'using declarations' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

CyrusNajmabadi commented 3 years ago

We check for the language version here: https://github.com/dotnet/roslyn/blob/f300a818940ea1acef66c946cfa83756729d5e59/src/Analyzers/CSharp/Analyzers/UseSimpleUsingStatement/UseSimpleUsingStatementDiagnosticAnalyzer.cs#L76-L78

So it looks like you're compiling with a very old language (we're already on C# 10.

Zannick commented 3 years ago

#error version informs me error CS8304: Compiler version: '3.0.19.17001 (1deafee3)'. Language version: default. (and in the IDE it says compiler version C# Tools 3.10.0-4.21329.37 Language version: default is 9.0)

My target framework appears to be .NET 4.7.2 according to the csproj. Based on https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version, it should be at least C# 8.

Strange how there's a discrepancy in compiler versions that get suggestions vs actual usage.

CyrusNajmabadi commented 3 years ago

IDE appears to be correct here. So this is not an IDE issue. It might be an issue with your compiler or SDK that is causing it to load an older compiler version. Taging @jmarolf in case he has any ideas waht might have caused this.

Zannick commented 3 years ago

Aha, packages.config specifies

<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="3.6.0" targetFramework="net472" />
<package id="Microsoft.Net.Compilers" version="3.0.0" targetFramework="net452" developmentDependency="true" requireReinstallation="true" />

for some reason. I guess since it's not specified in the csproj, IDE uses the newer compiler version, but because the package config specifies a different version, the compiler actually uses that.

Zannick commented 3 years ago

I think it's not really desirable to have the IDE assume the wrong compiler, but at least I ought to be able to ask the team about upgrading the compiler setting here.

Zannick commented 3 years ago

Running Update-Package Microsoft.Net.Compilers in the package manager console got me upgraded to 3.10 for net472, and I'm no longer receiving this suggestion. Perhaps the setting in packages.config is used by the IDE when it matches the target framework?