Skip to content

Commit 39b2a41

Browse files
committed
Auto merge of #104334 - compiler-errors:ufcs-sugg-wrong-def-id, r=estebank
Use impl's def id when calculating type to specify in UFCS Fixes #104327 Fixes #104328 Also addresses #102670 (comment)
2 parents 7a6af27 + 1225a65 commit 39b2a41

File tree

8 files changed

+75
-11
lines changed

8 files changed

+75
-11
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -2312,18 +2312,19 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23122312
let trait_impls = self.tcx.trait_impls_of(data.trait_ref.def_id);
23132313

23142314
if trait_impls.blanket_impls().is_empty()
2315-
&& let Some((impl_ty, _)) = trait_impls.non_blanket_impls().iter().next()
2316-
&& let Some(impl_def_id) = impl_ty.def() {
2317-
let message = if trait_impls.non_blanket_impls().len() == 1 {
2315+
&& let Some(impl_def_id) = trait_impls.non_blanket_impls().values().flatten().next()
2316+
{
2317+
let non_blanket_impl_count = trait_impls.non_blanket_impls().values().flatten().count();
2318+
let message = if non_blanket_impl_count == 1 {
23182319
"use the fully-qualified path to the only available implementation".to_string()
23192320
} else {
23202321
format!(
23212322
"use a fully-qualified path to a specific available implementation ({} found)",
2322-
trait_impls.non_blanket_impls().len()
2323+
non_blanket_impl_count
23232324
)
23242325
};
23252326
let mut suggestions = vec![(
2326-
trait_path_segment.ident.span.shrink_to_lo(),
2327+
path.span.shrink_to_lo(),
23272328
format!("<{} as ", self.tcx.type_of(impl_def_id))
23282329
)];
23292330
if let Some(generic_arg) = trait_path_segment.args {

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

+5
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

src/test/ui/error-codes/E0790.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ LL | inner::MyTrait::my_fn();
3737
|
3838
help: use the fully-qualified path to the only available implementation
3939
|
40-
LL | inner::<MyStruct as MyTrait>::my_fn();
41-
| ++++++++++++ +
40+
LL | <MyStruct as inner::MyTrait>::my_fn();
41+
| ++++++++++++ +
4242

4343
error[E0790]: cannot refer to the associated constant on trait without specifying the corresponding `impl` type
4444
--> $DIR/E0790.rs:30:13
@@ -51,8 +51,8 @@ LL | let _ = inner::MyTrait::MY_ASSOC_CONST;
5151
|
5252
help: use the fully-qualified path to the only available implementation
5353
|
54-
LL | let _ = inner::<MyStruct as MyTrait>::MY_ASSOC_CONST;
55-
| ++++++++++++ +
54+
LL | let _ = <MyStruct as inner::MyTrait>::MY_ASSOC_CONST;
55+
| ++++++++++++ +
5656

5757
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
5858
--> $DIR/E0790.rs:50:5
+12
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+
}
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`.
+12
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+
}
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`.

src/test/ui/traits/static-method-generic-inference.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | let _f: base::Foo = base::HasNew::new();
99
|
1010
help: use the fully-qualified path to the only available implementation
1111
|
12-
LL | let _f: base::Foo = base::<Foo as HasNew>::new();
13-
| +++++++ +
12+
LL | let _f: base::Foo = <Foo as base::HasNew>::new();
13+
| +++++++ +
1414

1515
error: aborting due to previous error
1616

0 commit comments

Comments
 (0)