Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl JsonRenderer<'_> {
name: name.map(|sym| sym.to_string()),
span: span.and_then(|span| span.into_json(self)),
visibility: visibility.into_json(self),
stability: item.stability(self.tcx).into_json(self),
docs,
attrs,
deprecation: deprecation.into_json(self),
Expand Down Expand Up @@ -203,6 +204,18 @@ impl FromClean<attrs::Deprecation> for Deprecation {
}
}

impl FromClean<hir::Stability> for Stability {
fn from_clean(stab: &hir::Stability, _renderer: &JsonRenderer<'_>) -> Self {
Stability {
feature: stab.feature.to_string(),
level: match stab.level {
hir::StabilityLevel::Unstable { .. } => StabilityLevel::Unstable,
hir::StabilityLevel::Stable { .. } => StabilityLevel::Stable,
},
}
}
}

impl FromClean<clean::GenericArgs> for Option<Box<GenericArgs>> {
fn from_clean(generic_args: &clean::GenericArgs, renderer: &JsonRenderer<'_>) -> Self {
use clean::GenericArgs::*;
Expand Down Expand Up @@ -922,6 +935,8 @@ fn maybe_from_hir_attr(attr: &hir::Attribute, item_id: ItemId, tcx: TyCtxt<'_>)

vec![match kind {
AK::Deprecated { .. } => return Vec::new(), // Handled separately into Item::deprecation.
AK::Stability { .. } => return Vec::new(), // Handled separately into Item::stability

AK::DocComment { .. } => unreachable!("doc comments stripped out earlier"),

AK::MacroExport { .. } => Attribute::MacroExport,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,6 @@ mod size_asserts {
// tidy-alphabetical-end

// These contains a `PathBuf`, which is different sizes on different OSes.
static_assert_size!(Item, 528 + size_of::<std::path::PathBuf>());
static_assert_size!(Item, 560 + size_of::<std::path::PathBuf>());
static_assert_size!(ExternalCrate, 48 + size_of::<std::path::PathBuf>());
}
24 changes: 22 additions & 2 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
// are deliberately not in a doc comment, because they need not be in public docs.)
//
// Latest feature: Add `ExternCrate::path`.
pub const FORMAT_VERSION: u32 = 57;
// Latest feature: Add `Item::stability`.
pub const FORMAT_VERSION: u32 = 58;

/// The root of the emitted JSON blob.
///
Expand Down Expand Up @@ -294,10 +294,30 @@ pub struct Item {
pub attrs: Vec<Attribute>,
/// Information about the item’s deprecation, if present.
pub deprecation: Option<Deprecation>,

pub stability: Option<Stability>,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this should go here, or as a new variant in Attribute. Thoughts appreciated.


/// The type-specific fields describing this item.
pub inner: ItemEnum,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
pub struct Stability {
pub feature: String,
pub level: StabilityLevel,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider Copy too? it's a breaking change to add it later :)

#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
#[serde(rename_all = "snake_case")]
pub enum StabilityLevel {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to expose more stuff here. Rustc has all of https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.StabilityLevel.html. I'm not sure how much of that anyone other than rustc cares about.

Thoughts appreciated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably interesting tooling that can be built on top of querying that info, but we can save that for rustdoc JSON v59+ if it's a hassle to add here. It's very nice to have but I wouldn't block this PR over it.

Stable,
Unstable,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
Expand Down
7 changes: 7 additions & 0 deletions src/tools/jsondoclint/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn errors_on_missing_links() {
links: FxHashMap::from_iter([("Not Found".to_owned(), Id(1))]),
attrs: vec![],
deprecation: None,
stability: None,
inner: ItemEnum::Module(Module {
is_crate: true,
items: vec![],
Expand Down Expand Up @@ -81,6 +82,7 @@ fn errors_on_local_in_paths_and_not_index() {
links: FxHashMap::from_iter([("prim@i32".to_owned(), Id(2))]),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Module(Module {
is_crate: true,
items: vec![Id(1)],
Expand All @@ -100,6 +102,7 @@ fn errors_on_local_in_paths_and_not_index() {
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Primitive(Primitive { name: "i32".to_owned(), impls: vec![] }),
},
),
Expand Down Expand Up @@ -153,6 +156,7 @@ fn errors_on_missing_path() {
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Module(Module {
is_crate: true,
items: vec![Id(1), Id(2)],
Expand All @@ -172,6 +176,7 @@ fn errors_on_missing_path() {
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Struct(Struct {
kind: StructKind::Unit,
generics: generics.clone(),
Expand All @@ -191,6 +196,7 @@ fn errors_on_missing_path() {
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Function(Function {
sig: FunctionSignature {
inputs: vec![],
Expand Down Expand Up @@ -253,6 +259,7 @@ fn checks_local_crate_id_is_correct() {
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
stability: None,
inner: ItemEnum::Module(Module {
is_crate: true,
items: vec![],
Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-json/attrs/stability/stable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(staged_api)]

//@ is "$.index[?(@.name=='foo')].stability.level" '"stable"'
//@ is "$.index[?(@.name=='foo')].stability.feature" '"eeeee"'
//@ is "$.index[?(@.name=='foo')].attrs" []
#[stable(since = "2.71.8", feature = "eeeee")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the since data available would be a neat nice-to-have, but we don't need to block this PR on a nice-to-have

pub fn foo() {}
3 changes: 3 additions & 0 deletions tests/rustdoc-json/attrs/stability/unmarked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//@ is "$.index[?(@.name=='foo')].stability" null
//@ is "$.index[?(@.name=='foo')].attrs" []
pub fn foo() {}
7 changes: 7 additions & 0 deletions tests/rustdoc-json/attrs/stability/unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(staged_api)]

//@ is "$.index[?(@.name=='foo')].stability.level" '"unstable"'
//@ is "$.index[?(@.name=='foo')].stability.feature" '"delights"'
//@ is "$.index[?(@.name=='foo')].attrs" []
#[unstable(feature = "delights", issue = "26")]
pub fn foo() {}
Loading