Skip to content

Reduce spurious errors when parsing malformed list #45074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

thombles
Copy link
Contributor

@thombles thombles commented Oct 6, 2017

Fixes #44339 (both cases) where you get extra errors beyond the main cause with input like use std::{foo. bar};

While parsing this there is a stack of calls like this:

parse_item_()
  -> parse_view_path()?
    -> parse_path_list_items()?
      -> parse_unspanned_seq() (returning that PResult)
        -> parse_seq_to_before_end() -> Vec<T> (1)
          -> parse_seq_to_before_tokens() -> Vec<T> (2)

An error occurs in (2), which is emitted and swallowed in (1). Then at the bottom in parse_item_() we end up emitting an unneeded second error:

        if self.eat_keyword(keywords::Use) {
            // USE ITEM
            let item_ = ItemKind::Use(self.parse_view_path()?); // <-- Is an Ok result despite error
            self.expect(&token::Semi)?; // <-- Tries to do this, creates spurious error

What I propose is to make (1) and (2) return a new structure that contains the Vec<T> and an Option<DiagnosticBuilder>. This way, the callers who don't mind can easily take the Vec<T> to continue the old behaviour, while parse_unspanned_seq can instead propagate the error in its PResult.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@petrochenkov petrochenkov self-assigned this Oct 7, 2017
//~| NOTE: no resolution found
//~| ERROR: unresolved function `bar`
//~| NOTE: no resolution found
//~| ERROR: expected one of `)`, `,`, `.`, `<`, `?`
Copy link
Contributor

Choose a reason for hiding this comment

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

This change highlights that error recovery in the parser no longer works in some cases (that's the purpose of this test indeed!).

So the effect of this patch is that it disables error recovery for one particular randomly selected function parse_unspanned_seq.
Yes, this happens to improve diagnostics for the example from #44339, but I don't think this is an acceptable solution.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I would do is to introduce the notion of "similar separators" (e.g. , and . from #44339 which are close to each other on a keyboard) and accept separators "similar" to the desired one in parse_seq_to_before_tokens after issuing a non-fatal error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I understand. Yes it makes sense to try harder to consume the rest of the sequence before we return. Okay I will explore this option. That will be a different solution so I'll close this PR. Thanks for the feedback!

@thombles thombles closed this Oct 8, 2017
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.

4 participants