Skip to content
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

Fix suggested bound addition diagnostic #65289

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,31 +785,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: FooBar`,
// instead we suggest `T: Foo + Bar` in that case.
let mut has_bounds = false;
let mut has_bounds = None;
let mut impl_trait = false;
if let Node::GenericParam(ref param) = hir.get(id) {
match param.kind {
hir::GenericParamKind::Type { synthetic: Some(_), .. } => {
// We've found `fn foo(x: impl Trait)` instead of
// `fn foo<T>(x: T)`. We want to suggest the correct
// `fn foo(x: impl Trait + TraitBound)` instead of
// `fn foo<T: TraitBound>(x: T)`. (#63706)
impl_trait = true;
has_bounds = param.bounds.len() > 1;
}
_ => {
has_bounds = !param.bounds.is_empty();
}
let kind = &param.kind;
if let hir::GenericParamKind::Type { synthetic: Some(_), .. } = kind {
// We've found `fn foo(x: impl Trait)` instead of
// `fn foo<T>(x: T)`. We want to suggest the correct
// `fn foo(x: impl Trait + TraitBound)` instead of
// `fn foo<T: TraitBound>(x: T)`. (See #63706.)
impl_trait = true;
has_bounds = param.bounds.get(1);
} else {
has_bounds = param.bounds.get(0);
}
}
let sp = hir.span(id);
// `sp` only covers `T`, change it so that it covers
// `T:` when appropriate
let sp = if has_bounds {
sp.to(self.tcx
.sess
.source_map()
.next_point(self.tcx.sess.source_map().next_point(sp)))
// `sp` only covers `T`, change it so that it covers `T:` when appropriate.
let sp = if let Some(first_bound) = has_bounds {
sp.until(first_bound.span())
} else {
sp
};
Expand All @@ -825,7 +819,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
param,
if impl_trait { " +" } else { ":" },
self.tcx.def_path_str(t.def_id),
if has_bounds { " +"} else { "" },
if has_bounds.is_some() { " + " } else { "" },
)),
Applicability::MaybeIncorrect,
);
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-65284-suggest-generic-trait-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait Foo {
fn foo(&self);
}

trait Bar {}

fn do_stuff<T : Bar>(t : T) {
t.foo() //~ ERROR no method named `foo` found for type `T` in the current scope
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/issues/issue-65284-suggest-generic-trait-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0599]: no method named `foo` found for type `T` in the current scope
--> $DIR/issue-65284-suggest-generic-trait-bound.rs:8:7
|
LL | t.foo()
| ^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `foo`, perhaps you need to restrict type parameter `T` with it:
|
LL | fn do_stuff<T: Foo + Bar>(t : T) {
| ^^^^^^^^

error: aborting due to previous error

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