Skip to content

Improve error message for misplaced variable designator in property patterns#81287

Merged
CyrusNajmabadi merged 15 commits intomainfrom
copilot/improve-error-messages-again
Dec 3, 2025
Merged

Improve error message for misplaced variable designator in property patterns#81287
CyrusNajmabadi merged 15 commits intomainfrom
copilot/improve-error-messages-again

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

Summary: Improve error messages when variable designation is in incorrect order with property pattern

Problem Statement

When users write o is string s { Length: 5 } (with designation before property pattern), the compiler produced confusing error messages:

  • error CS1026: ) expected
  • error CS1002: ; expected
  • error CS1513: } expected

The correct syntax should be o is string { Length: 5 } s (designation after property pattern).

Solution

This PR adds a new error code ERR_DesignatorBeneathPattern (CS8525) that provides a clear error message:

error CS8525: The variable designator 's' must come after any property pattern.

Changes Made

Error Code & Resources:

  • Added ERR_DesignatorBeneathPattern error code (CS8525) to ErrorCode.cs
  • Added error message to CSharpResources.resx
  • Updated all XLF localization files
  • Added error code to ErrorFacts.cs

Parser Implementation:

  • Modified parsePropertyPatternClause local function in ParsePatternContinued to detect identifier followed by {
  • When detected, the parser now:
    • Consumes the misplaced identifier token
    • Parses the property pattern
    • Reports the new CS8525 error with the variable name
    • Places the identifier token as skipped syntax on the property pattern
    • Continues parsing to provide better recovery

Testing:

Scope

This fix only handles the property pattern case (x { ... }). Positional patterns (x ( ... )) are not covered by this change.

Validation

  • ✅ Builds successfully with no warnings
  • ✅ All 302 pattern parsing tests pass (301 passed, 1 skipped)
  • ✅ Correct syntax continues to work without the new error
  • ✅ Error message is clear and concise
  • ✅ Simplified implementation with direct token consumption
  • ✅ No security vulnerabilities introduced

Fixes #50220


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 16, 2025 20:55
…nator

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve error messages for incorrect variable designation Improve error message for misplaced variable designator in patterns Nov 16, 2025
Copilot AI requested a review from CyrusNajmabadi November 16, 2025 21:05
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78899")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot update all urls to be #50220

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated all WorkItem URLs to reference the correct issue number 50220. (e25a7ad)

public void DesignatorBeforePositionalPattern()
{
UsingExpression("o is Point p (1, 2)",
// (1,12): error CS8525: The variable designator must come after any property or positional patterns. Change 'p { ... }' to '{ ... } p'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot update the error string to drop the Change 'p { ... }' to '{ ... } p'. part

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the error message to remove the "Change ... to ..." part. The new message is: "The variable designator '{0}' must come after any property pattern." (2fe3e0f)

{
var designation = TryParseSimpleDesignation(inSwitchArmPattern);
if (designation != null)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot let's simplify things. First, let's only fix the x { ... } case. We can ignore teh x ( ... ) case.

Second, intead of updating teh code here, you can update the parsePropertyPatternClause to see if we have identifier followed by { and to do the appropriate recovery tehre.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to only handle the x { ... } case. Moved the logic into parsePropertyPatternClause to detect identifier followed by { and perform appropriate recovery. Removed tests for positional and combined patterns. (2fe3e0f)

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title Improve error message for misplaced variable designator in patterns Improve error message for misplaced variable designator in property patterns Nov 16, 2025
{
// Check for the error case: identifier followed by '{'
// This handles "string s { Length: 5 }" which should be "string { Length: 5 } s"
if (this.IsTrueIdentifier() && this.IsValidPatternDesignation(inSwitchArmPattern) && this.PeekToken(1).Kind == SyntaxKind.OpenBraceToken)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this can be simplified. just check if we have a true identifier and we have a { one token ahead. If so, consume the token add teh error to it, and add it as a leading skipped token to the propertyPattern that is parsed out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified the implementation. Now directly checks for true identifier followed by {, consumes the token with EatToken(), adds the error to it, and attaches it as leading skipped syntax to the property pattern. (c2cf58b)

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 17, 2025 01:14
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 17, 2025 01:14
@CyrusNajmabadi
Copy link
Contributor

@dotnet/roslyn-compiler this is ready to review.

@CyrusNajmabadi
Copy link
Contributor

@jcouv @jjonescz ptal. tnx.

@CyrusNajmabadi
Copy link
Contributor

@jcouv @RikkiGibson ptal.

@CyrusNajmabadi
Copy link
Contributor

@333fred ptal :)

@CyrusNajmabadi CyrusNajmabadi merged commit aa3a5b2 into main Dec 3, 2025
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/improve-error-messages-again branch December 3, 2025 08:51
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 3, 2025
@davidwengier davidwengier modified the milestones: Next, 18.3 Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error messages when variable designation is in incorrect order with property pattern

5 participants