11use rustc_ast:: { ast, attr, MetaItemKind , NestedMetaItem } ;
22use rustc_attr:: { list_contains_name, InlineAttr , InstructionSetAttr , OptimizeAttr } ;
3+ use rustc_data_structures:: fx:: FxHashSet ;
34use rustc_errors:: codes:: * ;
45use rustc_errors:: { struct_span_code_err, DiagMessage , SubdiagMessage } ;
56use rustc_hir as hir;
@@ -16,8 +17,10 @@ use rustc_middle::ty::{self as ty, TyCtxt};
1617use rustc_session:: lint;
1718use rustc_session:: parse:: feature_err;
1819use rustc_span:: symbol:: Ident ;
19- use rustc_span:: { sym, Span } ;
20+ use rustc_span:: { sym, Span , Symbol } ;
21+ use rustc_target:: abi:: VariantIdx ;
2022use rustc_target:: spec:: { abi, SanitizerSet } ;
23+ use rustc_type_ir:: inherent:: * ;
2124
2225use crate :: errors;
2326use crate :: target_features:: { check_target_feature_trait_unsafe, from_target_feature} ;
@@ -78,23 +81,26 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
7881 let mut link_ordinal_span = None ;
7982 let mut no_sanitize_span = None ;
8083
84+ let fn_sig_outer = || {
85+ use DefKind :: * ;
86+
87+ let def_kind = tcx. def_kind ( did) ;
88+ if let Fn | AssocFn | Variant | Ctor ( ..) = def_kind { Some ( tcx. fn_sig ( did) ) } else { None }
89+ } ;
90+
8191 for attr in attrs. iter ( ) {
8292 // In some cases, attribute are only valid on functions, but it's the `check_attr`
8393 // pass that check that they aren't used anywhere else, rather this module.
8494 // In these cases, we bail from performing further checks that are only meaningful for
8595 // functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
8696 // report a delayed bug, just in case `check_attr` isn't doing its job.
8797 let fn_sig = || {
88- use DefKind :: * ;
89-
90- let def_kind = tcx. def_kind ( did) ;
91- if let Fn | AssocFn | Variant | Ctor ( ..) = def_kind {
92- Some ( tcx. fn_sig ( did) )
93- } else {
98+ let sig = fn_sig_outer ( ) ;
99+ if sig. is_none ( ) {
94100 tcx. dcx ( )
95101 . span_delayed_bug ( attr. span , "this attribute can only be applied to functions" ) ;
96- None
97102 }
103+ sig
98104 } ;
99105
100106 let Some ( Ident { name, .. } ) = attr. ident ( ) else {
@@ -613,6 +619,57 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
613619 }
614620 }
615621
622+ if tcx. features ( ) . struct_target_features
623+ && let Some ( sig) = fn_sig_outer ( )
624+ {
625+ // Collect target features from types reachable from arguments.
626+ // We define a type as "reachable" if:
627+ // - it is a function argument
628+ // - it is a field of a reachable struct
629+ // - there is a reachable reference to it
630+ // FIXME: we may want to cache the result of this computation.
631+ let mut visited_types = FxHashSet :: default ( ) ;
632+ let mut reachable_types: Vec < _ > = sig. skip_binder ( ) . inputs ( ) . skip_binder ( ) . to_owned ( ) ;
633+ let mut additional_tf = vec ! [ ] ;
634+
635+ while let Some ( ty) = reachable_types. pop ( ) {
636+ if visited_types. contains ( & ty) {
637+ continue ;
638+ }
639+ visited_types. insert ( ty) ;
640+ if ty. is_ref ( ) {
641+ reachable_types. push ( ty. builtin_deref ( false ) . unwrap ( ) ) ;
642+ } else if matches ! ( ty. kind( ) , ty:: Tuple ( _) ) {
643+ reachable_types. extend ( ty. tuple_fields ( ) . iter ( ) ) ;
644+ } else if let ty:: Adt ( adt_def, args) = ty. kind ( ) {
645+ additional_tf. extend_from_slice ( tcx. struct_target_features ( adt_def. did ( ) ) ) ;
646+ if adt_def. is_struct ( ) {
647+ reachable_types. extend (
648+ adt_def
649+ . variant ( VariantIdx :: from_usize ( 0 ) )
650+ . fields
651+ . iter ( )
652+ . map ( |field| field. ty ( tcx, args) ) ,
653+ ) ;
654+ }
655+ }
656+ }
657+
658+ if !additional_tf. is_empty ( ) && !sig. skip_binder ( ) . abi ( ) . is_rust ( ) {
659+ tcx. dcx ( ) . span_err (
660+ tcx. hir ( ) . span ( tcx. local_def_id_to_hir_id ( did) ) ,
661+ "cannot use a struct with target features in a function with non-Rust ABI" ,
662+ ) ;
663+ }
664+ if !additional_tf. is_empty ( ) && codegen_fn_attrs. inline == InlineAttr :: Always {
665+ tcx. dcx ( ) . span_err (
666+ tcx. hir ( ) . span ( tcx. local_def_id_to_hir_id ( did) ) ,
667+ "cannot use a struct with target features in a #[inline(always)] function" ,
668+ ) ;
669+ }
670+ codegen_fn_attrs. target_features . extend_from_slice ( & additional_tf) ;
671+ }
672+
616673 // If a function uses #[target_feature] it can't be inlined into general
617674 // purpose functions as they wouldn't have the right target features
618675 // enabled. For that reason we also forbid #[inline(always)] as it can't be
@@ -758,6 +815,20 @@ fn check_link_name_xor_ordinal(
758815 }
759816}
760817
818+ fn struct_target_features ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Vec < Symbol > {
819+ let mut features = vec ! [ ] ;
820+ let supported_features = tcx. supported_target_features ( LOCAL_CRATE ) ;
821+ for attr in tcx. get_attrs ( def_id, sym:: target_feature) {
822+ from_target_feature ( tcx, attr, supported_features, & mut features) ;
823+ }
824+ features
825+ }
826+
761827pub fn provide ( providers : & mut Providers ) {
762- * providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..* providers } ;
828+ * providers = Providers {
829+ codegen_fn_attrs,
830+ should_inherit_track_caller,
831+ struct_target_features,
832+ ..* providers
833+ } ;
763834}
0 commit comments