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

Disallow linking to items with a mismatched disambiguator #75079

Merged
merged 13 commits into from
Aug 7, 2020

Conversation

jyn514
Copy link
Member

@jyn514 jyn514 commented Aug 3, 2020

Closes #74851

r? @Manishearth

@jyn514 jyn514 added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name labels Aug 3, 2020
@rust-highfive
Copy link
Collaborator

Some changes occurred in intra-doc-links.

cc @jyn514

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 3, 2020
This caused the following false positive:

```
warning: unresolved link to `Default::default`
 --> /home/joshua/rustc2/default.rs:1:14
  |
1 | /// Link to [Default::default()]
  |              ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(broken_intra_doc_links)]` on by default
note: this item resolved to a trait, which did not match the disambiguator 'fn'
 --> /home/joshua/rustc2/default.rs:1:14
  |
1 | /// Link to [Default::default()]
  |              ^^^^^^^^^^^^^^^^^^
```
Now that we're returning the `Res` of the associated item,
not the trait itself, it got confused.
@@ -595,6 +596,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
link.trim_start_matches(prefix)
} else if link.ends_with("!()") {
kind = Some(MacroNS);
disambiguator = Some("bang");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set an enum instead of a string here (yes, it will have multiple variants for each spelling)? Bonus: we can make the kind derivation a method on it, so we just call disambiguator.kind(), and we can do the same thing with the DefKind, and we can also do the thing with the prefix-matching (something like fn strip_prefix(&mut String) -> Option<Self>)

We would have to add a method that converts it back to a useful string though. But we'd be able to nicely move a ton of code out of here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can reuse DefKind here. I agree that would be nicer than using strings everywhere, it already has article() and descr().

Copy link
Member

@Manishearth Manishearth Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyn514 I'd rather not use DefKind because we can have multiple disambiguators, like () vs fn vs function and we wanna be able to name them.

A cool thing we could do, however, is somehow generate the span for the disambiguator only, and then we don't need to explicitly name it and instead can call it "the function disambiguator here^^". For perf it might be desirable to generate this only when we need to raise an error, i.e. perform a search for it after the fact and adjust the span. Idk.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up describing the disambiguator with an article but still naming it . What do you think?

error: incompatible link kind for `c`
  --> $DIR/intra-links-disambiguator-mismatch.rs:59:14
   |
LL | /// Link to [c()]
   |              ^^^ help: to link to the constant, use its disambiguator: `const@c`
   |
   = note: this link resolved to a constant, which is not a function

Copy link
Member Author

@jyn514 jyn514 Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and if you know how to switch the order of the note and the suggestion please let me know, I'd rather it was

error: incompatible link kind for `c`
  --> $DIR/intra-links-disambiguator-mismatch.rs:59:14
   |
LL | /// Link to [c()]
   |              ^^^ note: this link resolved to a constant, which is not a function
   |
   = help: to link to the constant, use its disambiguator: `const@c`

jyn514 added 2 commits August 5, 2020 00:20
Clearly it has been resolved, because we say on the next line what it
resolved to.
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest that Disambiguator have different variants for mod vs module etc but it's fine.

@Manishearth
Copy link
Member

r=me when ready

@jyn514
Copy link
Member Author

jyn514 commented Aug 6, 2020

Ugh, this is failing to document libstd because the res for an associated function is the struct, not the method itself.

error: incompatible link kind for `Initializer::nop`
   --> library/std/src/io/mod.rs:595:33
    |
595 |     /// memory, it should call [`Initializer::nop()`]. See the documentation for
    |                                 ^^^^^^^^^^^^^^^^^^^^ help: to link to the struct, use its disambiguator: `struct@Initializer::nop`

Not sure how to fix this ... I think later parts of rustdoc do need the actual struct to know which page to link to.

@jyn514
Copy link
Member Author

jyn514 commented Aug 6, 2020

Oh wait, this is the issue I already fixed in https://github.com/rust-lang/rust/pull/75079/files#diff-2b73a40b29f966df2a77a3bbaf34a9f1 😆 should be pretty easy to rewrite it to an associated item.

jyn514 added 2 commits August 6, 2020 17:10
See comments in the diff; this is such a hack.

The reason this can't be done properly in `register_res` is because
there's no way to get back the parent type: calling
`tcx.parent(assoc_item)` gets you the _impl_, not the type.
You can call `tcx.impl_trait_ref(impl_).self_ty()`, but there's no way
to go from that to a DefId without unwrapping.
@jyn514
Copy link
Member Author

jyn514 commented Aug 6, 2020

I had to hack around associated items in the very last commit, you might want to look again to make sure that's fine. Otherwise this should hopefully be good to go.

@jyn514
Copy link
Member Author

jyn514 commented Aug 6, 2020

Oh hold on, I have a bad feeling https://github.com/rust-lang/rust/pull/75079/files#diff-2bcad7b16b56ef2ebb92f8e60ae33773R424 is broken. Let me write some more tests.

@jyn514
Copy link
Member Author

jyn514 commented Aug 6, 2020

Suprisingly it worked! I added a test anyway.

@Manishearth
Copy link
Member

Hack looks okay to me, but please add a docstr to the definition of the side channel field. r=me otherwise

@jyn514
Copy link
Member Author

jyn514 commented Aug 7, 2020

@bors r=Manishearth

@bors
Copy link
Contributor

bors commented Aug 7, 2020

📌 Commit 9914f73 has been approved by Manishearth

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 7, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 7, 2020
…arth

Rollup of 4 pull requests

Successful merges:

 - rust-lang#74774 (adds [*mut|*const] ptr::set_ptr_value)
 - rust-lang#75079 (Disallow linking to items with a mismatched disambiguator)
 - rust-lang#75203 (Make `IntoIterator` lifetime bounds of `&BTreeMap` match with `&HashMap` )
 - rust-lang#75227 (Fix ICE when using asm! on an unsupported architecture)

Failed merges:

r? @ghost
@bors bors merged commit 5b1ed09 into rust-lang:master Aug 7, 2020
@jyn514 jyn514 deleted the disambiguator branch August 7, 2020 11:44
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Aug 29, 2020
 Unify error reporting for intra-doc links

- Give a suggestion even if there is no span available
- Give a more accurate description of the change than 'use the
disambiguator'
- Write much less code

Closes rust-lang#75836.
r? @euclio
cc @pickfire - this gets rid of 'disambiguator' like you suggested in rust-lang#75079 (comment).
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 29, 2020
 Unify error reporting for intra-doc links

- Give a suggestion even if there is no span available
- Give a more accurate description of the change than 'use the
disambiguator'
- Write much less code

Closes rust-lang#75836.
r? @euclio
cc @pickfire - this gets rid of 'disambiguator' like you suggested in rust-lang#75079 (comment).
@cuviper cuviper added this to the 1.47.0 milestone May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

struct@ will resolve anything in the type namespace including things like mods and traits
6 participants