@@ -10,7 +10,7 @@ use std::collections::hash_map::Entry;
1010
1111use rustc_abi:: { Align , ExternAbi , Size } ;
1212use rustc_ast:: { AttrStyle , LitKind , MetaItemInner , MetaItemKind , MetaItemLit , ast} ;
13- use rustc_attr_parsing:: { AttributeKind , ReprAttr , find_attr} ;
13+ use rustc_attr_parsing:: { AttributeKind , InlineAttr , ReprAttr , find_attr} ;
1414use rustc_data_structures:: fx:: FxHashMap ;
1515use rustc_errors:: { Applicability , DiagCtxtHandle , IntoDiagArg , MultiSpan , StashKey } ;
1616use rustc_feature:: { AttributeDuplicates , AttributeType , BUILTIN_ATTRIBUTE_MAP , BuiltinAttribute } ;
@@ -124,6 +124,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124 AttributeKind :: Stability { span, .. }
125125 | AttributeKind :: ConstStability { span, .. } ,
126126 ) => self . check_stability_promotable ( * span, target) ,
127+ Attribute :: Parsed ( AttributeKind :: Inline ( _, attr_span) ) => {
128+ self . check_inline ( hir_id, * attr_span, span, target)
129+ }
127130 Attribute :: Parsed ( AttributeKind :: AllowInternalUnstable ( syms) ) => self
128131 . check_allow_internal_unstable (
129132 hir_id,
@@ -140,7 +143,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
140143 [ sym:: diagnostic, sym:: on_unimplemented, ..] => {
141144 self . check_diagnostic_on_unimplemented ( attr. span ( ) , hir_id, target)
142145 }
143- [ sym:: inline, ..] => self . check_inline ( hir_id, attr, span, target) ,
144146 [ sym:: coverage, ..] => self . check_coverage ( attr, span, target) ,
145147 [ sym:: optimize, ..] => self . check_optimize ( hir_id, attr, span, target) ,
146148 [ sym:: no_sanitize, ..] => {
@@ -351,11 +353,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
351353 self . check_rustc_force_inline ( hir_id, attrs, span, target) ;
352354 }
353355
354- fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr : & Attribute , sym : & str ) {
356+ fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr_span : Span , sym : & str ) {
355357 self . tcx . emit_node_span_lint (
356358 UNUSED_ATTRIBUTES ,
357359 hir_id,
358- attr . span ( ) ,
360+ attr_span ,
359361 errors:: IgnoredAttrWithMacro { sym } ,
360362 ) ;
361363 }
@@ -415,7 +417,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
415417 }
416418
417419 /// Checks if an `#[inline]` is applied to a function or a closure.
418- fn check_inline ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
420+ fn check_inline ( & self , hir_id : HirId , attr_span : Span , defn_span : Span , target : Target ) {
419421 match target {
420422 Target :: Fn
421423 | Target :: Closure
@@ -424,7 +426,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
424426 self . tcx . emit_node_span_lint (
425427 UNUSED_ATTRIBUTES ,
426428 hir_id,
427- attr . span ( ) ,
429+ attr_span ,
428430 errors:: IgnoredInlineAttrFnProto ,
429431 )
430432 }
@@ -435,18 +437,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
435437 Target :: AssocConst => self . tcx . emit_node_span_lint (
436438 UNUSED_ATTRIBUTES ,
437439 hir_id,
438- attr . span ( ) ,
440+ attr_span ,
439441 errors:: IgnoredInlineAttrConstants ,
440442 ) ,
441443 // FIXME(#80564): Same for fields, arms, and macro defs
442444 Target :: Field | Target :: Arm | Target :: MacroDef => {
443- self . inline_attr_str_error_with_macro_def ( hir_id, attr , "inline" )
445+ self . inline_attr_str_error_with_macro_def ( hir_id, attr_span , "inline" )
444446 }
445447 _ => {
446- self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure {
447- attr_span : attr. span ( ) ,
448- defn_span : span,
449- } ) ;
448+ self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure { attr_span, defn_span } ) ;
450449 }
451450 }
452451 }
@@ -640,7 +639,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
640639 // erroneously allowed it and some crates used it accidentally, to be compatible
641640 // with crates depending on them, we can't throw an error here.
642641 Target :: Field | Target :: Arm | Target :: MacroDef => {
643- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "naked" )
642+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "naked" )
644643 }
645644 _ => {
646645 self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
@@ -718,7 +717,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
718717 // with crates depending on them, we can't throw an error here.
719718 Target :: Field | Target :: Arm | Target :: MacroDef => {
720719 for attr in attrs {
721- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "track_caller" ) ;
720+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "track_caller" ) ;
722721 }
723722 }
724723 _ => {
@@ -761,7 +760,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
761760 // erroneously allowed it and some crates used it accidentally, to be compatible
762761 // with crates depending on them, we can't throw an error here.
763762 Target :: Field | Target :: Arm | Target :: MacroDef => {
764- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "non_exhaustive" ) ;
763+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "non_exhaustive" ) ;
765764 }
766765 _ => {
767766 self . dcx ( ) . emit_err ( errors:: NonExhaustiveWrongLocation {
@@ -781,7 +780,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
781780 // erroneously allowed it and some crates used it accidentally, to be compatible
782781 // with crates depending on them, we can't throw an error here.
783782 Target :: Field | Target :: Arm | Target :: MacroDef => {
784- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "marker" ) ;
783+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "marker" ) ;
785784 }
786785 _ => {
787786 self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToTrait {
@@ -835,7 +834,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
835834 // erroneously allowed it and some crates used it accidentally, to be compatible
836835 // with crates depending on them, we can't throw an error here.
837836 Target :: Field | Target :: Arm | Target :: MacroDef => {
838- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "target_feature" ) ;
837+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "target_feature" ) ;
839838 }
840839 _ => {
841840 self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
@@ -1546,7 +1545,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15461545 // erroneously allowed it and some crates used it accidentally, to be compatible
15471546 // with crates depending on them, we can't throw an error here.
15481547 Target :: Field | Target :: Arm | Target :: MacroDef => {
1549- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "cold" ) ;
1548+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "cold" ) ;
15501549 }
15511550 _ => {
15521551 // FIXME: #[cold] was previously allowed on non-functions and some crates used
@@ -1588,7 +1587,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15881587 // erroneously allowed it and some crates used it accidentally, to be compatible
15891588 // with crates depending on them, we can't throw an error here.
15901589 Target :: Field | Target :: Arm | Target :: MacroDef => {
1591- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_name" ) ;
1590+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_name" ) ;
15921591 }
15931592 _ => {
15941593 // FIXME: #[cold] was previously allowed on non-functions/statics and some crates
@@ -1622,7 +1621,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16221621 // erroneously allowed it and some crates used it accidentally, to be compatible
16231622 // with crates depending on them, we can't throw an error here.
16241623 Target :: Field | Target :: Arm | Target :: MacroDef => {
1625- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_link" ) ;
1624+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_link" ) ;
16261625 }
16271626 _ => {
16281627 self . dcx ( ) . emit_err ( errors:: NoLink { attr_span : attr. span ( ) , span } ) ;
@@ -1644,7 +1643,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16441643 // erroneously allowed it and some crates used it accidentally, to be compatible
16451644 // with crates depending on them, we can't throw an error here.
16461645 Target :: Field | Target :: Arm | Target :: MacroDef => {
1647- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "export_name" ) ;
1646+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "export_name" ) ;
16481647 }
16491648 _ => {
16501649 self . dcx ( ) . emit_err ( errors:: ExportName { attr_span : attr. span ( ) , span } ) ;
@@ -1818,7 +1817,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18181817 // erroneously allowed it and some crates used it accidentally, to be compatible
18191818 // with crates depending on them, we can't throw an error here.
18201819 Target :: Field | Target :: Arm | Target :: MacroDef => {
1821- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_section" ) ;
1820+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_section" ) ;
18221821 }
18231822 _ => {
18241823 // FIXME: #[link_section] was previously allowed on non-functions/statics and some
@@ -1843,7 +1842,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18431842 // erroneously allowed it and some crates used it accidentally, to be compatible
18441843 // with crates depending on them, we can't throw an error here.
18451844 Target :: Field | Target :: Arm | Target :: MacroDef => {
1846- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_mangle" ) ;
1845+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_mangle" ) ;
18471846 }
18481847 // FIXME: #[no_mangle] was previously allowed on non-functions/statics, this should be an error
18491848 // The error should specify that the item that is wrong is specifically a *foreign* fn/static
@@ -2190,9 +2189,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
21902189 // `#[allow_internal_unstable]` attribute with just a lint, because we previously
21912190 // erroneously allowed it and some crates used it accidentally, to be compatible
21922191 // with crates depending on them, we can't throw an error here.
2193- Target :: Field | Target :: Arm | Target :: MacroDef => {
2194- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "allow_internal_unstable" )
2195- }
2192+ Target :: Field | Target :: Arm | Target :: MacroDef => self
2193+ . inline_attr_str_error_with_macro_def (
2194+ hir_id,
2195+ attr. span ( ) ,
2196+ "allow_internal_unstable" ,
2197+ ) ,
21962198 _ => {
21972199 self . tcx
21982200 . dcx ( )
@@ -2568,8 +2570,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25682570 span : Span ,
25692571 target : Target ,
25702572 ) {
2571- let force_inline_attr = attrs. iter ( ) . find ( |attr| attr. has_name ( sym:: rustc_force_inline) ) ;
2572- match ( target, force_inline_attr) {
2573+ match (
2574+ target,
2575+ find_attr ! ( attrs, AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span) ,
2576+ ) {
25732577 ( Target :: Closure , None ) => {
25742578 let is_coro = matches ! (
25752579 self . tcx. hir( ) . expect_expr( hir_id) . kind,
@@ -2581,20 +2585,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25812585 ) ;
25822586 let parent_did = self . tcx . hir_get_parent_item ( hir_id) . to_def_id ( ) ;
25832587 let parent_span = self . tcx . def_span ( parent_did) ;
2584- let parent_force_inline_attr =
2585- self . tcx . get_attr ( parent_did, sym:: rustc_force_inline) ;
2586- if let Some ( attr) = parent_force_inline_attr
2587- && is_coro
2588+
2589+ if let Some ( attr_span) = find_attr ! (
2590+ self . tcx. get_all_attrs( parent_did) ,
2591+ AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span
2592+ ) && is_coro
25882593 {
2589- self . dcx ( ) . emit_err ( errors:: RustcForceInlineCoro {
2590- attr_span : attr. span ( ) ,
2591- span : parent_span,
2592- } ) ;
2594+ self . dcx ( )
2595+ . emit_err ( errors:: RustcForceInlineCoro { attr_span, span : parent_span } ) ;
25932596 }
25942597 }
25952598 ( Target :: Fn , _) => ( ) ,
2596- ( _, Some ( attr ) ) => {
2597- self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span : attr . span ( ) , span } ) ;
2599+ ( _, Some ( attr_span ) ) => {
2600+ self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span, span } ) ;
25982601 }
25992602 ( _, None ) => ( ) ,
26002603 }
@@ -2816,10 +2819,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28162819fn check_non_exported_macro_for_invalid_attrs ( tcx : TyCtxt < ' _ > , item : & Item < ' _ > ) {
28172820 let attrs = tcx. hir ( ) . attrs ( item. hir_id ( ) ) ;
28182821
2819- for attr in attrs {
2820- if attr. has_name ( sym:: inline) {
2821- tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span : attr. span ( ) } ) ;
2822- }
2822+ if let Some ( attr_span) = find_attr ! ( attrs, AttributeKind :: Inline ( _, span) => * span) {
2823+ tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span } ) ;
28232824 }
28242825}
28252826
@@ -2839,6 +2840,7 @@ pub(crate) fn provide(providers: &mut Providers) {
28392840 * providers = Providers { check_mod_attrs, ..* providers } ;
28402841}
28412842
2843+ // FIXME(jdonszelmann): remove, check during parsing
28422844fn check_duplicates (
28432845 tcx : TyCtxt < ' _ > ,
28442846 attr : & Attribute ,
0 commit comments