@@ -91,6 +91,23 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
9191
9292type CoerceResult < ' tcx > = InferResult < ' tcx , ( Vec < Adjustment < ' tcx > > , Ty < ' tcx > ) > ;
9393
94+ /// Make any adjustments necessary for a function signature to be compatible
95+ /// with reification to a `fn` pointer. In particular, intrinsics are imported
96+ /// using pseudo-ABIs (`extern "rust-intrinsic" {...}`) currently, but that's
97+ /// an implementation detail and any `fn` pointers that may be taken to them
98+ /// should be indistinguishable from those to regular Rust functions, in order
99+ /// to allow e.g. libcore public APIs to be replaced with intrinsics, without
100+ /// breaking code that was, explicitly or implicitly, creating `fn` pointers.
101+ // FIXME(eddyb) intrinsics shouldn't use pseudo-ABIs, but rather the Rust ABI
102+ // and some other way to indicate that they are intrinsics (e.g. new attributes).
103+ fn prepare_fn_sig_for_reify < ' tcx > ( mut sig : ty:: FnSig < ' tcx > ) -> ty:: FnSig < ' tcx > {
104+ if matches ! ( sig. abi, Abi :: RustIntrinsic ) {
105+ sig. abi = Abi :: Rust ;
106+ }
107+
108+ sig
109+ }
110+
94111/// Coercing a mutable reference to an immutable works, while
95112/// coercing `&T` to `&mut T` should be forbidden.
96113fn coerce_mutbls < ' tcx > (
@@ -843,12 +860,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
843860 match b. kind ( ) {
844861 ty:: FnPtr ( b_sig) => {
845862 let a_sig = a. fn_sig ( self . tcx ) ;
863+ // NOTE(eddyb) see comment on `prepare_fn_sig_for_reify`.
864+ let a_sig = a_sig. map_bound ( prepare_fn_sig_for_reify) ;
846865 if let ty:: FnDef ( def_id, _) = * a. kind ( ) {
847- // Intrinsics are not coercible to function pointers
848- if self . tcx . intrinsic ( def_id) . is_some ( ) {
849- return Err ( TypeError :: IntrinsicCast ) ;
850- }
851-
852866 // Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396).
853867
854868 if b_sig. safety ( ) == hir:: Safety :: Safe
@@ -1150,10 +1164,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11501164 }
11511165 } ;
11521166 if let ( Some ( a_sig) , Some ( b_sig) ) = ( a_sig, b_sig) {
1153- // Intrinsics are not coercible to function pointers.
1154- if a_sig. abi ( ) == Abi :: RustIntrinsic || b_sig. abi ( ) == Abi :: RustIntrinsic {
1155- return Err ( TypeError :: IntrinsicCast ) ;
1156- }
1167+ // NOTE(eddyb) see comment on `prepare_fn_sig_for_reify`.
1168+ let a_sig = a_sig. map_bound ( prepare_fn_sig_for_reify) ;
1169+ let b_sig = b_sig. map_bound ( prepare_fn_sig_for_reify) ;
11571170 // The signature must match.
11581171 let ( a_sig, b_sig) = self . normalize ( new. span , ( a_sig, b_sig) ) ;
11591172 let sig = self
0 commit comments