Skip to content

[parser] Fix false syntax error for match-like annotated assignments#23297

Merged
dhruvmanila merged 2 commits intoastral-sh:mainfrom
kar-ganap:fix/issue-22528
Feb 18, 2026
Merged

[parser] Fix false syntax error for match-like annotated assignments#23297
dhruvmanila merged 2 commits intoastral-sh:mainfrom
kar-ganap:fix/issue-22528

Conversation

@kar-ganap
Copy link
Contributor

Summary

Fixes #22528.

The parser incorrectly treats match[0]: int and match [x, y, z]: {dict} as the start of a match statement, producing a false syntax error. The issue is that the parser checks only for a colon after the subject expression to confirm a match statement, but annotated assignments with match as a variable name also have a colon.

This PR adds a self.peek() == TokenKind::Newline guard to the colon check, so that match is only treated as a keyword when the colon is followed by a newline (which is required for a match statement's body). When the colon is followed by something else (like a type annotation value), it falls through to the soft-keyword-as-identifier path.

Test Plan

All 42 existing match-related parser tests pass. No snapshot changes needed. Clippy clean.

Reproduction from the issue:

# These should parse without error:
match[0]: int
match [x, y, z]: {0: "a", 1: "b"}

ntBre
ntBre previously requested changes Feb 16, 2026
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Can you add a regression test for this showing that the issue is now resolved?

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 16, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@dhruvmanila dhruvmanila added the parser Related to the parser label Feb 17, 2026
@dhruvmanila
Copy link
Member

This sounds like a reasonable approach to me but I'm wondering if @dylwil3 had any examples in mind w.r.t. this comment. Do you think if this will create any issue in the error recovery? I can't think of any example.

@dylwil3
Copy link
Collaborator

dylwil3 commented Feb 17, 2026

This sounds like a reasonable approach to me but I'm wondering if @dylwil3 had any examples in mind w.r.t. this comment. Do you think if this will create any issue in the error recovery? I can't think of any example.

You're right, I think once we have the newline there's no valid syntax, non-match statement that could fit there so this is probably fine.

(Maybe CPython is doing more work than necessary in that code snippet I linked)

@ntBre ntBre dismissed their stale review February 17, 2026 20:39

Tests were added

@dhruvmanila
Copy link
Member

You're right, I think once we have the newline there's no valid syntax, non-match statement that could fit there so this is probably fine.

(Maybe CPython is doing more work than necessary in that code snippet I linked)

Yeah, I don't think so either.

Another thing to note is that this code path is only taken once the parser has ruled out all the obvious cases where it can easily classify match as a keyword or an identifier (

fn classify_match_token(&mut self) -> MatchTokenKind {
). It's only when the parser isn't sure that this code path is taken.

We were doing something similar to CPython before where we would perform infinite lookahead :)

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Thank you!

@dhruvmanila dhruvmanila added the bug Something isn't working label Feb 18, 2026
@dhruvmanila dhruvmanila merged commit 038235c into astral-sh:main Feb 18, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalid syntax error with something that looks like a match statement

4 participants