Skip to content

rustdoc-json: Improve Item::attrs to not use debug representation. #137645

Closed

Description

@aDotInTheVoid

Currently, Item::attrs uses a debug-printing

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.

https://github.com/rust-lang/rust/blame/85abb276361c424d64743c0965242dd0e7b866d1/src/rustdoc-json-types/lib.rs#L123-L126

This isn't particularly friendly to users, who need to deal with an internal (and undocumented 😱) attr representation.

This was changed in #135726, as part of the a larger attribute rework (#131229)

MCVE:

#[repr(C)]
pub struct Foo(i32);

which produces (abridged):

    "1": {
      "attrs": ["#[attr=\"Repr([ReprC])\")]\n"],
      "inner": {
        "struct": {
          "generics": {"params": [], "where_predicates": []},
          "impls": [2, 4, 6, 8, 10, 12, 15, 19, 23, 26, 31, 36, 39],
          "kind": {"tuple": [null]}
        }
      },
      "name": "Foo",
    }

CC @jdonszelmann @obi1kenobi

Activity

  1. added
    needs-triage
    on Feb 26, 2025
  2. added
    T-rustdoc
    A-attributes
    A-rustdoc-json
    and removed
    needs-triage
    on Feb 26, 2025
  3. aDotInTheVoid commented on Feb 26, 2025

    @aDotInTheVoid
    MemberAuthor

    So there's basically two options here:

    1. Go back to how things used to be, and return attrs as a Vec<String> of how they're represented in the surface syntax.

      This would change

      #[repr(C)]
      #[repc(align(8)]
       pub struct Foo {}

      to

      #[repr(C, align(8))]
       pub struct Foo {}

      but that's fine

    2. Expose attributes in a more structured way in rustdoc-json-types. Currently clean::Attributes wraps a list of hir::Attribute (modulo some stuff for doc-comments). This exposes both parsed and unparsed attributes. We could do something similar.

    Doing option 2 would require a lot more design work, but is probably worthwhile (even if we do option 1 initially). Some questions:

    • How do we want to deal with #[cfg], which for rustc are gone by HIR, but rustdoc has a kinda seperate treatment?
    • Do we care to persist stuff like #[inline] at all?
    • How do we treat stuff that exists in the interface between std and rustc that isn't exposed to normal crates? Should we include #[unstable] attrs? How about #[rustc_promotable]?
    • How do we deal with tool attributes that rustc knows nothing of?
  4. jdonszelmann commented on Feb 26, 2025

    @jdonszelmann
    Contributor

    One advantage of option 1 is that it also improves hir pretty printing. I personally think having them available in a nice and structured way is pretty neat, and hir is not required to look nice. Furthermore, it's also a lot of code (I initially tried). But it in an advantage at least

  5. jdonszelmann commented on Feb 26, 2025

    @jdonszelmann
    Contributor

    Tools attrs are still accessible as tokens. If you try to hir pretty print they even still look like they used to. So that would fit in with option 1 I guess

  6. jdonszelmann commented on Feb 26, 2025

    @jdonszelmann
    Contributor

    @obi1kenobi are there any other attrs except repr you would care about with semver checks? Although I dont think thats the only reason to expose things like this (I'd be in favor of exposing things like inline) it'd be interesting to know if that's literally the only one right now.

  7. obi1kenobi commented on Feb 27, 2025

    @obi1kenobi
    Member

    I'm in favor of Option 1 initially, and Option 2 in the long run as a nice-to-have. We already have code that does attribute parsing, so while I'd love to eliminate it eventually, that isn't really a priority. Option 1 sounds like it can be done with less work, and speed is good :)

    @obi1kenobi are there any other attrs except repr you would care about with semver checks? Although I dont think thats the only reason to expose things like this (I'd be in favor of exposing things like inline) it'd be interesting to know if that's literally the only one right now.

    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)].

    • How do we want to deal with #[cfg], which for rustc are gone by HIR, but rustdoc has a kinda seperate treatment?

    If we can keep #[cfg] somehow, that would enable new lints like "this API is no longer available on this feature/OS/architecture etc." Currently, users have to build rustdoc JSON for each feature combo they want to check, on each platform they want to check, which is fine but could obviously be improved.

    • Do we care to persist stuff like #[inline] at all?

    If we can keep it, it'd be great! There are some cases where in the future a warning may be desirable as a lint if e.g. #[inline] was removed from some public API where it might affect optimization. I don't recall the #[inline] implementation details off the top of my head enough to flesh this out further.

    Not a priority today though.

    • How do we treat stuff that exists in the interface between std and rustc that isn't exposed to normal crates? Should we include #[unstable] attrs? How about #[rustc_promotable]?

    I have no opinion on this, since none of cargo-semver-checks relies on it today. If we can present the attributes as-is (i.e. as pretty-printed Rust code, the status quo ante of other attributes) without a ton of work, I guess that'd be nice.

    • How do we deal with tool attributes that rustc knows nothing of?

    It's possible cargo-semver-checks might get its own tool attribute in the near future, so preserving tool attributes in as-is form (i.e. as pretty-printed Rust code, the status quo ante of other attributes) would be nice if possible. Users have expressed a desire to opt out of specific lints for specific types or modules, and tool attributes in rustdoc JSON would make that much easier to implement.

  8. obi1kenobi commented on Feb 27, 2025

    @obi1kenobi
    Member

    Thank you both for looking into this issue and helping address it! ❤

  9. jdonszelmann commented on Feb 27, 2025

    @jdonszelmann
    Contributor

    for the record, although I played devil's advocate a little and posted some arguments for option, I think option 2 gives a lot more flexibility and although I agree you already have parsing for option 1, with option 2 you potentially won't need any parsing at all nor any other tool which I think is very valuable

  10. obi1kenobi commented on Feb 27, 2025

    @obi1kenobi
    Member

    I agree option 2 would be nice! But my top priority is having whichever option we can ship faster :)

  11. obi1kenobi commented on Mar 4, 2025

    @obi1kenobi
    Member

    I've been triaging the impact to cargo-semver-checks and taking the opportunity to improve our test coverage. As far as I can tell, the only thing that's broken for us right now is #[repr(..)] attributes. The other attributes we use seem to be unaffected by the recent change, AFAICT.

    I noticed that rustdoc already has some machinery for pretty-printing #[repr(..)] attributes that appears to not be currently used by the JSON path (see Item::attributes()). We might be able to reuse that code path on the JSON side, fixing the #[repr(..)] part of the issue in a reasonably non-invasive manner. This won't address the representation of other attributes (so it won't fully resolve this issue), but would be sufficient to unblock cargo-semver-checks.

    Vibe check on switching to using the existing #[repr(..)] pretty-printing machinery on the JSON side? 👀

  12. aDotInTheVoid commented on Mar 4, 2025

    @aDotInTheVoid
    MemberAuthor

    rustdoc already has some machinery for pretty-printing #[repr(..)] attributes that appears to not be currently used by the JSON path (see Item::attributes())

    We go through clean::Item::attributes, but i don't think we enable keap_as_is so don't hit that branch.

    let attrs = item.attributes(self.tcx, self.cache(), true);

    Vibe check on switching to using the existing #[repr(..)] pretty-printing machinery on the JSON side? 👀

    Seems like a good approach for now, even if eventually we move to something more structured. It'd require careful explaining in the docs, but that's fine, they're not good here at the moment and I'm happy to accept incremental improvements that aren't perfect.

    While doing this, it's probably a good idea to add tests for the non-#[repr] attributes we emit.

  13. 9 remaining items

  14. added a commit that references this issue on Mar 8, 2025
  15. obi1kenobi commented on Mar 8, 2025

    @obi1kenobi
    Member

    This is what the nomicon says about repr(transparent):

    This repr is only considered part of the public ABI of a type if either the single field is pub, or if its layout is documented in prose. Otherwise, the layout should not be relied upon by other crates.

    AFAICT this is the same rule that the HTML behavior implements. cargo-semver-checks attempts to approximate this rule, but since we don't know in general which field(s) are 1-ZST (no info on that in the JSON), we currently miss some breakage — the lint is written to have false-negatives instead of false-positives.

    Having rustdoc JSON list #[repr(transparent)] only if it's public API would also be consistent with how #[doc(hidden)] pub items are not shown in rustdoc JSON. It would also make cargo-semver-checks better at its job, with less work :)

    I don't feel strongly about this. If you think it's best to always include #[repr(transparent)], that's fine by me — I'd much prefer this issue closed in any form, than any specific resolution of it. I just wanted to point out that we have a relatively strong and explicit rule to lean on here, not just a heuristic about authorial intent.

  16. added a commit that references this issue on Mar 8, 2025
  17. LukeMathWalker commented on Mar 11, 2025

    @LukeMathWalker
    Contributor

    Just found out about this by looking at the CHANGELOG.md of rustdoc-types@0.36.

    Throwing in my 2c, if it may help:

    • I'm fine with either option 1. or 2., as long as we have a well-documented format to refer to for 2. But I'm fairly sure 2. will have to keep falling back to 1. for attributes with an "open" format (thinking specifically about diagnostic here).
    • The one thing I care ~strongly about is not losing any attributes in the transformation process.
  18. added 2 commits that reference this issue on Mar 18, 2025
  19. added 2 commits that reference this issue on Mar 18, 2025
  20. added a commit that references this issue on Mar 23, 2025
  21. jyn514 commented on Apr 28, 2025

    @jyn514
    Member

    note also that the current representation does not distinguish inner and outer attrs:

    $ head -n1 src/lib.rs
    #![allow(clippy::all)]
    $ jq -c '.index[] | select(.name == "lib") | .attrs' < doc/lib.json
    ["#[allow(clippy::all)]"]
    
  22. GuillaumeGomez commented on Apr 28, 2025

    @GuillaumeGomez
    Member

    Would be nice to have this information indeed.

  23. aDotInTheVoid commented on May 21, 2025

    @aDotInTheVoid
    MemberAuthor

    Between #138033 / #138018 / #138577, I think we've done option 1 (#137645 (comment)), and gone back to attributes being strings that are the same as surface syntax. Anywhere they're not is unknown, and should be filed as a new bug.

    I've opened #141358 to track thinking about a more structured attribute representations (option 2).

  24. added a commit that references this issue on May 22, 2025
  25. added a commit that references this issue on May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesA-rustdoc-jsonT-rustdoc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions