@@ -6,6 +6,7 @@ use AttributeDuplicates::*;
6
6
use AttributeGate :: * ;
7
7
use AttributeType :: * ;
8
8
use rustc_data_structures:: fx:: FxHashMap ;
9
+ use rustc_span:: edition:: Edition ;
9
10
use rustc_span:: { Symbol , sym} ;
10
11
11
12
use crate :: { Features , Stability } ;
@@ -65,9 +66,12 @@ pub enum AttributeSafety {
65
66
/// Normal attribute that does not need `#[unsafe(...)]`
66
67
Normal ,
67
68
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 > } ,
71
75
}
72
76
73
77
#[ derive( Clone , Copy ) ]
@@ -187,12 +191,23 @@ macro_rules! template {
187
191
}
188
192
189
193
macro_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
+ } ;
190
205
( unsafe $attr: ident, $typ: expr, $tpl: expr, $duplicates: expr, $encode_cross_crate: expr $( , ) ?) => {
191
206
BuiltinAttribute {
192
207
name: sym:: $attr,
193
208
encode_cross_crate: $encode_cross_crate,
194
209
type_: $typ,
195
- safety: AttributeSafety :: Unsafe ,
210
+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
196
211
template: $tpl,
197
212
gate: Ungated ,
198
213
duplicates: $duplicates,
@@ -217,7 +232,7 @@ macro_rules! gated {
217
232
name: sym:: $attr,
218
233
encode_cross_crate: $encode_cross_crate,
219
234
type_: $typ,
220
- safety: AttributeSafety :: Unsafe ,
235
+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
221
236
template: $tpl,
222
237
duplicates: $duplicates,
223
238
gate: Gated ( Stability :: Unstable , sym:: $gate, $msg, Features :: $gate) ,
@@ -228,7 +243,7 @@ macro_rules! gated {
228
243
name: sym:: $attr,
229
244
encode_cross_crate: $encode_cross_crate,
230
245
type_: $typ,
231
- safety: AttributeSafety :: Unsafe ,
246
+ safety: AttributeSafety :: Unsafe { unsafe_since : None } ,
232
247
template: $tpl,
233
248
duplicates: $duplicates,
234
249
gate: Gated ( Stability :: Unstable , sym:: $attr, $msg, Features :: $attr) ,
@@ -423,9 +438,9 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
423
438
) ,
424
439
ungated ! ( no_link, Normal , template!( Word ) , WarnFollowing , EncodeCrossCrate :: No ) ,
425
440
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 ) ,
429
444
ungated ! ( used, Normal , template!( Word , List : "compiler|linker" ) , WarnFollowing , EncodeCrossCrate :: No ) ,
430
445
ungated ! ( link_ordinal, Normal , template!( List : "ordinal" ) , ErrorPreceding , EncodeCrossCrate :: Yes ) ,
431
446
0 commit comments