Skip to content

Return MissingPatternSyntax for empty pattern positions#3329

Open
broken-circle wants to merge 1 commit into
swiftlang:mainfrom
broken-circle:missing-pattern-diagnostic-3158
Open

Return MissingPatternSyntax for empty pattern positions#3329
broken-circle wants to merge 1 commit into
swiftlang:mainfrom
broken-circle:missing-pattern-diagnostic-3158

Conversation

@broken-circle
Copy link
Copy Markdown

Closes #3158.

When parsing code such as these snippets:

switch x {
  case // error: expected expression and ':' in switch case
}
switch expr {
  case : // error: expected expression in pattern
}

parseMatchingPattern() reaches its expression-fallback path, calls parseSequenceExpression(), and wraps the result in a RawExpressionPatternSyntax. If the current token starts neither a pattern nor an expression, this produces a missing expression wrapped in an expression-pattern, which the diagnostic engine describes as a missing expression rather than a missing pattern.

This PR adds an early return of RawMissingPatternSyntax before the expression fallback, which fires if the current token is neither an expression nor the start of a pattern. The fallback still fires for ambiguous forms. Now, the above examples produce "expected pattern..." diagnostics.

Testing

Updated ExpressionTests.testStandaloneAtCaseInSwitch(), StatementTests.testIf(), RecoveryTests.testRecovery180(), and SwitchTests.testSwitch4() to expect the correct diagnostics.

Added StatementTests.testIfCaseWithMissingPattern() and SwitchTests.testSwitch4b() to cover if case = x and case :, respectively.

All tests pass when run locally.

When `parseMatchingPattern()` reaches its expression-fallback path, it
calls `parseSequenceExpression()` and wraps the result in a
`RawExpressionPatternSyntax`. If the current token starts neither a
pattern nor an expression, this produces a missing expression wrapped
in an expression-pattern, which the diagnostic engine describes as a
missing expression rather than a missing pattern.

Instead, return `RawMissingPatternSyntax` directly before the fallback.
For example, `switch x { case }` now produces "expected pattern..."
rather than "expected expression...".

Closes swiftlang#3158.
@rintaro
Copy link
Copy Markdown
Member

rintaro commented May 11, 2026

@swift-ci Please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsound diagnostics for missing pattern after switchcase

2 participants