Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some of the C#8 features don't compile - Visual Studio v16.7.2 #5049

Closed
opcodewriter opened this issue Aug 26, 2020 · 11 comments · Fixed by #5054
Closed

Some of the C#8 features don't compile - Visual Studio v16.7.2 #5049

opcodewriter opened this issue Aug 26, 2020 · 11 comments · Fixed by #5054
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@opcodewriter
Copy link

opcodewriter commented Aug 26, 2020

Steps to Reproduce

  1. In Visual Studio v16.7.2 (it's the latest released) on Windows 10, create a Xamarin Android app

  2. In the MainActivity.cs, in the MainActivity class, add the following C#8 code which uses a switch expression:

        int FromRainbow(Rainbow colorBand) => colorBand switch
        {
            Rainbow.Red => 0,
            Rainbow.Orange => 1,
             _ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand))
        };
  1. Compiler shows error:
    CS8370 Feature 'recursive patterns' is not available in C# 7.3. Please use language version 8.0 or greater.

Interestingly, if I'm using a local function which is also a C# 8 feature, it compiles fine:

public class MainActivity : AppCompatActivity
{
        protected override void OnCreate(Bundle savedInstanceState)
        {
              ...

             LocalFunction();

              void LocalFunction() => Console.WriteLine("A");
        }
}

Why would one C# 8 feature work but not the other.

I am aware I could try edit the csproj and force by using LangVersion, but shouldn't this not be necessary anymore?

By default, the project targets Android 9. I also tried with Android 10, but it didn't work, gives the same compilation error.

I could swear the switch expression worked fine in some previous version of Visual Studio 2019! I have some code which compiled fine some months ago this year

Expected Behavior

Compile

Actual Behavior

Doesn't compile

Version Information

Log File

