Skip to content

Commit 0baa301

Browse files
authored
Rollup merge of #108459 - benediktwerner:rustdoc-fix-link-match, r=GuillaumeGomez
rustdoc: Fix LinkReplacer link matching It currently just uses the first link with the same href which might not necessarily be the matching one. This fixes replacements when there are several links to the same item but with different text (e.g. `[X] and [struct@X]`). It also fixes replacements in summaries since those use a links list with empty hrefs, so currently all links would always match the first link by href but then not match its text. This could also lead to a panic in the `original_lext[1..len() - 1]` part when the first link only has a single character, which is why the new code uses `.get(..)` instead.
2 parents ba1690b + 9968f3c commit 0baa301

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/librustdoc/html/markdown.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
381381
Some(Event::Code(text)) => {
382382
trace!("saw code {}", text);
383383
if let Some(link) = self.shortcut_link {
384-
trace!("original text was {}", link.original_text);
385384
// NOTE: this only replaces if the code block is the *entire* text.
386385
// If only part of the link has code highlighting, the disambiguator will not be removed.
387386
// e.g. [fn@`f`]
@@ -390,8 +389,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
390389
// So we could never be sure we weren't replacing too much:
391390
// [fn@my_`f`unc] is treated the same as [my_func()] in that pass.
392391
//
393-
// NOTE: &[1..len() - 1] is to strip the backticks
394-
if **text == link.original_text[1..link.original_text.len() - 1] {
392+
// NOTE: .get(1..len() - 1) is to strip the backticks
393+
if let Some(link) = self.links.iter().find(|l| {
394+
l.href == link.href
395+
&& Some(&**text) == l.original_text.get(1..l.original_text.len() - 1)
396+
}) {
395397
debug!("replacing {} with {}", text, link.new_text);
396398
*text = CowStr::Borrowed(&link.new_text);
397399
}
@@ -402,9 +404,12 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
402404
Some(Event::Text(text)) => {
403405
trace!("saw text {}", text);
404406
if let Some(link) = self.shortcut_link {
405-
trace!("original text was {}", link.original_text);
406407
// NOTE: same limitations as `Event::Code`
407-
if **text == *link.original_text {
408+
if let Some(link) = self
409+
.links
410+
.iter()
411+
.find(|l| l.href == link.href && **text == *l.original_text)
412+
{
408413
debug!("replacing {} with {}", text, link.new_text);
409414
*text = CowStr::Borrowed(&link.new_text);
410415
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(rustdoc::broken_intra_doc_links)]
2+
3+
pub struct S;
4+
pub mod char {}
5+
6+
// Ensure this doesn't ICE due to trying to slice off non-existent backticks from "S"
7+
8+
/// See [S] and [`S`]
9+
pub struct MyStruct1;
10+
11+
// Ensure that link texts are replaced correctly even if there are multiple links with
12+
// the same target but different text
13+
14+
/// See also [crate::char] and [mod@char] and [prim@char]
15+
// @has issue_108459/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char'
16+
// @has - '//*[@href="char/index.html"]' 'char'
17+
// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char'
18+
pub struct MyStruct2;
19+
20+
/// See also [mod@char] and [prim@char] and [crate::char]
21+
// @has issue_108459/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char'
22+
// @has - '//*[@href="char/index.html"]' 'char'
23+
// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char'
24+
pub struct MyStruct3;
25+
26+
// Ensure that links are correct even if there are multiple links with the same text but
27+
// different targets
28+
29+
/// See also [char][mod@char] and [char][prim@char]
30+
// @has issue_108459/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char'
31+
// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char'
32+
pub struct MyStruct4;
33+
34+
/// See also [char][prim@char] and [char][crate::char]
35+
// @has issue_108459/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char'
36+
// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char'
37+
pub struct MyStruct5;

tests/rustdoc/intra-doc/prim-precedence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ pub struct MyString;
1212

1313
/// See also [crate::char] and [mod@char]
1414
// @has prim_precedence/struct.MyString2.html '//*[@href="char/index.html"]' 'crate::char'
15-
// @has - '//*[@href="char/index.html"]' 'mod@char'
15+
// @has - '//*[@href="char/index.html"]' 'char'
1616
pub struct MyString2;

0 commit comments

Comments
 (0)