@@ -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:: { find_attr , AttributeKind , InlineAttr , ReprAttr } ;
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,
@@ -140,7 +143,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
140
143
[ sym:: diagnostic, sym:: on_unimplemented, ..] => {
141
144
self . check_diagnostic_on_unimplemented ( attr. span ( ) , hir_id, target)
142
145
}
143
- [ sym:: inline, ..] => self . check_inline ( hir_id, attr, span, target) ,
144
146
[ sym:: coverage, ..] => self . check_coverage ( attr, span, target) ,
145
147
[ sym:: optimize, ..] => self . check_optimize ( hir_id, attr, span, target) ,
146
148
[ sym:: no_sanitize, ..] => {
@@ -357,11 +359,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
357
359
self . check_rustc_force_inline ( hir_id, attrs, span, target) ;
358
360
}
359
361
360
- fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr : & Attribute , sym : & str ) {
362
+ fn inline_attr_str_error_with_macro_def ( & self , hir_id : HirId , attr_span : Span , sym : & str ) {
361
363
self . tcx . emit_node_span_lint (
362
364
UNUSED_ATTRIBUTES ,
363
365
hir_id,
364
- attr . span ( ) ,
366
+ attr_span ,
365
367
errors:: IgnoredAttrWithMacro { sym } ,
366
368
) ;
367
369
}
@@ -421,7 +423,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
421
423
}
422
424
423
425
/// Checks if an `#[inline]` is applied to a function or a closure.
424
- fn check_inline ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
426
+ fn check_inline ( & self , hir_id : HirId , attr_span : Span , defn_span : Span , kind : & InlineAttr , target : Target ) {
425
427
match target {
426
428
Target :: Fn
427
429
| Target :: Closure
@@ -430,7 +432,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
430
432
self . tcx . emit_node_span_lint (
431
433
UNUSED_ATTRIBUTES ,
432
434
hir_id,
433
- attr . span ( ) ,
435
+ attr_span ,
434
436
errors:: IgnoredInlineAttrFnProto ,
435
437
)
436
438
}
@@ -441,33 +443,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
441
443
Target :: AssocConst => self . tcx . emit_node_span_lint (
442
444
UNUSED_ATTRIBUTES ,
443
445
hir_id,
444
- attr . span ( ) ,
446
+ attr_span ,
445
447
errors:: IgnoredInlineAttrConstants ,
446
448
) ,
447
449
// FIXME(#80564): Same for fields, arms, and macro defs
448
450
Target :: Field | Target :: Arm | Target :: MacroDef => {
449
- self . inline_attr_str_error_with_macro_def ( hir_id, attr , "inline" )
451
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr_span , "inline" )
450
452
}
451
453
_ => {
452
- self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure {
453
- attr_span : attr. span ( ) ,
454
- defn_span : span,
455
- } ) ;
454
+ self . dcx ( ) . emit_err ( errors:: InlineNotFnOrClosure { attr_span, defn_span } ) ;
456
455
}
457
456
}
458
457
459
458
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
460
459
if let Some ( did) = hir_id. as_owner ( )
461
460
&& self . tcx . def_kind ( did) . has_codegen_attrs ( )
462
- && ! matches ! ( attr . meta_item_list ( ) . as_deref ( ) , Some ( [ item ] ) if item . has_name ( sym :: never ) )
461
+ && kind != & InlineAttr :: Never
463
462
{
464
463
let attrs = self . tcx . codegen_fn_attrs ( did) ;
465
464
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
466
465
if attrs. contains_extern_indicator ( ) {
467
466
self . tcx . emit_node_span_lint (
468
467
UNUSED_ATTRIBUTES ,
469
468
hir_id,
470
- attr . span ( ) ,
469
+ attr_span ,
471
470
errors:: InlineIgnoredForExported { } ,
472
471
) ;
473
472
}
@@ -701,6 +700,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
701
700
}
702
701
}
703
702
}
703
+ // FIXME(#80564): We permit struct fields, match arms and macro defs to have an
704
+ // `#[naked]` attribute with just a lint, because we previously
705
+ // erroneously allowed it and some crates used it accidentally, to be compatible
706
+ // with crates depending on them, we can't throw an error here.
707
+ Target :: Field | Target :: Arm | Target :: MacroDef => {
708
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "naked" )
709
+ }
704
710
_ => {
705
711
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
706
712
attr_span : attr. span ( ) ,
@@ -777,7 +783,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
777
783
// with crates depending on them, we can't throw an error here.
778
784
Target :: Field | Target :: Arm | Target :: MacroDef => {
779
785
for attr in attrs {
780
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "track_caller" ) ;
786
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "track_caller" ) ;
781
787
}
782
788
}
783
789
_ => {
@@ -820,7 +826,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
820
826
// erroneously allowed it and some crates used it accidentally, to be compatible
821
827
// with crates depending on them, we can't throw an error here.
822
828
Target :: Field | Target :: Arm | Target :: MacroDef => {
823
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "non_exhaustive" ) ;
829
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "non_exhaustive" ) ;
824
830
}
825
831
_ => {
826
832
self . dcx ( ) . emit_err ( errors:: NonExhaustiveWrongLocation {
@@ -840,7 +846,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
840
846
// erroneously allowed it and some crates used it accidentally, to be compatible
841
847
// with crates depending on them, we can't throw an error here.
842
848
Target :: Field | Target :: Arm | Target :: MacroDef => {
843
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "marker" ) ;
849
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "marker" ) ;
844
850
}
845
851
_ => {
846
852
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToTrait {
@@ -894,7 +900,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
894
900
// erroneously allowed it and some crates used it accidentally, to be compatible
895
901
// with crates depending on them, we can't throw an error here.
896
902
Target :: Field | Target :: Arm | Target :: MacroDef => {
897
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "target_feature" ) ;
903
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "target_feature" ) ;
898
904
}
899
905
_ => {
900
906
self . dcx ( ) . emit_err ( errors:: AttrShouldBeAppliedToFn {
@@ -1609,7 +1615,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1609
1615
// erroneously allowed it and some crates used it accidentally, to be compatible
1610
1616
// with crates depending on them, we can't throw an error here.
1611
1617
Target :: Field | Target :: Arm | Target :: MacroDef => {
1612
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "cold" ) ;
1618
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "cold" ) ;
1613
1619
}
1614
1620
_ => {
1615
1621
// FIXME: #[cold] was previously allowed on non-functions and some crates used
@@ -1651,7 +1657,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1651
1657
// erroneously allowed it and some crates used it accidentally, to be compatible
1652
1658
// with crates depending on them, we can't throw an error here.
1653
1659
Target :: Field | Target :: Arm | Target :: MacroDef => {
1654
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_name" ) ;
1660
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_name" ) ;
1655
1661
}
1656
1662
_ => {
1657
1663
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
@@ -1685,7 +1691,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1685
1691
// erroneously allowed it and some crates used it accidentally, to be compatible
1686
1692
// with crates depending on them, we can't throw an error here.
1687
1693
Target :: Field | Target :: Arm | Target :: MacroDef => {
1688
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_link" ) ;
1694
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_link" ) ;
1689
1695
}
1690
1696
_ => {
1691
1697
self . dcx ( ) . emit_err ( errors:: NoLink { attr_span : attr. span ( ) , span } ) ;
@@ -1707,7 +1713,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1707
1713
// erroneously allowed it and some crates used it accidentally, to be compatible
1708
1714
// with crates depending on them, we can't throw an error here.
1709
1715
Target :: Field | Target :: Arm | Target :: MacroDef => {
1710
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "export_name" ) ;
1716
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "export_name" ) ;
1711
1717
}
1712
1718
_ => {
1713
1719
self . dcx ( ) . emit_err ( errors:: ExportName { attr_span : attr. span ( ) , span } ) ;
@@ -1881,7 +1887,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1881
1887
// erroneously allowed it and some crates used it accidentally, to be compatible
1882
1888
// with crates depending on them, we can't throw an error here.
1883
1889
Target :: Field | Target :: Arm | Target :: MacroDef => {
1884
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "link_section" ) ;
1890
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "link_section" ) ;
1885
1891
}
1886
1892
_ => {
1887
1893
// FIXME: #[link_section] was previously allowed on non-functions/statics and some
@@ -1906,7 +1912,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1906
1912
// erroneously allowed it and some crates used it accidentally, to be compatible
1907
1913
// with crates depending on them, we can't throw an error here.
1908
1914
Target :: Field | Target :: Arm | Target :: MacroDef => {
1909
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "no_mangle" ) ;
1915
+ self . inline_attr_str_error_with_macro_def ( hir_id, attr. span ( ) , "no_mangle" ) ;
1910
1916
}
1911
1917
// FIXME: #[no_mangle] was previously allowed on non-functions/statics, this should be an error
1912
1918
// The error should specify that the item that is wrong is specifically a *foreign* fn/static
@@ -2253,9 +2259,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2253
2259
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
2254
2260
// erroneously allowed it and some crates used it accidentally, to be compatible
2255
2261
// with crates depending on them, we can't throw an error here.
2256
- Target :: Field | Target :: Arm | Target :: MacroDef => {
2257
- self . inline_attr_str_error_with_macro_def ( hir_id, attr, "allow_internal_unstable" )
2258
- }
2262
+ Target :: Field | Target :: Arm | Target :: MacroDef => self
2263
+ . inline_attr_str_error_with_macro_def (
2264
+ hir_id,
2265
+ attr. span ( ) ,
2266
+ "allow_internal_unstable" ,
2267
+ ) ,
2259
2268
_ => {
2260
2269
self . tcx
2261
2270
. dcx ( )
@@ -2628,8 +2637,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2628
2637
span : Span ,
2629
2638
target : Target ,
2630
2639
) {
2631
- let force_inline_attr = attrs. iter ( ) . find ( |attr| attr. has_name ( sym:: rustc_force_inline) ) ;
2632
- match ( target, force_inline_attr) {
2640
+ match (
2641
+ target,
2642
+ find_attr ! ( attrs, AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span) ,
2643
+ ) {
2633
2644
( Target :: Closure , None ) => {
2634
2645
let is_coro = matches ! (
2635
2646
self . tcx. hir_expect_expr( hir_id) . kind,
@@ -2641,20 +2652,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2641
2652
) ;
2642
2653
let parent_did = self . tcx . hir_get_parent_item ( hir_id) . to_def_id ( ) ;
2643
2654
let parent_span = self . tcx . def_span ( parent_did) ;
2644
- let parent_force_inline_attr =
2645
- self . tcx . get_attr ( parent_did, sym:: rustc_force_inline) ;
2646
- if let Some ( attr) = parent_force_inline_attr
2647
- && is_coro
2655
+
2656
+ if let Some ( attr_span) = find_attr ! (
2657
+ self . tcx. get_all_attrs( parent_did) ,
2658
+ AttributeKind :: Inline ( InlineAttr :: Force { attr_span, .. } , _) => * attr_span
2659
+ ) && is_coro
2648
2660
{
2649
- self . dcx ( ) . emit_err ( errors:: RustcForceInlineCoro {
2650
- attr_span : attr. span ( ) ,
2651
- span : parent_span,
2652
- } ) ;
2661
+ self . dcx ( )
2662
+ . emit_err ( errors:: RustcForceInlineCoro { attr_span, span : parent_span } ) ;
2653
2663
}
2654
2664
}
2655
2665
( Target :: Fn , _) => ( ) ,
2656
- ( _, Some ( attr ) ) => {
2657
- self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span : attr . span ( ) , span } ) ;
2666
+ ( _, Some ( attr_span ) ) => {
2667
+ self . dcx ( ) . emit_err ( errors:: RustcForceInline { attr_span, span } ) ;
2658
2668
}
2659
2669
( _, None ) => ( ) ,
2660
2670
}
@@ -2875,10 +2885,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
2875
2885
fn check_non_exported_macro_for_invalid_attrs ( tcx : TyCtxt < ' _ > , item : & Item < ' _ > ) {
2876
2886
let attrs = tcx. hir_attrs ( item. hir_id ( ) ) ;
2877
2887
2878
- for attr in attrs {
2879
- if attr. has_name ( sym:: inline) {
2880
- tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span : attr. span ( ) } ) ;
2881
- }
2888
+ if let Some ( attr_span) = find_attr ! ( attrs, AttributeKind :: Inline ( _, span) => * span) {
2889
+ tcx. dcx ( ) . emit_err ( errors:: NonExportedMacroInvalidAttrs { attr_span } ) ;
2882
2890
}
2883
2891
}
2884
2892
@@ -2898,6 +2906,7 @@ pub(crate) fn provide(providers: &mut Providers) {
2898
2906
* providers = Providers { check_mod_attrs, ..* providers } ;
2899
2907
}
2900
2908
2909
+ // FIXME(jdonszelmann): remove, check during parsing
2901
2910
fn check_duplicates (
2902
2911
tcx : TyCtxt < ' _ > ,
2903
2912
attr : & Attribute ,
0 commit comments