Rollup merge of #129288 - compiler-errors:unsafe-fn-coercion, r=lcnr

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
This commit is contained in:
Matthias Krüger 2024-08-26 01:49:00 +02:00 committed by GitHub
commit 621f2726fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -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 },
) {

View File

@ -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() {}