-
Notifications
You must be signed in to change notification settings - Fork 14k
[rustdoc] Fix invalid jump to def macro link generation #148080
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
base: main
Are you sure you want to change the base?
Conversation
|
ping @notriddle |
| @@ -0,0 +1,21 @@ | |||
| // This test ensures that the same link is generated in both intra-doc links | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put this file in the existing tests/rustdoc/jump-to-def dir. (created #148548 about moving existing files that should also be in there)
Also, I'm slightly confused as to how this test works, and what it's testing, as the PR description says "I realized that when there was no intra-doc link linking to the same item, then the generated link for macros in jump to def would be invalid.", but that's not what's happening here, and trying to run this test against current nightly actually results in an ICE, not an invalid link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is no intra-doc link linking to Debug and PartialEq derive proc macro in the docs, which is what we're testing here.
Also, that might be a newer change because originally, it didn't ICE but generated invalid links.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean "there's no intra-doc link linking to Debug and PartialEq in core"? Because line 10 and 11 literally test that a link exists. Or are you not counting those as intra-doc because they aren't written as a doc comment? If that's the case, you should probably remove the comment on line 9 that describes them as doc comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arf, I'm so bad at explaining.
So:
//! [PartialEq] [Debug]
#[derive(Debug, PartialEq)]
pub struct Foo;Before this PR, if the //! [PartialEq] [Debug] doc comment was not present, in "jump to def", the links for Debug and PartialEq would be wrongly generated there (in the source code pages). Because it wasn't in the cache and so the path computation was wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense, it's just that the PR description and the code comments in the test have conflicting definitions of "intra-doc link", which was very confusing at first.
87a7bdf to
ae76c7b
Compare
|
Moved the test file to where it's supposed to be. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realizing that rustdoc path handling is kinda a mess, should probably get cleaned up at some point.
| @@ -0,0 +1,21 @@ | |||
| // This test ensures that the same link is generated in both intra-doc links | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean "there's no intra-doc link linking to Debug and PartialEq in core"? Because line 10 and 11 literally test that a link exists. Or are you not counting those as intra-doc because they aren't written as a doc comment? If that's the case, you should probably remove the comment on line 9 that describes them as doc comments.
| @@ -26,8 +26,9 @@ impl C { | |||
| pub fn wat() {} | |||
| } | |||
|
|
|||
| //@ has - '//a[@href="{{channel}}/core/fmt/macros/macro.Debug.html"]' 'Debug' | |||
| //@ has - '//a[@href="{{channel}}/core/cmp/macro.PartialEq.html"]' 'PartialEq' | |||
| // These two links must not change and in particular must contain `/derive.`! | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why must they not change? Seems like an odd thing to say without explanation considering this PR is changing them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because they are valid as is and since it's derive macros, "derive" must be in the URL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, they're obviously the best thing to do currently, but that's the case for most things in tests, "must not" seems a bit strong for "this is the current best"
Also, does changing this break any existing links to these pages? It doesn't look like there are redirects.
src/librustdoc/html/format.rs
Outdated
| @@ -408,7 +404,7 @@ fn generate_macro_def_id_path( | |||
| return Err(HrefError::NotInExternalCache); | |||
| } | |||
| }; | |||
| Ok((url, ItemType::Macro, fqp)) | |||
| Ok((url, item_type, path)) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm reading something wrong, this changes behavior in a pretty undesirable way. Previously, the third element in the tuple was the rust path, such as ["std", "println"], now it's the url path, but split into components.
- I'm surprised this doesn't cause test failures
- We probably shouldn't have functions that return tuples not document what those tuples are
- Honestly there's a potential argument that maybe this should just be 3 different functions, tho this exact tuple does seem to be getting passed through quite a few layers, so maybe it should just be a struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to tend to a unification rather than having three different functions to achieve a same result. However you're right: it should be documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also extremely uncomfortable changing the behavior of this function like this, at least not without a lot of reviewing the code, it seems very likely that it would cause some sort of bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, the current behavior after this PR directly contradicts the newly added documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path is the same as fqp, this behaviour didn't change. Simply fixed the path generated for macros. The part generating the previous fqp is now done in get_item_path
Additionally, the current behavior after this PR directly contradicts the newly added documentation.
Which documentation are you referring to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path is the same as fqp
I have a really hard time believing that though, since before fqp wasn't getting modified, and now it is getting modified.
this is the contradiction i'm referring to, path is modified to turn it from a Rust path to a URL path. it contradicts with the docs that say it returns a Rust path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the macro "rust path" is `crate::macro_name", no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, and we're returning [crate, macro.macro_name.html] now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok now I see your point. I'll add some name= checks too because I think it's used for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were right, this generated wrong title attributes. Fixed it and added a regression test. Very good catch, thanks a lot!
|
Added missing docs. |
7017133 to
ca3f622
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good now, one doc comment i'd like to see improved, and a few potential improvements that aren't essential.
src/librustdoc/clean/inline.rs
Outdated
| tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect() | ||
| } | ||
|
|
||
| /// Get the path to an item in a URL sense: we use it to generate the URL to the actual item. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Get the path to an item in a URL sense: we use it to generate the URL to the actual item. | |
| /// Get the public Rust path to an item. This is used to generate the URL for the item's page. |
I think the previous docs were a bit confusing because "URL path" is it's own thing, notably including the kind.name.html form.
src/librustdoc/html/format.rs
Outdated
| def_id: DefId, | ||
| cx: &Context<'_>, | ||
| root_path: Option<&str>, | ||
| ) -> Result<(String, ItemType, Vec<Symbol>), HrefError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think you could at least extract this tuple (which is used in several functions) into a type alias, with docs?
I would like to eventually make it into a struct, but that would require decently involved refactoring that is probably outside of the scope of this PR, but maybe a type alias wouldn't be too much work?
That seems like less effort than copy-pasting a doc comment that explains the meaning of the return values everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it so I made the change.
| if let Some(last) = path.last_mut() { | ||
| *last = Symbol::intern(&format!("macro.{last}.html")); | ||
| *last = Symbol::intern(&format!("{}.{last}.html", item_type.as_str())); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a bit excessive, but the clone isn't actually needed since we only need path to be an iterator.
we could do something like let path = fqp[..fqp.len()-1].iter().chain(once(Symbol::intern(&format("{}.{last}.html"))).
no idea if this would be worth it, but it is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is surprisingly complex to do because of from_fn so for now I used pop and push on path and added a FIXME.
|
Applied suggestions. |
|
☔ The latest upstream changes (presumably #148817) made this pull request unmergeable. Please resolve the merge conflicts. |
a874e8e to
4e742dc
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Fixed merge conflict. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huge improvement, thanks!
one tiny nitpick, you can r=me with or without that addressed
src/librustdoc/html/format.rs
Outdated
| Ok(HrefInfo { url, kind: item_type, rust_path: path }) | ||
| } | ||
|
|
||
| /// If the function succeeded, it will return the full URL to the item, its type and a `Vec` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doc comment isn't really needed now that we're not returning a tuple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh indeed. It's rare to just remove docs because code is now explicit. I like it! 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's not really that we've removed docs, just that we've move the docs to the fields of the struct, so it doesn't need to be on the function anymore.
4e742dc to
4d3d3b3
Compare
|
@bors r=lolbinarycat rollup |
… r=lolbinarycat [rustdoc] Fix invalid jump to def macro link generation Follow-up of rust-lang#147820. I realized that when there was no intra-doc link linking to the same item, then the generated link for macros in jump to def would be invalid. To make the code less redundant, I merged the "registering" of items and the href generation use the same code for macros. r? `@notriddle`
Follow-up of #147820.
I realized that when there was no intra-doc link linking to the same item, then the generated link for macros in jump to def would be invalid.
To make the code less redundant, I merged the "registering" of items and the href generation use the same code for macros.
r? @notriddle