Skip to content

Commit c9b01f0

Browse files
committed
Fix unnecessary restrictions in struct-target-features.
Allow using struct-tf with functions with non-Rust ABI. Also allow converting struct-tf functions to function pointers / let them implement function traits.
1 parent 605a9d9 commit c9b01f0

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
1313
};
1414
use rustc_middle::mir::mono::Linkage;
1515
use rustc_middle::query::Providers;
16-
use rustc_middle::ty::{self as ty, Ty, TyCtxt};
16+
use rustc_middle::ty::{self as ty, TyCtxt};
1717
use rustc_session::parse::feature_err;
1818
use rustc_session::{Session, lint};
1919
use rustc_span::symbol::Ident;
@@ -608,13 +608,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
608608
&mut additional_tf,
609609
)
610610
}
611-
// FIXME(struct_target_features): is this really necessary?
612-
if !additional_tf.is_empty() && sig.skip_binder().abi() != abi::Abi::Rust {
613-
tcx.dcx().span_err(
614-
tcx.hir().span(tcx.local_def_id_to_hir_id(did)),
615-
"cannot use a struct with target features in a function with non-Rust ABI",
616-
);
617-
}
618611
if !additional_tf.is_empty() && codegen_fn_attrs.inline == InlineAttr::Always {
619612
tcx.dcx().span_err(
620613
tcx.hir().span(tcx.local_def_id_to_hir_id(did)),

compiler/rustc_hir_typeck/src/coercion.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -920,12 +920,15 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
920920
return Err(TypeError::IntrinsicCast);
921921
}
922922

923-
// Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396).
924-
// FIXME(struct_target_features): should this be true also for functions that inherit
925-
// target features from structs?
926-
923+
// Safe functions with explicit `#[target_feature]` attributes are not
924+
// assignable to safe fn pointers (RFC 2396).
927925
if b_hdr.safety == hir::Safety::Safe
928-
&& !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
926+
&& self
927+
.tcx
928+
.codegen_fn_attrs(def_id)
929+
.target_features
930+
.iter()
931+
.any(|x| !x.implied)
929932
{
930933
return Err(TypeError::TargetFeatureCast(def_id));
931934
}

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
464464
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
465465
*leaf_trait_ref.skip_binder().self_ty().kind()
466466
{
467-
// FIXME(struct_target_features): should a function that inherits
468-
// target_features through arguments implement Fn traits?
469-
!self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
467+
self.tcx.codegen_fn_attrs(def_id).target_features.iter().any(|x| !x.implied)
470468
} else {
471469
false
472470
};

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
546546
.push(FnPointerCandidate { fn_host_effect: self.tcx().consts.true_ });
547547
}
548548
}
549-
// Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396).
549+
// Provide an impl for suitable functions, rejecting functions with explicit
550+
// `#[target_feature]` attributes (RFC 2396).
550551
ty::FnDef(def_id, args) => {
551552
let tcx = self.tcx();
552-
// FIXME(struct_target_features): should a function that inherits target_features
553-
// through an argument implement Fn traits?
554553
if tcx.fn_sig(def_id).skip_binder().is_fn_trait_compatible()
555-
&& tcx.codegen_fn_attrs(def_id).target_features.is_empty()
554+
&& !tcx.codegen_fn_attrs(def_id).target_features.iter().any(|x| !x.implied)
556555
{
557556
candidates.vec.push(FnPointerCandidate {
558557
fn_host_effect: tcx

0 commit comments

Comments
 (0)