diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 73753382a7c..c8e99c0354a 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -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(); diff --git a/src/test/ui/closure_context/issue-26046-fn-mut.stderr b/src/test/ui/closure_context/issue-26046-fn-mut.stderr index 4dd33ef8a0e..dbf702e4503 100644 --- a/src/test/ui/closure_context/issue-26046-fn-mut.stderr +++ b/src/test/ui/closure_context/issue-26046-fn-mut.stderr @@ -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 diff --git a/src/test/ui/closure_context/issue-26046-fn-once.stderr b/src/test/ui/closure_context/issue-26046-fn-once.stderr index 4a7aae87439..3ec3f0cc9aa 100644 --- a/src/test/ui/closure_context/issue-26046-fn-once.stderr +++ b/src/test/ui/closure_context/issue-26046-fn-once.stderr @@ -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