Rollup merge of #65289 - varkor:issue-65284, r=estebank

Fix suggested bound addition diagnostic

Fixes #65284.
This commit is contained in:
Tyler Mandry 2019-10-11 15:09:58 -07:00 committed by GitHub
commit 811bd38ae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 21 deletions

View File

@ -809,31 +809,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
};
@ -849,7 +843,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,
);

View File

@ -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() {}

View File

@ -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`.