@@ -147,6 +147,16 @@ pub enum AttributeDuplicates {
147
147
FutureWarnPreceding ,
148
148
}
149
149
150
+ /// A conveniece macro to deal with `$($expr)?`.
151
+ macro_rules! or_default {
152
+ ( $default: expr, ) => {
153
+ $default
154
+ } ;
155
+ ( $default: expr, $next: expr) => {
156
+ $next
157
+ } ;
158
+ }
159
+
150
160
/// A convenience macro for constructing attribute templates.
151
161
/// E.g., `template!(Word, List: "description")` means that the attribute
152
162
/// supports forms `#[attr]` and `#[attr(description)]`.
@@ -168,9 +178,10 @@ macro_rules! template {
168
178
}
169
179
170
180
macro_rules! ungated {
171
- ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr $( , ) ?) => {
181
+ ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr $( , @only_local : $only_local : expr ) ? $ ( , ) ?) => {
172
182
BuiltinAttribute {
173
183
name: sym:: $attr,
184
+ only_local: or_default!( false , $( $only_local) ?) ,
174
185
type_: $typ,
175
186
template: $tpl,
176
187
gate: Ungated ,
@@ -180,18 +191,20 @@ macro_rules! ungated {
180
191
}
181
192
182
193
macro_rules! gated {
183
- ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $gate: ident, $msg: expr $( , ) ?) => {
194
+ ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr $ ( , @only_local : $only_local : expr ) ? , $gate: ident, $msg: expr $( , ) ?) => {
184
195
BuiltinAttribute {
185
196
name: sym:: $attr,
197
+ only_local: or_default!( false , $( $only_local) ?) ,
186
198
type_: $typ,
187
199
template: $tpl,
188
200
duplicates: $duplicates,
189
201
gate: Gated ( Stability :: Unstable , sym:: $gate, $msg, cfg_fn!( $gate) ) ,
190
202
}
191
203
} ;
192
- ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $msg: expr $( , ) ?) => {
204
+ ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr $ ( , @only_local : $only_local : expr ) ? , $msg: expr $( , ) ?) => {
193
205
BuiltinAttribute {
194
206
name: sym:: $attr,
207
+ only_local: or_default!( false , $( $only_local) ?) ,
195
208
type_: $typ,
196
209
template: $tpl,
197
210
duplicates: $duplicates,
@@ -201,12 +214,13 @@ macro_rules! gated {
201
214
}
202
215
203
216
macro_rules! rustc_attr {
204
- ( TEST , $attr: ident, $typ: expr, $tpl: expr, $duplicate: expr $( , ) ?) => {
217
+ ( TEST , $attr: ident, $typ: expr, $tpl: expr, $duplicate: expr $( , @only_local : $only_local : expr ) ? $ ( , ) ?) => {
205
218
rustc_attr!(
206
219
$attr,
207
220
$typ,
208
221
$tpl,
209
222
$duplicate,
223
+ $( @only_local: $only_local, ) ?
210
224
concat!(
211
225
"the `#[" ,
212
226
stringify!( $attr) ,
@@ -215,9 +229,10 @@ macro_rules! rustc_attr {
215
229
) ,
216
230
)
217
231
} ;
218
- ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $msg: expr $( , ) ?) => {
232
+ ( $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr $ ( , @only_local : $only_local : expr ) ? , $msg: expr $( , ) ?) => {
219
233
BuiltinAttribute {
220
234
name: sym:: $attr,
235
+ only_local: or_default!( false , $( $only_local) ?) ,
221
236
type_: $typ,
222
237
template: $tpl,
223
238
duplicates: $duplicates,
@@ -237,6 +252,10 @@ const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never b
237
252
238
253
pub struct BuiltinAttribute {
239
254
pub name : Symbol ,
255
+ /// Whether this attribute is only used in the local crate.
256
+ ///
257
+ /// If so, it is not encoded in the crate metadata.
258
+ pub only_local : bool ,
240
259
pub type_ : AttributeType ,
241
260
pub template : AttributeTemplate ,
242
261
pub duplicates : AttributeDuplicates ,
@@ -295,7 +314,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
295
314
ungated ! ( must_use, Normal , template!( Word , NameValueStr : "reason" ) , FutureWarnFollowing ) ,
296
315
gated ! (
297
316
must_not_suspend, Normal , template!( Word , NameValueStr : "reason" ) , WarnFollowing ,
298
- must_not_suspend , experimental!( must_not_suspend)
317
+ experimental!( must_not_suspend)
299
318
) ,
300
319
ungated ! (
301
320
deprecated, Normal ,
@@ -324,8 +343,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
324
343
ungated ! ( repr, Normal , template!( List : "C" ) , DuplicatesOk ) ,
325
344
ungated ! ( export_name, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding ) ,
326
345
ungated ! ( link_section, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding ) ,
327
- ungated ! ( no_mangle, Normal , template!( Word ) , WarnFollowing ) ,
328
- ungated ! ( used, Normal , template!( Word , List : "compiler|linker" ) , WarnFollowing ) ,
346
+ ungated ! ( no_mangle, Normal , template!( Word ) , WarnFollowing , @only_local : true ) ,
347
+ ungated ! ( used, Normal , template!( Word , List : "compiler|linker" ) , WarnFollowing , @only_local : true ) ,
329
348
330
349
// Limits:
331
350
ungated ! ( recursion_limit, CrateLevel , template!( NameValueStr : "N" ) , FutureWarnFollowing ) ,
@@ -358,8 +377,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
358
377
ungated ! ( panic_handler, Normal , template!( Word ) , WarnFollowing ) , // RFC 2070
359
378
360
379
// Code generation:
361
- ungated ! ( inline, Normal , template!( Word , List : "always|never" ) , FutureWarnFollowing ) ,
362
- ungated ! ( cold, Normal , template!( Word ) , WarnFollowing ) ,
380
+ ungated ! ( inline, Normal , template!( Word , List : "always|never" ) , FutureWarnFollowing , @only_local : true ) ,
381
+ ungated ! ( cold, Normal , template!( Word ) , WarnFollowing , @only_local : true ) ,
363
382
ungated ! ( no_builtins, CrateLevel , template!( Word ) , WarnFollowing ) ,
364
383
ungated ! ( target_feature, Normal , template!( List : r#"enable = "name""# ) , DuplicatesOk ) ,
365
384
ungated ! ( track_caller, Normal , template!( Word ) , WarnFollowing ) ,
@@ -385,7 +404,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
385
404
) ,
386
405
387
406
// Linking:
388
- gated ! ( naked, Normal , template!( Word ) , WarnFollowing , naked_functions, experimental!( naked) ) ,
407
+ gated ! ( naked, Normal , template!( Word ) , WarnFollowing , @only_local : true , naked_functions, experimental!( naked) ) ,
389
408
gated ! (
390
409
link_ordinal, Normal , template!( List : "ordinal" ) , ErrorPreceding , raw_dylib,
391
410
experimental!( link_ordinal)
@@ -394,6 +413,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
394
413
// Plugins:
395
414
BuiltinAttribute {
396
415
name : sym:: plugin,
416
+ only_local : false ,
397
417
type_ : CrateLevel ,
398
418
template : template ! ( List : "name" ) ,
399
419
duplicates : DuplicatesOk ,
@@ -475,7 +495,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
475
495
) ,
476
496
// DuplicatesOk since it has its own validation
477
497
ungated ! (
478
- stable, Normal , template!( List : r#"feature = "name", since = "version""# ) , DuplicatesOk
498
+ stable, Normal , template!( List : r#"feature = "name", since = "version""# ) , DuplicatesOk ,
479
499
) ,
480
500
ungated ! (
481
501
unstable, Normal ,
@@ -546,11 +566,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
546
566
// ==========================================================================
547
567
548
568
gated ! (
549
- linkage, Normal , template!( NameValueStr : "external|internal|..." ) , ErrorPreceding ,
569
+ linkage, Normal , template!( NameValueStr : "external|internal|..." ) , ErrorPreceding , @only_local : true ,
550
570
"the `linkage` attribute is experimental and not portable across platforms" ,
551
571
) ,
552
572
rustc_attr ! (
553
- rustc_std_internal_symbol, Normal , template!( Word ) , WarnFollowing , INTERNAL_UNSTABLE
573
+ rustc_std_internal_symbol, Normal , template!( Word ) , WarnFollowing , @only_local : true , INTERNAL_UNSTABLE
554
574
) ,
555
575
556
576
// ==========================================================================
@@ -633,7 +653,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
633
653
// Internal attributes, Misc:
634
654
// ==========================================================================
635
655
gated ! (
636
- lang, Normal , template!( NameValueStr : "name" ) , DuplicatesOk , lang_items,
656
+ lang, Normal , template!( NameValueStr : "name" ) , DuplicatesOk , @only_local : true , lang_items,
637
657
"language items are subject to change" ,
638
658
) ,
639
659
rustc_attr ! (
@@ -642,11 +662,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
642
662
"#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
643
663
) ,
644
664
rustc_attr ! (
645
- rustc_coherence_is_core, AttributeType :: CrateLevel , template!( Word ) , ErrorFollowing ,
665
+ rustc_coherence_is_core, AttributeType :: CrateLevel , template!( Word ) , ErrorFollowing , @only_local : true ,
646
666
"#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
647
667
) ,
648
668
rustc_attr ! (
649
- rustc_allow_incoherent_impl, AttributeType :: Normal , template!( Word ) , ErrorFollowing ,
669
+ rustc_allow_incoherent_impl, AttributeType :: Normal , template!( Word ) , ErrorFollowing , @only_local : true ,
650
670
"#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
651
671
) ,
652
672
rustc_attr ! (
@@ -656,6 +676,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
656
676
) ,
657
677
BuiltinAttribute {
658
678
name : sym:: rustc_diagnostic_item,
679
+ // FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
680
+ only_local : false ,
659
681
type_ : Normal ,
660
682
template : template ! ( NameValueStr : "name" ) ,
661
683
duplicates : ErrorFollowing ,
@@ -676,7 +698,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
676
698
"unboxed_closures are still evolving" ,
677
699
) ,
678
700
rustc_attr ! (
679
- rustc_inherit_overflow_checks, Normal , template!( Word ) , WarnFollowing ,
701
+ rustc_inherit_overflow_checks, Normal , template!( Word ) , WarnFollowing , @only_local : true ,
680
702
"the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
681
703
overflow checking behavior of several libcore functions that are inlined \
682
704
across crates and will never be stable",
@@ -778,6 +800,10 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
778
800
BUILTIN_ATTRIBUTE_MAP . get ( & name) . is_some ( )
779
801
}
780
802
803
+ pub fn is_builtin_only_local ( name : Symbol ) -> bool {
804
+ BUILTIN_ATTRIBUTE_MAP . get ( & name) . map_or ( false , |attr| attr. only_local )
805
+ }
806
+
781
807
pub static BUILTIN_ATTRIBUTE_MAP : SyncLazy < FxHashMap < Symbol , & BuiltinAttribute > > =
782
808
SyncLazy :: new ( || {
783
809
let mut map = FxHashMap :: default ( ) ;
0 commit comments