Skip to content

Rollup of 8 pull requests #138841

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

Merged
merged 19 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9e5c942
Mark some std tests as requiring `panic = "unwind"`
paulmenage Mar 10, 2025
409510c
rustdoc js: add nonnull helper and typecheck src-script.js
lolbinarycat Mar 13, 2025
1f64cb7
Add release notes for 1.85.1
cuviper Mar 18, 2025
e5fc7d6
Fix Thread::set_name on cygwin
Berrysoft Mar 21, 2025
7d3965e
Move make_input call
bjorn3 Feb 6, 2025
41f1ed1
Move some calls to before calling codegen_crate
bjorn3 Feb 6, 2025
cd929bf
Fix lint name in unused linker_messages warning
bjorn3 Feb 27, 2025
b2d7271
target spec check: better error when llvm-floatabi is missing
RalfJung Mar 21, 2025
110f1fe
Revert "Stabilize file_lock"
moxian Mar 22, 2025
f39478f
Clarify "Windows 1607"
cuviper Mar 22, 2025
bafdbca
rustdoc: Use own logic to print `#[repr(..)]` attributes in JSON output.
obi1kenobi Mar 4, 2025
c5a5f8a
Rollup merge of #138018 - obi1kenobi:pg/librustdoc_repr_attr, r=aDotI…
matthiaskrgr Mar 22, 2025
53076de
Rollup merge of #138294 - paulmenage:test-panic-unwind, r=bjorn3
matthiaskrgr Mar 22, 2025
26ecbc6
Rollup merge of #138468 - lolbinarycat:rustdoc-js-less-expect-error-p…
matthiaskrgr Mar 22, 2025
8986c53
Rollup merge of #138675 - cuviper:release-1.85.1, r=Urgau
matthiaskrgr Mar 22, 2025
9a98596
Rollup merge of #138765 - Berrysoft:cygwin-thread-name, r=joboet
matthiaskrgr Mar 22, 2025
4457da3
Rollup merge of #138786 - bjorn3:driver_code_move, r=compiler-errors
matthiaskrgr Mar 22, 2025
7372f28
Rollup merge of #138793 - RalfJung:arm-floatabi, r=Noratrieb
matthiaskrgr Mar 22, 2025
3f59916
Rollup merge of #138822 - moxian:unlock, r=joshtriplett
matthiaskrgr Mar 22, 2025
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
Prev Previous commit
Next Next commit
rustdoc: Use own logic to print #[repr(..)] attributes in JSON output.
  • Loading branch information
obi1kenobi committed Mar 22, 2025
commit bafdbcadd5e70e4a1a35647002c30efd315621b4
25 changes: 17 additions & 8 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,22 @@ impl Item {
.iter()
.filter_map(|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))
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_parsing::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
}
_ => Some(rustc_hir_pretty::attribute_to_string(&tcx, attr)),
}
} else if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
Some(
Expand All @@ -789,8 +799,7 @@ impl Item {
.collect();

// Add #[repr(...)]
if !is_json
&& let Some(def_id) = self.def_id()
if let Some(def_id) = self.def_id()
&& let ItemType::Struct | ItemType::Enum | ItemType::Union = self.type_()
{
let adt = tcx.adt_def(def_id);
Expand Down
22 changes: 18 additions & 4 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // 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 = 42;
pub const FORMAT_VERSION: u32 = 43;

/// The root of the emitted JSON blob.
///
Expand Down Expand Up @@ -120,9 +120,23 @@ pub struct Item {
pub docs: Option<String>,
/// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs
pub links: HashMap<String, Id>,
/// Stringified versions of parsed attributes on this item.
/// Essentially debug printed (e.g. `#[inline]` becomes something similar to `#[attr="Inline(Hint)"]`).
/// Equivalent to the hir pretty-printing of attributes.
/// Attributes on this item.
///
/// Does not include `#[deprecated]` attributes: see the [`Self::deprecation`] field instead.
///
/// Some attributes appear in pretty-printed Rust form, regardless of their formatting
/// in the original source code. For example:
/// - `#[non_exhaustive]` and `#[must_use]` are represented as themselves.
/// - `#[no_mangle]` and `#[export_name]` are also represented as themselves.
/// - `#[repr(C)]` and other reprs also appear as themselves,
/// though potentially with a different order: e.g. `repr(i8, C)` may become `repr(C, i8)`.
/// Multiple repr attributes on the same item may be combined into an equivalent single attr.
///
/// Other attributes may appear debug-printed. For example:
/// - `#[inline]` becomes something similar to `#[attr="Inline(Hint)"]`.
///
/// As an internal implementation detail subject to change, this debug-printing format
/// is currently equivalent to the HIR pretty-printing of parsed attributes.
pub attrs: Vec<String>,
/// Information about the item’s deprecation, if present.
pub deprecation: Option<Deprecation>,
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-json/attrs/repr_align.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

//@ is "$.index[?(@.name=='Aligned')].attrs" '["#[attr = Repr([ReprAlign(Align(4 bytes))])]\n"]'
//@ is "$.index[?(@.name=='Aligned')].attrs" '["#[repr(align(4))]"]'
#[repr(align(4))]
pub struct Aligned {
a: i8,
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-json/attrs/repr_c.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![no_std]

//@ is "$.index[?(@.name=='ReprCStruct')].attrs" '["#[attr = Repr([ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReprCStruct')].attrs" '["#[repr(C)]"]'
#[repr(C)]
pub struct ReprCStruct(pub i64);

//@ is "$.index[?(@.name=='ReprCEnum')].attrs" '["#[attr = Repr([ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReprCEnum')].attrs" '["#[repr(C)]"]'
#[repr(C)]
pub enum ReprCEnum {
First,
}

//@ is "$.index[?(@.name=='ReprCUnion')].attrs" '["#[attr = Repr([ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReprCUnion')].attrs" '["#[repr(C)]"]'
#[repr(C)]
pub union ReprCUnion {
pub left: i64,
Expand Down
23 changes: 12 additions & 11 deletions tests/rustdoc-json/attrs/repr_combination.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
#![no_std]

// Combinations of `#[repr(..)]` attributes.
// Rustdoc JSON emits normalized output, regardless of the original source.

//@ is "$.index[?(@.name=='ReprCI8')].attrs" '["#[attr = Repr([ReprC, ReprInt(SignedInt(I8))])]\n"]'
//@ is "$.index[?(@.name=='ReprCI8')].attrs" '["#[repr(C, i8)]"]'
#[repr(C, i8)]
pub enum ReprCI8 {
First,
}

//@ is "$.index[?(@.name=='SeparateReprCI16')].attrs" '["#[attr = Repr([ReprC, ReprInt(SignedInt(I16))])]\n"]'
//@ is "$.index[?(@.name=='SeparateReprCI16')].attrs" '["#[repr(C, i16)]"]'
#[repr(C)]
#[repr(i16)]
pub enum SeparateReprCI16 {
First,
}

//@ is "$.index[?(@.name=='ReversedReprCUsize')].attrs" '["#[attr = Repr([ReprInt(UnsignedInt(Usize)), ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReversedReprCUsize')].attrs" '["#[repr(C, usize)]"]'
#[repr(usize, C)]
pub enum ReversedReprCUsize {
First,
}

//@ is "$.index[?(@.name=='ReprCPacked')].attrs" '["#[attr = Repr([ReprC, ReprPacked(Align(1 bytes))])]\n"]'
//@ is "$.index[?(@.name=='ReprCPacked')].attrs" '["#[repr(C, packed(1))]"]'
#[repr(C, packed)]
pub struct ReprCPacked {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='SeparateReprCPacked')].attrs" '["#[attr = Repr([ReprC, ReprPacked(Align(2 bytes))])]\n"]'
//@ is "$.index[?(@.name=='SeparateReprCPacked')].attrs" '["#[repr(C, packed(2))]"]'
#[repr(C)]
#[repr(packed(2))]
pub struct SeparateReprCPacked {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='ReversedReprCPacked')].attrs" '["#[attr = Repr([ReprPacked(Align(2 bytes)), ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReversedReprCPacked')].attrs" '["#[repr(C, packed(2))]"]'
#[repr(packed(2), C)]
pub struct ReversedReprCPacked {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='ReprCAlign')].attrs" '["#[attr = Repr([ReprC, ReprAlign(Align(16 bytes))])]\n"]'
//@ is "$.index[?(@.name=='ReprCAlign')].attrs" '["#[repr(C, align(16))]"]'
#[repr(C, align(16))]
pub struct ReprCAlign {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='SeparateReprCAlign')].attrs" '["#[attr = Repr([ReprC, ReprAlign(Align(2 bytes))])]\n"]'
//@ is "$.index[?(@.name=='SeparateReprCAlign')].attrs" '["#[repr(C, align(2))]"]'
#[repr(C)]
#[repr(align(2))]
pub struct SeparateReprCAlign {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='ReversedReprCAlign')].attrs" '["#[attr = Repr([ReprAlign(Align(2 bytes)), ReprC])]\n"]'
//@ is "$.index[?(@.name=='ReversedReprCAlign')].attrs" '["#[repr(C, align(2))]"]'
#[repr(align(2), C)]
pub struct ReversedReprCAlign {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='AlignedExplicitRepr')].attrs" '["#[attr = Repr([ReprC, ReprAlign(Align(16 bytes)), ReprInt(SignedInt(Isize))])]\n"]'
//@ is "$.index[?(@.name=='AlignedExplicitRepr')].attrs" '["#[repr(C, align(16), isize)]"]'
#[repr(C, align(16), isize)]
pub enum AlignedExplicitRepr {
First,
}

