@@ -6,7 +6,7 @@ use std::{fmt, iter};
6
6
7
7
use arrayvec:: ArrayVec ;
8
8
use rustc_ast_pretty:: pprust;
9
- use rustc_attr:: { ConstStability , Deprecation , Stability , StabilityLevel , StableSince } ;
9
+ use rustc_attr:: { ConstStability , Deprecation , Stability , StableSince } ;
10
10
use rustc_const_eval:: const_eval:: is_unstable_const_fn;
11
11
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
12
12
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -333,6 +333,8 @@ pub(crate) struct ItemInner {
333
333
/// E.g., struct vs enum vs function.
334
334
pub ( crate ) kind : ItemKind ,
335
335
pub ( crate ) attrs : Attributes ,
336
+ /// The effective stability, filled out by the `propagate-stability` pass.
337
+ pub ( crate ) stability : Option < Stability > ,
336
338
}
337
339
338
340
impl std:: ops:: Deref for Item {
@@ -381,46 +383,17 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
381
383
}
382
384
383
385
impl Item {
386
+ /// Returns the effective stability of the item.
387
+ ///
388
+ /// This method should only be called after the `propagate-stability` pass has been run.
384
389
pub ( crate ) fn stability ( & self , tcx : TyCtxt < ' _ > ) -> Option < Stability > {
385
- let ( mut def_id, mut stability) = if let Some ( inlined) = self . inline_stmt_id {
386
- let inlined_def_id = inlined. to_def_id ( ) ;
387
- if let Some ( stability) = tcx. lookup_stability ( inlined_def_id) {
388
- ( inlined_def_id, stability)
389
- } else {
390
- // For re-exports into crates without `staged_api`, reuse the original stability.
391
- // This is necessary, because we always want to mark unstable items.
392
- let def_id = self . def_id ( ) ?;
393
- return tcx. lookup_stability ( def_id) ;
394
- }
395
- } else {
396
- let def_id = self . def_id ( ) ?;
397
- let stability = tcx. lookup_stability ( def_id) ?;
398
- ( def_id, stability)
399
- } ;
400
-
401
- let StabilityLevel :: Stable { mut since, allowed_through_unstable_modules : false } =
402
- stability. level
403
- else {
404
- return Some ( stability) ;
405
- } ;
406
-
407
- // If any of the item's ancestors was stabilized later or is still unstable,
408
- // then report the ancestor's stability instead.
409
- while let Some ( parent_def_id) = tcx. opt_parent ( def_id) {
410
- if let Some ( parent_stability) = tcx. lookup_stability ( parent_def_id) {
411
- match parent_stability. level {
412
- StabilityLevel :: Unstable { .. } => return Some ( parent_stability) ,
413
- StabilityLevel :: Stable { since : parent_since, .. } => {
414
- if parent_since > since {
415
- stability = parent_stability;
416
- since = parent_since;
417
- }
418
- }
419
- }
420
- }
421
- def_id = parent_def_id;
422
- }
423
- Some ( stability)
390
+ let stability = self . inner . stability ;
391
+ debug_assert ! (
392
+ stability. is_some( )
393
+ || self . def_id( ) . is_none_or( |did| tcx. lookup_stability( did) . is_none( ) ) ,
394
+ "missing stability for cleaned item: {self:?}" ,
395
+ ) ;
396
+ stability
424
397
}
425
398
426
399
pub ( crate ) fn const_stability ( & self , tcx : TyCtxt < ' _ > ) -> Option < ConstStability > {
@@ -502,7 +475,7 @@ impl Item {
502
475
503
476
Item {
504
477
item_id : def_id. into ( ) ,
505
- inner : Box :: new ( ItemInner { kind, attrs } ) ,
478
+ inner : Box :: new ( ItemInner { kind, attrs, stability : None } ) ,
506
479
name,
507
480
cfg,
508
481
inline_stmt_id : None ,
@@ -638,10 +611,7 @@ impl Item {
638
611
}
639
612
640
613
pub ( crate ) fn stable_since ( & self , tcx : TyCtxt < ' _ > ) -> Option < StableSince > {
641
- match self . stability ( tcx) ?. level {
642
- StabilityLevel :: Stable { since, .. } => Some ( since) ,
643
- StabilityLevel :: Unstable { .. } => None ,
644
- }
614
+ self . stability ( tcx) . and_then ( |stability| stability. stable_since ( ) )
645
615
}
646
616
647
617
pub ( crate ) fn is_non_exhaustive ( & self ) -> bool {
0 commit comments