-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
make error emitted on impl &Trait
nicer
#106712
Conversation
r? @cjgillot (rustbot has picked a reviewer for you, use r? to override) |
r? @estebank |
e311816
to
068d03d
Compare
Please address the comments above, but you'll have to rebase on top of a fresh master (we've moved tests from |
068d03d
to
32b95d7
Compare
@estebank I assume you just want me to address fmease's comments, you didn't have any?
|
b316295
to
b244273
Compare
Some changes occurred in src/tools/cargo cc @ehuss |
b244273
to
c3c6760
Compare
r=me after addressing the last comment (that I hadn't noticed earlier) |
@estebank Also, the tests are failing, I have to rewrite my impl. It'll be a bit. |
This comment has been minimized.
This comment has been minimized.
c3c6760
to
46e7961
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last nitpick before merging
// Just get the indirection part of the type. | ||
let span = ty.span.until(path.span); | ||
|
||
err.span_suggestion( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err.span_suggestion( | |
err.span_suggestion_verbose( |
and then re--bless
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
path | ||
} else if !self.token.is_path_start() && self.token.can_begin_type() { | ||
// Instead of finding a path (a trait), we found a type. | ||
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type"); | |
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type start"); |
Specify "type start" because we haven't parsed a type yet, and the span points at &
only, so it would be a "lie" (and potentially confusing) to talk about a type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done this slightly differently. Instead we will only emit the nice error is there is a proper type, "found type start" seems a bit confusing to me.
46e7961
to
e590b93
Compare
@@ -938,6 +940,35 @@ impl<'a> Parser<'a> { | |||
&& self.look_ahead(1, |tok| tok.kind == TokenKind::OpenDelim(Delimiter::Parenthesis)) | |||
&& let Some(path) = self.recover_path_from_fn() | |||
{ | |||
path | |||
} else if !self.token.is_path_start() && self.token.can_begin_type() && let Ok(ty) = self.parse_ty_no_plus() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue I'm seeing is that let Ok(ty) = self.parse_ty_no_plus()
can fail parsing the type and will be returning an Err(Diagnostic)
, which gets discarded. If this happens, an ICE will occur. We need to either bubble it up (wich self.parse_ty_no_plus()?
, which is fine if slightly misleading) or err.delay_as_bug()
it (which isn't great without another mechanism to complain about the parse failure).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, what about matching on error and cancelling the diagnostic. I didn't properly realize that it returned a diagnostic, not just an error to be later turned into a diagnostic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With my current code the following ICEs:
fn foo<T: &match>() {}
if we ?
the parse call then we get:
error: expected type, found keyword `match`
--> <anon>:1:12
|
1 | fn foo<T: &match>() {}
| ^^^^^ expected type
I'm happy with this, I don't think it's confusing. We could however add a note, something along the lines of: this error was encountered when trying to recover from a "expected a trait, found type" error
before bubbling it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do map_err(|mut err| { err.note(..); err })?
, but I think even without the extra context this output would be fine.
// to recover from errors, not make more). | ||
let path = if self.may_recover() | ||
&& matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..)) | ||
&& let TyKind::Path(_, path) = &ty.peel_refs().kind { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& let TyKind::Path(_, path) = &ty.peel_refs().kind { | |
&& let TyKind::Path(_, path) = &ty.peel_refs().kind | |
{ |
@bors r+ |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#106591 (suggestion for attempted integer identifier in patterns) - rust-lang#106712 (make error emitted on `impl &Trait` nicer) - rust-lang#106829 (Unify `Opaque`/`Projection` handling in region outlives code) - rust-lang#106869 (rustdoc: remove redundant item kind class from `.item-decl > pre`) - rust-lang#106949 (ConstBlocks are poly if their substs are poly) - rust-lang#106953 (Document `EarlyBinder::subst_identity` and `skip_binder`) - rust-lang#106958 (Don't add A-bootstrap to PRs modifying Cargo.lock) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
…macro-is-ok, r=estebank Do not eagerly recover for bad `impl Trait` types in macros Fixes rust-lang#107796 cc rust-lang#106712, `@estebank` and `@Ezrashaw` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries. This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
…macro-is-ok, r=estebank Do not eagerly recover for bad `impl Trait` types in macros Fixes rust-lang#107796 cc rust-lang#106712, ``@estebank`` and ``@Ezrashaw`` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries. This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
…macro-is-ok, r=estebank Do not eagerly recover for bad `impl Trait` types in macros Fixes rust-lang#107796 cc rust-lang#106712, ```@estebank``` and ```@Ezrashaw``` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries. This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
Fixes #106694
Turned out to be simpler than I thought, also added UI test.
Before: (playground)
After: