Suggest setting type param on function call

This commit is contained in:
Esteban Küber 2019-12-10 11:50:18 -08:00
parent 2924d38dd9
commit cc1ab3db8b
2 changed files with 28 additions and 1 deletions

View file

@ -1994,6 +1994,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
err.note(&format!("cannot resolve `{}`", predicate));
if let (Ok(ref snippet), ObligationCauseCode::BindingObligation(ref def_id, _)) = (
self.tcx.sess.source_map().span_to_snippet(span),
&obligation.cause.code,
) {
let generics = self.tcx.generics_of(*def_id);
if !generics.params.is_empty() {
err.span_suggestion(
span,
&format!(
"consider specifying the type argument{} in the function call",
if generics.params.len() > 1 {
"s"
} else {
""
},
),
format!("{}::<{}>", snippet, generics.params.iter()
.map(|p| p.name.to_string())
.collect::<Vec<String>>()
.join(", ")),
Applicability::HasPlaceholders,
);
}
}
err
}

View file

@ -5,7 +5,10 @@ LL | fn foo<T: Into<String>>(x: i32) {}
| --- ------------ required by this bound in `foo`
...
LL | foo(42);
| ^^^ cannot infer type for `T`
| ^^^
| |
| cannot infer type for `T`
| help: consider specifying the type argument in the function call: `foo::<T>`
|
= note: cannot resolve `_: std::convert::Into<std::string::String>`