Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Symbol/Symbols/IndexerTests.cs (C#)

Code before:
                    public A(int x, A a = new A()[1]) { }

                    public int this[int i] { get => 0; set { } }
                }
                """;
            var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp);

            var a = compilation.GlobalNamespace.GetTypeMember("A").InstanceConstructors.Where(c => !c.IsDefaultValueTypeConstructor()).Single();

            Assert.Null(a.Parameters[1].ExplicitDefaultValue);
            Assert.True(a.Parameters[1].HasExplicitDefaultValue);

            compilation.VerifyDiagnostics(
                // (3,2): error CS0616: 'A' is not an attribute class
                // [A(3, X = 6)]
                Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "A").WithArguments("A").WithLocation(3, 2),
                // (3,2): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
                // [A(3, X = 6)]
                Diagnostic(ErrorCode.ERR_BadAttributeArgument, "A(3, X = 6)").WithLocation(3, 2),
                // (8,27): error CS1736: Default parameter value for 'a' must be a compile-time constant
                //     public A(int x, A a = new A()[1]) { }
                Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "new A()[1]").WithArguments("a").WithLocation(8, 27));
        }

        [Fact]
        [WorkItem("https://github.com/dotnet/roslyn/issues/43145")]
        public void CompilerShouldNotCrashOnSyntaxErrorInIndexerDeclaration()
        {
            var source =
                """
                struct S
                {
                    public bool This[int t] { get { return false; } }
                }
                """;
            var compilation = CreateCompilation(source, options: TestOptions.DebugDll);
            compilation.GetDiagnostics();
            
            compilation.VerifyDiagnostics(
                // (3,17): warning CS0649: Field 'S.This' is never assigned to, and will always have its default value false
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.WRN_UnassignedInternalField, "This").WithArguments("S.This", "false").WithLocation(3, 17),
                // (3,21): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_CStyleArray, "[int t]").WithLocation(3, 21),
                // (3,22): error CS1525: Invalid expression term 'int'
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(3, 22),
                // (3,22): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "int").WithLocation(3, 22),

Review comment (suggestion):
```suggestion
        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43145")]
```

Code after:
                    public A(int x, A a = new A()[1]) { }

                    public int this[int i] { get => 0; set { } }
                }
                """;
            var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp);

            var a = compilation.GlobalNamespace.GetTypeMember("A").InstanceConstructors.Where(c => !c.IsDefaultValueTypeConstructor()).Single();

            Assert.Null(a.Parameters[1].ExplicitDefaultValue);
            Assert.True(a.Parameters[1].HasExplicitDefaultValue);

            compilation.VerifyDiagnostics(
                // (3,2): error CS0616: 'A' is not an attribute class
                // [A(3, X = 6)]
                Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "A").WithArguments("A").WithLocation(3, 2),
                // (3,2): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
                // [A(3, X = 6)]
                Diagnostic(ErrorCode.ERR_BadAttributeArgument, "A(3, X = 6)").WithLocation(3, 2),
                // (8,27): error CS1736: Default parameter value for 'a' must be a compile-time constant
                //     public A(int x, A a = new A()[1]) { }
                Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "new A()[1]").WithArguments("a").WithLocation(8, 27));
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43145")]
        public void CompilerShouldNotCrashOnSyntaxErrorInIndexerDeclaration()
        {
            var source =
                """
                struct S
                {
                    public bool This[int t] { get { return false; } }
                }
                """;
            var compilation = CreateCompilation(source, options: TestOptions.DebugDll);
            compilation.GetDiagnostics();
            compilation.VerifyDiagnostics(
                // (3,17): warning CS0649: Field 'S.This' is never assigned to, and will always have its default value false
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.WRN_UnassignedInternalField, "This").WithArguments("S.This", "false").WithLocation(3, 17),
                // (3,21): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_CStyleArray, "[int t]").WithLocation(3, 21),
                // (3,22): error CS1525: Invalid expression term 'int'
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(3, 22),
                // (3,22): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
                //     public bool This[int t] { get { return false; } }
                Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "int").WithLocation(3, 22),
                // (3,26): error CS1003: Syntax error, ',' expected
                //     public bool This[int t] { get { return false; } }