Use impl's def id when calculating type to specify UFCS
This commit is contained in:
parent
ba64ba8b0d
commit
ae60015e76
6 changed files with 68 additions and 4 deletions
|
@ -2290,14 +2290,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
let trait_impls = self.tcx.trait_impls_of(data.trait_ref.def_id);
|
||||
|
||||
if trait_impls.blanket_impls().is_empty()
|
||||
&& let Some((impl_ty, _)) = trait_impls.non_blanket_impls().iter().next()
|
||||
&& let Some(impl_def_id) = impl_ty.def() {
|
||||
let message = if trait_impls.non_blanket_impls().len() == 1 {
|
||||
&& let Some(impl_def_id) = trait_impls.non_blanket_impls().values().flatten().next()
|
||||
{
|
||||
let non_blanket_impl_count = trait_impls.non_blanket_impls().values().flatten().count();
|
||||
let message = if non_blanket_impl_count == 1 {
|
||||
"use the fully-qualified path to the only available implementation".to_string()
|
||||
} else {
|
||||
format!(
|
||||
"use a fully-qualified path to a specific available implementation ({} found)",
|
||||
trait_impls.non_blanket_impls().len()
|
||||
non_blanket_impl_count
|
||||
)
|
||||
};
|
||||
let mut suggestions = vec![(
|
||||
|
|
|
@ -6,6 +6,11 @@ LL | fn bar() -> isize;
|
|||
...
|
||||
LL | let x: isize = Foo::bar();
|
||||
| ^^^^^^^^ cannot call associated function of trait
|
||||
|
|
||||
help: use the fully-qualified path to the only available implementation
|
||||
|
|
||||
LL | let x: isize = <isize as Foo>::bar();
|
||||
| +++++++++ +
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
12
src/test/ui/suggestions/issue-104327.rs
Normal file
12
src/test/ui/suggestions/issue-104327.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
trait Bar {}
|
||||
|
||||
trait Foo {
|
||||
fn f() {}
|
||||
}
|
||||
|
||||
impl Foo for dyn Bar {}
|
||||
|
||||
fn main() {
|
||||
Foo::f();
|
||||
//~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type
|
||||
}
|
17
src/test/ui/suggestions/issue-104327.stderr
Normal file
17
src/test/ui/suggestions/issue-104327.stderr
Normal file
|
@ -0,0 +1,17 @@
|
|||
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
|
||||
--> $DIR/issue-104327.rs:10:5
|
||||
|
|
||||
LL | fn f() {}
|
||||
| --------- `Foo::f` defined here
|
||||
...
|
||||
LL | Foo::f();
|
||||
| ^^^^^^ cannot call associated function of trait
|
||||
|
|
||||
help: use the fully-qualified path to the only available implementation
|
||||
|
|
||||
LL | <(dyn Bar + 'static) as Foo>::f();
|
||||
| +++++++++++++++++++++++ +
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0790`.
|
12
src/test/ui/suggestions/issue-104328.rs
Normal file
12
src/test/ui/suggestions/issue-104328.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#![feature(object_safe_for_dispatch)]
|
||||
|
||||
trait Foo {
|
||||
fn f() {}
|
||||
}
|
||||
|
||||
impl Foo for dyn Sized {}
|
||||
|
||||
fn main() {
|
||||
Foo::f();
|
||||
//~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type
|
||||
}
|
17
src/test/ui/suggestions/issue-104328.stderr
Normal file
17
src/test/ui/suggestions/issue-104328.stderr
Normal file
|
@ -0,0 +1,17 @@
|
|||
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
|
||||
--> $DIR/issue-104328.rs:10:5
|
||||
|
|
||||
LL | fn f() {}
|
||||
| --------- `Foo::f` defined here
|
||||
...
|
||||
LL | Foo::f();
|
||||
| ^^^^^^ cannot call associated function of trait
|
||||
|
|
||||
help: use the fully-qualified path to the only available implementation
|
||||
|
|
||||
LL | <(dyn Sized + 'static) as Foo>::f();
|
||||
| +++++++++++++++++++++++++ +
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0790`.
|
Loading…
Add table
Add a link
Reference in a new issue