From 6702df109e2cca15dea06db662dbcd681750e207 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Tue, 21 Jan 2025 23:17:20 -0600 Subject: [PATCH 1/4] For E0223, suggest associated functions that are similar to the path, even if there are multiple inherent impls to check. --- .../rustc_hir_analysis/src/hir_ty_lowering/errors.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 5d751a250805f..c7136078411d0 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -999,11 +999,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { .. }) = node && let Some(adt_def) = qself_ty.ty_adt_def() - && let [inherent_impl] = tcx.inherent_impls(adt_def.did()) - && let name = format!("{ident2}_{ident3}") + && let name = Symbol::intern(&format!("{ident2}_{ident3}")) && let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx - .associated_items(inherent_impl) - .filter_by_name_unhygienic(Symbol::intern(&name)) + .inherent_impls(adt_def.did()) + .iter() + .flat_map(|inherent_impl| { + tcx.associated_items(inherent_impl).filter_by_name_unhygienic(name) + }) .next() { Err(struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type") From 221b6214c05bae34f6814177bb26f2a88b358ca3 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Tue, 21 Jan 2025 23:57:15 -0600 Subject: [PATCH 2/4] Add test that multiple impls works with E0223 similar-name suggestion. --- tests/ui/suggestions/issue-109195.rs | 19 ++++++++++ tests/ui/suggestions/issue-109195.stderr | 47 ++++++++++++++++++++---- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/tests/ui/suggestions/issue-109195.rs b/tests/ui/suggestions/issue-109195.rs index cc499b0d77618..6c5704f8d2e87 100644 --- a/tests/ui/suggestions/issue-109195.rs +++ b/tests/ui/suggestions/issue-109195.rs @@ -1,3 +1,13 @@ +struct Foo; + +impl Foo { + fn bar_baz() {} +} + +impl Foo { + fn bar_quux() {} +} + fn main() { String::from::utf8; //~^ ERROR ambiguous associated type [E0223] @@ -17,4 +27,13 @@ fn main() { str::from::utf8_mut(); //~^ ERROR ambiguous associated type [E0223] //~| HELP if there were a trait named `Example` with associated type `from` + Foo::bar::baz; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP there is an associated function with a similar name: `bar_baz` + Foo::bar::quux; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP there is an associated function with a similar name: `bar_quux` + Foo::bar::fizz; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP if there were a trait named `Example` with associated type `bar` } diff --git a/tests/ui/suggestions/issue-109195.stderr b/tests/ui/suggestions/issue-109195.stderr index 10cf9cfd28ca7..7887d88280196 100644 --- a/tests/ui/suggestions/issue-109195.stderr +++ b/tests/ui/suggestions/issue-109195.stderr @@ -1,5 +1,5 @@ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:2:5 + --> $DIR/issue-109195.rs:12:5 | LL | String::from::utf8; | ^^^^^^^^^^^^ @@ -10,7 +10,7 @@ LL | String::from_utf8; | ~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:5:5 + --> $DIR/issue-109195.rs:15:5 | LL | String::from::utf8(); | ^^^^^^^^^^^^ @@ -21,7 +21,7 @@ LL | String::from_utf8(); | ~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:8:5 + --> $DIR/issue-109195.rs:18:5 | LL | String::from::utf16(); | ^^^^^^^^^^^^ @@ -32,7 +32,7 @@ LL | String::from_utf16(); | ~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:11:5 + --> $DIR/issue-109195.rs:21:5 | LL | String::from::method_that_doesnt_exist(); | ^^^^^^^^^^^^ @@ -43,7 +43,7 @@ LL | ::from::method_that_doesnt_exist(); | ~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:14:5 + --> $DIR/issue-109195.rs:24:5 | LL | str::from::utf8(); | ^^^^^^^^^ @@ -54,7 +54,7 @@ LL | ::from::utf8(); | ~~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:17:5 + --> $DIR/issue-109195.rs:27:5 | LL | str::from::utf8_mut(); | ^^^^^^^^^ @@ -64,6 +64,39 @@ help: if there were a trait named `Example` with associated type `from` implemen LL | ::from::utf8_mut(); | ~~~~~~~~~~~~~~~~~~~~~~ -error: aborting due to 6 previous errors +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:30:5 + | +LL | Foo::bar::baz; + | ^^^^^^^^ + | +help: there is an associated function with a similar name: `bar_baz` + | +LL | Foo::bar_baz; + | ~~~~~~~ + +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:33:5 + | +LL | Foo::bar::quux; + | ^^^^^^^^ + | +help: there is an associated function with a similar name: `bar_quux` + | +LL | Foo::bar_quux; + | ~~~~~~~~ + +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:36:5 + | +LL | Foo::bar::fizz; + | ^^^^^^^^ + | +help: if there were a trait named `Example` with associated type `bar` implemented for `Foo`, you could use the fully-qualified path + | +LL | ::bar::fizz; + | ~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to 9 previous errors For more information about this error, try `rustc --explain E0223`. From 7e1a8bd63312353ba97e186df18a61d05dc067d2 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Wed, 22 Jan 2025 01:57:27 -0600 Subject: [PATCH 3/4] Also check for associated fns on primitives in E0223 similar-path check. --- .../src/hir_ty_lowering/errors.rs | 12 +++-- tests/ui/suggestions/issue-109195.rs | 19 +++++-- tests/ui/suggestions/issue-109195.stderr | 51 +++++++++++++++---- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index c7136078411d0..36b214b6ae734 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -9,6 +9,7 @@ use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_middle::bug; +use rustc_middle::ty::fast_reject::{TreatParams, simplify_type}; use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _}; use rustc_middle::ty::{ self, AdtDef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt, @@ -998,10 +999,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { )), .. }) = node - && let Some(adt_def) = qself_ty.ty_adt_def() + && let Some(inherent_impls) = qself_ty + .ty_adt_def() + .map(|adt_def| tcx.inherent_impls(adt_def.did())) + .or_else(|| { + simplify_type(tcx, qself_ty, TreatParams::InstantiateWithInfer) + .map(|simple_ty| tcx.incoherent_impls(simple_ty)) + }) && let name = Symbol::intern(&format!("{ident2}_{ident3}")) - && let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx - .inherent_impls(adt_def.did()) + && let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = inherent_impls .iter() .flat_map(|inherent_impl| { tcx.associated_items(inherent_impl).filter_by_name_unhygienic(name) diff --git a/tests/ui/suggestions/issue-109195.rs b/tests/ui/suggestions/issue-109195.rs index 6c5704f8d2e87..ba66cae384abb 100644 --- a/tests/ui/suggestions/issue-109195.rs +++ b/tests/ui/suggestions/issue-109195.rs @@ -21,12 +21,12 @@ fn main() { String::from::method_that_doesnt_exist(); //~^ ERROR ambiguous associated type [E0223] //~| HELP if there were a trait named `Example` with associated type `from` - str::from::utf8(); + str::into::string(); //~^ ERROR ambiguous associated type [E0223] - //~| HELP if there were a trait named `Example` with associated type `from` - str::from::utf8_mut(); + //~| HELP there is an associated function with a similar name: `into_string` + str::char::indices(); //~^ ERROR ambiguous associated type [E0223] - //~| HELP if there were a trait named `Example` with associated type `from` + //~| HELP there is an associated function with a similar name: `char_indices` Foo::bar::baz; //~^ ERROR ambiguous associated type [E0223] //~| HELP there is an associated function with a similar name: `bar_baz` @@ -36,4 +36,15 @@ fn main() { Foo::bar::fizz; //~^ ERROR ambiguous associated type [E0223] //~| HELP if there were a trait named `Example` with associated type `bar` + i32::wrapping::add; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP there is an associated function with a similar name: `wrapping_add` + i32::wrapping::method_that_doesnt_exist; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP if there were a trait named `Example` with associated type `wrapping` + + // this one ideally should suggest `downcast_mut_unchecked` + ::downcast::mut_unchecked; + //~^ ERROR ambiguous associated type [E0223] + //~| HELP if there were a trait named `Example` with associated type `downcast` } diff --git a/tests/ui/suggestions/issue-109195.stderr b/tests/ui/suggestions/issue-109195.stderr index 7887d88280196..166c9e609ca59 100644 --- a/tests/ui/suggestions/issue-109195.stderr +++ b/tests/ui/suggestions/issue-109195.stderr @@ -45,24 +45,24 @@ LL | ::from::method_that_doesnt_exist(); error[E0223]: ambiguous associated type --> $DIR/issue-109195.rs:24:5 | -LL | str::from::utf8(); +LL | str::into::string(); | ^^^^^^^^^ | -help: if there were a trait named `Example` with associated type `from` implemented for `str`, you could use the fully-qualified path +help: there is an associated function with a similar name: `into_string` | -LL | ::from::utf8(); - | ~~~~~~~~~~~~~~~~~~~~~~ +LL | str::into_string(); + | ~~~~~~~~~~~ error[E0223]: ambiguous associated type --> $DIR/issue-109195.rs:27:5 | -LL | str::from::utf8_mut(); +LL | str::char::indices(); | ^^^^^^^^^ | -help: if there were a trait named `Example` with associated type `from` implemented for `str`, you could use the fully-qualified path +help: there is an associated function with a similar name: `char_indices` | -LL | ::from::utf8_mut(); - | ~~~~~~~~~~~~~~~~~~~~~~ +LL | str::char_indices(); + | ~~~~~~~~~~~~ error[E0223]: ambiguous associated type --> $DIR/issue-109195.rs:30:5 @@ -97,6 +97,39 @@ help: if there were a trait named `Example` with associated type `bar` implement LL | ::bar::fizz; | ~~~~~~~~~~~~~~~~~~~~~ -error: aborting due to 9 previous errors +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:39:5 + | +LL | i32::wrapping::add; + | ^^^^^^^^^^^^^ + | +help: there is an associated function with a similar name: `wrapping_add` + | +LL | i32::wrapping_add; + | ~~~~~~~~~~~~ + +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:42:5 + | +LL | i32::wrapping::method_that_doesnt_exist; + | ^^^^^^^^^^^^^ + | +help: if there were a trait named `Example` with associated type `wrapping` implemented for `i32`, you could use the fully-qualified path + | +LL | ::wrapping::method_that_doesnt_exist; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0223]: ambiguous associated type + --> $DIR/issue-109195.rs:47:5 + | +LL | ::downcast::mut_unchecked; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: if there were a trait named `Example` with associated type `downcast` implemented for `(dyn Any + 'static)`, you could use the fully-qualified path + | +LL | <(dyn Any + 'static) as Example>::downcast::mut_unchecked; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0223`. From 2c58212619e32035d5c9b15e834c89314ba1d900 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Thu, 23 Jan 2025 13:17:16 -0600 Subject: [PATCH 4/4] Give E0223 similar-item suggestion test more descriptive name. --- src/tools/tidy/src/issues.txt | 1 - ...s-assoc-type-path-suggest-similar-item.rs} | 1 + ...soc-type-path-suggest-similar-item.stderr} | 24 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) rename tests/ui/suggestions/{issue-109195.rs => ambiguous-assoc-type-path-suggest-similar-item.rs} (97%) rename tests/ui/suggestions/{issue-109195.stderr => ambiguous-assoc-type-path-suggest-similar-item.stderr} (81%) diff --git a/src/tools/tidy/src/issues.txt b/src/tools/tidy/src/issues.txt index de3380502bfcd..5878cce5ffbdd 100644 --- a/src/tools/tidy/src/issues.txt +++ b/src/tools/tidy/src/issues.txt @@ -3857,7 +3857,6 @@ ui/suggestions/issue-106443-sugg-clone-for-arg.rs ui/suggestions/issue-106443-sugg-clone-for-bound.rs ui/suggestions/issue-107860.rs ui/suggestions/issue-108470.rs -ui/suggestions/issue-109195.rs ui/suggestions/issue-109291.rs ui/suggestions/issue-109396.rs ui/suggestions/issue-109436.rs diff --git a/tests/ui/suggestions/issue-109195.rs b/tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.rs similarity index 97% rename from tests/ui/suggestions/issue-109195.rs rename to tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.rs index ba66cae384abb..a9c2c20ef374a 100644 --- a/tests/ui/suggestions/issue-109195.rs +++ b/tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.rs @@ -1,3 +1,4 @@ +// https://github.com/rust-lang/rust/issues/109195 struct Foo; impl Foo { diff --git a/tests/ui/suggestions/issue-109195.stderr b/tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.stderr similarity index 81% rename from tests/ui/suggestions/issue-109195.stderr rename to tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.stderr index 166c9e609ca59..5863aa28f41f3 100644 --- a/tests/ui/suggestions/issue-109195.stderr +++ b/tests/ui/suggestions/ambiguous-assoc-type-path-suggest-similar-item.stderr @@ -1,5 +1,5 @@ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:12:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:13:5 | LL | String::from::utf8; | ^^^^^^^^^^^^ @@ -10,7 +10,7 @@ LL | String::from_utf8; | ~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:15:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:16:5 | LL | String::from::utf8(); | ^^^^^^^^^^^^ @@ -21,7 +21,7 @@ LL | String::from_utf8(); | ~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:18:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:19:5 | LL | String::from::utf16(); | ^^^^^^^^^^^^ @@ -32,7 +32,7 @@ LL | String::from_utf16(); | ~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:21:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:22:5 | LL | String::from::method_that_doesnt_exist(); | ^^^^^^^^^^^^ @@ -43,7 +43,7 @@ LL | ::from::method_that_doesnt_exist(); | ~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:24:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:25:5 | LL | str::into::string(); | ^^^^^^^^^ @@ -54,7 +54,7 @@ LL | str::into_string(); | ~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:27:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:28:5 | LL | str::char::indices(); | ^^^^^^^^^ @@ -65,7 +65,7 @@ LL | str::char_indices(); | ~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:30:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:31:5 | LL | Foo::bar::baz; | ^^^^^^^^ @@ -76,7 +76,7 @@ LL | Foo::bar_baz; | ~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:33:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:34:5 | LL | Foo::bar::quux; | ^^^^^^^^ @@ -87,7 +87,7 @@ LL | Foo::bar_quux; | ~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:36:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:37:5 | LL | Foo::bar::fizz; | ^^^^^^^^ @@ -98,7 +98,7 @@ LL | ::bar::fizz; | ~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:39:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:40:5 | LL | i32::wrapping::add; | ^^^^^^^^^^^^^ @@ -109,7 +109,7 @@ LL | i32::wrapping_add; | ~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:42:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:43:5 | LL | i32::wrapping::method_that_doesnt_exist; | ^^^^^^^^^^^^^ @@ -120,7 +120,7 @@ LL | ::wrapping::method_that_doesnt_exist; | ~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type - --> $DIR/issue-109195.rs:47:5 + --> $DIR/ambiguous-assoc-type-path-suggest-similar-item.rs:48:5 | LL | ::downcast::mut_unchecked; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^