Skip to content

struct pattern parsing and diagnostic tweaks #47242

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

Merged
merged 1 commit into from
Jan 13, 2018

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jan 6, 2018

  • Recover from struct parse error on match and point out missing match
    body.
  • Point at struct when finding non-identifier while parsing its fields.
  • Add label to "expected identifier, found {}" error.

Fix #15980.

@rust-highfive
Copy link
Contributor

r? @michaelwoerister

(rust_highfive has picked a reviewer for you, use r? to override)

@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 6, 2018
@estebank estebank force-pushed the issue-15980 branch 2 times, most recently from 9f83d48 to 515f216 Compare January 7, 2018 17:58
| ------------- while parsing this struct
19 | //~^ NOTE while parsing this struct
20 | return
| ^^^^^^ expected identifier, found keyword
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the only line originally being emitted for this input. Now it correctly complains about missing => below, as well as continue to type checking.

@estebank estebank force-pushed the issue-15980 branch 3 times, most recently from 1cfe680 to 0098ed8 Compare January 7, 2018 23:33
 - Recover from struct parse error on match and point out missing match
   body.
 - Point at struct when finding non-identifier while parsing its fields.
 - Add label to "expected identifier, found {}" error.
token_descr));
} else {
err.span_label(self.span, "expected identifier");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see much point in a label that duplicates the primary message exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There have been (off hand) talks about RLS and other tools being able to reliably show only the error message or the label and although the message works better in isolation, the primary span (should) works better when pointing at the code, and should be shorter as they don't have to duplicate the code's context.

I also want to accommodate people that we might have been "training" to ignore the error message in lieu of the primary label (I know I have that behavior at times).

Because of those reasons, I've been trying to move away from errors that have highlighted spans devoid of labels.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, sounds reasonable.

@@ -2,7 +2,7 @@ error: expected identifier, found keyword `true`
--> $DIR/issue-44406.rs:18:10
|
18 | foo!(true); //~ ERROR expected type, found keyword
| ^^^^
| ^^^^ expected identifier, found keyword
Copy link
Contributor

Choose a reason for hiding this comment

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

This case looks interesting.
We don't actually expect an identifier here, why does the note appears?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A previous commit in a separate PR actually removed this incorrect issue (mostly by accident), but it broke too many things to be mergeable.

The macro is expecting a token tree as an argument, so I'm sure it's coming from that. Both cases should probably be pointing at $rest instead, like macro errors that happen after parsing.

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't actually expect an identifier here

My eyes deceive me, we expect an identifier (but this is not related to tt).
First we expect a type, but then we try to do recovery in parse_assoc_op_cast and get one more message (not entirely desirable) about expecting an identifier.
So, it's ok, not related to this PR.

@@ -2056,7 +2085,7 @@ impl<'a> Parser<'a> {
self.bump();
Ok(Ident::with_empty_ctxt(name))
} else {
self.parse_ident()
self.parse_ident_common(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should fix this in parse_ident.
The condition is "doesn't start like a struct literal", not "is_reserved_ident".
#15980 just happens to use return as something that isn't a struct literal, but the block can start with any other statement and the fix won't work.
I'd rather move the fix into parsing code for struct literals and used "not a non-reserved ident followed by colon" as a condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you provide incorrect code that you'd expect to fail to be addressed by this PR? I'm confused, as currently (and after this PR) other types of statements will have the expected behavior (continue and point out the lack of =>).

Copy link
Contributor Author

@estebank estebank Jan 10, 2018

Choose a reason for hiding this comment

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

Also, for the record, I'm slowly going through the parser looking for places where the span of an error is context dependent on the current state parser, in this case for example, the reason a non reserved ident was found is because we're parsing a struct, in the case where

fn main() {
if 3 == {
    4
}
}

is expecting { is because it wants to parse the block following the 3 == { 4 } if condition, and closer to this case, the reason "- expected one of ., =>, ?, or an operator here" (the actual correct error to be displayed with the original issue's code) is displayed here is because it is expecting a block for the Err(ref e) if e.kind == io::EndOfFile { return } match arm.

Do you feel this is too heavy handed? The main advantage I see of this is 1) these are easy enough mistakes to make and 2) the diagnostic doesn't show you enough context as it is now to identify why the problem is happening in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we get a fatal error from Struct { non_ident } from which we cannot recover, but after rereading it turns out not to be the case.
And ability to not recover from a keyword in parse_ident seems usable in other contexts too.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2018
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 11, 2018

📌 Commit d17e38f has been approved by petrochenkov

@petrochenkov petrochenkov added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 11, 2018
@bors
Copy link
Collaborator

bors commented Jan 13, 2018

⌛ Testing commit d17e38f with merge 9b2f8ac...

bors added a commit that referenced this pull request Jan 13, 2018
`struct` pattern parsing and diagnostic tweaks

 - Recover from struct parse error on match and point out missing match
   body.
 - Point at struct when finding non-identifier while parsing its fields.
 - Add label to "expected identifier, found {}" error.

Fix #15980.
@bors
Copy link
Collaborator

bors commented Jan 13, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 9b2f8ac to master...

@bors bors merged commit d17e38f into rust-lang:master Jan 13, 2018
@estebank estebank deleted the issue-15980 branch November 9, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants