dotnet/roslyn

Unexpected diagnostic in `with` expression with indexer on a struct

Open

#81,666 创建于 2025年12月12日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)C# (4,257 fork)batch import
Area-CompilersConcept-Diagnostic Clarityhelp wanted

仓库指标

Star
 (20,414 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

Although this is an error scenario, the first diagnostic is unexpected.

    [Fact]
    public void TODO2()
    {
        var src = """
var c = new S() with { [0] = 1 };

public struct S
{
    public int this[int i] { set { } }
}
""";

        CreateCompilation(src).VerifyEmitDiagnostics(
            // (1,24): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
            // var c = new S() with { [0] = 1 };
            Diagnostic(ErrorCode.ERR_AssgLvalueExpected, "[0]").WithLocation(1, 24),
            // (1,24): error CS0747: Invalid initializer member declarator
            // var c = new S() with { [0] = 1 };
            Diagnostic(ErrorCode.ERR_InvalidInitializerElementInitializer, "[0] = 1").WithLocation(1, 24));
    }

贡献者指南