@@ -10,16 +10,16 @@ use std::collections::hash_map::Entry;
10
10
use rustc_ast:: token:: TokenKind ;
11
11
use rustc_ast:: tokenstream:: TokenTree ;
12
12
use rustc_ast:: {
13
- AttrKind , AttrStyle , Attribute , LitKind , MetaItemInner , MetaItemKind , MetaItemLit , ast ,
13
+ ast , AttrKind , AttrStyle , Attribute , LitKind , MetaItemInner , MetaItemKind , MetaItemLit ,
14
14
} ;
15
15
use rustc_data_structures:: fx:: FxHashMap ;
16
16
use rustc_errors:: { Applicability , DiagCtxtHandle , IntoDiagArg , MultiSpan , StashKey } ;
17
- use rustc_feature:: { AttributeDuplicates , AttributeType , BUILTIN_ATTRIBUTE_MAP , BuiltinAttribute } ;
17
+ use rustc_feature:: { AttributeDuplicates , AttributeType , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
18
18
use rustc_hir:: def_id:: LocalModDefId ;
19
19
use rustc_hir:: intravisit:: { self , Visitor } ;
20
20
use rustc_hir:: {
21
- self as hir, self , AssocItemKind , CRATE_HIR_ID , CRATE_OWNER_ID , FnSig , ForeignItem , HirId ,
22
- Item , ItemKind , MethodKind , Safety , Target , TraitItem ,
21
+ self , self as hir, AssocItemKind , FnSig , FnSig , ForeignItem , ForeignItem , HirId , Item ,
22
+ ItemKind , MethodKind , Safety , Target , Target , TraitItem , CRATE_HIR_ID , CRATE_OWNER_ID ,
23
23
} ;
24
24
use rustc_macros:: LintDiagnostic ;
25
25
use rustc_middle:: hir:: nested_filter;
@@ -34,8 +34,8 @@ use rustc_session::lint::builtin::{
34
34
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES , UNUSED_ATTRIBUTES ,
35
35
} ;
36
36
use rustc_session:: parse:: feature_err;
37
- use rustc_span:: symbol:: { Symbol , kw, sym} ;
38
- use rustc_span:: { BytePos , DUMMY_SP , Span } ;
37
+ use rustc_span:: symbol:: { kw, sym, Symbol } ;
38
+ use rustc_span:: { BytePos , Span , DUMMY_SP } ;
39
39
use rustc_target:: abi:: Size ;
40
40
use rustc_target:: spec:: abi:: Abi ;
41
41
use rustc_trait_selection:: error_reporting:: InferCtxtErrorExt ;
@@ -349,8 +349,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
349
349
}
350
350
351
351
fn inline_attr_str_error_without_macro_def ( & self , hir_id : HirId , attr : & Attribute , sym : & str ) {
352
- self . tcx
353
- . emit_node_span_lint ( UNUSED_ATTRIBUTES , hir_id, attr. span , errors:: IgnoredAttr { sym } ) ;
352
+ self . tcx . emit_node_span_lint (
353
+ UNUSED_ATTRIBUTES ,
354
+ hir_id,
355
+ attr. span ,
356
+ errors:: IgnoredAttr { sym } ,
357
+ ) ;
354
358
}
355
359
356
360
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
@@ -1417,10 +1421,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1417
1421
_ => {
1418
1422
// FIXME: #[cold] was previously allowed on non-functions and some crates used
1419
1423
// this, so only emit a warning.
1420
- self . tcx . emit_node_span_lint ( UNUSED_ATTRIBUTES , hir_id, attr. span , errors:: Cold {
1421
- span,
1422
- on_crate : hir_id == CRATE_HIR_ID ,
1423
- } ) ;
1424
+ self . tcx . emit_node_span_lint (
1425
+ UNUSED_ATTRIBUTES ,
1426
+ hir_id,
1427
+ attr. span ,
1428
+ errors:: Cold { span, on_crate : hir_id == CRATE_HIR_ID } ,
1429
+ ) ;
1424
1430
}
1425
1431
}
1426
1432
}
@@ -1435,9 +1441,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1435
1441
return ;
1436
1442
}
1437
1443
1438
- self . tcx . emit_node_span_lint ( UNUSED_ATTRIBUTES , hir_id, attr. span , errors:: Link {
1439
- span : ( target != Target :: ForeignMod ) . then_some ( span) ,
1440
- } ) ;
1444
+ self . tcx . emit_node_span_lint (
1445
+ UNUSED_ATTRIBUTES ,
1446
+ hir_id,
1447
+ attr. span ,
1448
+ errors:: Link { span : ( target != Target :: ForeignMod ) . then_some ( span) } ,
1449
+ ) ;
1441
1450
}
1442
1451
1443
1452
/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
@@ -1907,7 +1916,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1907
1916
|| ( int_reprs == 1
1908
1917
&& is_c
1909
1918
&& item. is_some_and ( |item| {
1910
- if let ItemLike :: Item ( item) = item { is_c_like_enum ( item) } else { false }
1919
+ if let ItemLike :: Item ( item) = item {
1920
+ is_c_like_enum ( item)
1921
+ } else {
1922
+ false
1923
+ }
1911
1924
} ) )
1912
1925
{
1913
1926
self . tcx . emit_node_span_lint (
@@ -2247,10 +2260,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2247
2260
return ;
2248
2261
} ;
2249
2262
2250
- self . tcx . emit_node_span_lint ( UNUSED_ATTRIBUTES , hir_id, attr. span , errors:: Unused {
2251
- attr_span : attr. span ,
2252
- note,
2253
- } ) ;
2263
+ self . tcx . emit_node_span_lint (
2264
+ UNUSED_ATTRIBUTES ,
2265
+ hir_id,
2266
+ attr. span ,
2267
+ errors:: Unused { attr_span : attr. span , note } ,
2268
+ ) ;
2254
2269
}
2255
2270
2256
2271
/// A best effort attempt to create an error for a mismatching proc macro signature.
@@ -2406,40 +2421,75 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
2406
2421
}
2407
2422
}
2408
2423
2409
- fn check_instruction_set ( & self , attr : & Attribute , _item : Option < ItemLike < ' _ > > ) {
2410
- if let AttrKind :: Normal ( ref p) = attr. kind {
2411
- let inner_tokens = p. item . args . inner_tokens ( ) ;
2412
- let mut tokens = inner_tokens. trees ( ) ;
2413
-
2414
- // Valid item for `instruction_set()` is:
2415
- // - arm::a32
2416
- // - arm::t32
2417
- match ( tokens. next ( ) , tokens. next ( ) , tokens. next ( ) ) {
2418
- (
2419
- Some ( TokenTree :: Token ( first_token, _) ) ,
2420
- Some ( TokenTree :: Token ( second_token, _) ) ,
2421
- Some ( TokenTree :: Token ( third_token, _) ) ,
2422
- ) => match ( first_token. ident ( ) , second_token. kind . clone ( ) , third_token. ident ( ) ) {
2423
- ( Some ( first_ident) , TokenKind :: PathSep , Some ( third_ident) )
2424
- if first_ident. 0 . name == sym:: arm =>
2425
- {
2426
- if third_ident. 0 . name == sym:: a32 || third_ident. 0 . name == sym:: t32 {
2427
- return ;
2428
- } else {
2429
- self . dcx ( ) . emit_err ( errors:: InvalidInstructionSet { span : attr. span } ) ;
2430
- }
2431
- }
2432
- _ => {
2433
- self . dcx ( ) . emit_err ( errors:: InvalidInstructionSet { span : attr. span } ) ;
2424
+ fn check_instruction_set ( & self , attr : & Attribute , item : Option < ItemLike < ' _ > > ) {
2425
+ // Ensure the attribute is applied to a function or closure
2426
+ match item {
2427
+ Some ( ItemLike :: Item ( inner_item) ) => match inner_item. kind {
2428
+ ItemKind :: Fn ( _, _, _) => {
2429
+ // Validate the tokens for `instruction_set()` attribute
2430
+ if let AttrKind :: Normal ( ref p) = attr. kind {
2431
+ let inner_tokens = p. item . args . inner_tokens ( ) ;
2432
+ let mut tokens = inner_tokens. trees ( ) ;
2433
+
2434
+ match ( tokens. next ( ) , tokens. next ( ) , tokens. next ( ) ) {
2435
+ (
2436
+ Some ( TokenTree :: Token ( first_token, _) ) ,
2437
+ Some ( TokenTree :: Token ( second_token, _) ) ,
2438
+ Some ( TokenTree :: Token ( third_token, _) ) ,
2439
+ ) => match (
2440
+ first_token. ident ( ) ,
2441
+ second_token. kind . clone ( ) ,
2442
+ third_token. ident ( ) ,
2443
+ ) {
2444
+ ( Some ( first_ident) , TokenKind :: PathSep , Some ( third_ident) )
2445
+ if first_ident. 0 . name == sym:: arm =>
2446
+ {
2447
+ if third_ident. 0 . name == sym:: a32
2448
+ || third_ident. 0 . name == sym:: t32
2449
+ {
2450
+ return ;
2451
+ } else {
2452
+ self . dcx ( ) . emit_err ( errors:: InvalidInstructionSet {
2453
+ span : attr. span ,
2454
+ } ) ;
2455
+ }
2456
+ }
2457
+ _ => {
2458
+ self . dcx ( ) . emit_err ( errors:: InvalidInstructionSet {
2459
+ span : attr. span ,
2460
+ } ) ;
2461
+ }
2462
+ } ,
2463
+ ( None , None , None ) => {
2464
+ self . dcx ( )
2465
+ . emit_err ( errors:: EmptyInstructionSet { span : attr. span } ) ;
2466
+ }
2467
+ _ => {
2468
+ self . dcx ( )
2469
+ . emit_err ( errors:: InvalidInstructionSet { span : attr. span } ) ;
2470
+ }
2471
+ } ;
2434
2472
}
2435
- } ,
2436
- ( None , None , None ) => {
2437
- self . dcx ( ) . emit_err ( errors:: EmptyInstructionSet { span : attr. span } ) ;
2438
2473
}
2439
2474
_ => {
2440
- self . dcx ( ) . emit_err ( errors:: InvalidInstructionSet { span : attr. span } ) ;
2475
+ self . dcx ( ) . emit_err ( errors:: InvalidTargetForInstructionSet {
2476
+ span : attr. span ,
2477
+ item_kind : inner_item. kind . descr ( ) ,
2478
+ } ) ;
2479
+ return ;
2441
2480
}
2442
- } ;
2481
+ } ,
2482
+ Some ( ItemLike :: ForeignItem ) => {
2483
+ self . dcx ( ) . emit_err ( errors:: InvalidTargetForInstructionSet {
2484
+ span : attr. span ,
2485
+ item_kind : "foreign item" ,
2486
+ } ) ;
2487
+ return ;
2488
+ }
2489
+ None => {
2490
+ self . dcx ( ) . emit_err ( errors:: AttributeNotAllowed { span : attr. span } ) ;
2491
+ return ;
2492
+ }
2443
2493
}
2444
2494
}
2445
2495
}
0 commit comments