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.74k stars 3.99k forks source link

Invalid Code Generation & Potential Parsing Error #62997

Open TonyValenti opened 1 year ago

TonyValenti commented 1 year ago

Version Used: VS 17.2.6

Steps to Reproduce:

  1. Write this code:
    
    public class Foo {
    protected virtual T? Serialization_Remove<T>(string Value) {
        return default;
    }
    }

public class Foo1 : Foo {

}


2.  Then go inside Foo1 and type ```override ser``` and press ```TAB``` and then Foo1 will have the following code:

public class Foo1 : Foo {

protected override T? Serialization_Remove<T>(string Value) {
    return base.Serialization_Remove<T>(Value);
}

}


3.  The auto-generated code does not compile!  Oddly, however, altering the code as follows causes it to work:

public class Foo1 : Foo { protected override T? Serialization_Remove(string Value) where T : default { // Note the new where clause here return base.Serialization_Remove(Value); } }



**Expected Behavior**:
I believe there are two separate-but-related issues.

***Issue 1***
2. According to the [docs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) the ```where T : default``` is necessary ```...when a base class or interface declares two overloads of a method, one that specifies the struct constraint, and one that doesn't have either the struct or class constraint applied```, however, as you see in the code above, there are not two overloads of a method.  Because of this, I believe that the ```T : default``` should not be necessary.

***Issue 2***
The auto-generated code does not work.  It should, regardless of how Issue1 is dealt with.

**Actual Behavior**:
Auto-generated code should work; ideally without needing the ```T : default``` constraint.
Youssef1313 commented 1 year ago

I think the compiler behavior is by-design (see https://github.com/dotnet/roslyn/issues/53713#issuecomment-849731257), there is a suggestion for a better error message in https://github.com/dotnet/roslyn/issues/46458

The IDE completion behavior is probably a bug (similar to (but different from) https://github.com/dotnet/roslyn/issues/53012), I think.