]> git.lizzy.rs Git - rust.git/commitdiff
Rework expected closure error
authorEsteban Küber <esteban@kuber.com.ar>
Mon, 18 Dec 2017 06:22:24 +0000 (22:22 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Mon, 18 Dec 2017 16:55:52 +0000 (08:55 -0800)
* point at def span
* add label to primary span
* use `span_label`s instead of `span_note`s

src/librustc/traits/error_reporting.rs
src/test/ui/closure_context/issue-26046-fn-mut.stderr
src/test/ui/closure_context/issue-26046-fn-once.stderr
src/test/ui/unboxed-closures-infer-fn-once-move-from-projection.stderr

index 635921134be779a7858a86ba55a42c12e9364252..f249a6131aec450fe0e7de9e8a51053d5b6dbbec 100644 (file)
@@ -647,7 +647,8 @@ pub fn report_selection_error(&self,
 
                     ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
                         let found_kind = self.closure_kind(closure_def_id, closure_substs).unwrap();
-                        let closure_span = self.tcx.hir.span_if_local(closure_def_id).unwrap();
+                        let closure_span = self.tcx.sess.codemap()
+                            .def_span(self.tcx.hir.span_if_local(closure_def_id).unwrap());
                         let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
                         let mut err = struct_span_err!(
                             self.tcx.sess, closure_span, E0525,
@@ -656,6 +657,9 @@ pub fn report_selection_error(&self,
                             kind,
                             found_kind);
 
+                        err.span_label(
+                            closure_span,
+                            format!("this closure implements `{}`, not `{}`", found_kind, kind));
                         err.span_label(
                             obligation.cause.span,
                             format!("the requirement to implement `{}` derives from here", kind));
@@ -667,12 +671,12 @@ pub fn report_selection_error(&self,
                             let closure_hir_id = self.tcx.hir.node_to_hir_id(node_id);
                             match (found_kind, tables.closure_kind_origins().get(closure_hir_id)) {
                                 (ty::ClosureKind::FnOnce, Some((span, name))) => {
-                                    err.span_note(*span, &format!(
+                                    err.span_label(*span, format!(
                                         "closure is `FnOnce` because it moves the \
                                          variable `{}` out of its environment", name));
                                 },
                                 (ty::ClosureKind::FnMut, Some((span, name))) => {
-                                    err.span_note(*span, &format!(
+                                    err.span_label(*span, format!(
                                         "closure is `FnMut` because it mutates the \
                                          variable `{}` here", name));
                                 },
index 82c83da4daec78ac7ff2220a113d2f3575bad740..77ce1176b5cd0f964275ab6be2570804d877b7af 100644 (file)
@@ -1,20 +1,13 @@
 error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
   --> $DIR/issue-26046-fn-mut.rs:14:19
    |
-14 |       let closure = || { //~ ERROR expected a closure that
-   |  ___________________^
-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
-   |
+14 |     let closure = || { //~ ERROR expected a closure that
+   |                   ^^ this closure implements `FnMut`, not `Fn`
 15 |         num += 1;
-   |         ^^^
+   |         --- closure is `FnMut` because it mutates the variable `num` here
+...
+18 |     Box::new(closure)
+   |     ----------------- the requirement to implement `Fn` derives from here
 
 error: aborting due to previous error
 
index 0bc84872dde5f38ba3a7c9542bf258150639468b..4eed4461ebafe9037845e833174a3c7704d5508e 100644 (file)
@@ -1,20 +1,13 @@
 error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
   --> $DIR/issue-26046-fn-once.rs:14:19
    |
-14 |       let closure = move || { //~ ERROR expected a closure
-   |  ___________________^
-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
-   |
+14 |     let closure = move || { //~ ERROR expected a closure
+   |                   ^^^^^^^ this closure implements `FnOnce`, not `Fn`
 15 |         vec
-   |         ^^^
+   |         --- closure is `FnOnce` because it moves the variable `vec` out of its environment
+...
+18 |     Box::new(closure)
+   |     ----------------- the requirement to implement `Fn` derives from here
 
 error: aborting due to previous error
 
index d3d9ce2b34a1c355fe4b9a6f81d42501ad842062..2c16c5b619a8bd387371e5616b18896e3b4f46ea 100644 (file)
@@ -2,15 +2,12 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
   --> $DIR/unboxed-closures-infer-fn-once-move-from-projection.rs:24:13
    |
 24 |     let c = || drop(y.0); //~ ERROR expected a closure that implements the `Fn` trait
-   |             ^^^^^^^^^^^^
+   |             ^^^^^^^^-^^^
+   |             |       |
+   |             |       closure is `FnOnce` because it moves the variable `y` out of its environment
+   |             this closure implements `FnOnce`, not `Fn`
 25 |     foo(c);
    |     --- the requirement to implement `Fn` derives from here
-   |
-note: closure is `FnOnce` because it moves the variable `y` out of its environment
-  --> $DIR/unboxed-closures-infer-fn-once-move-from-projection.rs:24:21
-   |
-24 |     let c = || drop(y.0); //~ ERROR expected a closure that implements the `Fn` trait
-   |                     ^
 
 error: aborting due to previous error