update parsing to better handle unclosed delimeters in input file #4472
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4466
We have historically assumed that the initial crate parsing (rustc_parse's
parse_crate_mod
) was successful and that we have a formattable root AST node when all of the following conditions are true:Crate
However, this is apparently not a completely safe assumption. This is because unclosed delim errors are buffered and not guaranteed to have been emitted until after the parser has been dropped. In cases where the input file has unclosed delims, and no other parser errors (such as #4466 which could easily occur in editor-format-on-save scenarios) then the parse session was still empty of errors when we do our initial check on the result of
parse_crate_mod
. As such rustfmt was assuming the crate had been parsed without errors and the AST could be used for formatting, which was incorrect.AFAICT there's nothing available outside of rustc_parse that grants us insight into the unclosed delims, so this PR refactors our parsing just a bit to ensure that the parser has indeed been dropped (and thus unclosed delim errors emitted) before we check for session errors.
I think it may be worth pursuing some tweaks upstream in
rustc_parse
, for example ensuring (or at least conditionally supporting) the emission of delim errors as partparse_crate_mod
, or adding a helper function on the parserhas_unclosed_delims()
, but I do think we should go ahead with a rustfmt fix anyway