Skip to content

Commit 7aff6ad

Browse files
committed
Remove extra note and revert name in message
1 parent 89ae2ca commit 7aff6ad

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/librustc/traits/error_reporting.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -883,40 +883,50 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
883883
})
884884
}
885885

886+
fn extract_type_name(&self, ty: &'a Ty<'tcx>) -> String {
887+
if let ty::TyInfer(ty::TyVar(ty_vid)) = (*ty).sty {
888+
let ty_vars = self.type_variables.borrow();
889+
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
890+
*ty_vars.var_origin(ty_vid) {
891+
name.to_string()
892+
} else {
893+
ty.to_string()
894+
}
895+
} else {
896+
ty.to_string()
897+
}
898+
}
886899

887-
fn need_type_info(&self,
888-
obligation: &PredicateObligation<'tcx>,
889-
ty: Ty<'tcx>) {
890-
900+
fn need_type_info(&self, obligation: &PredicateObligation<'tcx>, ty: Ty<'tcx>) {
891901
let ty = self.resolve_type_vars_if_possible(&ty);
902+
let name = self.extract_type_name(&ty);
892903
let ref cause = obligation.cause;
893904

894905
let mut err = struct_span_err!(self.tcx.sess,
895906
cause.span,
896907
E0282,
897-
"unable to fully infer type(s)");
908+
"type annotations needed");
898909

899-
err.note("type annotations or generic parameter binding required");
900-
err.span_label(cause.span, &format!("cannot infer type"));
910+
err.span_label(cause.span, &format!("cannot infer type for `{}`", name));
901911

902912
let expr = self.tcx.hir.expect_expr(cause.body_id);
903913

904914
let mut local_visitor = FindLocalByTypeVisitor {
905915
infcx: &self,
906916
target_ty: &ty,
907-
found_pattern: None
917+
found_pattern: None,
908918
};
909919

910920
local_visitor.visit_expr(expr);
911921

912922
if let Some(pattern) = local_visitor.found_pattern {
913923
let pattern_span = pattern.span;
914-
if let Some(n) = pattern.simple_name() {
924+
if let Some(simple_name) = pattern.simple_name() {
915925
err.span_label(pattern_span,
916-
&format!("annotating the type for the variable `{}` would help", n));
926+
&format!("consider giving `{}` a type",
927+
simple_name));
917928
} else {
918-
err.span_label(pattern_span,
919-
&format!("annotating the type of pattern would help"));
929+
err.span_label(pattern_span, &format!("consider giving a type to pattern"));
920930
}
921931
}
922932

0 commit comments

Comments
 (0)