@@ -28,7 +28,7 @@ use rustc_hir as hir;
2828use rustc_hir:: def:: { DefKind , Res } ;
2929use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LocalDefId } ;
3030use rustc_hir:: intravisit:: FnKind as HirFnKind ;
31- use rustc_hir:: { Body , FnDecl , GenericParamKind , PatKind , PredicateOrigin } ;
31+ use rustc_hir:: { Body , FnDecl , PatKind , PredicateOrigin } ;
3232use rustc_middle:: bug;
3333use rustc_middle:: lint:: LevelAndSource ;
3434use rustc_middle:: ty:: layout:: LayoutOf ;
@@ -952,36 +952,34 @@ declare_lint! {
952952
953953declare_lint_pass ! ( InvalidNoMangleItems => [ NO_MANGLE_CONST_ITEMS , NO_MANGLE_GENERIC_ITEMS ] ) ;
954954
955+ impl InvalidNoMangleItems {
956+ fn check_no_mangle_on_generic_fn (
957+ & self ,
958+ cx : & LateContext < ' _ > ,
959+ attr_span : Span ,
960+ def_id : LocalDefId ,
961+ ) {
962+ let generics = cx. tcx . generics_of ( def_id) ;
963+ if generics. requires_monomorphization ( cx. tcx ) {
964+ cx. emit_span_lint (
965+ NO_MANGLE_GENERIC_ITEMS ,
966+ cx. tcx . def_span ( def_id) ,
967+ BuiltinNoMangleGeneric { suggestion : attr_span } ,
968+ ) ;
969+ }
970+ }
971+ }
972+
955973impl < ' tcx > LateLintPass < ' tcx > for InvalidNoMangleItems {
956974 fn check_item ( & mut self , cx : & LateContext < ' _ > , it : & hir:: Item < ' _ > ) {
957975 let attrs = cx. tcx . hir_attrs ( it. hir_id ( ) ) ;
958- let check_no_mangle_on_generic_fn = |attr_span : Span ,
959- impl_generics : Option < & hir:: Generics < ' _ > > ,
960- generics : & hir:: Generics < ' _ > ,
961- span| {
962- for param in
963- generics. params . iter ( ) . chain ( impl_generics. map ( |g| g. params ) . into_iter ( ) . flatten ( ) )
964- {
965- match param. kind {
966- GenericParamKind :: Lifetime { .. } => { }
967- GenericParamKind :: Type { .. } | GenericParamKind :: Const { .. } => {
968- cx. emit_span_lint (
969- NO_MANGLE_GENERIC_ITEMS ,
970- span,
971- BuiltinNoMangleGeneric { suggestion : attr_span } ,
972- ) ;
973- break ;
974- }
975- }
976- }
977- } ;
978976 match it. kind {
979- hir:: ItemKind :: Fn { generics , .. } => {
977+ hir:: ItemKind :: Fn { .. } => {
980978 if let Some ( attr_span) =
981979 find_attr ! ( attrs, AttributeKind :: ExportName { span, ..} => * span)
982980 . or_else ( || find_attr ! ( attrs, AttributeKind :: NoMangle ( span) => * span) )
983981 {
984- check_no_mangle_on_generic_fn ( attr_span , None , generics , it. span ) ;
982+ self . check_no_mangle_on_generic_fn ( cx , attr_span , it. owner_id . def_id ) ;
985983 }
986984 }
987985 hir:: ItemKind :: Const ( ..) => {
@@ -1006,24 +1004,19 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
10061004 ) ;
10071005 }
10081006 }
1009- hir:: ItemKind :: Impl ( hir:: Impl { generics, items, .. } ) => {
1010- for it in * items {
1011- if let hir:: AssocItemKind :: Fn { .. } = it. kind {
1012- let attrs = cx. tcx . hir_attrs ( it. id . hir_id ( ) ) ;
1013- if let Some ( attr_span) =
1014- find_attr ! ( attrs, AttributeKind :: ExportName { span, ..} => * span)
1015- . or_else (
1016- || find_attr ! ( attrs, AttributeKind :: NoMangle ( span) => * span) ,
1017- )
1018- {
1019- check_no_mangle_on_generic_fn (
1020- attr_span,
1021- Some ( generics) ,
1022- cx. tcx . hir_get_generics ( it. id . owner_id . def_id ) . unwrap ( ) ,
1023- it. span ,
1024- ) ;
1025- }
1026- }
1007+ _ => { }
1008+ }
1009+ }
1010+
1011+ fn check_impl_item ( & mut self , cx : & LateContext < ' _ > , it : & hir:: ImplItem < ' _ > ) {
1012+ let attrs = cx. tcx . hir_attrs ( it. hir_id ( ) ) ;
1013+ match it. kind {
1014+ hir:: ImplItemKind :: Fn { .. } => {
1015+ if let Some ( attr_span) =
1016+ find_attr ! ( attrs, AttributeKind :: ExportName { span, ..} => * span)
1017+ . or_else ( || find_attr ! ( attrs, AttributeKind :: NoMangle ( span) => * span) )
1018+ {
1019+ self . check_no_mangle_on_generic_fn ( cx, attr_span, it. owner_id . def_id ) ;
10271020 }
10281021 }
10291022 _ => { }
0 commit comments