Skip to content

Commit 3c6fe0a

Browse files
committed
move stability structs' feature fields into StabilityLevel::Unstable
This is a refactor for supporting multiple unstable attributes on items. Seemingly, the `feature` field isn't used with the `StabilityLevel::Stable` variant, so I haven't included it. `rustc_passes::lib_features` uses the 'feature' meta-item for 'stable' attributes, but it extracts them itself, rather than relying on `rustc_attr`.
1 parent be01dab commit 3c6fe0a

File tree

6 files changed

+33
-41
lines changed

6 files changed

+33
-41
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub enum OptimizeAttr {
7070
#[derive(HashStable_Generic)]
7171
pub struct Stability {
7272
pub level: StabilityLevel,
73-
pub feature: Symbol,
7473
}
7574

7675
impl Stability {
@@ -92,7 +91,6 @@ impl Stability {
9291
#[derive(HashStable_Generic)]
9392
pub struct ConstStability {
9493
pub level: StabilityLevel,
95-
pub feature: Symbol,
9694
/// whether the function has a `#[rustc_promotable]` attribute
9795
pub promotable: bool,
9896
}
@@ -112,7 +110,6 @@ impl ConstStability {
112110
#[derive(HashStable_Generic)]
113111
pub struct DefaultBodyStability {
114112
pub level: StabilityLevel,
115-
pub feature: Symbol,
116113
}
117114

118115
/// The available stability levels.
@@ -121,6 +118,7 @@ pub struct DefaultBodyStability {
121118
pub enum StabilityLevel {
122119
/// `#[unstable]`
123120
Unstable {
121+
feature: Symbol,
124122
/// Reason for the current stability level.
125123
reason: UnstableReason,
126124
/// Relevant `rust-lang/rust` issue.
@@ -229,8 +227,8 @@ pub fn find_stability(
229227
break;
230228
}
231229

232-
if let Some((feature, level)) = parse_unstability(sess, attr) {
233-
stab = Some((Stability { level, feature }, attr.span));
230+
if let Some(level) = parse_unstability(sess, attr) {
231+
stab = Some((Stability { level }, attr.span));
234232
}
235233
}
236234
sym::stable => {
@@ -239,8 +237,8 @@ pub fn find_stability(
239237
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
240238
break;
241239
}
242-
if let Some((feature, level)) = parse_stability(sess, attr) {
243-
stab = Some((Stability { level, feature }, attr.span));
240+
if let Some(level) = parse_stability(sess, attr) {
241+
stab = Some((Stability { level }, attr.span));
244242
}
245243
}
246244
_ => {}
@@ -286,9 +284,8 @@ pub fn find_const_stability(
286284
break;
287285
}
288286

289-
if let Some((feature, level)) = parse_unstability(sess, attr) {
290-
const_stab =
291-
Some((ConstStability { level, feature, promotable: false }, attr.span));
287+
if let Some(level) = parse_unstability(sess, attr) {
288+
const_stab = Some((ConstStability { level, promotable: false }, attr.span));
292289
}
293290
}
294291
sym::rustc_const_stable => {
@@ -297,9 +294,8 @@ pub fn find_const_stability(
297294
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
298295
break;
299296
}
300-
if let Some((feature, level)) = parse_stability(sess, attr) {
301-
const_stab =
302-
Some((ConstStability { level, feature, promotable: false }, attr.span));
297+
if let Some(level) = parse_stability(sess, attr) {
298+
const_stab = Some((ConstStability { level, promotable: false }, attr.span));
303299
}
304300
}
305301
_ => {}
@@ -337,8 +333,8 @@ pub fn find_body_stability(
337333
break;
338334
}
339335

340-
if let Some((feature, level)) = parse_unstability(sess, attr) {
341-
body_stab = Some((DefaultBodyStability { level, feature }, attr.span));
336+
if let Some(level) = parse_unstability(sess, attr) {
337+
body_stab = Some((DefaultBodyStability { level }, attr.span));
342338
}
343339
}
344340
}
@@ -364,7 +360,7 @@ fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -
364360

365361
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
366362
/// its stability information.
367-
fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
363+
fn parse_stability(sess: &Session, attr: &Attribute) -> Option<StabilityLevel> {
368364
let meta = attr.meta()?;
369365
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
370366

@@ -418,17 +414,16 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
418414
};
419415

420416
match feature {
421-
Ok(feature) => {
422-
let level = StabilityLevel::Stable { since, allowed_through_unstable_modules: false };
423-
Some((feature, level))
417+
Ok(_feature) => {
418+
Some(StabilityLevel::Stable { since, allowed_through_unstable_modules: false })
424419
}
425420
Err(ErrorGuaranteed { .. }) => None,
426421
}
427422
}
428423

429424
/// Read the content of a `unstable`/`rustc_const_unstable`/`rustc_default_body_unstable`
430425
/// attribute, and return the feature name and its stability information.
431-
fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
426+
fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<StabilityLevel> {
432427
let meta = attr.meta()?;
433428
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
434429

@@ -508,12 +503,13 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
508503
match (feature, issue) {
509504
(Ok(feature), Ok(_)) => {
510505
let level = StabilityLevel::Unstable {
506+
feature,
511507
reason: UnstableReason::from_opt_reason(reason),
512508
issue: issue_num,
513509
is_soft,
514510
implied_by,
515511
};
516-
Some((feature, level))
512+
Some(level)
517513
}
518514
(Err(ErrorGuaranteed { .. }), _) | (_, Err(ErrorGuaranteed { .. })) => None,
519515
}

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<(Symbol, O
1111
if tcx.is_const_fn_raw(def_id) {
1212
let const_stab = tcx.lookup_const_stability(def_id)?;
1313
match const_stab.level {
14-
attr::StabilityLevel::Unstable { implied_by, .. } => {
15-
Some((const_stab.feature, implied_by))
14+
attr::StabilityLevel::Unstable { feature, implied_by, .. } => {
15+
Some((feature, implied_by))
1616
}
1717
attr::StabilityLevel::Stable { .. } => None,
1818
}

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ impl<'tcx> TyCtxt<'tcx> {
414414

415415
match stability {
416416
Some(Stability {
417-
level: attr::Unstable { reason, issue, is_soft, implied_by },
418-
feature,
419-
..
417+
level: attr::Unstable { feature, reason, issue, is_soft, implied_by },
420418
}) => {
421419
if span.allows_unstable(feature) {
422420
debug!("stability: skipping span={:?} since it is internal", span);
@@ -502,8 +500,7 @@ impl<'tcx> TyCtxt<'tcx> {
502500

503501
match stability {
504502
Some(DefaultBodyStability {
505-
level: attr::Unstable { reason, issue, is_soft, .. },
506-
feature,
503+
level: attr::Unstable { feature, reason, issue, is_soft, .. },
507504
}) => {
508505
if span.allows_unstable(feature) {
509506
debug!("body stability: skipping span={:?} since it is internal", span);

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,10 +3110,13 @@ impl<'tcx> TyCtxt<'tcx> {
31103110
pub fn is_const_fn(self, def_id: DefId) -> bool {
31113111
if self.is_const_fn_raw(def_id) {
31123112
match self.lookup_const_stability(def_id) {
3113-
Some(stability) if stability.is_const_unstable() => {
3113+
Some(rustc_attr::ConstStability {
3114+
level: rustc_attr::Unstable { feature, .. },
3115+
..
3116+
}) => {
31143117
// has a `rustc_const_unstable` attribute, check whether the user enabled the
31153118
// corresponding feature gate.
3116-
self.features().enabled(stability.feature)
3119+
self.features().enabled(feature)
31173120
}
31183121
// functions without const stability are either stable user written
31193122
// const fn or the user is using feature gates and we thus don't

compiler/rustc_passes/src/stability.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,18 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
247247
}
248248
}
249249

250-
if let Stability { level: Unstable { .. }, feature } = stab {
250+
if let Unstable { feature, .. } = stab.level {
251251
if ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some() {
252252
self.tcx
253253
.dcx()
254254
.emit_err(errors::UnstableAttrForAlreadyStableFeature { span, item_sp });
255255
}
256256
}
257-
if let Stability { level: Unstable { implied_by: Some(implied_by), .. }, feature } =
258-
stab
259-
{
257+
if let Unstable { feature, implied_by: Some(implied_by), .. } = stab.level {
260258
self.index.implications.insert(implied_by, feature);
261259
}
262260

263-
if let Some(ConstStability { level: Unstable { .. }, feature, .. }) = const_stab {
261+
if let Some(ConstStability { level: Unstable { feature, .. }, .. }) = const_stab {
264262
if ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some() {
265263
self.tcx.dcx().emit_err(errors::UnstableAttrForAlreadyStableFeature {
266264
span: const_span.unwrap(), // If const_stab contains Some(..), same is true for const_span
@@ -269,8 +267,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
269267
}
270268
}
271269
if let Some(ConstStability {
272-
level: Unstable { implied_by: Some(implied_by), .. },
273-
feature,
270+
level: Unstable { feature, implied_by: Some(implied_by), .. },
274271
..
275272
}) = const_stab
276273
{
@@ -656,12 +653,12 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
656653
if tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
657654
let stability = Stability {
658655
level: attr::StabilityLevel::Unstable {
656+
feature: sym::rustc_private,
659657
reason: UnstableReason::Default,
660658
issue: NonZero::new(27812),
661659
is_soft: false,
662660
implied_by: None,
663661
},
664-
feature: sym::rustc_private,
665662
};
666663
annotator.parent_stab = Some(stability);
667664
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,10 +1003,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10031003
) {
10041004
let span = path.span;
10051005
if let Some(stability) = &ext.stability {
1006-
if let StabilityLevel::Unstable { reason, issue, is_soft, implied_by } = stability.level
1006+
if let StabilityLevel::Unstable { feature, reason, issue, is_soft, implied_by } =
1007+
stability.level
10071008
{
1008-
let feature = stability.feature;
1009-
10101009
let is_allowed =
10111010
|feature| self.tcx.features().enabled(feature) || span.allows_unstable(feature);
10121011
let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));

0 commit comments

Comments
 (0)