Cover all cases in closure errors

This commit is contained in:
Tommy Ip 2017-06-07 21:26:28 +01:00
parent b1b6490c5d
commit 345b8332bd
3 changed files with 12 additions and 5 deletions

View File

@ -648,6 +648,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
kind,
found_kind);
err.span_label(
obligation.cause.span,
format!("the requirement to implement `{}` derives from here", kind));
let infer_tables = match self.tables {
InferTables::Interned(tables) =>
Some(InferTablesRef::Interned(tables)),
@ -656,6 +660,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
InferTables::Missing => None,
};
// Additional context information explaining why the closure only implements
// a particular trait.
if let Some(tables) = infer_tables {
match tables.closure_kinds.get(&node_id) {
Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) => {
@ -670,11 +676,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
},
_ => {}
}
} else {
err.span_note(
obligation.cause.span,
&format!("the requirement to implement `{}` \
derives from here", kind));
}
err.emit();

View File

@ -6,6 +6,9 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
15 | | num += 1;
16 | | };
| |_____^
17 |
18 | Box::new(closure)
| ----------------- the requirement to implement `Fn` derives from here
|
note: closure is `FnMut` because it mutates the variable `num` here
--> $DIR/issue-26046-fn-mut.rs:15:9

View File

@ -6,6 +6,9 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
15 | | vec
16 | | };
| |_____^
17 |
18 | Box::new(closure)
| ----------------- the requirement to implement `Fn` derives from here
|
note: closure is `FnOnce` because it moves the variable `vec` out of its environment
--> $DIR/issue-26046-fn-once.rs:15:9