Skip to content

rustdoc-json: Structured representation of attributes #141358

Description

@aDotInTheVoid

Today, rustdoc-json presents attributes as a Vec<String>

/// 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>,

This means that users wanting to use these attributes (and not just display them) must implement a parser for these strings. It'd be nice if rustdoc exposed the semantics of the attribute, and not just the syntax.

This'd mean going from Vec<String> to Vec<Attr>, and adding some enum Attr with all the possible kinds of attribute. The exact details would require design work. See #137645 (comment) for more detail.

Possibly this should wait untill the state of rustc's own attribute system is more settled (#131229).

CC @jdonszelmann @obi1kenobi

Activity

  1. added
    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    on May 21, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 21, 2025
  3. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 21, 2025
  4. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on May 21, 2025
  5. aDotInTheVoid commented on Jun 19, 2025

    @aDotInTheVoid
    MemberAuthor

    This has a newfound urgency:

    mhm, I messaged Alona today as well, since it's technically also a breaking change in rustdoc json. However, it's really annoying since I'll be making about a 100 separate breaking changes like this in the coming months.... and practically few people will care

    Originally posted by @jdonszelmann in #138165 (comment)

    A half-baked idea from someone who should be asleep: If we moved to a structured representation of attributes in rustdoc-json (#141358), we could say "Anything structured is guarded by FORMAT_VERSION, anything in the Other(String) is compiler debug output, and not stable". That way for the attributes we know people care about. (E.g. #[repr]), we defiantly defiantly won't break them. But if we change how #[rustc_const_promotable] is, that's much more eh.

    That way, we do a much better job signposting the attributes that we actually supports (vs the current ~backroom understanding about what's important to CSC). We can also say something like "if you really on a attribute that's currently Attribute::Other, talk to us and we'll try to add a variant.

    And when we add variants to rustdoc_json_types::Attribute, we can do a load in 1 PR, to avoid generated a load of format version churn.

    Originally posted by @aDotInTheVoid in #138165 (comment)

  6. aDotInTheVoid commented on Jun 19, 2025

    @aDotInTheVoid
    MemberAuthor

    For an idea of the things that are important to support initially

    No, unfortunately we need many more. At minimum (probably not an exhaustive list): #[non_exhaustive], #[automatically_derived], #[must_use], #[doc(hidden)], #[deprecated], #[no_mangle] and #[export_name] (plus their new unsafe variants like #[unsafe(no_mangle)]) , #[repr(packed)], #[repr(C)], #[repr(transparent)], enum integer reprs like #[repr(i8)].

    And also now #[target_feature]

    Originally posted by @obi1kenobi in #137645

  7. added a commit that references this issue on Jul 15, 2025
    bd66814
  8. added 3 commits that reference this issue on Jul 15, 2025
    0e1fe5a
    7474bdc
    9f2015d
  9. added a commit that references this issue on Jul 16, 2025
    fdd7a8d
  10. added a commit that references this issue on Jul 17, 2025
    f54a424
  11. added a commit that references this issue on Jul 21, 2025
    db268f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-rustdoc-jsonArea: Rustdoc JSON backendC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions