From 0be3ce43ff07494796387a4ceb732ef509c403e9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 19 Apr 2022 16:07:57 +0200 Subject: [PATCH 1/2] Fix intra-doc failure because of missing doc comment unindent call --- src/librustdoc/clean/types.rs | 4 ++++ .../passes/collect_intra_doc_links/early.rs | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4b473df155f58..f514e38984d27 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1165,6 +1165,10 @@ impl Attributes { ret } + crate fn doc_contains(&self, c: char) -> bool { + self.doc_strings.iter().any(|s| s.doc.as_str().contains(c)) + } + /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined /// with newlines. crate fn collapsed_doc_value(&self) -> Option { diff --git a/src/librustdoc/passes/collect_intra_doc_links/early.rs b/src/librustdoc/passes/collect_intra_doc_links/early.rs index dffceff045d0b..87642e6c2c9be 100644 --- a/src/librustdoc/passes/collect_intra_doc_links/early.rs +++ b/src/librustdoc/passes/collect_intra_doc_links/early.rs @@ -164,9 +164,14 @@ impl EarlyDocLinkResolver<'_, '_> { fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute]) { let module_id = self.current_mod.to_def_id(); let mut need_traits_in_scope = false; - for (doc_module, doc) in - Attributes::from_ast(attrs, None).collapsed_doc_value_by_module_level() - { + let mut attrs = Attributes::from_ast(attrs, None); + if !attrs.doc_contains('[') { + // No need to load anything if there is no link inside this doc comment... + return; + } + // The `unindent_comments` pass hasn't been run yet! + attrs.unindent_doc_comments(); + for (doc_module, doc) in attrs.collapsed_doc_value_by_module_level() { assert_eq!(doc_module, None); for link in markdown_links(&doc.as_str()) { if let Some(Ok(pinfo)) = preprocess_link(&link) { From c0ce7ac28c13cf77ae4d4356241e2544b485f911 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 19 Apr 2022 16:08:22 +0200 Subject: [PATCH 2/2] Add regression test for #96079 --- src/test/rustdoc/early-unindent.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/rustdoc/early-unindent.rs diff --git a/src/test/rustdoc/early-unindent.rs b/src/test/rustdoc/early-unindent.rs new file mode 100644 index 0000000000000..791a452c9571b --- /dev/null +++ b/src/test/rustdoc/early-unindent.rs @@ -0,0 +1,26 @@ +// This is a regression for https://github.com/rust-lang/rust/issues/96079. + +#![crate_name = "foo"] + +pub mod app { + pub struct S; + + impl S { + // @has 'foo/app/struct.S.html' + // @has - '//a[@href="../enums/enum.Foo.html#method.by_name"]' 'Foo::by_name' + /** + Doc comment hello! [`Foo::by_name`](`crate::enums::Foo::by_name`). + */ + pub fn whatever(&self) {} + } +} + +pub mod enums { + pub enum Foo { + Bar, + } + + impl Foo { + pub fn by_name(&self) {} + } +}