From 73e1459acb73e1703b0f192ff5d99b41834f3c4c Mon Sep 17 00:00:00 2001 From: benediktwerner <1benediktwerner@gmail.com> Date: Sat, 25 Feb 2023 17:52:25 +0100 Subject: [PATCH 1/5] rustdoc: Fix LinkReplacer link matching --- src/librustdoc/html/markdown.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 9ef0b501c0850..72064da3087ae 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -382,7 +382,6 @@ impl<'a, I: Iterator>> Iterator for LinkReplacer<'a, I> { Some(Event::Code(text)) => { trace!("saw code {}", text); if let Some(link) = self.shortcut_link { - trace!("original text was {}", link.original_text); // NOTE: this only replaces if the code block is the *entire* text. // If only part of the link has code highlighting, the disambiguator will not be removed. // e.g. [fn@`f`] @@ -391,8 +390,11 @@ impl<'a, I: Iterator>> Iterator for LinkReplacer<'a, I> { // So we could never be sure we weren't replacing too much: // [fn@my_`f`unc] is treated the same as [my_func()] in that pass. // - // NOTE: &[1..len() - 1] is to strip the backticks - if **text == link.original_text[1..link.original_text.len() - 1] { + // NOTE: .get(1..len() - 1) is to strip the backticks + if let Some(link) = self.links.iter().find(|l| { + l.href == link.href + && Some(&**text) == l.original_text.get(1..l.original_text.len() - 1) + }) { debug!("replacing {} with {}", text, link.new_text); *text = CowStr::Borrowed(&link.new_text); } @@ -403,9 +405,10 @@ impl<'a, I: Iterator>> Iterator for LinkReplacer<'a, I> { Some(Event::Text(text)) => { trace!("saw text {}", text); if let Some(link) = self.shortcut_link { - trace!("original text was {}", link.original_text); // NOTE: same limitations as `Event::Code` - if **text == *link.original_text { + if let Some(link) = + self.links.iter().find(|l| l.href == link.href && **text == l.original_text) + { debug!("replacing {} with {}", text, link.new_text); *text = CowStr::Borrowed(&link.new_text); } From ec162508608e2fb32a1b4433296cda2b01b003aa Mon Sep 17 00:00:00 2001 From: benediktwerner <1benediktwerner@gmail.com> Date: Sun, 26 Feb 2023 20:07:31 +0100 Subject: [PATCH 2/5] Fix rustdoc/intra-doc/prim-precedence test --- tests/rustdoc/intra-doc/prim-precedence.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rustdoc/intra-doc/prim-precedence.rs b/tests/rustdoc/intra-doc/prim-precedence.rs index 25625b95277fe..c5a64e42a0124 100644 --- a/tests/rustdoc/intra-doc/prim-precedence.rs +++ b/tests/rustdoc/intra-doc/prim-precedence.rs @@ -12,5 +12,5 @@ pub struct MyString; /// See also [crate::char] and [mod@char] // @has prim_precedence/struct.MyString2.html '//*[@href="char/index.html"]' 'crate::char' -// @has - '//*[@href="char/index.html"]' 'mod@char' +// @has - '//*[@href="char/index.html"]' 'char' pub struct MyString2; From f5494e172d89386afbcbf2c16f63b1a54451a05b Mon Sep 17 00:00:00 2001 From: benediktwerner <1benediktwerner@gmail.com> Date: Sun, 26 Feb 2023 20:36:04 +0100 Subject: [PATCH 3/5] rustdoc: Add test for correct LinkReplacer link matching --- tests/rustdoc/intra-doc/issue-108459.rs | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/rustdoc/intra-doc/issue-108459.rs diff --git a/tests/rustdoc/intra-doc/issue-108459.rs b/tests/rustdoc/intra-doc/issue-108459.rs new file mode 100644 index 0000000000000..5ce0618a063d7 --- /dev/null +++ b/tests/rustdoc/intra-doc/issue-108459.rs @@ -0,0 +1,35 @@ +#![deny(rustdoc::broken_intra_doc_links)] + +pub struct S; +pub mod char {} + +// Ensure this doesn't ICE due to trying to slice off non-existent backticks from "S" + +/// See [S] and [`S`] +pub struct MyStruct1; + +// Ensure that link texts are replaced correctly even if there are multiple links with the same target but different text + +/// See also [crate::char] and [mod@char] and [prim@char] +// @has prim_precedence/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char' +// @has - '//*[@href="char/index.html"]' 'char' +// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' +pub struct MyStruct2; + +/// See also [mod@char] and [prim@char] and [crate::char] +// @has prim_precedence/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char' +// @has - '//*[@href="char/index.html"]' 'char' +// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' +pub struct MyStruct3; + +// Ensure that links are correct even if there are multiple links with the same text but different targets + +/// See also [char][mod@char] and [char][prim@char] +// @has prim_precedence/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char' +// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' +pub struct MyStruct4; + +/// See also [char][prim@char] and [char][crate::char] +// @has prim_precedence/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char' +// @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' +pub struct MyStruct5; From bf77fed46371c858eb2c903cbbe8f3d3f3530bd7 Mon Sep 17 00:00:00 2001 From: benediktwerner <1benediktwerner@gmail.com> Date: Mon, 27 Feb 2023 13:05:35 +0100 Subject: [PATCH 4/5] Fix and format test --- tests/rustdoc/intra-doc/issue-108459.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/rustdoc/intra-doc/issue-108459.rs b/tests/rustdoc/intra-doc/issue-108459.rs index 5ce0618a063d7..b9d0a26f3d07c 100644 --- a/tests/rustdoc/intra-doc/issue-108459.rs +++ b/tests/rustdoc/intra-doc/issue-108459.rs @@ -8,28 +8,30 @@ pub mod char {} /// See [S] and [`S`] pub struct MyStruct1; -// Ensure that link texts are replaced correctly even if there are multiple links with the same target but different text +// Ensure that link texts are replaced correctly even if there are multiple links with +// the same target but different text /// See also [crate::char] and [mod@char] and [prim@char] -// @has prim_precedence/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char' +// @has issue-108459/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char' // @has - '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct2; /// See also [mod@char] and [prim@char] and [crate::char] -// @has prim_precedence/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char' +// @has issue-108459/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char' // @has - '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct3; -// Ensure that links are correct even if there are multiple links with the same text but different targets +// Ensure that links are correct even if there are multiple links with the same text but +// different targets /// See also [char][mod@char] and [char][prim@char] -// @has prim_precedence/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char' +// @has issue-108459/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct4; /// See also [char][prim@char] and [char][crate::char] -// @has prim_precedence/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char' +// @has issue-108459/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct5; From 8fed6df0f36e9badd91ae79c7f5bcd2436d29c93 Mon Sep 17 00:00:00 2001 From: benediktwerner <1benediktwerner@gmail.com> Date: Mon, 27 Feb 2023 15:18:07 +0100 Subject: [PATCH 5/5] Fix test for real --- tests/rustdoc/intra-doc/issue-108459.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/rustdoc/intra-doc/issue-108459.rs b/tests/rustdoc/intra-doc/issue-108459.rs index b9d0a26f3d07c..eb1c7a05e5409 100644 --- a/tests/rustdoc/intra-doc/issue-108459.rs +++ b/tests/rustdoc/intra-doc/issue-108459.rs @@ -12,13 +12,13 @@ pub struct MyStruct1; // the same target but different text /// See also [crate::char] and [mod@char] and [prim@char] -// @has issue-108459/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char' +// @has issue_108459/struct.MyStruct2.html '//*[@href="char/index.html"]' 'crate::char' // @has - '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct2; /// See also [mod@char] and [prim@char] and [crate::char] -// @has issue-108459/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char' +// @has issue_108459/struct.MyStruct3.html '//*[@href="char/index.html"]' 'crate::char' // @has - '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct3; @@ -27,11 +27,11 @@ pub struct MyStruct3; // different targets /// See also [char][mod@char] and [char][prim@char] -// @has issue-108459/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char' +// @has issue_108459/struct.MyStruct4.html '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct4; /// See also [char][prim@char] and [char][crate::char] -// @has issue-108459/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char' +// @has issue_108459/struct.MyStruct5.html '//*[@href="char/index.html"]' 'char' // @has - '//*[@href="{{channel}}/std/primitive.char.html"]' 'char' pub struct MyStruct5;