Skip to content
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

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

Open
Urgau opened this issue Jan 31, 2022 · 4 comments
Open

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

Urgau opened this issue Jan 31, 2022 · 4 comments
Labels
A-rustdoc-json Area: Rustdoc JSON backend C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Urgau
Copy link
Member

Urgau commented Jan 31, 2022

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.

@CraftSpider CraftSpider added A-rustdoc-json Area: Rustdoc JSON backend C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jan 31, 2022
@Enselic
Copy link
Member

Enselic commented Feb 8, 2022

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".

@Urgau
Copy link
Member Author

Urgau commented Feb 8, 2022

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.

@Enselic
Copy link
Member

Enselic commented Feb 9, 2022

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.

@Urgau
Copy link
Member Author

Urgau commented Feb 9, 2022

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
Labels
A-rustdoc-json Area: Rustdoc JSON backend C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants