Skip to content

Commit

Permalink
Unrolled build for rust-lang#128082
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#128082 - compiler-errors:closure-cap, r=estebank

Note closure captures when reporting cast to fn ptr failed

Fixes rust-lang#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.
  • Loading branch information
rust-timer authored Jul 23, 2024
2 parents 84c257e + b7495b4 commit 740d40b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_label(self.span, "invalid cast");
}

fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
self.try_suggest_collection_to_bool(fcx, &mut err);

err.emit();
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/closures/closure-no-fn-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0605]: non-primitive cast: `{closure@$DIR/closure-no-fn-3.rs:6:28: 6:30}`
|
LL | let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
|
note: closures can only be coerced to `fn` types if they do not capture any variables
--> $DIR/closure-no-fn-3.rs:6:33
|
LL | let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
| ^ `b` captured here

error: aborting due to 1 previous error

Expand Down

0 comments on commit 740d40b

Please sign in to comment.