Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc-json: Don't also include #[deprecated] in Item::attrs #138577

Merged
merged 3 commits into from
Mar 17, 2025
Merged
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
23 changes: 13 additions & 10 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use std::{fmt, iter};

use arrayvec::ArrayVec;
use rustc_abi::{ExternAbi, VariantIdx};
use rustc_attr_parsing::{ConstStability, Deprecation, Stability, StableSince};
use rustc_attr_parsing::{AttributeKind, ConstStability, Deprecation, Stability, StableSince};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
@@ -756,12 +756,7 @@ impl Item {
Some(tcx.visibility(def_id))
}

pub(crate) fn attributes(
&self,
tcx: TyCtxt<'_>,
cache: &Cache,
keep_as_is: bool,
) -> Vec<String> {
pub(crate) fn attributes(&self, tcx: TyCtxt<'_>, cache: &Cache, is_json: bool) -> Vec<String> {
const ALLOWED_ATTRIBUTES: &[Symbol] =
&[sym::export_name, sym::link_section, sym::no_mangle, sym::non_exhaustive];

@@ -772,8 +767,14 @@ impl Item {
.other_attrs
.iter()
.filter_map(|attr| {
if keep_as_is {
Some(rustc_hir_pretty::attribute_to_string(&tcx, attr))
if is_json {
if matches!(attr, hir::Attribute::Parsed(AttributeKind::Deprecation { .. })) {
// rustdoc-json stores this in `Item::deprecation`, so we
// don't want it it `Item::attrs`.
None
} else {
Some(rustc_hir_pretty::attribute_to_string(&tcx, attr))
}
} else if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
Some(
rustc_hir_pretty::attribute_to_string(&tcx, attr)
@@ -786,7 +787,9 @@ impl Item {
}
})
.collect();
if !keep_as_is

// Add #[repr(...)]
if !is_json
&& let Some(def_id) = self.def_id()
&& let ItemType::Struct | ItemType::Enum | ItemType::Union = self.type_()
{
38 changes: 38 additions & 0 deletions tests/rustdoc-json/attrs/deprecated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//@ is "$.index[*][?(@.name=='not')].attrs" []
//@ is "$.index[*][?(@.name=='not')].deprecation" null
pub fn not() {}

//@ is "$.index[*][?(@.name=='raw')].attrs" []
//@ is "$.index[*][?(@.name=='raw')].deprecation" '{"since": null, "note": null}'
#[deprecated]
pub fn raw() {}

//@ is "$.index[*][?(@.name=='equals_string')].attrs" []
//@ is "$.index[*][?(@.name=='equals_string')].deprecation" '{"since": null, "note": "here is a reason"}'
#[deprecated = "here is a reason"]
pub fn equals_string() {}

//@ is "$.index[*][?(@.name=='since')].attrs" []
//@ is "$.index[*][?(@.name=='since')].deprecation" '{"since": "yoinks ago", "note": null}'
#[deprecated(since = "yoinks ago")]
pub fn since() {}

//@ is "$.index[*][?(@.name=='note')].attrs" []
//@ is "$.index[*][?(@.name=='note')].deprecation" '{"since": null, "note": "7"}'
#[deprecated(note = "7")]
pub fn note() {}

//@ is "$.index[*][?(@.name=='since_and_note')].attrs" []
//@ is "$.index[*][?(@.name=='since_and_note')].deprecation" '{"since": "tomorrow", "note": "sorry"}'
#[deprecated(since = "tomorrow", note = "sorry")]
pub fn since_and_note() {}

//@ is "$.index[*][?(@.name=='note_and_since')].attrs" []
//@ is "$.index[*][?(@.name=='note_and_since')].deprecation" '{"since": "a year from tomorrow", "note": "your welcome"}'
#[deprecated(note = "your welcome", since = "a year from tomorrow")]
pub fn note_and_since() {}

//@ is "$.index[*][?(@.name=='neither_but_parens')].attrs" []
//@ is "$.index[*][?(@.name=='neither_but_parens')].deprecation" '{"since": null, "note": null}'
#[deprecated()]
pub fn neither_but_parens() {}
Loading