@@ -147,6 +147,16 @@ pub enum AttributeDuplicates {
147147 FutureWarnPreceding ,
148148}
149149
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+
150160/// A convenience macro for constructing attribute templates.
151161/// E.g., `template!(Word, List: "description")` means that the attribute
152162/// supports forms `#[attr]` and `#[attr(description)]`.
@@ -168,9 +178,10 @@ macro_rules! template {
168178}
169179
170180macro_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 ) ? $ ( , ) ?) => {
172182 BuiltinAttribute {
173183 name: sym:: $attr,
184+ only_local: or_default!( false , $( $only_local) ?) ,
174185 type_: $typ,
175186 template: $tpl,
176187 gate: Ungated ,
@@ -180,18 +191,20 @@ macro_rules! ungated {
180191}
181192
182193macro_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 $( , ) ?) => {
184195 BuiltinAttribute {
185196 name: sym:: $attr,
197+ only_local: or_default!( false , $( $only_local) ?) ,
186198 type_: $typ,
187199 template: $tpl,
188200 duplicates: $duplicates,
189201 gate: Gated ( Stability :: Unstable , sym:: $gate, $msg, cfg_fn!( $gate) ) ,
190202 }
191203 } ;
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 $( , ) ?) => {
193205 BuiltinAttribute {
194206 name: sym:: $attr,
207+ only_local: or_default!( false , $( $only_local) ?) ,
195208 type_: $typ,
196209 template: $tpl,
197210 duplicates: $duplicates,
@@ -201,12 +214,13 @@ macro_rules! gated {
201214}
202215
203216macro_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 ) ? $ ( , ) ?) => {
205218 rustc_attr!(
206219 $attr,
207220 $typ,
208221 $tpl,
209222 $duplicate,
223+ $( @only_local: $only_local, ) ?
210224 concat!(
211225 "the `#[" ,
212226 stringify!( $attr) ,
@@ -215,9 +229,10 @@ macro_rules! rustc_attr {
215229 ) ,
216230 )
217231 } ;
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 $( , ) ?) => {
219233 BuiltinAttribute {
220234 name: sym:: $attr,
235+ only_local: or_default!( false , $( $only_local) ?) ,
221236 type_: $typ,
222237 template: $tpl,
223238 duplicates: $duplicates,
@@ -237,6 +252,10 @@ const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never b
237252
238253pub struct BuiltinAttribute {
239254 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 ,
240259 pub type_ : AttributeType ,
241260 pub template : AttributeTemplate ,
242261 pub duplicates : AttributeDuplicates ,
@@ -295,7 +314,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
295314 ungated ! ( must_use, Normal , template!( Word , NameValueStr : "reason" ) , FutureWarnFollowing ) ,
296315 gated ! (
297316 must_not_suspend, Normal , template!( Word , NameValueStr : "reason" ) , WarnFollowing ,
298- must_not_suspend , experimental!( must_not_suspend)
317+ experimental!( must_not_suspend)
299318 ) ,
300319 ungated ! (
301320 deprecated, Normal ,
@@ -324,8 +343,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
324343 ungated ! ( repr, Normal , template!( List : "C" ) , DuplicatesOk ) ,
325344 ungated ! ( export_name, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding ) ,
326345 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 ) ,
329348
330349 // Limits:
331350 ungated ! ( recursion_limit, CrateLevel , template!( NameValueStr : "N" ) , FutureWarnFollowing ) ,
@@ -358,8 +377,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
358377 ungated ! ( panic_handler, Normal , template!( Word ) , WarnFollowing ) , // RFC 2070
359378
360379 // 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 ) ,
363382 ungated ! ( no_builtins, CrateLevel , template!( Word ) , WarnFollowing ) ,
364383 ungated ! ( target_feature, Normal , template!( List : r#"enable = "name""# ) , DuplicatesOk ) ,
365384 ungated ! ( track_caller, Normal , template!( Word ) , WarnFollowing ) ,
@@ -385,7 +404,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
385404 ) ,
386405
387406 // 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) ) ,
389408 gated ! (
390409 link_ordinal, Normal , template!( List : "ordinal" ) , ErrorPreceding , raw_dylib,
391410 experimental!( link_ordinal)
@@ -394,6 +413,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
394413 // Plugins:
395414 BuiltinAttribute {
396415 name : sym:: plugin,
416+ only_local : false ,
397417 type_ : CrateLevel ,
398418 template : template ! ( List : "name" ) ,
399419 duplicates : DuplicatesOk ,
@@ -475,7 +495,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
475495 ) ,
476496 // DuplicatesOk since it has its own validation
477497 ungated ! (
478- stable, Normal , template!( List : r#"feature = "name", since = "version""# ) , DuplicatesOk
498+ stable, Normal , template!( List : r#"feature = "name", since = "version""# ) , DuplicatesOk ,
479499 ) ,
480500 ungated ! (
481501 unstable, Normal ,
@@ -546,11 +566,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
546566 // ==========================================================================
547567
548568 gated ! (
549- linkage, Normal , template!( NameValueStr : "external|internal|..." ) , ErrorPreceding ,
569+ linkage, Normal , template!( NameValueStr : "external|internal|..." ) , ErrorPreceding , @only_local : true ,
550570 "the `linkage` attribute is experimental and not portable across platforms" ,
551571 ) ,
552572 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
554574 ) ,
555575
556576 // ==========================================================================
@@ -633,7 +653,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
633653 // Internal attributes, Misc:
634654 // ==========================================================================
635655 gated ! (
636- lang, Normal , template!( NameValueStr : "name" ) , DuplicatesOk , lang_items,
656+ lang, Normal , template!( NameValueStr : "name" ) , DuplicatesOk , @only_local : true , lang_items,
637657 "language items are subject to change" ,
638658 ) ,
639659 rustc_attr ! (
@@ -642,11 +662,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
642662 "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
643663 ) ,
644664 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 ,
646666 "#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
647667 ) ,
648668 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 ,
650670 "#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
651671 ) ,
652672 rustc_attr ! (
@@ -656,6 +676,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
656676 ) ,
657677 BuiltinAttribute {
658678 name : sym:: rustc_diagnostic_item,
679+ // FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
680+ only_local : false ,
659681 type_ : Normal ,
660682 template : template ! ( NameValueStr : "name" ) ,
661683 duplicates : ErrorFollowing ,
@@ -676,7 +698,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
676698 "unboxed_closures are still evolving" ,
677699 ) ,
678700 rustc_attr ! (
679- rustc_inherit_overflow_checks, Normal , template!( Word ) , WarnFollowing ,
701+ rustc_inherit_overflow_checks, Normal , template!( Word ) , WarnFollowing , @only_local : true ,
680702 "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
681703 overflow checking behavior of several libcore functions that are inlined \
682704 across crates and will never be stable",
@@ -778,6 +800,10 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
778800 BUILTIN_ATTRIBUTE_MAP . get ( & name) . is_some ( )
779801}
780802
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+
781807pub static BUILTIN_ATTRIBUTE_MAP : SyncLazy < FxHashMap < Symbol , & BuiltinAttribute > > =
782808 SyncLazy :: new ( || {
783809 let mut map = FxHashMap :: default ( ) ;
0 commit comments