@@ -91,6 +91,23 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
91
91
92
92
type CoerceResult < ' tcx > = InferResult < ' tcx , ( Vec < Adjustment < ' tcx > > , Ty < ' tcx > ) > ;
93
93
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
+
94
111
/// Coercing a mutable reference to an immutable works, while
95
112
/// coercing `&T` to `&mut T` should be forbidden.
96
113
fn coerce_mutbls < ' tcx > (
@@ -843,12 +860,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
843
860
match b. kind ( ) {
844
861
ty:: FnPtr ( b_sig) => {
845
862
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) ;
846
865
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
-
852
866
// Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396).
853
867
854
868
if b_sig. safety ( ) == hir:: Safety :: Safe
@@ -1150,10 +1164,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1150
1164
}
1151
1165
} ;
1152
1166
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) ;
1157
1170
// The signature must match.
1158
1171
let ( a_sig, b_sig) = self . normalize ( new. span , ( a_sig, b_sig) ) ;
1159
1172
let sig = self
0 commit comments