Skip to content

Commit 2fdeb3b

Browse files
author
Lukas Markeffsky
committed
rustdoc: inherit parent's stability where applicable
1 parent 67bb749 commit 2fdeb3b

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub enum StabilityLevel {
153153
}
154154

155155
/// Rust release in which a feature is stabilized.
156-
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
156+
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, PartialOrd, Ord, Hash)]
157157
#[derive(HashStable_Generic)]
158158
pub enum StableSince {
159159
Version(RustcVersion),

src/librustdoc/clean/types.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,45 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
384384

385385
impl Item {
386386
pub(crate) fn stability(&self, tcx: TyCtxt<'_>) -> Option<Stability> {
387-
self.def_id().and_then(|did| tcx.lookup_stability(did))
387+
let (mut def_id, mut stability) = if let Some(inlined) = self.inline_stmt_id {
388+
let inlined_def_id = inlined.to_def_id();
389+
if let Some(stability) = tcx.lookup_stability(inlined_def_id) {
390+
(inlined_def_id, stability)
391+
} else {
392+
// For re-exports into crates without `staged_api`, reuse the original stability.
393+
// This is necessary, because we always want to mark unstable items.
394+
let def_id = self.def_id()?;
395+
return tcx.lookup_stability(def_id);
396+
}
397+
} else {
398+
let def_id = self.def_id()?;
399+
let stability = tcx.lookup_stability(def_id)?;
400+
(def_id, stability)
401+
};
402+
403+
let StabilityLevel::Stable { mut since, allowed_through_unstable_modules: false } =
404+
stability.level
405+
else {
406+
return Some(stability);
407+
};
408+
409+
// If any of the item's ancestors was stabilized later or is still unstable,
410+
// then report the ancestor's stability instead.
411+
while let Some(parent_def_id) = tcx.opt_parent(def_id) {
412+
if let Some(parent_stability) = tcx.lookup_stability(parent_def_id) {
413+
match parent_stability.level {
414+
StabilityLevel::Unstable { .. } => return Some(parent_stability),
415+
StabilityLevel::Stable { since: parent_since, .. } => {
416+
if parent_since > since {
417+
stability = parent_stability;
418+
since = parent_since;
419+
}
420+
}
421+
}
422+
}
423+
def_id = parent_def_id;
424+
}
425+
Some(stability)
388426
}
389427

390428
pub(crate) fn const_stability(&self, tcx: TyCtxt<'_>) -> Option<ConstStability> {

tests/rustdoc/const-display.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![crate_name = "foo"]
22

3-
#![unstable(feature = "humans",
4-
reason = "who ever let humans program computers, we're apparently really bad at it",
5-
issue = "none")]
3+
#![stable(feature = "rust1", since = "1.0.0")]
64

75
#![feature(foo, foo2)]
86
#![feature(staged_api)]
@@ -48,10 +46,18 @@ pub const unsafe fn foo2_gated() -> u32 { 42 }
4846
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
4947
pub const unsafe fn bar2_gated() -> u32 { 42 }
5048

51-
//@ has 'foo/fn.bar_not_gated.html' '//pre' 'pub const unsafe fn bar_not_gated() -> u32'
52-
//@ !hasraw - '//span[@class="since"]'
53-
pub const unsafe fn bar_not_gated() -> u32 { 42 }
49+
#[unstable(
50+
feature = "humans",
51+
reason = "who ever let humans program computers, we're apparently really bad at it",
52+
issue = "none",
53+
)]
54+
pub mod unstable {
55+
//@ has 'foo/unstable/fn.bar_not_gated.html' '//pre' 'pub const unsafe fn bar_not_gated() -> u32'
56+
//@ !hasraw - '//span[@class="since"]'
57+
pub const unsafe fn bar_not_gated() -> u32 { 42 }
58+
}
5459

60+
#[stable(feature = "rust1", since = "1.0.0")]
5561
pub struct Foo;
5662

5763
impl Foo {

tests/rustdoc/stability.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(staged_api)]
22

3-
#![unstable(feature = "test", issue = "none")]
3+
#![stable(feature = "rust1", since = "1.0.0")]
44

55
//@ has stability/index.html
66
//@ has - '//ul[@class="item-table"]/li[1]//a' AaStable
@@ -10,6 +10,7 @@
1010
#[stable(feature = "rust2", since = "2.2.2")]
1111
pub struct AaStable;
1212

13+
#[unstable(feature = "test", issue = "none")]
1314
pub struct Unstable {
1415
//@ has stability/struct.Unstable.html \
1516
// '//span[@class="item-info"]//div[@class="stab unstable"]' \
@@ -21,3 +22,31 @@ pub struct Unstable {
2122

2223
#[stable(feature = "rust2", since = "2.2.2")]
2324
pub struct ZzStable;
25+
26+
#[unstable(feature = "unstable", issue = "none")]
27+
pub mod unstable {
28+
//@ !hasraw stability/unstable/struct.Foo.html '//span[@class="since"]'
29+
//@ has - '//div[@class="stab unstable"]' 'experimental'
30+
#[stable(feature = "rust1", since = "1.0.0")]
31+
pub struct Foo;
32+
}
33+
34+
#[stable(feature = "rust2", since = "2.2.2")]
35+
pub mod stable_later {
36+
//@ has stability/stable_later/struct.Bar.html '//span[@class="since"]' '2.2.2'
37+
#[stable(feature = "rust1", since = "1.0.0")]
38+
pub struct Bar;
39+
}
40+
41+
#[stable(feature = "rust1", since = "1.0.0")]
42+
pub mod stable_earlier {
43+
//@ has stability/stable_earlier/struct.Foo.html '//span[@class="since"]' '1.0.0'
44+
#[doc(inline)]
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
pub use crate::unstable::Foo;
47+
48+
//@ has stability/stable_earlier/struct.Bar.html '//span[@class="since"]' '1.0.0'
49+
#[doc(inline)]
50+
#[stable(feature = "rust1", since = "1.0.0")]
51+
pub use crate::stable_later::Bar;
52+
}

0 commit comments

Comments
 (0)