@@ -8,7 +8,9 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
88use rustc_hir:: def_id:: DefId ;
99use rustc_hir:: intravisit:: VisitorExt ;
1010use rustc_hir:: lang_items:: LangItem ;
11- use rustc_hir:: { self as hir, AmbigArg , ExprKind , GenericArg , HirId , Node , QPath , intravisit} ;
11+ use rustc_hir:: {
12+ self as hir, AmbigArg , ExprKind , GenericArg , HirId , Node , QPath , Safety , intravisit,
13+ } ;
1214use rustc_hir_analysis:: hir_ty_lowering:: errors:: GenericsArgsErrExtend ;
1315use rustc_hir_analysis:: hir_ty_lowering:: generics:: {
1416 check_generic_arg_count_for_call, lower_generic_args,
@@ -20,7 +22,9 @@ use rustc_hir_analysis::hir_ty_lowering::{
2022use rustc_infer:: infer:: canonical:: { Canonical , OriginalQueryValues , QueryResponse } ;
2123use rustc_infer:: infer:: { DefineOpaqueTypes , InferResult } ;
2224use rustc_lint:: builtin:: SELF_CONSTRUCTOR_FROM_OUTER_ITEM ;
23- use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow , AutoBorrowMutability } ;
25+ use rustc_middle:: ty:: adjustment:: {
26+ Adjust , Adjustment , AutoBorrow , AutoBorrowMutability , PointerCoercion ,
27+ } ;
2428use rustc_middle:: ty:: {
2529 self , AdtKind , CanonicalUserType , GenericArgsRef , GenericParamDefKind , IsIdentity ,
2630 SizedTraitKind , Ty , TyCtxt , TypeFoldable , TypeVisitable , TypeVisitableExt , UserArgs ,
@@ -336,10 +340,37 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
336340 // A reborrow has no effect before a dereference, so we can safely replace adjustments.
337341 * entry. get_mut ( ) = adj;
338342 }
343+ (
344+ & mut [
345+ Adjustment {
346+ kind :
347+ Adjust :: Pointer ( PointerCoercion :: ClosureFnPointer ( Safety :: Safe ) ) ,
348+ target : intermediate_target,
349+ ..
350+ } ,
351+ ] ,
352+ & [
353+ Adjustment {
354+ kind : Adjust :: Pointer ( PointerCoercion :: UnsafeFnPointer ) ,
355+ ..
356+ } ,
357+ ] ,
358+ ) => {
359+ // dont allow coerce `unsafe fn()` to `fn()`
360+ self . dcx ( )
361+ . struct_span_err (
362+ expr. span ,
363+ format ! (
364+ "cannot coerce between `{}` and unsafe function pointers" ,
365+ intermediate_target
366+ ) ,
367+ )
368+ . emit ( ) ;
369+ }
339370
340371 _ => {
341372 // FIXME: currently we never try to compose autoderefs
342- // and ReifyFnPointer/UnsafeFnPointer , but we could.
373+ // and ReifyFnPointer, but we could.
343374 self . dcx ( ) . span_delayed_bug (
344375 expr. span ,
345376 format ! (
0 commit comments