Skip to content

[rustdoc-json] absolute position of item is hard to get #93524

Open

Description

@Urgau

Problem:
Currently, the only way to accurately (see #93522 for reason why paths is not accurate) know the position of an item is to traverse all modules and items down to the item that we want, this is costly and inefficient especially for Ids that comes from the documentation (ie intra-doc-links) because they can link to outside.

Solution:
Have a parent_id field that link the current Item to it's direct parent. This would mean that we could go from down (item) to upper (module) in a straightforward way.

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Jan 31, 2022
  2. Enselic commented on Feb 8, 2022

    @Enselic
    Member

    It is not quite that simple unfortunately, because an item can have many "parents". Consider this code insrc/lib.rs:

    mod m {
        pub const C: usize = 7;
    }
    
    pub mod a {
        pub use super::m::C;
    }
    
    pub mod b {
        pub use super::m::C;
    }

    The rustdoc JSON will be (some details omitted):

            "0:4": {
                "id": "0:4",
                "crate_id": 0,
                "name": "C",
                "visibility": "public",
                "kind": "constant",
                "inner": {
                    "type": {
                        "kind": "primitive",
                        "inner": "usize"
                    },
                }
            },
            "0:5": {
                "id": "0:5",
                "crate_id": 0,
                "name": "a",
                "visibility": "public",
                "kind": "module",
                "inner": {
                    "is_crate": false,
                    "items": [
                        "0:4"           <--- Note: Contains C
                    ]
                }
            },
            "0:9": {
                "id": "0:9",
                "crate_id": 0,
                "name": "b",
                "visibility": "public",
                "kind": "module",
                "inner": {
                    "is_crate": false,
                    "items": [
                        "0:4"           <--- Note: Also contains C
                    ]
                }
            },

    So if you were to encounter only "0:4", you would not know what parent is "correct".

  3. Urgau commented on Feb 8, 2022

    @Urgau
    MemberAuthor

    It is not quite that simple unfortunately, because an item can have many "parents".

    Yes many relative "parents" but there is only one absolute parent. Ex: a struct A is defined in the module my_mod with a re-export from a another module, in this case the parent of A is my_mod and nothing else.
    Here parent should be seeing as where the item was defined. A item can only be defined in one place and so can only have one parent.

  4. Enselic commented on Feb 9, 2022

    @Enselic
    Member

    What you call the "absolute parent" is not always present in the rustdoc JSON output though. For example in the sample code above, because mod m is not pub. I use this command: RUSTDOCFLAGS='-Z unstable-options --output-format json' cargo +nightly doc --lib --no-deps.

    Should we simply omit parent_id in that case? That doesn't seem quite right either.

  5. Urgau commented on Feb 9, 2022

    @Urgau
    MemberAuthor

    What you call the "absolute parent" is not always present in the rustdoc JSON output though.

    This will solved by #93518 who is introducing the StrippedModule item.

    I have already been locally experimenting with #93518 and DefIdTree::parent, and it seems to work (decently) well. I will continue to experiment/test to see the all up and downside.

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-rustdoc-jsonArea: Rustdoc JSON backendC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions