Skip to content

Commit

Permalink
Rollup merge of #129288 - compiler-errors:unsafe-fn-coercion, r=lcnr
Browse files Browse the repository at this point in the history
Use subtyping for `UnsafeFnPointer` coercion, too

I overlooked this in #129059, which changed MIR typechecking to use subtyping for other fn pointer coercions.

Fixes #129285
  • Loading branch information
matthiaskrgr authored Aug 25, 2024
2 parents c6db1ca + a97b41f commit 621f272
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,9 +2043,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

let ty_fn_ptr_from = tcx.safe_to_unsafe_fn_ty(fn_sig);

if let Err(terr) = self.eq_types(
*ty,
if let Err(terr) = self.sub_types(
ty_fn_ptr_from,
*ty,
location.to_locations(),
ConstraintCategory::Cast { unsize_to: None },
) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/coercion/cast-higher-ranked-unsafe-fn-ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ check-pass

fn higher_ranked_fndef(ctx: &mut ()) {}

fn test(higher_ranked_fnptr: fn(&mut ())) {
fn as_unsafe<T>(_: unsafe fn(T)) {}

// Make sure that we can cast higher-ranked fn items and pointers to
// a non-higher-ranked target.
as_unsafe(higher_ranked_fndef);
as_unsafe(higher_ranked_fnptr);
}

fn main() {}

0 comments on commit 621f272

Please sign in to comment.