1
Fork 0

review comments

This commit is contained in:
Esteban Küber 2019-08-08 12:32:18 -07:00
parent b7f7756566
commit 0d53f699ea
2 changed files with 56 additions and 46 deletions

View file

@ -550,18 +550,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
assert!(e_ty.is_unit());
let ty = coerce.expected_ty();
coerce.coerce_forced_unit(self, &cause, &mut |err| {
let msg = "give it a value of the expected type";
let label = destination.label
.map(|l| format!(" {}", l.ident))
.unwrap_or_else(String::new);
let sugg = format!("break{} {}", label, match ty.sty {
let val = match ty.sty {
ty::Bool => "true",
ty::Char => "'a'",
ty::Int(_) | ty::Uint(_) => "42",
ty::Float(_) => "3.14159",
ty::Error | ty::Never => return,
_ => "value",
});
};
let msg = "give it a value of the expected type";
let label = destination.label
.map(|l| format!(" {}", l.ident))
.unwrap_or_else(String::new);
let sugg = format!("break{} {}", label, val);
err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders);
}, false);
}

View file

@ -3819,6 +3819,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pointing_at_return_type
}
/// When encountering an fn-like ctor that needs to unify with a value, check whether calling
/// the ctor would successfully solve the type mismatch and if so, suggest it:
/// ```
/// fn foo(x: usize) -> usize { x }
/// let x: usize = foo; // suggest calling the `foo` function: `foo(42)`
/// ```
fn suggest_fn_call(
&self,
err: &mut DiagnosticBuilder<'tcx>,
@ -3826,7 +3832,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
found: Ty<'tcx>,
) -> bool {
if let ty::FnDef(..) | ty::FnPtr(_) = &found.sty {
match found.sty {
ty::FnDef(..) | ty::FnPtr(_) => {}
_ => return false,
}
let sig = found.fn_sig(self.tcx);
let sig = self
.replace_bound_vars_with_fresh_vars(expr.span, infer::FnCall, &sig)
@ -3869,7 +3879,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return true;
}
}
}
false
}