From 98bd1a6a3a56951b2fe676988d8cb005a6b752c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Sat, 17 May 2025 14:38:26 +0200 Subject: [PATCH] rustdoc JSON: Don't apply `#[repr]` privacy heuristics --- src/librustdoc/clean/types.rs | 22 ++++-------- src/rustdoc-json-types/lib.rs | 2 +- tests/rustdoc-json/attrs/repr_combination.rs | 4 +++ tests/rustdoc-json/attrs/repr_transparent.rs | 37 -------------------- 4 files changed, 12 insertions(+), 53 deletions(-) delete mode 100644 tests/rustdoc-json/attrs/repr_transparent.rs diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index e45f28444fe0d..07ecd98f77596 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -774,20 +774,11 @@ impl Item { .filter_map(|attr| { if is_json { match attr { - hir::Attribute::Parsed(AttributeKind::Deprecation { .. }) => { - // rustdoc-json stores this in `Item::deprecation`, so we - // don't want it it `Item::attrs`. - None - } - rustc_hir::Attribute::Parsed( - rustc_attr_data_structures::AttributeKind::Repr(..), - ) => { - // We have separate pretty-printing logic for `#[repr(..)]` attributes. - // For example, there are circumstances where `#[repr(transparent)]` - // is applied but should not be publicly shown in rustdoc - // because it isn't public API. - None - } + // rustdoc-json stores this in `Item::deprecation`, so we + // don't want it it `Item::attrs`. + hir::Attribute::Parsed(AttributeKind::Deprecation { .. }) => None, + // We have separate pretty-printing logic for `#[repr(..)]` attributes. + hir::Attribute::Parsed(AttributeKind::Repr(..)) => None, _ => Some({ let mut s = rustc_hir_pretty::attribute_to_string(&tcx, attr); assert_eq!(s.pop(), Some('\n')); @@ -820,7 +811,8 @@ impl Item { if repr.transparent() { // Render `repr(transparent)` iff the non-1-ZST field is public or at least one // field is public in case all fields are 1-ZST fields. - let render_transparent = cache.document_private + let render_transparent = is_json + || cache.document_private || adt .all_fields() .find(|field| { diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 0b8a906529466..c091c955ed5eb 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -30,7 +30,7 @@ pub type FxHashMap = HashMap; // re-export for use in src/librustdoc /// This integer is incremented with every breaking change to the API, /// and is returned along with the JSON blob as [`Crate::format_version`]. /// Consuming code should assert that this value matches the format version(s) that it supports. -pub const FORMAT_VERSION: u32 = 45; +pub const FORMAT_VERSION: u32 = 46; /// The root of the emitted JSON blob. /// diff --git a/tests/rustdoc-json/attrs/repr_combination.rs b/tests/rustdoc-json/attrs/repr_combination.rs index 0e8e2ef0d83e9..6fe29c5eac093 100644 --- a/tests/rustdoc-json/attrs/repr_combination.rs +++ b/tests/rustdoc-json/attrs/repr_combination.rs @@ -77,3 +77,7 @@ pub enum AlignedExplicitRepr { pub enum ReorderedAlignedExplicitRepr { First, } + +//@ is "$.index[?(@.name=='Transparent')].attrs" '["#[repr(transparent)]"]' +#[repr(transparent)] +pub struct Transparent(i64); diff --git a/tests/rustdoc-json/attrs/repr_transparent.rs b/tests/rustdoc-json/attrs/repr_transparent.rs deleted file mode 100644 index 1e634ca901dc4..0000000000000 --- a/tests/rustdoc-json/attrs/repr_transparent.rs +++ /dev/null @@ -1,37 +0,0 @@ -#![no_std] - -// Rustdoc JSON *only* includes `#[repr(transparent)]` -// if the transparency is public API: -// - if a non-1-ZST field exists, it has to be public -// - otherwise, all fields are 1-ZST and at least one of them is public -// -// More info: https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent - -// Here, the non-1-ZST field is public. -// We expect `#[repr(transparent)]` in the attributes. -// -//@ is "$.index[?(@.name=='Transparent')].attrs" '["#[repr(transparent)]"]' -#[repr(transparent)] -pub struct Transparent(pub i64); - -// Here the non-1-ZST field isn't public, so the attribute isn't included. -// -//@ has "$.index[?(@.name=='TransparentNonPub')]" -//@ is "$.index[?(@.name=='TransparentNonPub')].attrs" '[]' -#[repr(transparent)] -pub struct TransparentNonPub(i64); - -// Only 1-ZST fields here, and one of them is public. -// We expect `#[repr(transparent)]` in the attributes. -// -//@ is "$.index[?(@.name=='AllZst')].attrs" '["#[repr(transparent)]"]' -#[repr(transparent)] -pub struct AllZst<'a>(pub core::marker::PhantomData<&'a ()>, ()); - -// Only 1-ZST fields here but none of them are public. -// The attribute isn't included. -// -//@ has "$.index[?(@.name=='AllZstNotPublic')]" -//@ is "$.index[?(@.name=='AllZstNotPublic')].attrs" '[]' -#[repr(transparent)] -pub struct AllZstNotPublic<'a>(core::marker::PhantomData<&'a ()>, ());