Skip to content

fix parser diagnostic regression when the error points to an empty span immediately after a line terminator #15510

@BurntSushi

Description

@BurntSushi

While #15359 fixed a ton of bugs with our diagnostic rendering, it did introduce one regression related to parser diagnostics. Specifically, prior to #15359, we would get the following diagnostic in one case of a syntax error:

   |
12 | exept:  # spellchecker:disable-line
13 |     pass
14 | b = 1
   |  Syntax Error: Expected a statement
   |

(This is from crates/ruff_python_parser/tests/snapshots/invalid_syntax@try_stmt_misspelled_except.py.snap.)

Now, the ^ is missing from the above. After #15359, the caret is added, but the diagnostic is now pointing at the end of the preceding line:

   |
12 | exept:  # spellchecker:disable-line
13 |     pass
   |         ^ Syntax Error: Expected a statement
14 | b = 1
   |

We might be able to fix this by tweaking the ranges emitted by the parser when creating a diagnostic:

self.add_error(
recovery_context_kind.create_error(self),
self.current_token_range(),
);

But I think it would probably be better to fix the renderer itself so that it points to the beginning of the following line automatically.

See #15509 which is related to this issue. These issues are technically separate since this one could be fixed independently of #15509. But if #15509 is fixed, then I think this one will automatically resolve itself as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdiagnosticsRelated to reporting of diagnostics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions