Skip to content

Commit 9737f92

Browse files
authored
Rollup merge of rust-lang#130798 - lukas-code:doc-stab, r=notriddle
rustdoc: inherit parent's stability where applicable It is currently not possible for a re-export to have a different stability (rust-lang#30827). Therefore the standard library uses a hack when moving items like `std::error::Error` or `std::net::IpAddr` into `core` by marking the containing module (`core::error` / `core::net`) as unstable or stable in a later version than the items the module contains. Previously, rustdoc would always show the *stability as declared* for an item rather than the *stability as publicly reachable* (i.e. the features required to actually access the item), which could be confusing when viewing the docs. This PR changes it so that we show the stability of the first unstable parent or the most recently stabilized parent instead, to hopefully make things less confusing. fixes rust-lang#130765 screenshots: ![error in std](https://github.com/user-attachments/assets/2ab9bdb9-ed81-4e45-a832-ac7d3ba1be3f) ![error in core](https://github.com/user-attachments/assets/46f46182-5642-4ac5-b92e-0b99a8e2496d)
2 parents 7de1082 + 2fdeb3b commit 9737f92

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

Diff for: 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),

Diff for: 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> {

Diff for: 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 {

Diff for: 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)