Skip to content

Improve error recovery when object initializer uses : instead of = #80537

@RikkiGibson

Description

@RikkiGibson

.NET Lab

class Program
{
    public int X { get; set; }
    static void Main()
    {
        var y = new Program { X: 42 };
    }
}

Expected: parsing can recover by treating the : as if it was intended to be =.
Actual: parsing appears to abort the object creation, and gets a whole bunch of run-on parse errors, which makes it harder to tell..oh, this is happening because I used : and not =.

User may be prone to using : instead of = here due to e.g. moving from a constructor call with labeled arguments new Program(A: 42, B: 43), to an object creation form, or, if they are making a new object based on a property pattern Program{ A: 42, B: 43}, etc.

The suggested error recovery would be quite similar to some existing recovery, where we treat a ; in new Program { X = 42; Y = 43 }, as if you meant to put a ,. We simply consume the ; instead of the , and put an error on it.

// /Program.cs(15,32): error CS1513: } expected
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_RbraceExpected, ":").WithLocation(15, 32),
// /Program.cs(15,32): error CS1002: ; expected
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, ":").WithLocation(15, 32),
// /Program.cs(15,32): error CS1513: } expected
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_RbraceExpected, ":").WithLocation(15, 32),
// /Program.cs(15,37): error CS1002: ; expected
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "}").WithLocation(15, 37),
// /Program.cs(15,38): error CS1597: Semicolon after method or accessor block is not valid
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_UnexpectedSemicolon, ";").WithLocation(15, 38),
// /Program.cs(17,26): error CS1519: Invalid token '(' in a member declaration
//         Console.WriteLine("Hello.");
Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "(").WithArguments("(").WithLocation(17, 26),
// /Program.cs(17,27): error CS1031: Type expected
//         Console.WriteLine("Hello.");
Diagnostic(ErrorCode.ERR_TypeExpected, @"""Hello.""").WithLocation(17, 27),
// /Program.cs(17,27): error CS8124: Tuple must contain at least two elements.
//         Console.WriteLine("Hello.");
Diagnostic(ErrorCode.ERR_TupleTooFewElements, @"""Hello.""").WithLocation(17, 27),
// /Program.cs(17,27): error CS1026: ) expected
//         Console.WriteLine("Hello.");
Diagnostic(ErrorCode.ERR_CloseParenExpected, @"""Hello.""").WithLocation(17, 27),
// /Program.cs(17,27): error CS1519: Invalid token '"Hello."' in a member declaration
//         Console.WriteLine("Hello.");
Diagnostic(ErrorCode.ERR_InvalidMemberDecl, @"""Hello.""").WithArguments(@"""Hello.""").WithLocation(17, 27),
// /Program.cs(19,1): error CS1022: Type or namespace definition, or end-of-file expected
// }
Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(19, 1),
// /Program.cs(15,31): error CS0120: An object reference is required for the non-static field, method, or property 'Program.X'
//         var y = new Program { X: 42 };
Diagnostic(ErrorCode.ERR_ObjectRequired, "X").WithArguments("Program.X").WithLocation(15, 31)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersConcept-Diagnostic ClarityThe issues deals with the ease of understanding of errors and warnings.help wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions