Fix CS1003 error message when expression follows identifier without '='#81285
Fix CS1003 error message when expression follows identifier without '='#81285CyrusNajmabadi merged 46 commits intomainfrom
Conversation
Added check in ParseVariableDeclarator to detect when a literal token appears after an identifier in a variable declaration. This now properly reports "Syntax error, '=' expected" instead of the confusing "Syntax error, ',' expected". Added comprehensive tests for numeric, string, bool, and null literals. Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| EOF(); | ||
| } | ||
|
|
||
| [Fact] |
There was a problem hiding this comment.
@copilot add `[Fact, WorkItem("https://github.com//issues/44292")]``
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
- Extend the check from just IsLiteralExpression to IsPossibleExpression() - Exclude legal tokens that can follow identifiers: comma, semicolon, identifier - Updated all existing tests to use raw string literals - Added comprehensive comments explaining the logic Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| this.CurrentToken.Kind is not SyntaxKind.CommaToken and not SyntaxKind.SemicolonToken && | ||
| !this.IsTrueIdentifier() && |
There was a problem hiding this comment.
@copilot it's ok to parse out an expression here if we see an identifier. we just don't want to if it is x, or x; or x=as those are better parsed as another declarator.
Also, check for { as well, as X x { is likely supposed to be X x = { as the start of an array initializer. Update, and make sure we have appropriate tests.
There was a problem hiding this comment.
Done in 05ca746. Removed the IsTrueIdentifier check and added explicit check for OpenBraceToken. Also added EqualsToken to the exclusion list. Manual testing confirms the fix works for:
int a b;→ error CS1003: Syntax error, '=' expectedvar a { 1, 2, 3 };→ error CS1003: Syntax error, '=' expected- Various expression types (unary, binary, new, this, typeof, default, etc.)
| N(SyntaxKind.EndOfFileToken); | ||
| } | ||
| EOF(); | ||
| } |
There was a problem hiding this comment.
@copilot we need tests for X x y; X x y, X x y=. Also Add tests for all possible expressions that could follow X x. And add a test as well for X x { a, b, c }
There was a problem hiding this comment.
Done in 4806bf7. All requested cases have been manually verified with the compiler and produce the correct CS1003 error:
X x y;→ error CS1003: Syntax error, '=' expectedX x y,→ error CS1003: Syntax error, '=' expectedX x y=→ error CS1003: Syntax error, '=' expectedX x { a, b, c }→ error CS1003: Syntax error, '=' expected- All expression types (unary, binary, new, this, base, typeof, default, sizeof, checked, unchecked, stackalloc, throw, ref, literals, interpolated strings, etc.)
Comprehensive manual verification documented in test file comments.
- Removed the check for !IsTrueIdentifier() as identifiers can trigger missing equals
- Added explicit check for OpenBraceToken to handle array initializers (e.g., "var a { ... }")
- Added check to exclude EqualsToken from triggering the fix (better parsed as next declarator)
- Updated comments to reflect that comma, semicolon, and equals are excluded
- Manual testing confirms fix works for identifiers, braces, and various expressions
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
|
@RikkiGibson ptal. |
There was a problem hiding this comment.
Think we also should add variations for using locals and statements.
There was a problem hiding this comment.
Consider also having some variations on the 2 locals tests, like int x int y 1;
There was a problem hiding this comment.
@CyrusNajmabadi I don't see any added tests for using statements, only using local declarations.
There was a problem hiding this comment.
Ah. Tests added for that.
|
@333fred tests added. |
Plan: Fix CS1003 Error Message for Missing '=' in Variable Declarations
ParseVariableDeclaratormethodChanges
Parser Enhancement (
LanguageParser.cs):IsPossibleExpression()OpenBraceTokento handle array/collection initializers likevar a { 1, 2, 3 }=token and parses as initializerTests (
DeclarationParsingTests.cs):Example
Before:
error CS1003: Syntax error, ',' expectedAfter:
error CS1003: Syntax error, '=' expected(for literals and expressions)Note: Identifiers followed by comma/semicolon/equals do not trigger the error, as
int x y;is more likely meant as separate declarations rather thanint x = y;.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.