Skip to content

Commit 345b833

Browse files
committed
Cover all cases in closure errors
1 parent b1b6490 commit 345b833

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/librustc/traits/error_reporting.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
648648
kind,
649649
found_kind);
650650

651+
err.span_label(
652+
obligation.cause.span,
653+
format!("the requirement to implement `{}` derives from here", kind));
654+
651655
let infer_tables = match self.tables {
652656
InferTables::Interned(tables) =>
653657
Some(InferTablesRef::Interned(tables)),
@@ -656,6 +660,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
656660
InferTables::Missing => None,
657661
};
658662

663+
// Additional context information explaining why the closure only implements
664+
// a particular trait.
659665
if let Some(tables) = infer_tables {
660666
match tables.closure_kinds.get(&node_id) {
661667
Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) => {
@@ -670,11 +676,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
670676
},
671677
_ => {}
672678
}
673-
} else {
674-
err.span_note(
675-
obligation.cause.span,
676-
&format!("the requirement to implement `{}` \
677-
derives from here", kind));
678679
}
679680

680681
err.emit();

src/test/ui/closure_context/issue-26046-fn-mut.stderr

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

src/test/ui/closure_context/issue-26046-fn-once.stderr

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

0 commit comments

Comments
 (0)