Skip to content

Improve diagnostics when parsing angle args #80065

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 3 commits into from
Jan 23, 2021

Conversation

b-naber
Copy link
Contributor

@b-naber b-naber commented Dec 15, 2020

#79266 introduced parsing of generic arguments in associated type constraints, this however resulted in possibly very confusing error messages in cases in which closing angle brackets were missing such as in Vec<(u32, _, _) = vec![], which outputs an incorrectly parsed equality constraint error, as noted by @cynecx.

This PR tries to provide better error messages in such cases.

r? @petrochenkov

@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 @petrochenkov (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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 15, 2020
@rust-log-analyzer

This comment has been minimized.

@jyn514 jyn514 added A-parser Area: The lexing & parsing of Rust source code to an AST A-diagnostics Area: Messages for errors, warnings, and lints labels Dec 16, 2020
@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch from 6e79a98 to fbe3df0 Compare December 16, 2020 13:50
@rust-log-analyzer

This comment has been minimized.

@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch from 12d91f4 to f9ebeff Compare December 16, 2020 15:26
@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2020

I love the change in diagnostics. I do have some reserves about the implementation though. I wonder if it's tackling it from the wrong side. I'm not sure if this would get us the same result, but it could be worth a try:

At the error reporting site of

"only path types can be used in associated type constraints",
we could add a structured suggestion to add a >. While this would be wrong in various cases, a sufficiently explanatory diagnostic message on the suggestion could probably make that clear?

We could also try to dedupliate/condense the current PR if you prefer. I'll let @rust-lang/wg-diagnostics weigh in, before you have to put in work that others may end up disagreeing with.

@b-naber
Copy link
Contributor Author

b-naber commented Dec 16, 2020

@oli-obk Thanks for looking at the PR. I understand your concerns, I'm also not really satisfied with the implementation that handles the case involving the error you mentioned. I think handling this the way you proposed is probably the better solution. I can remove that part and implement what you suggested. The case in which a syntactically correct equality constraint is parsed (e.g. as in let v : Vec<T = vec![]), is, I think at least a little better (acceptably?) handled. This should also occur frequently enough that it warrants being handled specifically. Thoughts?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2020

The case in which a syntactically correct equality constraint is parsed (e.g. as in let v : Vec<T = vec![]), is, I think at least a little better (acceptably?) handled. This should also occur frequently enough that it warrants being handled specifically. Thoughts?

I'm not sure how to detect let v: Vec<T = vec![]; at

"only path types can be used in associated type constraints",
without also detecting
error: only path types can be used in associated type constraints
--> $DIR/trait-path-types.rs:9:29
|
LL | fn f<'a>(arg : Box<dyn X< [u8; 1] = u32>>) {}
| ^^^^^^^
in which the suggestion would be wrong. I consider it totally ok for having a bad suggestion in the latter, as long as the suggestion comment makes it clear that it is not a guaranteed correct suggestion. Is this what you were also saying?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2020

r? @oli-obk

@b-naber
Copy link
Contributor Author

b-naber commented Dec 16, 2020

No, that's not what I meant. You're right, we cannot catch a 'missing closing angle args' error inside parse_angle_args when we parse a valid equality constraint such as T = vec![]. What I had in mind was to implement what you suggested, i.e. to add the 'missing closing angle args' error suggestion in

"only path types can be used in associated type constraints",

and remove this part

https://github.com/rust-lang/rust/blob/d66096b8356b132eced7f118a84f5eba3e23f016/compiler/rustc_parse/src/parser/diagnostics.rs#L1990

But we keep the part in which we handle correctly parsed equality constraints as the last element in args, which is done here:

https://github.com/rust-lang/rust/blob/d66096b8356b132eced7f118a84f5eba3e23f016/compiler/rustc_parse/src/parser/diagnostics.rs#L1957

Or do you have a problem with this part in parse_path_segment in general?

https://github.com/rust-lang/rust/blob/d66096b8356b132eced7f118a84f5eba3e23f016/compiler/rustc_parse/src/parser/path.rs#L224-L227

@petrochenkov
Copy link
Contributor

A much simpler alternative - petrochenkov@a759e56.
If the type in get_ident_from_generic_arg doesn't look like Ident<Args>, then just treat it as a type argument and treat the following = or : as an unexpected token.

@b-naber
Copy link
Contributor Author

b-naber commented Dec 19, 2020

@petrochenkov I was hoping someone from the diagnostics team would weigh in on this. To me these 'expected token' errors are probably good enough in case there is actually a missing closing angle bracket, but I think they might be confusing if we actually have incorrect generic argument types in constraints. E.g. in fn f1<'a>(arg : Box<dyn X<X::Y = u32>>) {} an 'expected token' error would be a bad diagnostic imo. The other cases that are currently caught in get_ident_from_generic_arg are probably a lot less likely to occur in practice, so that a 'missing angle bracket' error is likely the reason for these errors occuring. So in those cases, i.e. all errors that are detected inside get_ident_from_generic_arg except the 'multiple path segment' error, your errors should probably be sufficient. How should we proceed with this?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 19, 2020

I do like the simplicity of @petrochenkov 's solution, but wearing my wg-diagnostics hat I would also like to see an additional message at the

expected [..] found =

site. Now since all of these errors are about a "found =", maybe we can add some additional note right there instead of trying patch it in at the various sites like it was happening before?

@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch from 037a4da to f4f2c54 Compare December 21, 2020 14:21
@b-naber
Copy link
Contributor Author

b-naber commented Dec 21, 2020

@oli-obk Adding a suggestion to a 'found =' error in @petrochenkov 's solution would have not worked cleanly. Instead I tried to implement what you suggested initially, i.e. giving a suggestion for a 'possibly missing closing angle bracket' whenever there's an 'only types' error when parsing equality constraints.

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Oh, so it looks to me like we can do both your current PR and @petrochenkov 's change. Are you ok with this @petrochenkov ?

@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch 2 times, most recently from 1461c9a to fd28411 Compare December 21, 2020 15:33
@petrochenkov
Copy link
Contributor

@oli-obk
Yes, combining the PR as it exists now with my change should be ok.

@petrochenkov
Copy link
Contributor

Could you assign this to me for a final look once it's otherwise ready?
(Or just ping, but I'm more likely to miss it.)

@oli-obk
Copy link
Contributor

oli-obk commented Dec 28, 2020

@b-naber please pull in the commit from petrochenkov mentioned in #80065 (comment) and adjust all tests. I will then reassign to petrochenkov for a last review

@b-naber
Copy link
Contributor Author

b-naber commented Dec 28, 2020

I don't see how these two commits are mergeable. In @petrochenkov 's solution we return Ok(Some(AngleBracketedArg::Arg(arg))) in 'parse_angle_arg', control then passes to some upper level call which finds an unexpected token. If we don't output any error in get_ident_from_generic_arg, but intercept the resulting Err(gen_arg) in parse_angle_arg as is done in my last commit, I'm not sure what would be a good error to use here for outputting the suggestion (or rather why not just keep the errors that 'get_ident_from_generic_arg` already created?). Catching the 'unexpected token error' and then modifying it for a 'possibly missing closing angle bracket' seems to not make any sense to me at all.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 1, 2021

Catching the 'unexpected token error' and then modifying it for a 'possibly missing closing angle bracket' seems to not make any sense to me at all.

Hmm... while I see that it is a first to special case a generic "expected, found" error where a = was encountered, but one of the expected tokens is >, I think it could be a reasonable middle ground between parser simplicity and helpful diagnostics

@b-naber
Copy link
Contributor Author

b-naber commented Jan 13, 2021

@estebank Thanks for the review. Addressed your comments.

@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 16, 2021
@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch from 8bfc096 to d228142 Compare January 18, 2021 15:45
@b-naber
Copy link
Contributor Author

b-naber commented Jan 18, 2021

@petrochenkov Thanks for your review. Addressed your comments.

@petrochenkov
Copy link
Contributor

r=me after rebase.

@b-naber
Copy link
Contributor Author

b-naber commented Jan 19, 2021

@petrochenkov I get pretty bad conflicts in const-generics tests:

error[E0658]: default values for const generic parameters are experimental
  --> $DIR/default_trait_param.rs:1:28
   |
LL | trait Foo<const KIND: bool = true> {}
<<<<<<< HEAD
   |                            ^^^^^^
   |
   = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
   = help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
=======
   |                       ---- ^ expected one of 7 possible tokens
   |                       |
   |                       maybe try to close unmatched angle bracket
>>>>>>> d228142f35f (add and update tests)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.

Should this error at all? It seems that on master this only errors because the feature is experimental. This seems very wrong, we shouldn't output a parsing error here, should we?

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

oli-obk commented Jan 20, 2021

in stderr files, you can just ignore the conflicts and re-run ./x.py test src/test/ui --bless, it will fix the files even if they have conflicts. There is no need to manually resolve conflicts in .stderr files

@petrochenkov
Copy link
Contributor

@b-naber
After rebase and --bless all the weirdness disappear and trait Foo<const KIND: bool = true> does not report maybe try to close unmatched angle bracket, so I don't see any issues here.

@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 20, 2021
@b-naber b-naber force-pushed the parse-angle-arg-diagnostics branch from d228142 to 3ba6cf1 Compare January 22, 2021 17:32
@b-naber
Copy link
Contributor Author

b-naber commented Jan 22, 2021

@petrochenkov rebased.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2021

@bors r=petrochenkov

@bors
Copy link
Collaborator

bors commented Jan 22, 2021

📌 Commit 3ba6cf1 has been approved by petrochenkov

@bors bors 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 22, 2021
@bors
Copy link
Collaborator

bors commented Jan 23, 2021

⌛ Testing commit 3ba6cf1 with merge 1986b58...

@bors
Copy link
Collaborator

bors commented Jan 23, 2021

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 1986b58 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 23, 2021
@bors bors merged commit 1986b58 into rust-lang:master Jan 23, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 23, 2021
@b-naber b-naber deleted the parse-angle-arg-diagnostics branch January 23, 2021 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST merged-by-bors This PR was explicitly merged by bors. 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.

9 participants