-
Notifications
You must be signed in to change notification settings - Fork 13.4k
make error emitted on impl &Trait
nicer
#106712
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -727,11 +727,13 @@ impl<'a> Parser<'a> { | |||||||
let mut bounds = Vec::new(); | ||||||||
let mut negative_bounds = Vec::new(); | ||||||||
|
||||||||
// In addition to looping while we find generic bounds: | ||||||||
// We continue even if we find a keyword. This is necessary for error recovery on, | ||||||||
// for example, `impl fn()`. The only keyword that can go after generic bounds is | ||||||||
// `where`, so stop if it's it. | ||||||||
// We also continue if we find types (not traits), again for error recovery. | ||||||||
while self.can_begin_bound() | ||||||||
// Continue even if we find a keyword. | ||||||||
// This is necessary for error recover on, for example, `impl fn()`. | ||||||||
// | ||||||||
// The only keyword that can go after generic bounds is `where`, so stop if it's it. | ||||||||
|| self.token.can_begin_type() | ||||||||
|| (self.token.is_reserved_ident() && !self.token.is_keyword(kw::Where)) | ||||||||
{ | ||||||||
if self.token.is_keyword(kw::Dyn) { | ||||||||
|
@@ -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() { | ||||||||
// Instead of finding a path (a trait), we found a type. | ||||||||
let mut err = self.struct_span_err(ty.span, "expected a trait, found type"); | ||||||||
|
||||||||
// If we can recover, try to extract a path from the type. Note | ||||||||
// that we do not use the try operator when parsing the type because | ||||||||
// if it fails then we get a parser error which we don't want (we're trying | ||||||||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
// Just get the indirection part of the type. | ||||||||
let span = ty.span.until(path.span); | ||||||||
|
||||||||
err.span_suggestion_verbose( | ||||||||
span, | ||||||||
"consider removing the indirection", | ||||||||
"", | ||||||||
Applicability::MaybeIncorrect, | ||||||||
); | ||||||||
|
||||||||
path.clone() | ||||||||
} else { | ||||||||
return Err(err); | ||||||||
}; | ||||||||
|
||||||||
err.emit(); | ||||||||
|
||||||||
path | ||||||||
} else { | ||||||||
self.parse_path(PathStyle::Type)? | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
trait Trait {} | ||
|
||
fn foo(_: impl &Trait) {} | ||
//~^ ERROR expected a trait, found type | ||
|
||
fn bar<T: &Trait>(_: T) {} | ||
//~^ ERROR expected a trait, found type | ||
|
||
fn partially_correct_impl(_: impl &*const &Trait + Copy) {} | ||
//~^ ERROR expected a trait, found type | ||
|
||
fn foo_bad(_: impl &BadTrait) {} | ||
//~^ ERROR expected a trait, found type | ||
//~^^ ERROR cannot find trait `BadTrait` in this scope | ||
|
||
fn bar_bad<T: &BadTrait>(_: T) {} | ||
//~^ ERROR expected a trait, found type | ||
//~^^ ERROR cannot find trait `BadTrait` in this scope | ||
|
||
fn partially_correct_impl_bad(_: impl &*const &BadTrait + Copy) {} | ||
//~^ ERROR expected a trait, found type | ||
//~^^ ERROR cannot find trait `BadTrait` in this scope | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:3:16 | ||
| | ||
LL | fn foo(_: impl &Trait) {} | ||
| ^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn foo(_: impl &Trait) {} | ||
LL + fn foo(_: impl Trait) {} | ||
| | ||
|
||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:6:11 | ||
| | ||
LL | fn bar<T: &Trait>(_: T) {} | ||
| ^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn bar<T: &Trait>(_: T) {} | ||
LL + fn bar<T: Trait>(_: T) {} | ||
| | ||
|
||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:9:35 | ||
| | ||
LL | fn partially_correct_impl(_: impl &*const &Trait + Copy) {} | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn partially_correct_impl(_: impl &*const &Trait + Copy) {} | ||
LL + fn partially_correct_impl(_: impl Trait + Copy) {} | ||
| | ||
|
||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:12:20 | ||
| | ||
LL | fn foo_bad(_: impl &BadTrait) {} | ||
| ^^^^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn foo_bad(_: impl &BadTrait) {} | ||
LL + fn foo_bad(_: impl BadTrait) {} | ||
| | ||
|
||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:16:15 | ||
| | ||
LL | fn bar_bad<T: &BadTrait>(_: T) {} | ||
| ^^^^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn bar_bad<T: &BadTrait>(_: T) {} | ||
LL + fn bar_bad<T: BadTrait>(_: T) {} | ||
| | ||
|
||
error: expected a trait, found type | ||
--> $DIR/issue-106694.rs:20:39 | ||
| | ||
LL | fn partially_correct_impl_bad(_: impl &*const &BadTrait + Copy) {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the indirection | ||
| | ||
LL - fn partially_correct_impl_bad(_: impl &*const &BadTrait + Copy) {} | ||
LL + fn partially_correct_impl_bad(_: impl BadTrait + Copy) {} | ||
| | ||
|
||
error[E0405]: cannot find trait `BadTrait` in this scope | ||
--> $DIR/issue-106694.rs:12:21 | ||
| | ||
LL | fn foo_bad(_: impl &BadTrait) {} | ||
| ^^^^^^^^ not found in this scope | ||
|
||
error[E0405]: cannot find trait `BadTrait` in this scope | ||
--> $DIR/issue-106694.rs:16:16 | ||
| | ||
LL | fn bar_bad<T: &BadTrait>(_: T) {} | ||
| ^^^^^^^^ not found in this scope | ||
|
||
error[E0405]: cannot find trait `BadTrait` in this scope | ||
--> $DIR/issue-106694.rs:20:48 | ||
| | ||
LL | fn partially_correct_impl_bad(_: impl &*const &BadTrait + Copy) {} | ||
| ^^^^^^^^ not found in this scope | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0405`. |
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.
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 anErr(Diagnostic)
, which gets discarded. If this happens, an ICE will occur. We need to either bubble it up (wichself.parse_ty_no_plus()?
, which is fine if slightly misleading) orerr.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:
if we
?
the parse call then we get: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.