Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions compiler/rustc_resolve/src/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,15 @@ pub fn has_primitive_or_keyword_or_attribute_docs(attrs: &[impl AttributeExt]) -
}

/// Simplified version of the corresponding function in rustdoc.
/// If the rustdoc version returns a successful result, this function must return the same result.
/// Otherwise this function may return anything.
fn preprocess_link(link: &str) -> Box<str> {
// IMPORTANT: To be kept in sync with the corresponding function in rustdoc.
// Namely, whenever the rustdoc function returns a successful result for a
// given input, this function *MUST* return the same result!
Comment on lines 395 to +399
Copy link
Contributor

Choose a reason for hiding this comment

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

Somewhere in here we should mention that path_str is the field of PreprocessingInfo this return value corresponds to.


let link = link.replace('`', "");
let link = link.split('#').next().unwrap();
let link = link.trim();
let link = link.rsplit('@').next().unwrap();
let link = link.split_once('@').map_or(link, |(_, rhs)| rhs);
let link = link.strip_suffix("()").unwrap_or(link);
let link = link.strip_suffix("{}").unwrap_or(link);
let link = link.strip_suffix("[]").unwrap_or(link);
Expand Down
8 changes: 6 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,14 +938,18 @@ pub(crate) struct PreprocessedMarkdownLink(

/// Returns:
/// - `None` if the link should be ignored.
/// - `Some(Err)` if the link should emit an error
/// - `Some(Ok)` if the link is valid
/// - `Some(Err(_))` if the link should emit an error
/// - `Some(Ok(_))` if the link is valid
///
/// `link_buffer` is needed for lifetime reasons; it will always be overwritten and the contents ignored.
fn preprocess_link(
ori_link: &MarkdownLink,
dox: &str,
) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
// IMPORTANT: To be kept in sync with the corresponding function in `rustc_resolve::rustdoc`.
// Namely, whenever this function successfully returns a preprocessed link for a given input,
// the rustc counterpart *MUST* return the same link!

// certain link kinds cannot have their path be urls,
// so they should not be ignored, no matter how much they look like urls.
// e.g. [https://example.com/] is not a link to example.com.
Expand Down
8 changes: 0 additions & 8 deletions tests/rustdoc-ui/intra-doc/empty-associated-items.rs

This file was deleted.

17 changes: 17 additions & 0 deletions tests/rustdoc-ui/intra-doc/malformed-paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This test ensures that (syntactically) malformed paths will not crash rustdoc.
#![deny(rustdoc::broken_intra_doc_links)]

// This is a regression test for <https://github.com/rust-lang/rust/issues/140026>.
//! [`Type::`]
//~^ ERROR

// This is a regression test for <https://github.com/rust-lang/rust/issues/147981>
//! [`struct@Type@field`]
//~^ ERROR

//! [Type&content]
//~^ ERROR
//! [`Type::field%extra`]
//~^ ERROR

pub struct Type { pub field: () }
36 changes: 36 additions & 0 deletions tests/rustdoc-ui/intra-doc/malformed-paths.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: unresolved link to `Type::`
--> $DIR/malformed-paths.rs:5:7
|
LL | //! [`Type::`]
| ^^^^^^ the struct `Type` has no field or associated item named ``
|
note: the lint level is defined here
--> $DIR/malformed-paths.rs:2:9
|
LL | #![deny(rustdoc::broken_intra_doc_links)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unresolved link to `Type@field`
--> $DIR/malformed-paths.rs:9:7
|
LL | //! [`struct@Type@field`]
| ^^^^^^^^^^^^^^^^^ no item named `Type@field` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

error: unresolved link to `Type&content`
--> $DIR/malformed-paths.rs:12:6
|
LL | //! [Type&content]
| ^^^^^^^^^^^^ no item named `Type&content` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

error: unresolved link to `Type::field%extra`
--> $DIR/malformed-paths.rs:14:7
|
LL | //! [`Type::field%extra`]
| ^^^^^^^^^^^^^^^^^ the struct `Type` has no field or associated item named `field%extra`

error: aborting due to 4 previous errors

Loading