Skip to content

Reduce syntax error noise by swallowing dedents like indents - #27170

Merged
epage merged 2 commits into
astral-sh:mainfrom
epage:dedent
Jul 24, 2026
Merged

Reduce syntax error noise by swallowing dedents like indents#27170
epage merged 2 commits into
astral-sh:mainfrom
epage:dedent

Conversation

@epage

@epage epage commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

The error recover code swallows an indent but then acts on the dedent, making things imbalanced, causing a cascade of more failures.

Some of these failures are confusing with ^ Syntax Error: Expected a statement in a location that doesn't make sense. #27033 shifts the rendering of that error location, making it make even less sense. Instead, we can just not create these spurious extra errors which this PR aims to handle, at least for some cases.

Test Plan

Added a few more indentation cases

@epage
epage requested review from a team, MichaReiser and dhruvmanila as code owners July 24, 2026 18:55
@AlexWaygood AlexWaygood added diagnostics Related to reporting of diagnostics. and removed diagnostics Related to reporting of diagnostics. labels Jul 24, 2026
{
// For this invalid `Indent`, ensure the matching `Dedent` gets consumed as well
unexpected_indents += 1;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a bit late here, so I don't have a great suggestion. But it feels a bit off to put this specific statement recovery logic into the generic recovery flow

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If nothing else, this is at least giving some ideas of what to dig into to further understand this code.

The best I've come up with so far for keeping the statement-specific logic in the statement code is we treat an indent as a list element for block statements and then have the parse statement deal with the indent and recover on the matching dedent. I've not yet checked to see what error cases this seemingly-innocent lie might cause.

@astral-sh-bot

astral-sh-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

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.

@MichaReiser

Copy link
Copy Markdown
Member

The syntax errors are a huge improvement. Thank you

@MichaReiser MichaReiser added the parser Related to the parser label Jul 24, 2026
@epage

epage commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

While I know this isn't the prettiest, I'm going to go ahead and merge. Sometimes good error recovery is messy and sometimes making things more general / cleaner can increase the complexity. It might be best to be surgical for this change for now and see how it evolves and refactor in the future. However, if people have ideas before that future date, I'd be up for trying them out.

@epage
epage merged commit 34d0944 into astral-sh:main Jul 24, 2026
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants