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