rustdoc/passes/
propagate_stability.rs

1//! Propagates stability to child items.
2//!
3//! The purpose of this pass is to make items whose parents are "more unstable"
4//! than the item itself inherit the parent's stability.
5//! For example, [`core::error::Error`] is marked as stable since 1.0.0, but the
6//! [`core::error`] module is marked as stable since 1.81.0, so we want to show
7//! [`core::error::Error`] as stable since 1.81.0 as well.
8
9use rustc_attr_parsing::{Stability, StabilityLevel};
10use rustc_hir::def_id::CRATE_DEF_ID;
11
12use crate::clean::{Crate, Item, ItemId, ItemKind};
13use crate::core::DocContext;
14use crate::fold::DocFolder;
15use crate::passes::Pass;
16
17pub(crate) const PROPAGATE_STABILITY: Pass = Pass {
18    name: "propagate-stability",
19    run: Some(propagate_stability),
20    description: "propagates stability to child items",
21};
22
23pub(crate) fn propagate_stability(cr: Crate, cx: &mut DocContext<'_>) -> Crate {
24    let crate_stability = cx.tcx.lookup_stability(CRATE_DEF_ID);
25    StabilityPropagator { parent_stability: crate_stability, cx }.fold_crate(cr)
26}
27
28struct StabilityPropagator<'a, 'tcx> {
29    parent_stability: Option<Stability>,
30    cx: &'a mut DocContext<'tcx>,
31}
32
33impl DocFolder for StabilityPropagator<'_, '_> {
34    fn fold_item(&mut self, mut item: Item) -> Option<Item> {
35        let parent_stability = self.parent_stability;
36
37        let stability = match item.item_id {
38            ItemId::DefId(def_id) => {
39                let item_stability = self.cx.tcx.lookup_stability(def_id);
40                let inline_stability =
41                    item.inline_stmt_id.and_then(|did| self.cx.tcx.lookup_stability(did));
42                let is_glob_export = item.inline_stmt_id.map(|id| {
43                    let hir_id = self.cx.tcx.local_def_id_to_hir_id(id);
44                    matches!(
45                        self.cx.tcx.hir_node(hir_id),
46                        rustc_hir::Node::Item(rustc_hir::Item {
47                            kind: rustc_hir::ItemKind::Use(_, rustc_hir::UseKind::Glob),
48                            ..
49                        })
50                    )
51                });
52                let own_stability = if let Some(item_stab) = item_stability
53                    && let StabilityLevel::Stable { since: _, allowed_through_unstable_modules } =
54                        item_stab.level
55                    && let Some(mut inline_stab) = inline_stability
56                    && let StabilityLevel::Stable {
57                        since: inline_since,
58                        allowed_through_unstable_modules: _,
59                    } = inline_stab.level
60                    && let Some(is_global_export) = is_glob_export
61                    && !is_global_export
62                {
63                    inline_stab.level = StabilityLevel::Stable {
64                        since: inline_since,
65                        allowed_through_unstable_modules,
66                    };
67                    Some(inline_stab)
68                } else {
69                    item_stability
70                };
71
72                let (ItemKind::StrippedItem(box kind) | kind) = &item.kind;
73                match kind {
74                    ItemKind::ExternCrateItem { .. }
75                    | ItemKind::ImportItem(..)
76                    | ItemKind::StructItem(..)
77                    | ItemKind::UnionItem(..)
78                    | ItemKind::EnumItem(..)
79                    | ItemKind::FunctionItem(..)
80                    | ItemKind::ModuleItem(..)
81                    | ItemKind::TypeAliasItem(..)
82                    | ItemKind::StaticItem(..)
83                    | ItemKind::TraitItem(..)
84                    | ItemKind::TraitAliasItem(..)
85                    | ItemKind::StructFieldItem(..)
86                    | ItemKind::VariantItem(..)
87                    | ItemKind::ForeignFunctionItem(..)
88                    | ItemKind::ForeignStaticItem(..)
89                    | ItemKind::ForeignTypeItem
90                    | ItemKind::MacroItem(..)
91                    | ItemKind::ProcMacroItem(..)
92                    | ItemKind::ConstantItem(..) => {
93                        // If any of the item's parents was stabilized later or is still unstable,
94                        // then use the parent's stability instead.
95                        merge_stability(own_stability, parent_stability)
96                    }
97
98                    // Don't inherit the parent's stability for these items, because they
99                    // are potentially accessible even if the parent is more unstable.
100                    ItemKind::ImplItem(..)
101                    | ItemKind::RequiredMethodItem(..)
102                    | ItemKind::MethodItem(..)
103                    | ItemKind::RequiredAssocConstItem(..)
104                    | ItemKind::ProvidedAssocConstItem(..)
105                    | ItemKind::ImplAssocConstItem(..)
106                    | ItemKind::RequiredAssocTypeItem(..)
107                    | ItemKind::AssocTypeItem(..)
108                    | ItemKind::PrimitiveItem(..)
109                    | ItemKind::KeywordItem => own_stability,
110
111                    ItemKind::StrippedItem(..) => unreachable!(),
112                }
113            }
114            ItemId::Auto { .. } | ItemId::Blanket { .. } => {
115                // For now, we do now show stability for synthesized impls.
116                None
117            }
118        };
119
120        item.inner.stability = stability;
121        self.parent_stability = stability;
122        let item = self.fold_item_recur(item);
123        self.parent_stability = parent_stability;
124
125        Some(item)
126    }
127}
128
129fn merge_stability(
130    own_stability: Option<Stability>,
131    parent_stability: Option<Stability>,
132) -> Option<Stability> {
133    if let Some(own_stab) = own_stability
134        && let StabilityLevel::Stable { since: own_since, allowed_through_unstable_modules: None } =
135            own_stab.level
136        && let Some(parent_stab) = parent_stability
137        && (parent_stab.is_unstable()
138            || parent_stab.stable_since().is_some_and(|parent_since| parent_since > own_since))
139    {
140        parent_stability
141    } else if let Some(mut own_stab) = own_stability
142        && let StabilityLevel::Stable { since, allowed_through_unstable_modules: Some(_) } =
143            own_stab.level
144        && parent_stability.is_some_and(|stab| stab.is_stable())
145    {
146        // this property does not apply transitively through re-exports
147        own_stab.level = StabilityLevel::Stable { since, allowed_through_unstable_modules: None };
148        Some(own_stab)
149    } else {
150        own_stability
151    }
152}