Skip to content

Commit 0555d25

Browse files
committed
Rework expected closure error
* point at def span * add label to primary span * use `span_label`s instead of `span_note`s
1 parent 3cc68ba commit 0555d25

File tree

4 files changed

+23
-36
lines changed

4 files changed

+23
-36
lines changed

src/librustc/traits/error_reporting.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
647647

648648
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
649649
let found_kind = self.closure_kind(closure_def_id, closure_substs).unwrap();
650-
let closure_span = self.tcx.hir.span_if_local(closure_def_id).unwrap();
650+
let closure_span = self.tcx.sess.codemap()
651+
.def_span(self.tcx.hir.span_if_local(closure_def_id).unwrap());
651652
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
652653
let mut err = struct_span_err!(
653654
self.tcx.sess, closure_span, E0525,
@@ -656,6 +657,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
656657
kind,
657658
found_kind);
658659

660+
err.span_label(
661+
closure_span,
662+
format!("this closure implements `{}`, not `{}`", found_kind, kind));
659663
err.span_label(
660664
obligation.cause.span,
661665
format!("the requirement to implement `{}` derives from here", kind));
@@ -667,12 +671,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
667671
let closure_hir_id = self.tcx.hir.node_to_hir_id(node_id);
668672
match (found_kind, tables.closure_kind_origins().get(closure_hir_id)) {
669673
(ty::ClosureKind::FnOnce, Some((span, name))) => {
670-
err.span_note(*span, &format!(
674+
err.span_label(*span, format!(
671675
"closure is `FnOnce` because it moves the \
672676
variable `{}` out of its environment", name));
673677
},
674678
(ty::ClosureKind::FnMut, Some((span, name))) => {
675-
err.span_note(*span, &format!(
679+
err.span_label(*span, format!(
676680
"closure is `FnMut` because it mutates the \
677681
variable `{}` here", name));
678682
},
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
22
--> $DIR/issue-26046-fn-mut.rs:14:19
33
|
4-
14 | let closure = || { //~ ERROR expected a closure that
5-
| ___________________^
6-
15 | | num += 1;
7-
16 | | };
8-
| |_____^
9-
17 |
10-
18 | Box::new(closure)
11-
| ----------------- the requirement to implement `Fn` derives from here
12-
|
13-
note: closure is `FnMut` because it mutates the variable `num` here
14-
--> $DIR/issue-26046-fn-mut.rs:15:9
15-
|
4+
14 | let closure = || { //~ ERROR expected a closure that
5+
| ^^ this closure implements `FnMut`, not `Fn`
166
15 | num += 1;
17-
| ^^^
7+
| --- closure is `FnMut` because it mutates the variable `num` here
8+
...
9+
18 | Box::new(closure)
10+
| ----------------- the requirement to implement `Fn` derives from here
1811

1912
error: aborting due to previous error
2013

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
22
--> $DIR/issue-26046-fn-once.rs:14:19
33
|
4-
14 | let closure = move || { //~ ERROR expected a closure
5-
| ___________________^
6-
15 | | vec
7-
16 | | };
8-
| |_____^
9-
17 |
10-
18 | Box::new(closure)
11-
| ----------------- the requirement to implement `Fn` derives from here
12-
|
13-
note: closure is `FnOnce` because it moves the variable `vec` out of its environment
14-
--> $DIR/issue-26046-fn-once.rs:15:9
15-
|
4+
14 | let closure = move || { //~ ERROR expected a closure
5+
| ^^^^^^^ this closure implements `FnOnce`, not `Fn`
166
15 | vec
17-
| ^^^
7+
| --- closure is `FnOnce` because it moves the variable `vec` out of its environment
8+
...
9+
18 | Box::new(closure)
10+
| ----------------- the requirement to implement `Fn` derives from here
1811

1912
error: aborting due to previous error
2013

src/test/ui/unboxed-closures-infer-fn-once-move-from-projection.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
22
--> $DIR/unboxed-closures-infer-fn-once-move-from-projection.rs:24:13
33
|
44
24 | let c = || drop(y.0); //~ ERROR expected a closure that implements the `Fn` trait
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^^^-^^^
6+
| | |
7+
| | closure is `FnOnce` because it moves the variable `y` out of its environment
8+
| this closure implements `FnOnce`, not `Fn`
69
25 | foo(c);
710
| --- the requirement to implement `Fn` derives from here
8-
|
9-
note: closure is `FnOnce` because it moves the variable `y` out of its environment
10-
--> $DIR/unboxed-closures-infer-fn-once-move-from-projection.rs:24:21
11-
|
12-
24 | let c = || drop(y.0); //~ ERROR expected a closure that implements the `Fn` trait
13-
| ^
1411

1512
error: aborting due to previous error
1613

0 commit comments

Comments
 (0)