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

An assert is fired when params parameter of inferred anonymous delegate has type parameter as its type #73242

Open
AlekseyTs opened this issue Apr 26, 2024 · 0 comments
Assignees
Labels
Area-Compilers New Feature - ParamsCollections untriaged Issues and PRs which have not yet been triaged by a lead
Milestone

Comments

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Apr 26, 2024

        [Fact]
        public void DelegateNaturalType_07()
        {
            var src = @"
class Helper<T> 
    where T : System.Collections.Generic.List<long>, new()
{
    static public void Test3(params T a) { System.Console.WriteLine("" {0}"", a is not null); }
}

class Program
{
    static void Main()
    {
        DoTest3<System.Collections.Generic.List<long>>();
    }
    static void DoTest3<T>()
        where T : System.Collections.Generic.List<long>, new()
    {
        var a3 = Helper<T>.Test3;
        M(a3)();
    }

    static T M<T>(T t) { System.Console.WriteLine(typeof(T)); return t; }
}
";
            var comp = CreateCompilation(src, options: TestOptions.ReleaseExe);

            var verifier = CompileAndVerify(
                comp,
                symbolValidator: (m) =>
                {
                    var delegateType = m.ContainingAssembly.GetTypeByMetadataName("<>f__AnonymousDelegate0`1");
                    MethodSymbol delegateInvokeMethod = delegateType.DelegateInvokeMethod;
                    AssertEx.Equal("void <>f__AnonymousDelegate0<T1>.Invoke(params T1 arg)", delegateInvokeMethod.ToTestDisplayString());

                    // Strictly speaking it is a violation of language rules to decorate this parameter with ParamCollectionAttribute
                    // because it lacks constraints that would make it a valid 'params' type
                    // However, going through the trouble of porting constraints and differentiating/merging anonymous delegates based on them
                    // is probably not worth the trouble for this edge scenario.
                    // The types involved might be inaccessible on assembly level, etc.
                    VerifyParamsAndAttribute(delegateInvokeMethod.Parameters[0], isParamArray: false, isParamCollection: true);
                    Assert.False(delegateType.TypeParameters[0].HasConstructorConstraint);
                    Assert.Empty(delegateType.TypeParameters[0].ConstraintTypesNoUseSiteDiagnostics);
                },
                expectedOutput: ExpectedOutput(@"
<>f__AnonymousDelegate0`1[System.Collections.Generic.List`1[System.Int64]]
 True
")).VerifyDiagnostics();
        }

Observed:

Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics.ParamsCollectionTests.DelegateNaturalType_07 [FAIL]
  System.InvalidOperationException : Assertion failed
  Stack Trace:
    at Microsoft.CodeAnalysis.ThrowingTraceListener.Fail(String message, String detailMessage)
       at System.Diagnostics.TraceInternal.Fail(String message, String detailMessage)
       at System.Diagnostics.Debug.Fail(String message, String detailMessage)
    src\Compilers\CSharp\Portable\Symbols\AnonymousTypes\AnonymousTypeField.cs(46,0): at Microsoft.CodeAnalysis.CSharp.Symbols.AnonymousTypeField..ctor(String name, Location location, TypeWithAnnotations typeWithAnnotations, RefKind refKind, ScopedKind scope, ConstantValue defaultValue, Boolean isParams, Boolean hasUnscopedRefAttribute)

            Debug.Assert(!isParams || !typeWithAnnotations.Type.IsTypeParameter());

The assert is ignorable, i.e. no other changes are necessary apart from removing it in order to get the test passing.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 26, 2024
@AlekseyTs AlekseyTs self-assigned this Apr 26, 2024
@jaredpar jaredpar added this to the 17.11 milestone Apr 27, 2024
AlekseyTs added a commit that referenced this issue Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Feature - ParamsCollections untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants