@@ -70,7 +70,6 @@ pub enum OptimizeAttr {
70
70
#[ derive( HashStable_Generic ) ]
71
71
pub struct Stability {
72
72
pub level : StabilityLevel ,
73
- pub feature : Symbol ,
74
73
}
75
74
76
75
impl Stability {
@@ -91,10 +90,7 @@ impl Stability {
91
90
#[ derive( Encodable , Decodable , Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
92
91
#[ derive( HashStable_Generic ) ]
93
92
pub struct ConstStability {
94
- pub level : StabilityLevel ,
95
- /// This can be `None` for functions that do not have an explicit const feature.
96
- /// We still track them for recursive const stability checks.
97
- pub feature : Option < Symbol > ,
93
+ pub level : ConstStabilityLevel ,
98
94
/// This is true iff the `const_stable_indirect` attribute is present.
99
95
pub const_stable_indirect : bool ,
100
96
/// whether the function has a `#[rustc_promotable]` attribute
@@ -103,11 +99,15 @@ pub struct ConstStability {
103
99
104
100
impl ConstStability {
105
101
pub fn is_const_unstable ( & self ) -> bool {
106
- self . level . is_unstable ( )
102
+ matches ! ( self . level, ConstStabilityLevel :: Unstable { .. } )
107
103
}
108
104
109
105
pub fn is_const_stable ( & self ) -> bool {
110
- self . level . is_stable ( )
106
+ matches ! ( self . level, ConstStabilityLevel :: Stable { .. } )
107
+ }
108
+
109
+ pub fn is_implicit ( & self ) -> bool {
110
+ matches ! ( self . level, ConstStabilityLevel :: Implicit )
111
111
}
112
112
}
113
113
@@ -116,7 +116,6 @@ impl ConstStability {
116
116
#[ derive( HashStable_Generic ) ]
117
117
pub struct DefaultBodyStability {
118
118
pub level : StabilityLevel ,
119
- pub feature : Symbol ,
120
119
}
121
120
122
121
/// The available stability levels.
@@ -125,31 +124,9 @@ pub struct DefaultBodyStability {
125
124
pub enum StabilityLevel {
126
125
/// `#[unstable]`
127
126
Unstable {
128
- /// Reason for the current stability level.
129
- reason : UnstableReason ,
130
- /// Relevant `rust-lang/rust` issue.
131
- issue : Option < NonZero < u32 > > ,
127
+ /// The information unique to each `#[unstable]` attribute
128
+ unstables : Unstability ,
132
129
is_soft : bool ,
133
- /// If part of a feature is stabilized and a new feature is added for the remaining parts,
134
- /// then the `implied_by` attribute is used to indicate which now-stable feature previously
135
- /// contained an item.
136
- ///
137
- /// ```pseudo-Rust
138
- /// #[unstable(feature = "foo", issue = "...")]
139
- /// fn foo() {}
140
- /// #[unstable(feature = "foo", issue = "...")]
141
- /// fn foobar() {}
142
- /// ```
143
- ///
144
- /// ...becomes...
145
- ///
146
- /// ```pseudo-Rust
147
- /// #[stable(feature = "foo", since = "1.XX.X")]
148
- /// fn foo() {}
149
- /// #[unstable(feature = "foobar", issue = "...", implied_by = "foo")]
150
- /// fn foobar() {}
151
- /// ```
152
- implied_by : Option < Symbol > ,
153
130
} ,
154
131
/// `#[stable]`
155
132
Stable {
@@ -161,6 +138,20 @@ pub enum StabilityLevel {
161
138
} ,
162
139
}
163
140
141
+ /// The available const-stability levels for const functions.
142
+ /// For details see [#131349](https://github.com/rust-lang/rust/pull/131349).
143
+ #[ derive( Encodable , Decodable , PartialEq , Copy , Clone , Debug , Eq , Hash ) ]
144
+ #[ derive( HashStable_Generic ) ]
145
+ pub enum ConstStabilityLevel {
146
+ /// For functions declared const-stable
147
+ Stable { since : StableSince } ,
148
+ /// For functions declared const-unstable
149
+ Unstable { unstables : Unstability } ,
150
+ /// For functions with no explicit const-stability attribute that require checking recursive
151
+ /// const stability. This is either an unmarked const fn or a `const_stable_indirect` intrinsic.
152
+ Implicit ,
153
+ }
154
+
164
155
/// Rust release in which a feature is stabilized.
165
156
#[ derive( Encodable , Decodable , PartialEq , Copy , Clone , Debug , Eq , PartialOrd , Ord , Hash ) ]
166
157
#[ derive( HashStable_Generic ) ]
@@ -187,6 +178,37 @@ impl StabilityLevel {
187
178
}
188
179
}
189
180
181
+ /// An instance of an `#[unstable]`, `#[rustc_const_unstable]`, or similar attribute
182
+ #[ derive( Encodable , Decodable , PartialEq , Copy , Clone , Debug , Eq , Hash ) ]
183
+ #[ derive( HashStable_Generic ) ]
184
+ pub struct Unstability {
185
+ pub feature : Symbol ,
186
+ /// Reason for the current stability level.
187
+ pub reason : UnstableReason ,
188
+ /// Relevant `rust-lang/rust` issue.
189
+ pub issue : Option < NonZero < u32 > > ,
190
+ /// If part of a feature is stabilized and a new feature is added for the remaining parts,
191
+ /// then the `implied_by` attribute is used to indicate which now-stable feature previously
192
+ /// contained an item.
193
+ ///
194
+ /// ```pseudo-Rust
195
+ /// #[unstable(feature = "foo", issue = "...")]
196
+ /// fn foo() {}
197
+ /// #[unstable(feature = "foo", issue = "...")]
198
+ /// fn foobar() {}
199
+ /// ```
200
+ ///
201
+ /// ...becomes...
202
+ ///
203
+ /// ```pseudo-Rust
204
+ /// #[stable(feature = "foo", since = "1.XX.X")]
205
+ /// fn foo() {}
206
+ /// #[unstable(feature = "foobar", issue = "...", implied_by = "foo")]
207
+ /// fn foobar() {}
208
+ /// ```
209
+ pub implied_by : Option < Symbol > ,
210
+ }
211
+
190
212
#[ derive( Encodable , Decodable , PartialEq , Copy , Clone , Debug , Eq , Hash ) ]
191
213
#[ derive( HashStable_Generic ) ]
192
214
pub enum UnstableReason {
@@ -233,8 +255,8 @@ pub fn find_stability(
233
255
break ;
234
256
}
235
257
236
- if let Some ( ( feature , level) ) = parse_unstability ( sess, attr) {
237
- stab = Some ( ( Stability { level, feature } , attr. span ) ) ;
258
+ if let Some ( level) = parse_unstability ( sess, attr) {
259
+ stab = Some ( ( Stability { level } , attr. span ) ) ;
238
260
}
239
261
}
240
262
sym:: stable => {
@@ -243,8 +265,8 @@ pub fn find_stability(
243
265
. emit_err ( session_diagnostics:: MultipleStabilityLevels { span : attr. span } ) ;
244
266
break ;
245
267
}
246
- if let Some ( ( feature , level) ) = parse_stability ( sess, attr) {
247
- stab = Some ( ( Stability { level, feature } , attr. span ) ) ;
268
+ if let Some ( level) = parse_stability ( sess, attr) {
269
+ stab = Some ( ( Stability { level } , attr. span ) ) ;
248
270
}
249
271
}
250
272
_ => { }
@@ -283,6 +305,10 @@ pub fn find_const_stability(
283
305
let mut const_stab: Option < ( ConstStability , Span ) > = None ;
284
306
let mut promotable = false ;
285
307
let mut const_stable_indirect = None ;
308
+ let const_stability_level = |level| match level {
309
+ StabilityLevel :: Unstable { unstables, .. } => ConstStabilityLevel :: Unstable { unstables } ,
310
+ StabilityLevel :: Stable { since, .. } => ConstStabilityLevel :: Stable { since } ,
311
+ } ;
286
312
287
313
for attr in attrs {
288
314
match attr. name_or_empty ( ) {
@@ -295,11 +321,10 @@ pub fn find_const_stability(
295
321
break ;
296
322
}
297
323
298
- if let Some ( ( feature , level) ) = parse_unstability ( sess, attr) {
324
+ if let Some ( level) = parse_unstability ( sess, attr) {
299
325
const_stab = Some ( (
300
326
ConstStability {
301
- level,
302
- feature : Some ( feature) ,
327
+ level : const_stability_level ( level) ,
303
328
const_stable_indirect : false ,
304
329
promotable : false ,
305
330
} ,
@@ -313,11 +338,10 @@ pub fn find_const_stability(
313
338
. emit_err ( session_diagnostics:: MultipleStabilityLevels { span : attr. span } ) ;
314
339
break ;
315
340
}
316
- if let Some ( ( feature , level) ) = parse_stability ( sess, attr) {
341
+ if let Some ( level) = parse_stability ( sess, attr) {
317
342
const_stab = Some ( (
318
343
ConstStability {
319
- level,
320
- feature : Some ( feature) ,
344
+ level : const_stability_level ( level) ,
321
345
const_stable_indirect : false ,
322
346
promotable : false ,
323
347
} ,
@@ -365,15 +389,9 @@ pub fn find_const_stability(
365
389
// staged_api crate.
366
390
if ( is_const_fn || const_stable_indirect. is_some ( ) ) && const_stab. is_none ( ) {
367
391
let c = ConstStability {
368
- feature : None ,
392
+ level : ConstStabilityLevel :: Implicit ,
369
393
const_stable_indirect : const_stable_indirect. is_some ( ) ,
370
394
promotable : false ,
371
- level : StabilityLevel :: Unstable {
372
- reason : UnstableReason :: Default ,
373
- issue : None ,
374
- is_soft : false ,
375
- implied_by : None ,
376
- } ,
377
395
} ;
378
396
const_stab = Some ( ( c, const_stable_indirect. unwrap_or ( DUMMY_SP ) ) ) ;
379
397
}
@@ -397,8 +415,8 @@ pub fn find_body_stability(
397
415
break ;
398
416
}
399
417
400
- if let Some ( ( feature , level) ) = parse_unstability ( sess, attr) {
401
- body_stab = Some ( ( DefaultBodyStability { level, feature } , attr. span ) ) ;
418
+ if let Some ( level) = parse_unstability ( sess, attr) {
419
+ body_stab = Some ( ( DefaultBodyStability { level } , attr. span ) ) ;
402
420
}
403
421
}
404
422
}
@@ -424,7 +442,7 @@ fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -
424
442
425
443
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
426
444
/// its stability information.
427
- fn parse_stability ( sess : & Session , attr : & Attribute ) -> Option < ( Symbol , StabilityLevel ) > {
445
+ fn parse_stability ( sess : & Session , attr : & Attribute ) -> Option < StabilityLevel > {
428
446
let meta = attr. meta ( ) ?;
429
447
let MetaItem { kind : MetaItemKind :: List ( ref metas) , .. } = meta else { return None } ;
430
448
@@ -478,17 +496,16 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
478
496
} ;
479
497
480
498
match feature {
481
- Ok ( feature) => {
482
- let level = StabilityLevel :: Stable { since, allowed_through_unstable_modules : false } ;
483
- Some ( ( feature, level) )
499
+ Ok ( _feature) => {
500
+ Some ( StabilityLevel :: Stable { since, allowed_through_unstable_modules : false } )
484
501
}
485
502
Err ( ErrorGuaranteed { .. } ) => None ,
486
503
}
487
504
}
488
505
489
506
/// Read the content of a `unstable`/`rustc_const_unstable`/`rustc_default_body_unstable`
490
507
/// attribute, and return the feature name and its stability information.
491
- fn parse_unstability ( sess : & Session , attr : & Attribute ) -> Option < ( Symbol , StabilityLevel ) > {
508
+ fn parse_unstability ( sess : & Session , attr : & Attribute ) -> Option < StabilityLevel > {
492
509
let meta = attr. meta ( ) ?;
493
510
let MetaItem { kind : MetaItemKind :: List ( ref metas) , .. } = meta else { return None } ;
494
511
@@ -568,12 +585,15 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
568
585
match ( feature, issue) {
569
586
( Ok ( feature) , Ok ( _) ) => {
570
587
let level = StabilityLevel :: Unstable {
571
- reason : UnstableReason :: from_opt_reason ( reason) ,
572
- issue : issue_num,
588
+ unstables : Unstability {
589
+ feature,
590
+ reason : UnstableReason :: from_opt_reason ( reason) ,
591
+ issue : issue_num,
592
+ implied_by,
593
+ } ,
573
594
is_soft,
574
- implied_by,
575
595
} ;
576
- Some ( ( feature , level) )
596
+ Some ( level)
577
597
}
578
598
( Err ( ErrorGuaranteed { .. } ) , _) | ( _, Err ( ErrorGuaranteed { .. } ) ) => None ,
579
599
}
0 commit comments