Microsoft Visual Studio Enterprise 2019
Version 16.7.2
VisualStudio.16.Release/16.7.2+30413.136
Microsoft .NET Framework
Version 4.8.03752
``` Installed Version: Enterprise

Visual C++ 2019 00433-90101-78013-AA222
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019 16.7.532.28833
ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services 16.1.0.2035807+72d099b977d3a85e65fa3b0614ca8cfc803fef02
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019 16.7.532.28833
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.7.532.28833
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.7.532.28833
Azure Functions and Web Jobs Tools

C# Tools 3.7.0-6.20412.3+d3c3a44a4e7ad31cc75c59be0d3df4a19ff33878
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.

Extensibility Message Bus 1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub.VisualStudio 2.11.104.25275
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension 1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30701.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.79+ge3567815aa.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.7.5 (112c7bc)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.7.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Snapshot Debugging Extension 1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.62008.03130
Microsoft SQL Server Data Tools

Syntax Visualizer 1.0
An extension for visualizing Roslyn SyntaxTrees.

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 16.0.20702.2001
TypeScript Tools for Microsoft Visual Studio

UnoPlatformPackage Extension 1.0
UnoPlatformPackage Visual Studio Extension Detailed Info

Visual Basic Tools 3.7.0-6.20412.3+d3c3a44a4e7ad31cc75c59be0d3df4a19ff33878
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) 16.0.30309.115
Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Cross Platform Mobile Development (iOS) 16.0.30309.115
Visual C++ for Cross Platform Mobile Development (iOS)

Visual F# Tools 10.10.0.0 for F# 4.7 16.7.0-beta.20361.3+3ef6f0b514198c0bfa6c2c09fefe41a740b024d5
Microsoft Visual F# Tools 10.10.0.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview) 1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake 1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers 1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog 1.0
Information about my package

VisualStudio.Foo 1.0
Information about my package

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 16.7.000.440 (d16-7@358f3c6)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.7.0.495 (remotes/origin/d16-7@79c0c522c)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.7.85 (1bcbbdf)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 11.0.2.0 (d16-7/025fde9)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: 83105ba
Java.Interop: xamarin/java.interop/d16-7@1f3388a
ProGuard: Guardsquare/proguard@ebe9000
SQLite: xamarin/sqlite@1a3276b
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-7@017078f

Xamarin.iOS and Xamarin.Mac SDK 13.20.2.2 (817b6f72a)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.



<!--
Switch to the "Preview" tab to ensure your issue renders correctly.
-->
@opcodewriter opcodewriter added the Area: App+Library Build Issues when building Library projects or Application projects. label Aug 26, 2020
@jpobst
Copy link
Contributor

jpobst commented Aug 26, 2020

Confirmed that applications now default to 7.3. This was originally changed in #3815 and seems to have regressed.

From a diagnostic build:

MaxSupportedLangVersion = 8.0
_MaxSupportedLangVersion = 7.3

@jpobst
Copy link
Contributor

jpobst commented Aug 26, 2020

(Local functions are a C# 7.0 feature: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions.)

@jonathanpeppers
Copy link
Member

@jpobst it looks like this was broken in:

dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8

For some reason they removed $(MaxSupportedLangVersion) in favor of $(_MaxSupportedLangVersion)? I will have to ask someone about this.

@jpobst
Copy link
Contributor

jpobst commented Aug 26, 2020

Ah: dotnet/roslyn#44911 (comment)

@opcodewriter
Copy link
Author

So when should I expect it to be fixed? Is it next Xamarin (Android and/or iOS) release? Or next Visual Studio release?

@jonathanpeppers
Copy link
Member

@opcodewriter I would put <LangVersion>latest</LangVersion> in your .csproj until this is sorted out.

@rlasker-b2w
Copy link

This is also affecting Azure DevOps build pipelines as the agents are defaulting to Xamarin.Android version 11.0.2.0 and any C# 8.0 features are not compiling. As of the 26th I am getting an error:

error CS8370: Feature 'coalescing assignment' is not available in C# 7.3. Please use language version 8.0 or greater.

The only difference between that and the last successful build is that the windows-2019 build agent is now defaulting to 11.0.2.0 instead of 10.3.1.4

@jonathanpeppers
Copy link
Member

The only difference between that and the last successful build is that the windows-2019 build agent is now defaulting to 11.0.2.0 instead of 10.3.1.4

@rlasker-b2w we identified the change in Roslyn that caused this to stop working. Roslyn ships along with Visual Studio itself. Does this workaround work for now?

<LangVersion>latest</LangVersion>

@rlasker-b2w
Copy link

@jonathanpeppers yes the work around works for now. Thank you. I just wanted to add the Azure DevOps issue to hopefully draw bring people here to find a possible solution if they have the same problem.

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Aug 27, 2020
Fixes: dotnet#5049
Context: dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8
Context: dotnet/roslyn#44911 (comment)

The `BuildTest.CSharp8Features` test was failing for me locally where
I have Visual Studio 2019 16.7.2 installed:

    Foo.cs(1,23): error CS8370: Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater.

After review, it appears that a change in Roslyn's MSBuild targets
removed `$(MaxSupportedLangVersion)` and made the property private:
`$(_MaxSupportedLangVersion)`.

After some discussion around C# 9.0:

* It is unknown if C# 9.0 will be supported by mono/mono/2020-02. Some
  features like covariant return types and function pointers require
  runtime changes.
* Using a build of .NET 5 rc1, `$(LangVersion)` is already being set
  to `9.0` by default.

We should just change the current behavior of:

    <MaxSupportedLangVersion Condition=" '$(MaxSupportedLangVersion)' == '' ">8.0</MaxSupportedLangVersion>

And change it to:

    <LangVersion Condition=" '$(LangVersion)' == '' ">8.0</LangVersion>

This matches what Roslyn's default behavior is:

https://github.com/dotnet/roslyn/blob/1aeda2e94fb9371b96d4bfd94b074860064ec3d2/src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets#L6-L21

.NET 5+ projects should be unaffected by this change, as they do not
import `Xamarin.Android.CSharp.targets`.

If at some point, mono/mono/2020-02 fully supports C# we can bump this
value. Users can opt into `9.0` on their own if desired.

`BuildTest.CSharp8Features` now passes for me locally.
jonpryor pushed a commit that referenced this issue Aug 28, 2020
Fixes: #5049

Context: dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8
Context: dotnet/roslyn#44911 (comment)

The `BuildTest.CSharp8Features()` test was failing for me locally when
I have Visual Studio 2019 16.7.2 installed:

	Foo.cs(1,23): error CS8370: Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater.

After review, it appears that a change in Roslyn's MSBuild targets
[removed `$(MaxSupportedLangVersion)`][0] and made the property\
private: `$(_MaxSupportedLangVersion)`.

After some discussion around C# 9.0:

  * It is unknown if C# 9.0 will be supported by mono/mono/2020-02.
    Some features like covariant return types and function pointers
    require runtime changes.

  * Using a build of .NET 5 rc1, `$(LangVersion)` is already being set
    to `9.0` by default.

We should just change the current behavior of:

	<MaxSupportedLangVersion Condition=" '$(MaxSupportedLangVersion)' == '' ">8.0</MaxSupportedLangVersion>

and change it to:

	<LangVersion Condition=" '$(LangVersion)' == '' ">8.0</LangVersion>

This matches [Roslyn's default behavior][1]:

.NET 5+ projects should be unaffected by this change, as they do not
import `Xamarin.Android.CSharp.targets`.

If at some point, if mono/mono/2020-02 fully supports C# 9 we can bump
this value.  Users can opt into `9.0` on their own if desired.

`BuildTest.CSharp8Features()` now passes for me locally.

[0]: dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8
[1]: https://github.com/dotnet/roslyn/blob/1aeda2e94fb9371b96d4bfd94b074860064ec3d2/src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets#L6-L21
jonpryor pushed a commit that referenced this issue Aug 28, 2020
Fixes: #5049

Context: dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8
Context: dotnet/roslyn#44911 (comment)

The `BuildTest.CSharp8Features()` test was failing for me locally when
I have Visual Studio 2019 16.7.2 installed:

	Foo.cs(1,23): error CS8370: Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater.

After review, it appears that a change in Roslyn's MSBuild targets
[removed `$(MaxSupportedLangVersion)`][0] and made the property\
private: `$(_MaxSupportedLangVersion)`.

After some discussion around C# 9.0:

  * It is unknown if C# 9.0 will be supported by mono/mono/2020-02.
    Some features like covariant return types and function pointers
    require runtime changes.

  * Using a build of .NET 5 rc1, `$(LangVersion)` is already being set
    to `9.0` by default.

We should just change the current behavior of:

	<MaxSupportedLangVersion Condition=" '$(MaxSupportedLangVersion)' == '' ">8.0</MaxSupportedLangVersion>

and change it to:

	<LangVersion Condition=" '$(LangVersion)' == '' ">8.0</LangVersion>

This matches [Roslyn's default behavior][1]:

.NET 5+ projects should be unaffected by this change, as they do not
import `Xamarin.Android.CSharp.targets`.

If at some point, if mono/mono/2020-02 fully supports C# 9 we can bump
this value.  Users can opt into `9.0` on their own if desired.

`BuildTest.CSharp8Features()` now passes for me locally.

[0]: dotnet/roslyn@12f1b8f#diff-125e2d8c52dd9033f9b248f79f78c3f8
[1]: https://github.com/dotnet/roslyn/blob/1aeda2e94fb9371b96d4bfd94b074860064ec3d2/src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets#L6-L21
@brendanzagaeski
Copy link
Contributor

Release status update

A new Preview version of Xamarin.Android has now been published that includes the fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.

Fix included in Xamarin.Android SDK version 11.1.0.3.

Fix included on Windows in Visual Studio 2019 version 16.8 Preview 3. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8 Preview 3. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

@brendanzagaeski
Copy link
Contributor

Release status update

A new Release version of Xamarin.Android has now been published that includes the fix for this item.

Fix included in Xamarin.Android SDK version 11.1.0.17.

Fix included on Windows in Visual Studio 2019 version 16.8. To get the new version that includes the fix, check for the latest updates or install the most recent release from https://visualstudio.microsoft.com/downloads/.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants