@@ -6,7 +6,7 @@ use std::{fmt, iter};
66
77use arrayvec:: ArrayVec ;
88use rustc_ast_pretty:: pprust;
9- use rustc_attr:: { ConstStability , Deprecation , Stability , StabilityLevel , StableSince } ;
9+ use rustc_attr:: { ConstStability , Deprecation , Stability , StableSince } ;
1010use rustc_const_eval:: const_eval:: is_unstable_const_fn;
1111use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
1212use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -333,6 +333,8 @@ pub(crate) struct ItemInner {
333333 /// E.g., struct vs enum vs function.
334334 pub ( crate ) kind : ItemKind ,
335335 pub ( crate ) attrs : Attributes ,
336+ /// The effective stability, filled out by the `propagate-stability` pass.
337+ pub ( crate ) stability : Option < Stability > ,
336338}
337339
338340impl std:: ops:: Deref for Item {
@@ -381,46 +383,17 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
381383}
382384
383385impl 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.
384389 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
424397 }
425398
426399 pub ( crate ) fn const_stability ( & self , tcx : TyCtxt < ' _ > ) -> Option < ConstStability > {
@@ -502,7 +475,7 @@ impl Item {
502475
503476 Item {
504477 item_id : def_id. into ( ) ,
505- inner : Box :: new ( ItemInner { kind, attrs } ) ,
478+ inner : Box :: new ( ItemInner { kind, attrs, stability : None } ) ,
506479 name,
507480 cfg,
508481 inline_stmt_id : None ,
@@ -638,10 +611,7 @@ impl Item {
638611 }
639612
640613 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 ( ) )
645615 }
646616
647617 pub ( crate ) fn is_non_exhaustive ( & self ) -> bool {
0 commit comments