Skip to content

Commit 5497ba1

Browse files
committed
When suggesting assoc function with type params, include turbofish
1 parent f2023ac commit 5497ba1

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/librustc_typeck/check/method/suggest.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
462462
}
463463
if static_sources.len() == 1 {
464464
if let SelfSource::MethodCall(expr) = source {
465-
err.span_suggestion(expr.span.to(span),
466-
"use associated function syntax instead",
467-
format!("{}::{}",
468-
self.ty_to_string(actual),
469-
item_name),
470-
Applicability::MachineApplicable);
465+
err.span_suggestion(
466+
expr.span.to(span),
467+
"use associated function syntax instead",
468+
format!("{}::{}", self.ty_to_value_string(actual), item_name),
469+
Applicability::MachineApplicable,
470+
);
471471
} else {
472-
err.help(&format!("try with `{}::{}`",
473-
self.ty_to_string(actual), item_name));
472+
err.help(&format!(
473+
"try with `{}::{}`",
474+
self.ty_to_value_string(actual),
475+
item_name,
476+
));
474477
}
475478

476479
report_candidates(span, &mut err, static_sources);
@@ -579,6 +582,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
579582
None
580583
}
581584

585+
/// Print out the type for use in value namespace.
586+
fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
587+
match ty.kind {
588+
ty::Adt(def, substs) => format!("{}", ty::Instance::new(def.did, substs)),
589+
_ => self.ty_to_string(ty),
590+
}
591+
}
592+
582593
fn suggest_use_candidates(&self,
583594
err: &mut DiagnosticBuilder<'_>,
584595
mut msg: String,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct GenericAssocMethod<T>(T);
2+
3+
impl<T> GenericAssocMethod<T> {
4+
fn default_hello() {}
5+
}
6+
7+
fn main() {
8+
let x = GenericAssocMethod(33i32);
9+
x.default_hello();
10+
//~^ ERROR no method named `default_hello` found for type `GenericAssocMethod<i32>`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0599]: no method named `default_hello` found for type `GenericAssocMethod<i32>` in the current scope
2+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:9:7
3+
|
4+
LL | struct GenericAssocMethod<T>(T);
5+
| -------------------------------- method `default_hello` not found for this
6+
...
7+
LL | x.default_hello();
8+
| --^^^^^^^^^^^^^
9+
| | |
10+
| | this is an associated function, not a method
11+
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::default_hello`
12+
|
13+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
14+
note: the candidate is defined in an impl for the type `GenericAssocMethod<_>`
15+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:4:5
16+
|
17+
LL | fn default_hello() {}
18+
| ^^^^^^^^^^^^^^^^^^
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)