Skip to content

For E0223, suggest associated functions that are similar to the path #120632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_middle::middle::stability::AllowUnstable;
use rustc_middle::query::Key;
use rustc_middle::ty::{
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, IsSuggestable, ParamEnv, Ty,
TyCtxt, TypeVisitableExt,
Expand Down Expand Up @@ -1373,6 +1374,57 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
)
.emit() // Already reported in an earlier stage.
} else {
// suggest methods that look similar to the path
// e.g. for `String::from::utf8`, suggest `String::from_utf8` (#109195)
for (_, node) in tcx.hir().parent_iter(qself.hir_id) {
if let hir::Node::Expr(hir::Expr {
kind:
hir::ExprKind::Path(hir::QPath::TypeRelative(
hir::Ty {
kind:
hir::TyKind::Path(hir::QPath::TypeRelative(
_,
hir::PathSegment { ident: ident2, .. },
)),
..
},
hir::PathSegment { ident: ident3, .. },
)),
..
}) = node
{
let name = format!("{ident2}_{ident3}");
if if let Some(ty_def_id) = qself_ty.ty_def_id()
&& let Ok([inherent_impl]) = tcx.inherent_impls(ty_def_id)
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx
.associated_items(inherent_impl)
.filter_by_name_unhygienic(Symbol::intern(&name))
.next()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use something like the method resolution logic to find the method, even if it's hidden behind derefs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
true
} else {
qself_ty.is_str()
&& ["from_utf8", "from_utf8_mut"].contains(&name.as_str())
} {
let reported = struct_span_code_err!(
tcx.dcx(),
span,
E0223,
"ambiguous associated type"
)
.with_span_suggestion_verbose(
ident2.span.to(ident3.span),
format!("you might have meant to use `{name}`"),
name,
Applicability::MaybeIncorrect,
)
.emit();
self.set_tainted_by_errors(reported);
return Err(reported);
}
}
}

let traits: Vec<_> =
self.probe_traits_that_match_assoc_ty(qself_ty, assoc_ident);

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/suggestions/issue-109195.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn main() {
String::from::utf8;
//~^ ERROR ambiguous associated type [E0223]
//~| HELP you might have meant to use `from_utf8`
String::from::utf8();
//~^ ERROR ambiguous associated type [E0223]
//~| HELP you might have meant to use `from_utf8`
String::from::utf16();
//~^ ERROR ambiguous associated type [E0223]
//~| HELP you might have meant to use `from_utf16`
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();
//~^ ERROR ambiguous associated type [E0223]
//~| HELP you might have meant to use `from_utf8`
str::from::utf8_mut();
//~^ ERROR ambiguous associated type [E0223]
//~| HELP you might have meant to use `from_utf8_mut`
}
69 changes: 69 additions & 0 deletions tests/ui/suggestions/issue-109195.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:2:5
|
LL | String::from::utf8;
| ^^^^^^^^^^^^
|
help: you might have meant to use `from_utf8`
|
LL | String::from_utf8;
| ~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:5:5
|
LL | String::from::utf8();
| ^^^^^^^^^^^^
|
help: you might have meant to use `from_utf8`
|
LL | String::from_utf8();
| ~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:8:5
|
LL | String::from::utf16();
| ^^^^^^^^^^^^
|
help: you might have meant to use `from_utf16`
|
LL | String::from_utf16();
| ~~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:11:5
|
LL | String::from::method_that_doesnt_exist();
| ^^^^^^^^^^^^
|
help: if there were a trait named `Example` with associated type `from` implemented for `String`, you could use the fully-qualified path
|
LL | <String as Example>::from::method_that_doesnt_exist();
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:14:5
|
LL | str::from::utf8();
| ^^^^^^^^^
|
help: you might have meant to use `from_utf8`
|
LL | str::from_utf8();
| ~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:17:5
|
LL | str::from::utf8_mut();
| ^^^^^^^^^
|
help: you might have meant to use `from_utf8_mut`
|
LL | str::from_utf8_mut();
| ~~~~~~~~~~~~~

error: aborting due to 6 previous errors

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