Skip to content

Commit c2ba4b1

Browse files
authored
Rollup merge of #128082 - compiler-errors:closure-cap, r=estebank
Note closure captures when reporting cast to fn ptr failed Fixes #128078 We already had logic to point out a closure having captures when that's possibly the source of a coercion error to `fn()`, but we weren't reporting it during an explicit `as` cast.
2 parents 417bdc7 + b7495b4 commit c2ba4b1

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)