@@ -6,6 +6,7 @@ use AttributeDuplicates::*;
66use AttributeGate :: * ;
77use AttributeType :: * ;
88use rustc_data_structures:: fx:: FxHashMap ;
9+ use rustc_span:: edition:: Edition ;
910use rustc_span:: { Symbol , sym} ;
1011
1112use crate :: { Features , Stability } ;
@@ -65,9 +66,12 @@ pub enum AttributeSafety {
6566 /// Normal attribute that does not need `#[unsafe(...)]`
6667 Normal ,
6768
68- /// Unsafe attribute that requires safety obligations
69- /// to be discharged
70- Unsafe ,
69+ /// Unsafe attribute that requires safety obligations to be discharged.
70+ ///
71+ /// An error is emitted when `#[unsafe(...)]` is omitted, except when the attribute's edition
72+ /// is less than the one stored in `unsafe_since`. This handles attributes that were safe in
73+ /// earlier editions, but become unsafe in later ones.
74+ Unsafe { unsafe_since : Option < Edition > } ,
7175}
7276
7377#[ derive( Clone , Copy ) ]
@@ -187,12 +191,23 @@ macro_rules! template {
187191}
188192
189193macro_rules! ungated {
194+ ( unsafe ( $edition: ident) $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $encode_cross_crate: expr $( , ) ?) => {
195+ BuiltinAttribute {
196+ name: sym:: $attr,
197+ encode_cross_crate: $encode_cross_crate,
198+ type_: $typ,
199+ safety: AttributeSafety :: Unsafe { unsafe_since: Some ( Edition :: $edition) } ,
200+ template: $tpl,
201+ gate: Ungated ,
202+ duplicates: $duplicates,
203+ }
204+ } ;
190205 ( unsafe $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $encode_cross_crate: expr $( , ) ?) => {
191206 BuiltinAttribute {
192207 name: sym:: $attr,
193208 encode_cross_crate: $encode_cross_crate,
194209 type_: $typ,
195- safety: AttributeSafety :: Unsafe ,
210+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
196211 template: $tpl,
197212 gate: Ungated ,
198213 duplicates: $duplicates,
@@ -217,7 +232,7 @@ macro_rules! gated {
217232 name: sym:: $attr,
218233 encode_cross_crate: $encode_cross_crate,
219234 type_: $typ,
220- safety: AttributeSafety :: Unsafe ,
235+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
221236 template: $tpl,
222237 duplicates: $duplicates,
223238 gate: Gated ( Stability :: Unstable , sym:: $gate, $msg, Features :: $gate) ,
@@ -228,7 +243,7 @@ macro_rules! gated {
228243 name: sym:: $attr,
229244 encode_cross_crate: $encode_cross_crate,
230245 type_: $typ,
231- safety: AttributeSafety :: Unsafe ,
246+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
232247 template: $tpl,
233248 duplicates: $duplicates,
234249 gate: Gated ( Stability :: Unstable , sym:: $attr, $msg, Features :: $attr) ,
@@ -423,9 +438,9 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
423438 ) ,
424439 ungated ! ( no_link, Normal , template!( Word ) , WarnFollowing , EncodeCrossCrate :: No ) ,
425440 ungated ! ( repr, Normal , template!( List : "C" ) , DuplicatesOk , EncodeCrossCrate :: No ) ,
426- ungated ! ( unsafe export_name, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding , EncodeCrossCrate :: No ) ,
427- ungated ! ( unsafe link_section, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding , EncodeCrossCrate :: No ) ,
428- ungated ! ( unsafe no_mangle, Normal , template!( Word ) , WarnFollowing , EncodeCrossCrate :: No ) ,
441+ ungated ! ( unsafe ( Edition2024 ) export_name, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding , EncodeCrossCrate :: No ) ,
442+ ungated ! ( unsafe ( Edition2024 ) link_section, Normal , template!( NameValueStr : "name" ) , FutureWarnPreceding , EncodeCrossCrate :: No ) ,
443+ ungated ! ( unsafe ( Edition2024 ) no_mangle, Normal , template!( Word ) , WarnFollowing , EncodeCrossCrate :: No ) ,
429444 ungated ! ( used, Normal , template!( Word , List : "compiler|linker" ) , WarnFollowing , EncodeCrossCrate :: No ) ,
430445 ungated ! ( link_ordinal, Normal , template!( List : "ordinal" ) , ErrorPreceding , EncodeCrossCrate :: Yes ) ,
431446
0 commit comments