Skip to content

Commit ae60015

Browse files
Use impl's def id when calculating type to specify UFCS
1 parent ba64ba8 commit ae60015

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,14 +2290,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22902290
let trait_impls = self.tcx.trait_impls_of(data.trait_ref.def_id);
22912291

22922292
if trait_impls.blanket_impls().is_empty()
2293-
&& let Some((impl_ty, _)) = trait_impls.non_blanket_impls().iter().next()
2294-
&& let Some(impl_def_id) = impl_ty.def() {
2295-
let message = if trait_impls.non_blanket_impls().len() == 1 {
2293+
&& let Some(impl_def_id) = trait_impls.non_blanket_impls().values().flatten().next()
2294+
{
2295+
let non_blanket_impl_count = trait_impls.non_blanket_impls().values().flatten().count();
2296+
let message = if non_blanket_impl_count == 1 {
22962297
"use the fully-qualified path to the only available implementation".to_string()
22972298
} else {
22982299
format!(
22992300
"use a fully-qualified path to a specific available implementation ({} found)",
2300-
trait_impls.non_blanket_impls().len()
2301+
non_blanket_impl_count
23012302
)
23022303
};
23032304
let mut suggestions = vec![(

src/test/ui/associated-types/associated-types-unconstrained.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | fn bar() -> isize;
66
...
77
LL | let x: isize = Foo::bar();
88
| ^^^^^^^^ cannot call associated function of trait
9+
|
10+
help: use the fully-qualified path to the only available implementation
11+
|
12+
LL | let x: isize = <isize as Foo>::bar();
13+
| +++++++++ +
914

1015
error: aborting due to previous error
1116

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait Bar {}
2+
3+
trait Foo {
4+
fn f() {}
5+
}
6+
7+
impl Foo for dyn Bar {}
8+
9+
fn main() {
10+
Foo::f();
11+
//~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
2+
--> $DIR/issue-104327.rs:10:5
3+
|
4+
LL | fn f() {}
5+
| --------- `Foo::f` defined here
6+
...
7+
LL | Foo::f();
8+
| ^^^^^^ cannot call associated function of trait
9+
|
10+
help: use the fully-qualified path to the only available implementation
11+
|
12+
LL | <(dyn Bar + 'static) as Foo>::f();
13+
| +++++++++++++++++++++++ +
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0790`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(object_safe_for_dispatch)]
2+
3+
trait Foo {
4+
fn f() {}
5+
}
6+
7+
impl Foo for dyn Sized {}
8+
9+
fn main() {
10+
Foo::f();
11+
//~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
2+
--> $DIR/issue-104328.rs:10:5
3+
|
4+
LL | fn f() {}
5+
| --------- `Foo::f` defined here
6+
...
7+
LL | Foo::f();
8+
| ^^^^^^ cannot call associated function of trait
9+
|
10+
help: use the fully-qualified path to the only available implementation
11+
|
12+
LL | <(dyn Sized + 'static) as Foo>::f();
13+
| +++++++++++++++++++++++++ +
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0790`.

0 commit comments

Comments
 (0)