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

dotnet build error #10180

Closed
Varorbc opened this issue Mar 26, 2019 · 11 comments
Closed

dotnet build error #10180

Varorbc opened this issue Mar 26, 2019 · 11 comments
Assignees
Milestone

Comments

@Varorbc
Copy link

Varorbc commented Mar 26, 2019

Steps to reproduce

1.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

</Project>

using System;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            if (GetT<Program>() is null)
            {
                Console.WriteLine("null");
            }
        }

        public static T GetT<T>() where T : new()
        {
            var t = new T();
            if (t is null)
            {
                throw new Exception();
            }

            return t;
        }
    }
}

2. dotnet build dotnet build ConsoleApp1.csproj error

3.build by VS2019 16.0.0 RC.3 is success

Expected behavior

dotnet build success

Actual behavior

dotnet build error

Program.cs(19,22): error CS0403: 无法将 null 转换为类型参数“T”,因为它可能是不可以为 null 的值类型。请考虑改用“default(T)”

Environment data

dotnet --info output:

.NET Core SDK(反映任何 global.json):
Version: 2.2.105
Commit: 7cecb35

运行时环境:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.105\

Host (useful for support):
Version: 2.2.3
Commit: 6b8ad509b6

.NET Core SDKs installed:
2.2.105 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download

@peterhuene
Copy link
Contributor

peterhuene commented Mar 26, 2019

I would expect the need for a class constraint on the generic parameter T for Get<T> to get past the CS0403 error.

However, with a 3.0 SDK, I can't seem to get it to error with a CS0403 regardless of the LangVersion I use (7.0+ since this example uses a pattern).

@jaredpar is this some subtle behavior difference between the compilers that ship with 2.2 and 3.0?

Regardless, this probably belongs as a Roslyn issue, but I'd like to discuss it before moving.

@jaredpar
Copy link
Member

@cston can confirm but believe this is a deliberate change between C# 7.3 and C# 8.0 (the latter allows the code now).

@Varorbc
Copy link
Author

Varorbc commented Mar 26, 2019

@jaredpar I set the language version to 7.0 in vs 2019 and still compiled successfully.

@jaredpar
Copy link
Member

@Varorbc I noticed that as well and that appears to be a bug. If you actually use a 7.0 compiler though you'll see it doesn't compile.

@Varorbc
Copy link
Author

Varorbc commented Mar 26, 2019

@jaredpar Yes, I don't know exactly what version is used in the project system.@davkean Can you tell us?

@cston
Copy link
Member

cston commented Mar 26, 2019

Adding @gafter for the compiler change since t is null is bound as an is pattern now.

@jcouv
Copy link
Member

jcouv commented Mar 26, 2019

I think the change is deliberate and comes from dotnet/roslyn#25995 ("recursive-patterns(18): Permit a constant pattern to be used with an open type as input")

Here's the championed issue: dotnet/csharplang#1284

@peterhuene
Copy link
Contributor

peterhuene commented Mar 26, 2019

@jcouv Thanks for the info! However, should -langversion:7.0 with a Dev16 compiler emit CS0403 for the code above or not?

If the answer is "no", then I think we can close this by design. If the answer is "yes", then we should transfer this to issue to Roslyn (with some changes, since this is expected behavior with a .NET Core 2.2 SDK, but it should be possible to get the error with a LangVersion set to 7.0 with a 3.0 SDK).

@peterhuene
Copy link
Contributor

Ping @jcouv for follow-up question above.

@jcouv
Copy link
Member

jcouv commented Apr 2, 2019

I checked with @gafter who said this was a new feature (as opposed to a bug fix). That means that t is null should produce a diagnostic when LangVersion is < 8. That diagnostic should indicate that upgrading the LangVersion would make the code compile.

@jcouv
Copy link
Member

jcouv commented Apr 2, 2019

Moving the issue over to Roslyn repo: dotnet/roslyn#34678

@jcouv jcouv closed this as completed Apr 2, 2019
@msftgits msftgits transferred this issue from dotnet/cli Jan 31, 2020
@msftgits msftgits added this to the Discussion milestone Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants