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

Correctly suggest adding bounds to impl Trait argument #63811

Merged
merged 2 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// 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 impl_trait = false;
if let Node::GenericParam(ref param) = hir.get(id) {
has_bounds = !param.bounds.is_empty();
match param.kind {
hir::GenericParamKind::Type { synthetic: Some(_), .. } => {
impl_trait = true; // #63706
estebank marked this conversation as resolved.
Show resolved Hide resolved
has_bounds = param.bounds.len() > 1;
}
_ => {
has_bounds = !param.bounds.is_empty();
}
}
}
let sp = hir.span(id);
// `sp` only covers `T`, change it so that it covers
Expand All @@ -765,8 +774,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sp,
&msg[..],
candidates.iter().map(|t| format!(
"{}: {}{}",
"{}{} {}{}",
param,
if impl_trait { " +" } else { ":" },
self.tcx.def_path_str(t.def_id),
if has_bounds { " +"} else { "" },
)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// run-rustfix

trait Foo {}

trait Bar {
fn hello(&self) {}
}

struct S;

impl Foo for S {}
impl Bar for S {}

fn test(foo: impl Foo + Bar) {
foo.hello(); //~ ERROR E0599
}

fn main() {
test(S);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// run-rustfix

trait Foo {}

trait Bar {
fn hello(&self) {}
}

struct S;

impl Foo for S {}
impl Bar for S {}

fn test(foo: impl Foo) {
foo.hello(); //~ ERROR E0599
}

fn main() {
test(S);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0599]: no method named `hello` found for type `impl Foo` in the current scope
--> $DIR/impl-trait-with-missing-trait-bounds-in-arg.rs:15:9
|
LL | foo.hello();
| ^^^^^
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `hello`, perhaps you need to restrict type parameter `impl Foo` with it:
|
LL | fn test(foo: impl Foo + Bar) {
| ^^^^^^^^^^^^^^

error: aborting due to previous error

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