mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
Correctly suggest adding bounds to impl Trait
argument
This commit is contained in:
parent
7b0085a613
commit
2d438d6993
@ -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
|
||||
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
|
||||
@ -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 { "" },
|
||||
)),
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user