@@ -10,7 +10,7 @@ use std::collections::hash_map::Entry;
10
10
11
11
use rustc_abi:: { Align , ExternAbi , Size } ;
12
12
use rustc_ast:: { AttrStyle , LitKind , MetaItemInner , MetaItemKind , MetaItemLit , ast} ;
13
- use rustc_attr_data_structures:: { AttributeKind , ReprAttr , find_attr} ;
13
+ use rustc_attr_data_structures:: { AttributeKind , InlineAttr , ReprAttr , find_attr} ;
14
14
use rustc_data_structures:: fx:: FxHashMap ;
15
15
use rustc_errors:: { Applicability , DiagCtxtHandle , IntoDiagArg , MultiSpan , StashKey } ;
16
16
use rustc_feature:: { AttributeDuplicates , AttributeType , BUILTIN_ATTRIBUTE_MAP , BuiltinAttribute } ;
@@ -124,6 +124,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124
124
AttributeKind :: Stability { span, .. }
125
125
| AttributeKind :: ConstStability { span, .. } ,
126
126
) => self . check_stability_promotable ( * span, target) ,
127
+ Attribute :: Parsed ( AttributeKind :: Inline ( kind, attr_span) ) => {
128
+ self . check_inline ( hir_id, * attr_span, span, kind, target)
129
+ }
127
130
Attribute :: Parsed ( AttributeKind :: AllowInternalUnstable ( syms) ) => self
128
131
. check_allow_internal_unstable (
129
132
hir_id,
@@ -155,7 +158,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
155
158
[ sym:: diagnostic, sym:: on_unimplemented, ..] => {
156
159
self . check_diagnostic_on_unimplemented ( attr. span ( ) , hir_id, target)
157
160
}
158
- [ sym:: inline, ..] => self . check_inline ( hir_id, attr, span, target) ,
159
161
[ sym:: coverage, ..] => self . check_coverage ( attr, span, target) ,
160
162
[ sym:: optimize, ..] => self . check_optimize ( hir_id, attr, span, target) ,
161
163
[ sym:: no_sanitize, ..] => {
@@ -367,11 +369,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
367
369
self . check_rustc_force_inline ( hir_id, attrs, span, target) ;
368
370
}
369
371
370
- fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr : & Attribute , sym : & str ) {
372
+ fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr_span : Span , sym : & str ) {
371
373
self . tcx . emit_node_span_lint (
372
374
UNUSED_ATTRIBUTES ,
373
375
hir_id,
374
- attr . span ( ) ,
376
+ attr_span ,
375
377
errors:: IgnoredAttrWithMacro { sym } ,
376
378
) ;
377
379
}
@@ -431,7 +433,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
431
433
}
432
434
433
435
/// Checks if an `#[inline]` is applied to a function or a closure.
434
- fn check_inline ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
436
+ fn check_inline (
437
+ & self ,
438
+ hir_id : HirId ,
439
+ attr_span : Span ,
440
+ defn_span : Span ,
441
+ kind : & InlineAttr ,
442
+ target : Target ,
443
+ ) {
435
444
match target {
436
445
Target :: Fn
437
446
| Target :: Closure
@@ -440,7 +449,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
440
449
self . tcx . emit_node_span_lint (
441
450
UNUSED_ATTRIBUTES ,
442
451
hir_id,
443
- attr . span ( ) ,
452
+ attr_span ,
444
453
errors:: IgnoredInlineAttrFnProto ,
445
454
)
446
455
}
@@ -451,33 +460,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
451
460
Target :: AssocConst => self . tcx . emit_node_span_lint (
452
461
UNUSED_ATTRIBUTES ,
453
462
hir_id,
454
- attr . span ( ) ,
463
+ attr_span ,
455
464
errors:: IgnoredInlineAttrConstants ,
456
465
) ,
457
466
// FIXME(#80564): Same for fields, arms, and macro defs
458
467
Target :: Field | Target :: Arm | Target :: MacroDef => {
459
- self . inline_attr_str_error_with_macro_def ( hir_id, attr , "inline" )
468
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr_span , "inline" )
460
469
}
461
470
_ => {
462
- self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure {
463
- attr_span : attr. span ( ) ,
464
- defn_span : span,
465
- } ) ;
471
+ self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure { attr_span, defn_span } ) ;
466
472
}
467
473
}
468
474
469
475
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
470
476
if let Some ( did) = hir_id. as_owner ( )
471
477
&& self . tcx . def_kind ( did) . has_codegen_attrs ( )
472
- && ! matches ! ( attr . meta_item_list ( ) . as_deref ( ) , Some ( [ item ] ) if item . has_name ( sym :: never ) )
478
+ && kind != & InlineAttr :: Never
473
479
{
474
480
let attrs = self . tcx . codegen_fn_attrs ( did) ;
475
481
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
476
482
if attrs. contains_extern_indicator ( ) {
477
483
self . tcx . emit_node_span_lint (
478
484
UNUSED_ATTRIBUTES ,
479
485
hir_id,
480
- attr . span ( ) ,
486
+ attr_span ,
481
487
errors:: InlineIgnoredForExported { } ,
482
488
) ;
483
489
}
@@ -711,6 +717,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
711
717
}
712
718
}
713
719
}
720
+ // FIXME(#80564): We permit struct fields, match arms and macro defs to have an
721
+ // `#[naked]` attribute with just a lint, because we previously
722
+ // erroneously allowed it and some crates used it accidentally, to be compatible
723
+ // with crates depending on them, we can't throw an error here.
724
+ Target :: Field | Target :: Arm | Target :: MacroDef => {
725
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "naked" )
726
+ }
714
727
_ => {
715
728
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
716
729
attr_span : attr. span ( ) ,
@@ -787,7 +800,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
787
800
// with crates depending on them, we can't throw an error here.
788
801
Target :: Field | Target :: Arm | Target :: MacroDef => {
789
802
for attr in attrs {
790
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "track_caller" ) ;
803
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "track_caller" ) ;
791
804
}
792
805
}
793
806
_ => {
@@ -830,7 +843,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
830
843
// erroneously allowed it and some crates used it accidentally, to be compatible
831
844
// with crates depending on them, we can't throw an error here.
832
845
Target :: Field | Target :: Arm | Target :: MacroDef => {
833
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "non_exhaustive" ) ;
846
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "non_exhaustive" ) ;
834
847
}
835
848
_ => {
836
849
self . dcx ( ) . emit_err ( errors:: NonExhaustiveWrongLocation {
@@ -850,7 +863,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
850
863
// erroneously allowed it and some crates used it accidentally, to be compatible
851
864
// with crates depending on them, we can't throw an error here.
852
865
Target :: Field | Target :: Arm | Target :: MacroDef => {
853
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "marker" ) ;
866
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "marker" ) ;
854
867
}
855
868
_ => {
856
869
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToTrait {
@@ -904,7 +917,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
904
917
// erroneously allowed it and some crates used it accidentally, to be compatible
905
918
// with crates depending on them, we can't throw an error here.
906
919
Target :: Field | Target :: Arm | Target :: MacroDef => {
907
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "target_feature" ) ;
920
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "target_feature" ) ;
908
921
}
909
922
_ => {
910
923
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
@@ -1619,7 +1632,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1619
1632
// erroneously allowed it and some crates used it accidentally, to be compatible
1620
1633
// with crates depending on them, we can't throw an error here.
1621
1634
Target :: Field | Target :: Arm | Target :: MacroDef => {
1622
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "cold" ) ;
1635
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "cold" ) ;
1623
1636
}
1624
1637
_ => {
1625
1638
// FIXME: #[cold] was previously allowed on non-functions and some crates used
@@ -1661,7 +1674,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1661
1674
// erroneously allowed it and some crates used it accidentally, to be compatible
1662
1675
// with crates depending on them, we can't throw an error here.
1663
1676
Target :: Field | Target :: Arm | Target :: MacroDef => {
1664
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_name" ) ;
1677
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_name" ) ;
1665
1678
}
1666
1679
_ => {
1667
1680
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
@@ -1695,7 +1708,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1695
1708
// erroneously allowed it and some crates used it accidentally, to be compatible
1696
1709
// with crates depending on them, we can't throw an error here.
1697
1710
Target :: Field | Target :: Arm | Target :: MacroDef => {
1698
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_link" ) ;
1711
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_link" ) ;
1699
1712
}
1700
1713
_ => {
1701
1714
self . dcx ( ) . emit_err ( errors:: NoLink { attr_span : attr. span ( ) , span } ) ;
@@ -1717,7 +1730,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1717
1730
// erroneously allowed it and some crates used it accidentally, to be compatible
1718
1731
// with crates depending on them, we can't throw an error here.
1719
1732
Target :: Field | Target :: Arm | Target :: MacroDef => {
1720
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "export_name" ) ;
1733
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "export_name" ) ;
1721
1734
}
1722
1735
_ => {
1723
1736
self . dcx ( ) . emit_err ( errors:: ExportName { attr_span : attr. span ( ) , span } ) ;
@@ -1891,7 +1904,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1891
1904
// erroneously allowed it and some crates used it accidentally, to be compatible
1892
1905
// with crates depending on them, we can't throw an error here.
1893
1906
Target :: Field | Target :: Arm | Target :: MacroDef => {
1894
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_section" ) ;
1907
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_section" ) ;
1895
1908
}
1896
1909
_ => {
1897
1910
// FIXME: #[link_section] was previously allowed on non-functions/statics and some
@@ -1916,7 +1929,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1916
1929
// erroneously allowed it and some crates used it accidentally, to be compatible
1917
1930
// with crates depending on them, we can't throw an error here.
1918
1931
Target :: Field | Target :: Arm | Target :: MacroDef => {
1919
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_mangle" ) ;
1932
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_mangle" ) ;
1920
1933
}
1921
1934
// FIXME: #[no_mangle] was previously allowed on non-functions/statics, this should be an error
1922
1935
// The error should specify that the item that is wrong is specifically a *foreign* fn/static
@@ -2263,9 +2276,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2263
2276
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
2264
2277
// erroneously allowed it and some crates used it accidentally, to be compatible
2265
2278
// with crates depending on them, we can't throw an error here.
2266
- Target :: Field | Target :: Arm | Target :: MacroDef => {
2267
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "allow_internal_unstable" )
2268
- }
2279
+ Target :: Field | Target :: Arm | Target :: MacroDef => self
2280
+ . inline_attr_str_error_with_macro_def (
2281
+ hir_id,
2282
+ attr. span ( ) ,
2283
+ "allow_internal_unstable" ,
2284
+ ) ,
2269
2285
_ => {
2270
2286
self . tcx
2271
2287
. dcx ( )
@@ -2638,8 +2654,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2638
2654
span : Span ,
2639
2655
target : Target ,
2640
2656
) {
2641
- let force_inline_attr = attrs. iter ( ) . find ( |attr| attr. has_name ( sym:: rustc_force_inline) ) ;
2642
- match ( target, force_inline_attr) {
2657
+ match (
2658
+ target,
2659
+ find_attr ! ( attrs, AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span) ,
2660
+ ) {
2643
2661
( Target :: Closure , None ) => {
2644
2662
let is_coro = matches ! (
2645
2663
self . tcx. hir_expect_expr( hir_id) . kind,
@@ -2651,20 +2669,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2651
2669
) ;
2652
2670
let parent_did = self . tcx . hir_get_parent_item ( hir_id) . to_def_id ( ) ;
2653
2671
let parent_span = self . tcx . def_span ( parent_did) ;
2654
- let parent_force_inline_attr =
2655
- self . tcx . get_attr ( parent_did, sym:: rustc_force_inline) ;
2656
- if let Some ( attr) = parent_force_inline_attr
2657
- && is_coro
2672
+
2673
+ if let Some ( attr_span) = find_attr ! (
2674
+ self . tcx. get_all_attrs( parent_did) ,
2675
+ AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span
2676
+ ) && is_coro
2658
2677
{
2659
- self . dcx ( ) . emit_err ( errors:: RustcForceInlineCoro {
2660
- attr_span : attr. span ( ) ,
2661
- span : parent_span,
2662
- } ) ;
2678
+ self . dcx ( )
2679
+ . emit_err ( errors:: RustcForceInlineCoro { attr_span, span : parent_span } ) ;
2663
2680
}
2664
2681
}
2665
2682
( Target :: Fn , _) => ( ) ,
2666
- ( _, Some ( attr ) ) => {
2667
- self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span : attr . span ( ) , span } ) ;
2683
+ ( _, Some ( attr_span ) ) => {
2684
+ self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span, span } ) ;
2668
2685
}
2669
2686
( _, None ) => ( ) ,
2670
2687
}
@@ -2885,10 +2902,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
2885
2902
fn check_non_exported_macro_for_invalid_attrs ( tcx : TyCtxt < ' _ > , item : & Item < ' _ > ) {
2886
2903
let attrs = tcx. hir_attrs ( item. hir_id ( ) ) ;
2887
2904
2888
- for attr in attrs {
2889
- if attr. has_name ( sym:: inline) {
2890
- tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span : attr. span ( ) } ) ;
2891
- }
2905
+ if let Some ( attr_span) = find_attr ! ( attrs, AttributeKind :: Inline ( _, span) => * span) {
2906
+ tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span } ) ;
2892
2907
}
2893
2908
}
2894
2909
@@ -2908,6 +2923,7 @@ pub(crate) fn provide(providers: &mut Providers) {
2908
2923
* providers = Providers { check_mod_attrs, ..* providers } ;
2909
2924
}
2910
2925
2926
+ // FIXME(jdonszelmann): remove, check during parsing
2911
2927
fn check_duplicates (
2912
2928
tcx : TyCtxt < ' _ > ,
2913
2929
attr : & Attribute ,
0 commit comments