Skip to content

Commit cc1ab3d

Browse files
committed
Suggest setting type param on function call
1 parent 2924d38 commit cc1ab3d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19941994
}
19951995
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
19961996
err.note(&format!("cannot resolve `{}`", predicate));
1997+
if let (Ok(ref snippet), ObligationCauseCode::BindingObligation(ref def_id, _)) = (
1998+
self.tcx.sess.source_map().span_to_snippet(span),
1999+
&obligation.cause.code,
2000+
) {
2001+
let generics = self.tcx.generics_of(*def_id);
2002+
if !generics.params.is_empty() {
2003+
err.span_suggestion(
2004+
span,
2005+
&format!(
2006+
"consider specifying the type argument{} in the function call",
2007+
if generics.params.len() > 1 {
2008+
"s"
2009+
} else {
2010+
""
2011+
},
2012+
),
2013+
format!("{}::<{}>", snippet, generics.params.iter()
2014+
.map(|p| p.name.to_string())
2015+
.collect::<Vec<String>>()
2016+
.join(", ")),
2017+
Applicability::HasPlaceholders,
2018+
);
2019+
}
2020+
}
19972021
err
19982022
}
19992023

src/test/ui/type/type-annotation-needed.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | fn foo<T: Into<String>>(x: i32) {}
55
| --- ------------ required by this bound in `foo`
66
...
77
LL | foo(42);
8-
| ^^^ cannot infer type for `T`
8+
| ^^^
9+
| |
10+
| cannot infer type for `T`
11+
| help: consider specifying the type argument in the function call: `foo::<T>`
912
|
1013
= note: cannot resolve `_: std::convert::Into<std::string::String>`
1114

0 commit comments

Comments
 (0)