Improve parser error recovery for property declarations with missing identifiers#81003
Improve parser error recovery for property declarations with missing identifiers#81003CyrusNajmabadi merged 20 commits intomainfrom
Conversation
…ifiers
Add error recovery in TryParseIndexerOrPropertyDeclaration to handle cases where a property has a missing identifier but the accessor list or expression body is present. When we detect a property body start token ('{' or '=>') but no identifier, we synthesize a missing identifier and continue parsing as a property rather than creating an incomplete member.
This improves parser recovery when users forget to add a property name, allowing the rest of the file to parse correctly instead of cascading errors.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Update RequiredModifierProperty tests to reflect the improved error recovery. With the new parser changes, properties with missing identifiers now produce clearer ERR_IdentifierExpected errors instead of generic ERR_InvalidMemberDecl errors, which is better for users. Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Add clarifying comment to PropertyWithMissingIdentifier_WithAccessors test to highlight that the key validation is that all members were parsed as properties (not incomplete members), which confirms the error recovery is working correctly. Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
…otnet/roslyn into copilot/improve-error-recovery
src/Compilers/CSharp/Test/Syntax/Parsing/MemberDeclarationParsingTests.cs
Outdated
Show resolved
Hide resolved
- Add [Fact, WorkItem(...)] attributes to PropertyWithMissingIdentifier tests - Move closing parenthesis to end of last Diagnostic line for consistency Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| @@ -160,22 +160,22 @@ class Symbol | |||
|
|
|||
| void M(object o, bool b0, bool b1) | |||
There was a problem hiding this comment.
this test was affected because the line above is public ContainingSymbol { get; }. Instead of going off the rails, we now properly parse and recover. So formatting works properly below.
|
@dotnet/roslyn-compiler ptal. |
|
Will try to review in depth tomorrow. |
Co-authored-by: Rikki Gibson <rigibson@microsoft.com>
| // (6,1): error CS1022: Type or namespace definition, or end-of-file expected | ||
| // } | ||
| Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(6, 1)); | ||
| Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(3, 20)); |
There was a problem hiding this comment.
Shouldn't there be some non-empty squiggled text?
There was a problem hiding this comment.
in this case, we're saying that we expect an identifier after the \e but before the {. Since it's a missing token, and we're at the end of a line, we just identify the position. The host will squiggle something in this event, but this is the expected way for the diagnostic itself to work.
src/Compilers/CSharp/Test/Syntax/Parsing/DeclarationParsingTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jan Jones <janjones@microsoft.com>
|
@RikkiGibson ptal |
| out MemberDeclarationSyntax result) | ||
| { | ||
| if (identifierOrThisOpt.Kind == SyntaxKind.ThisKeyword) | ||
| if (identifierOrThisOpt?.Kind == SyntaxKind.ThisKeyword) |
There was a problem hiding this comment.
note to self: check what the parse is for public int [int index] { get; set; }
There was a problem hiding this comment.
It's a decent chunk of stuff
Cannot execute due to compilation errors:
/Program.cs(17,26): error CS1525: Invalid expression term 'int'
/Program.cs(17,30): error CS1003: Syntax error, ',' expected
/Program.cs(17,37): error CS1519: Invalid token '{' in a member declaration
/Program.cs(17,42): error CS1519: Invalid token ';' in a member declaration
/Program.cs(17,42): error CS1519: Invalid token ';' in a member declaration
/Program.cs(17,47): error CS1519: Invalid token ';' in a member declaration
/Program.cs(17,47): error CS1519: Invalid token ';' in a member declaration
/Program.cs(18,1): error CS1022: Type or namespace definition, or end-of-file expected
However, there isn't a need to make a change to that as part of this PR.
|
@CyrusNajmabadi Please consider trimming the commit message before merging. The multi-page OP description generated by Copilot doesn't seem to useful in the git history: 28b77b6 |
Improve parser error recovery for property declarations with missing identifiers
Summary
Successfully implemented error recovery for property declarations with missing identifiers. The parser now correctly recognizes properties even when the identifier is missing, as long as the property body (
{ get; set; }or=>) is present.Plan:
Technical Implementation:
The fix adds error recovery in three key places:
Validation:
✅ Core Fix Validated: The test
PropertyWithMissingIdentifier_WithAccessorsdemonstrates that:PropertyDeclaration(notIncompleteMember)This matches the requirement from the issue: "The parser properly recovers by implying a missing identifier/type name on the property declaration."
Remaining Work:
18 existing tests need their expectations updated. These are NOT regressions - they represent IMPROVED error reporting:
ERR_InvalidMemberDeclerrorsERR_IdentifierExpectederrors (more helpful to users)The changes provide better error messages that guide users more precisely to the actual problem (missing identifier) rather than a vague "invalid member declaration" message.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.