-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
Some changes occurred in intra-doc-links. cc @jyn514 |
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"); |
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.
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.
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 think we can reuse DefKind
here. I agree that would be nicer than using strings everywhere, it already has article()
and descr()
.
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.
@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.
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 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
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 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`
Clearly it has been resolved, because we say on the next line what it resolved 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.
I was going to suggest that Disambiguator have different variants for mod
vs module
etc but it's fine.
r=me when ready |
Ugh, this is failing to document libstd because the
Not sure how to fix this ... I think later parts of rustdoc do need the actual struct to know which page to link to. |
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. |
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.
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. |
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. |
Suprisingly it worked! I added a test anyway. |
Hack looks okay to me, but please add a docstr to the definition of the side channel field. r=me otherwise |
@bors r=Manishearth |
📌 Commit 9914f73 has been approved by |
…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
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).
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).
Closes #74851
r? @Manishearth