Skip to content

Commit a27f530

Browse files
committed
Fix bad intra-doc-link preprocessing
1 parent 04ff05c commit a27f530

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,15 @@ pub fn has_primitive_or_keyword_or_attribute_docs(attrs: &[impl AttributeExt]) -
393393
}
394394

395395
/// Simplified version of the corresponding function in rustdoc.
396-
/// If the rustdoc version returns a successful result, this function must return the same result.
397-
/// Otherwise this function may return anything.
398396
fn preprocess_link(link: &str) -> Box<str> {
397+
// IMPORTANT: To be kept in sync with the corresponding function in rustdoc.
398+
// Namely, whenever the rustdoc function returns a successful result for a
399+
// given input, this function *MUST* return the same result!
400+
399401
let link = link.replace('`', "");
400402
let link = link.split('#').next().unwrap();
401403
let link = link.trim();
402-
let link = link.rsplit('@').next().unwrap();
404+
let link = link.split_once('@').map_or(link, |(_, rhs)| rhs);
403405
let link = link.strip_suffix("()").unwrap_or(link);
404406
let link = link.strip_suffix("{}").unwrap_or(link);
405407
let link = link.strip_suffix("[]").unwrap_or(link);

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,14 +938,18 @@ pub(crate) struct PreprocessedMarkdownLink(
938938

939939
/// Returns:
940940
/// - `None` if the link should be ignored.
941-
/// - `Some(Err)` if the link should emit an error
942-
/// - `Some(Ok)` if the link is valid
941+
/// - `Some(Err(_))` if the link should emit an error
942+
/// - `Some(Ok(_))` if the link is valid
943943
///
944944
/// `link_buffer` is needed for lifetime reasons; it will always be overwritten and the contents ignored.
945945
fn preprocess_link(
946946
ori_link: &MarkdownLink,
947947
dox: &str,
948948
) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
949+
// IMPORTANT: To be kept in sync with the corresponding function in `rustc_resolve::rustdoc`.
950+
// Namely, whenever this function successfully returns a preprocessed link for a given input,
951+
// the rustc counterpart *MUST* return the same link!
952+
949953
// certain link kinds cannot have their path be urls,
950954
// so they should not be ignored, no matter how much they look like urls.
951955
// e.g. [https://example.com/] is not a link to example.com.

tests/rustdoc-ui/intra-doc/empty-associated-items.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test ensures that (syntactically) malformed paths will not crash rustdoc.
2+
#![deny(rustdoc::broken_intra_doc_links)]
3+
4+
// This is a regression test for <https://github.com/rust-lang/rust/issues/140026>.
5+
//! [`Type::`]
6+
//~^ ERROR
7+
8+
// This is a regression test for <https://github.com/rust-lang/rust/issues/147981>
9+
//! [`struct@Type@field`]
10+
//~^ ERROR
11+
12+
//! [Type&content]
13+
//~^ ERROR
14+
//! [`Type::field%extra`]
15+
//~^ ERROR
16+
17+
pub struct Type { pub field: () }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: unresolved link to `Type::`
2+
--> $DIR/malformed-paths.rs:5:7
3+
|
4+
LL | //! [`Type::`]
5+
| ^^^^^^ the struct `Type` has no field or associated item named ``
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/malformed-paths.rs:2:9
9+
|
10+
LL | #![deny(rustdoc::broken_intra_doc_links)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: unresolved link to `Type@field`
14+
--> $DIR/malformed-paths.rs:9:7
15+
|
16+
LL | //! [`struct@Type@field`]
17+
| ^^^^^^^^^^^^^^^^^ no item named `Type@field` in scope
18+
|
19+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
20+
21+
error: unresolved link to `Type&content`
22+
--> $DIR/malformed-paths.rs:12:6
23+
|
24+
LL | //! [Type&content]
25+
| ^^^^^^^^^^^^ no item named `Type&content` in scope
26+
|
27+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
28+
29+
error: unresolved link to `Type::field%extra`
30+
--> $DIR/malformed-paths.rs:14:7
31+
|
32+
LL | //! [`Type::field%extra`]
33+
| ^^^^^^^^^^^^^^^^^ the struct `Type` has no field or associated item named `field%extra`
34+
35+
error: aborting due to 4 previous errors
36+

0 commit comments

Comments
 (0)