Skip to content

Commit b7495b4

Browse files
Note closure captures when reporting deferred cast to fn ptr failed
1 parent 20f23ab commit b7495b4

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
495495
err.span_label(self.span, "invalid cast");
496496
}
497497

498+
fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
498499
self.try_suggest_collection_to_bool(fcx, &mut err);
499500

500501
err.emit();

tests/ui/closures/closure-no-fn-3.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0605]: non-primitive cast: `{closure@$DIR/closure-no-fn-3.rs:6:28: 6:30}`
33
|
44
LL | let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
6+
|
7+
note: closures can only be coerced to `fn` types if they do not capture any variables
8+
--> $DIR/closure-no-fn-3.rs:6:33
9+
|
10+
LL | let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
11+
| ^ `b` captured here
612

713
error: aborting due to 1 previous error
814

0 commit comments

Comments
 (0)