Skip to content
/ rust Public
forked from rust-lang/rust

Commit 7e1a8bd

Browse files
committed
Also check for associated fns on primitives in E0223 similar-path check.
1 parent 221b621 commit 7e1a8bd

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_middle::bug;
12+
use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
1213
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
1314
use rustc_middle::ty::{
1415
self, AdtDef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt,
@@ -998,10 +999,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
998999
)),
9991000
..
10001001
}) = node
1001-
&& let Some(adt_def) = qself_ty.ty_adt_def()
1002+
&& let Some(inherent_impls) = qself_ty
1003+
.ty_adt_def()
1004+
.map(|adt_def| tcx.inherent_impls(adt_def.did()))
1005+
.or_else(|| {
1006+
simplify_type(tcx, qself_ty, TreatParams::InstantiateWithInfer)
1007+
.map(|simple_ty| tcx.incoherent_impls(simple_ty))
1008+
})
10021009
&& let name = Symbol::intern(&format!("{ident2}_{ident3}"))
1003-
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx
1004-
.inherent_impls(adt_def.did())
1010+
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = inherent_impls
10051011
.iter()
10061012
.flat_map(|inherent_impl| {
10071013
tcx.associated_items(inherent_impl).filter_by_name_unhygienic(name)

tests/ui/suggestions/issue-109195.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ fn main() {
2121
String::from::method_that_doesnt_exist();
2222
//~^ ERROR ambiguous associated type [E0223]
2323
//~| HELP if there were a trait named `Example` with associated type `from`
24-
str::from::utf8();
24+
str::into::string();
2525
//~^ ERROR ambiguous associated type [E0223]
26-
//~| HELP if there were a trait named `Example` with associated type `from`
27-
str::from::utf8_mut();
26+
//~| HELP there is an associated function with a similar name: `into_string`
27+
str::char::indices();
2828
//~^ ERROR ambiguous associated type [E0223]
29-
//~| HELP if there were a trait named `Example` with associated type `from`
29+
//~| HELP there is an associated function with a similar name: `char_indices`
3030
Foo::bar::baz;
3131
//~^ ERROR ambiguous associated type [E0223]
3232
//~| HELP there is an associated function with a similar name: `bar_baz`
@@ -36,4 +36,15 @@ fn main() {
3636
Foo::bar::fizz;
3737
//~^ ERROR ambiguous associated type [E0223]
3838
//~| HELP if there were a trait named `Example` with associated type `bar`
39+
i32::wrapping::add;
40+
//~^ ERROR ambiguous associated type [E0223]
41+
//~| HELP there is an associated function with a similar name: `wrapping_add`
42+
i32::wrapping::method_that_doesnt_exist;
43+
//~^ ERROR ambiguous associated type [E0223]
44+
//~| HELP if there were a trait named `Example` with associated type `wrapping`
45+
46+
// this one ideally should suggest `downcast_mut_unchecked`
47+
<dyn std::any::Any>::downcast::mut_unchecked;
48+
//~^ ERROR ambiguous associated type [E0223]
49+
//~| HELP if there were a trait named `Example` with associated type `downcast`
3950
}

tests/ui/suggestions/issue-109195.stderr

+42-9
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ LL | <String as Example>::from::method_that_doesnt_exist();
4545
error[E0223]: ambiguous associated type
4646
--> $DIR/issue-109195.rs:24:5
4747
|
48-
LL | str::from::utf8();
48+
LL | str::into::string();
4949
| ^^^^^^^^^
5050
|
51-
help: if there were a trait named `Example` with associated type `from` implemented for `str`, you could use the fully-qualified path
51+
help: there is an associated function with a similar name: `into_string`
5252
|
53-
LL | <str as Example>::from::utf8();
54-
| ~~~~~~~~~~~~~~~~~~~~~~
53+
LL | str::into_string();
54+
| ~~~~~~~~~~~
5555

5656
error[E0223]: ambiguous associated type
5757
--> $DIR/issue-109195.rs:27:5
5858
|
59-
LL | str::from::utf8_mut();
59+
LL | str::char::indices();
6060
| ^^^^^^^^^
6161
|
62-
help: if there were a trait named `Example` with associated type `from` implemented for `str`, you could use the fully-qualified path
62+
help: there is an associated function with a similar name: `char_indices`
6363
|
64-
LL | <str as Example>::from::utf8_mut();
65-
| ~~~~~~~~~~~~~~~~~~~~~~
64+
LL | str::char_indices();
65+
| ~~~~~~~~~~~~
6666

6767
error[E0223]: ambiguous associated type
6868
--> $DIR/issue-109195.rs:30:5
@@ -97,6 +97,39 @@ help: if there were a trait named `Example` with associated type `bar` implement
9797
LL | <Foo as Example>::bar::fizz;
9898
| ~~~~~~~~~~~~~~~~~~~~~
9999

100-
error: aborting due to 9 previous errors
100+
error[E0223]: ambiguous associated type
101+
--> $DIR/issue-109195.rs:39:5
102+
|
103+
LL | i32::wrapping::add;
104+
| ^^^^^^^^^^^^^
105+
|
106+
help: there is an associated function with a similar name: `wrapping_add`
107+
|
108+
LL | i32::wrapping_add;
109+
| ~~~~~~~~~~~~
110+
111+
error[E0223]: ambiguous associated type
112+
--> $DIR/issue-109195.rs:42:5
113+
|
114+
LL | i32::wrapping::method_that_doesnt_exist;
115+
| ^^^^^^^^^^^^^
116+
|
117+
help: if there were a trait named `Example` with associated type `wrapping` implemented for `i32`, you could use the fully-qualified path
118+
|
119+
LL | <i32 as Example>::wrapping::method_that_doesnt_exist;
120+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
121+
122+
error[E0223]: ambiguous associated type
123+
--> $DIR/issue-109195.rs:47:5
124+
|
125+
LL | <dyn std::any::Any>::downcast::mut_unchecked;
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127+
|
128+
help: if there were a trait named `Example` with associated type `downcast` implemented for `(dyn Any + 'static)`, you could use the fully-qualified path
129+
|
130+
LL | <(dyn Any + 'static) as Example>::downcast::mut_unchecked;
131+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132+
133+
error: aborting due to 12 previous errors
101134

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

0 commit comments

Comments
 (0)