Skip to content

Commit 759a6df

Browse files
committed
rustdoc-json: Expose Item stability
This is motivated by cargo-semver-checks wanting to work on the standard library, but that requires being able to filter out breaking changes to unstable items, so it can only flag breaking changes to stable items. This in turn is motivated by https://www.github.com/rust-lang/rust/issues/153486.
1 parent cf7da0b commit 759a6df

6 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/librustdoc/json/conversions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl JsonRenderer<'_> {
7575
name: name.map(|sym| sym.to_string()),
7676
span: span.and_then(|span| span.into_json(self)),
7777
visibility: visibility.into_json(self),
78+
stability: item.stability(self.tcx).into_json(self),
7879
docs,
7980
attrs,
8081
deprecation: deprecation.into_json(self),
@@ -203,6 +204,18 @@ impl FromClean<attrs::Deprecation> for Deprecation {
203204
}
204205
}
205206

207+
impl FromClean<hir::Stability> for Stability {
208+
fn from_clean(stab: &hir::Stability, _renderer: &JsonRenderer<'_>) -> Self {
209+
Stability {
210+
feature: stab.feature.to_string(),
211+
level: match stab.level {
212+
hir::StabilityLevel::Unstable { .. } => StabilityLevel::Unstable,
213+
hir::StabilityLevel::Stable { .. } => StabilityLevel::Stable,
214+
}
215+
}
216+
}
217+
}
218+
206219
impl FromClean<clean::GenericArgs> for Option<Box<GenericArgs>> {
207220
fn from_clean(generic_args: &clean::GenericArgs, renderer: &JsonRenderer<'_>) -> Self {
208221
use clean::GenericArgs::*;

src/librustdoc/json/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,6 @@ mod size_asserts {
362362
// tidy-alphabetical-end
363363

364364
// These contains a `PathBuf`, which is different sizes on different OSes.
365-
static_assert_size!(Item, 528 + size_of::<std::path::PathBuf>());
365+
static_assert_size!(Item, 560 + size_of::<std::path::PathBuf>());
366366
static_assert_size!(ExternalCrate, 48 + size_of::<std::path::PathBuf>());
367367
}

src/rustdoc-json-types/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
114114
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
115115
// are deliberately not in a doc comment, because they need not be in public docs.)
116116
//
117-
// Latest feature: Add `ExternCrate::path`.
118-
pub const FORMAT_VERSION: u32 = 57;
117+
// Latest feature: Add `Item::stability`.
118+
pub const FORMAT_VERSION: u32 = 58;
119119

120120
/// The root of the emitted JSON blob.
121121
///
@@ -294,10 +294,30 @@ pub struct Item {
294294
pub attrs: Vec<Attribute>,
295295
/// Information about the item’s deprecation, if present.
296296
pub deprecation: Option<Deprecation>,
297+
298+
pub stability: Option<Stability>,
299+
297300
/// The type-specific fields describing this item.
298301
pub inner: ItemEnum,
299302
}
300303

304+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
305+
#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
306+
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
307+
pub struct Stability {
308+
pub feature: String,
309+
pub level: StabilityLevel,
310+
}
311+
312+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
313+
#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
314+
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
315+
#[serde(rename_all = "snake_case")]
316+
pub enum StabilityLevel {
317+
Stable,
318+
Unstable,
319+
}
320+
301321
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
302322
#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
303323
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(staged_api)]
2+
3+
//@ is "$.index[?(@.name=='foo')].stability.level" '"stable"'
4+
//@ is "$.index[?(@.name=='foo')].stability.feature" '"eeeee"'
5+
//@ is "$.index[?(@.name=='foo')].attrs" []
6+
#[stable(since = "2.71.8", feature = "eeeee")]
7+
pub fn foo() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//@ is "$.index[?(@.name=='foo')].stability" null
2+
//@ is "$.index[?(@.name=='foo')].attrs" []
3+
pub fn foo() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(staged_api)]
2+
3+
//@ is "$.index[?(@.name=='foo')].stability.level" '"unstable"'
4+
//@ is "$.index[?(@.name=='foo')].stability.feature" '"delights"'
5+
//@ is "$.index[?(@.name=='foo')].attrs" []
6+
#[unstable(feature = "delights", issue = "26")]
7+
pub fn foo() {}

0 commit comments

Comments
 (0)