From d4011e12708bc760f02ed27d6226e27317ed8629 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 5 Apr 2021 04:56:47 -0400 Subject: [PATCH] Use DiagnosticInfo for anchor failure This gets rid of a lot of parameters, as well as fixing a diagnostic bug. --- .../passes/collect_intra_doc_links.rs | 62 +++++-------------- src/test/rustdoc-ui/intra-doc/anchors.rs | 4 ++ src/test/rustdoc-ui/intra-doc/anchors.stderr | 16 +++-- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 40a93855cf2..09823d4b059 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -251,6 +251,7 @@ struct ResolutionInfo { extra_fragment: Option, } +#[derive(Clone)] struct DiagnosticInfo<'a> { item: &'a Item, dox: &'a str, @@ -949,19 +950,19 @@ impl LinkCollector<'_, '_> { return None; } + let diag_info = DiagnosticInfo { + item, + dox, + ori_link: &ori_link.link, + link_range: ori_link.range.clone(), + }; + let link = ori_link.link.replace("`", ""); let no_backticks_range = range_between_backticks(&ori_link); let parts = link.split('#').collect::>(); let (link, extra_fragment) = if parts.len() > 2 { // A valid link can't have multiple #'s - anchor_failure( - self.cx, - &item, - &link, - dox, - ori_link.range, - AnchorFailure::MultipleAnchors, - ); + anchor_failure(self.cx, diag_info, AnchorFailure::MultipleAnchors); return None; } else if parts.len() == 2 { if parts[0].trim().is_empty() { @@ -1092,12 +1093,6 @@ impl LinkCollector<'_, '_> { return None; } - let diag_info = DiagnosticInfo { - item, - dox, - ori_link: &ori_link.link, - link_range: ori_link.range.clone(), - }; let (mut res, mut fragment) = self.resolve_with_disambiguator_cached( ResolutionInfo { module_id, @@ -1105,7 +1100,7 @@ impl LinkCollector<'_, '_> { path_str: path_str.to_owned(), extra_fragment, }, - diag_info, + diag_info.clone(), // this struct should really be Copy, but Range is not :( matches!(ori_link.kind, LinkType::Reference | LinkType::Shortcut), )?; @@ -1123,10 +1118,7 @@ impl LinkCollector<'_, '_> { if fragment.is_some() { anchor_failure( self.cx, - &item, - path_str, - dox, - ori_link.range, + diag_info, AnchorFailure::RustdocAnchorConflict(prim), ); return None; @@ -1360,14 +1352,7 @@ impl LinkCollector<'_, '_> { None } Err(ErrorKind::AnchorFailure(msg)) => { - anchor_failure( - self.cx, - diag.item, - diag.ori_link, - diag.dox, - diag.link_range, - msg, - ); + anchor_failure(self.cx, diag, msg); None } } @@ -1384,14 +1369,7 @@ impl LinkCollector<'_, '_> { Ok(res) } Err(ErrorKind::AnchorFailure(msg)) => { - anchor_failure( - self.cx, - diag.item, - diag.ori_link, - diag.dox, - diag.link_range, - msg, - ); + anchor_failure(self.cx, diag, msg); return None; } Err(ErrorKind::Resolve(box kind)) => Err(kind), @@ -1399,14 +1377,7 @@ impl LinkCollector<'_, '_> { value_ns: match self.resolve(path_str, ValueNS, base_node, extra_fragment) { Ok(res) => Ok(res), Err(ErrorKind::AnchorFailure(msg)) => { - anchor_failure( - self.cx, - diag.item, - diag.ori_link, - diag.dox, - diag.link_range, - msg, - ); + anchor_failure(self.cx, diag, msg); return None; } Err(ErrorKind::Resolve(box kind)) => Err(kind), @@ -2004,10 +1975,7 @@ fn resolution_failure( /// Report an anchor failure. fn anchor_failure( cx: &DocContext<'_>, - item: &Item, - ori_link: &str, - dox: &str, - link_range: Range, + DiagnosticInfo { item, ori_link, dox, link_range }: DiagnosticInfo<'_>, failure: AnchorFailure, ) { let msg = match failure { diff --git a/src/test/rustdoc-ui/intra-doc/anchors.rs b/src/test/rustdoc-ui/intra-doc/anchors.rs index 009b291be1f..6785cb7abea 100644 --- a/src/test/rustdoc-ui/intra-doc/anchors.rs +++ b/src/test/rustdoc-ui/intra-doc/anchors.rs @@ -43,3 +43,7 @@ pub fn enum_link() {} /// [u32#hello] //~^ ERROR `u32#hello` contains an anchor pub fn x() {} + +/// [prim@usize#x] +//~^ ERROR `prim@usize#x` contains an anchor +pub mod usize {} diff --git a/src/test/rustdoc-ui/intra-doc/anchors.stderr b/src/test/rustdoc-ui/intra-doc/anchors.stderr index 97b0cea0c1e..787a68ed969 100644 --- a/src/test/rustdoc-ui/intra-doc/anchors.stderr +++ b/src/test/rustdoc-ui/intra-doc/anchors.stderr @@ -1,8 +1,8 @@ -error: `Foo::f#hola` contains an anchor, but links to fields are already anchored - --> $DIR/anchors.rs:25:15 +error: `prim@usize#x` contains an anchor, but links to builtin types are already anchored + --> $DIR/anchors.rs:47:6 | -LL | /// Or maybe [Foo::f#hola]. - | ^^^^^^^^^^^ contains invalid anchor +LL | /// [prim@usize#x] + | ^^^^^^^^^^^^ contains invalid anchor | note: the lint level is defined here --> $DIR/anchors.rs:1:9 @@ -10,6 +10,12 @@ note: the lint level is defined here LL | #![deny(rustdoc::broken_intra_doc_links)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: `Foo::f#hola` contains an anchor, but links to fields are already anchored + --> $DIR/anchors.rs:25:15 + | +LL | /// Or maybe [Foo::f#hola]. + | ^^^^^^^^^^^ contains invalid anchor + error: `hello#people#!` contains multiple anchors --> $DIR/anchors.rs:31:28 | @@ -28,5 +34,5 @@ error: `u32#hello` contains an anchor, but links to builtin types are already an LL | /// [u32#hello] | ^^^^^^^^^ contains invalid anchor -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors