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
19.04k stars 4.03k forks source link

An application of a CollectionBuilder attribute in source is not validated #71172

Open AlekseyTs opened 11 months ago

AlekseyTs commented 11 months ago
        [Fact]
        public void Test()
        {
            string sourceA = """
                using System;
                using System.Collections;
                using System.Collections.Generic;
                using System.Runtime.CompilerServices;
                [CollectionBuilder(typeof(MyCollectionBuilder), "Dummy")]
                class MyCollection1 : IEnumerable<int>
                {
                    IEnumerator<int> IEnumerable<int>.GetEnumerator() => throw null;
                    IEnumerator IEnumerable.GetEnumerator() => throw null;
                }
                class MyCollectionBuilder
                {
                }

                class Program
                {
                    static void Main()
                    {
                        MyCollection1 x = [1];
                    }
                }
                """;
            CreateCompilation(
                sourceA,
                targetFramework: TargetFramework.Net80).VerifyEmitDiagnostics(
                // (19,27): error CS9187: Could not find an accessible 'Dummy' method with the expected signature: a static method with a single parameter of type 'ReadOnlySpan<int>' and return type 'MyCollection1'.
                //         MyCollection1 x = [1];
                Diagnostic(ErrorCode.ERR_CollectionBuilderAttributeMethodNotFound, "[1]").WithArguments("Dummy", "int", "MyCollection1").WithLocation(19, 27)
                );
        }

Expected: An error at the application of a CollectionBuilder attribute about the fact that it doesn't point at the right place.

AlekseyTs commented 11 months ago

CC @cston