Skip to content

Commit de82fc4

Browse files
Fix obscure compilation error
1 parent e45c795 commit de82fc4

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

src/librustc/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,5 +1544,5 @@ register_diagnostics! {
15441544
E0490, // a value of type `..` is borrowed for too long
15451545
E0491, // in type `..`, reference has a longer lifetime than the data it...
15461546
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
1547-
E0524, // the closure implements `..` but not `..`
1547+
E0524, // expected a closure that implements `..` but this closure only implements `..`
15481548
}

src/librustc/traits/error_reporting.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,10 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
471471
let closure_span = infcx.tcx.map.span_if_local(closure_def_id).unwrap();
472472
let mut err = struct_span_err!(
473473
infcx.tcx.sess, closure_span, E0524,
474-
"the closure implements `{}` but not `{}`",
475-
found_kind,
476-
kind);
474+
"expected a closure that implements the `{}` trait, but this closure \
475+
only implements `{}`",
476+
kind,
477+
found_kind);
477478
err.span_note(
478479
obligation.cause.span,
479480
&format!("the requirement to implement `{}` derives from here", kind));

src/librustc_typeck/check/closure.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,16 @@ fn deduce_expectations_from_obligations<'a,'tcx>(
179179
ty::Predicate::TypeOutlives(..) => None,
180180
ty::Predicate::WellFormed(..) => None,
181181
ty::Predicate::ObjectSafe(..) => None,
182-
ty::Predicate::ClosureKind(_closure_def_id, kind) => {
183-
return Some(kind);
184-
}
182+
183+
// NB: This predicate is created by breaking down a
184+
// `ClosureType: FnFoo()` predicate, where
185+
// `ClosureType` represents some `TyClosure`. It can't
186+
// possibly be referring to the current closure,
187+
// because we haven't produced the `TyClosure` for
188+
// this closure yet; this is exactly why the other
189+
// code is looking for a self type of a unresolved
190+
// inference variable.
191+
ty::Predicate::ClosureKind(..) => None,
185192
};
186193
opt_trait_ref
187194
.and_then(|trait_ref| self_type_matches_expected_vid(fcx, trait_ref, expected_vid))

src/test/compile-fail/closure-wrong-kind.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn bar<T: Fn(u32)>(_: T) {}
1717

1818
fn main() {
1919
let x = X;
20-
let closure = |_| foo(x);
21-
//~^ ERROR the closure implements `FnOnce` but not `Fn`
20+
let closure = |_| foo(x); //~ ERROR E0524
2221
bar(closure);
2322
}

0 commit comments

Comments
 (0)