//@ is "$.index[?(@.name=='ReorderedAlignedExplicitRepr')].attrs" '["#[attr = Repr([ReprInt(SignedInt(Isize)), ReprC, ReprAlign(Align(16 bytes))])]\n"]'
//@ is "$.index[?(@.name=='ReorderedAlignedExplicitRepr')].attrs" '["#[repr(C, align(16), isize)]"]'
#[repr(isize, C, align(16))]
pub enum ReorderedAlignedExplicitRepr {
First,
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-json/attrs/repr_int_enum.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#![no_std]

//@ is "$.index[?(@.name=='I8')].attrs" '["#[attr = Repr([ReprInt(SignedInt(I8))])]\n"]'
//@ is "$.index[?(@.name=='I8')].attrs" '["#[repr(i8)]"]'
#[repr(i8)]
pub enum I8 {
First,
}

//@ is "$.index[?(@.name=='I32')].attrs" '["#[attr = Repr([ReprInt(SignedInt(I32))])]\n"]'
//@ is "$.index[?(@.name=='I32')].attrs" '["#[repr(i32)]"]'
#[repr(i32)]
pub enum I32 {
First,
}

//@ is "$.index[?(@.name=='Usize')].attrs" '["#[attr = Repr([ReprInt(UnsignedInt(Usize))])]\n"]'
//@ is "$.index[?(@.name=='Usize')].attrs" '["#[repr(usize)]"]'
#[repr(usize)]
pub enum Usize {
First,
Expand Down
8 changes: 4 additions & 4 deletions tests/rustdoc-json/attrs/repr_packed.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![no_std]

// Note the normalization:
// `#[repr(packed)]` in has the implict "1" in rustdoc JSON.

//@ is "$.index[?(@.name=='Packed')].attrs" '["#[attr = Repr([ReprPacked(Align(1 bytes))])]\n"]'
// `#[repr(packed)]` in source becomes `#[repr(packed(1))]` in rustdoc JSON.
//
//@ is "$.index[?(@.name=='Packed')].attrs" '["#[repr(packed(1))]"]'
#[repr(packed)]
pub struct Packed {
a: i8,
b: i64,
}

//@ is "$.index[?(@.name=='PackedAligned')].attrs" '["#[attr = Repr([ReprPacked(Align(4 bytes))])]\n"]'
//@ is "$.index[?(@.name=='PackedAligned')].attrs" '["#[repr(packed(4))]"]'
#[repr(packed(4))]
pub struct PackedAligned {
a: i8,
Expand Down
29 changes: 22 additions & 7 deletions tests/rustdoc-json/attrs/repr_transparent.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#![no_std]

// Rustdoc JSON currently includes `#[repr(transparent)]`
// even if the transparency is not part of the public API
// 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
//
// https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
// More info: https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent

//@ is "$.index[?(@.name=='Transparent')].attrs" '["#[attr = Repr([ReprTransparent])]\n"]'
// 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);

//@ is "$.index[?(@.name=='TransparentNonPub')].attrs" '["#[attr = Repr([ReprTransparent])]\n"]'
// 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);

//@ is "$.index[?(@.name=='AllZst')].attrs" '["#[attr = Repr([ReprTransparent])]\n"]'
// 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 ()>, ());

//@ is "$.index[?(@.name=='AllZstNotPublic')].attrs" '["#[attr = Repr([ReprTransparent])]\n"]'
// 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 ()>, ());
2 changes: 1 addition & 1 deletion tests/rustdoc-json/enums/discriminant/struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[repr(i32)]
//@ is "$.index[?(@.name=='Foo')].attrs" '["#[attr = Repr([ReprInt(SignedInt(I32))])]\n"]'
//@ is "$.index[?(@.name=='Foo')].attrs" '["#[repr(i32)]"]'
pub enum Foo {
//@ is "$.index[?(@.name=='Struct')].inner.variant.discriminant" null
//@ count "$.index[?(@.name=='Struct')].inner.variant.kind.struct.fields[*]" 0
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-json/enums/discriminant/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[repr(u32)]
//@ is "$.index[?(@.name=='Foo')].attrs" '["#[attr = Repr([ReprInt(UnsignedInt(U32))])]\n"]'
//@ is "$.index[?(@.name=='Foo')].attrs" '["#[repr(u32)]"]'
pub enum Foo {
//@ is "$.index[?(@.name=='Tuple')].inner.variant.discriminant" null
//@ count "$.index[?(@.name=='Tuple')].inner.variant.kind.tuple[*]" 0
Expand Down
Loading