Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d4011e1

Browse files
committed
Use DiagnosticInfo for anchor failure
This gets rid of a lot of parameters, as well as fixing a diagnostic bug.
1 parent 6e4ef54 commit d4011e1

File tree

3 files changed

+30
-52
lines changed

3 files changed

+30
-52
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ struct ResolutionInfo {
251251
extra_fragment: Option<String>,
252252
}
253253

254+
#[derive(Clone)]
254255
struct DiagnosticInfo<'a> {
255256
item: &'a Item,
256257
dox: &'a str,
@@ -949,19 +950,19 @@ impl LinkCollector<'_, '_> {
949950
return None;
950951
}
951952

953+
let diag_info = DiagnosticInfo {
954+
item,
955+
dox,
956+
ori_link: &ori_link.link,
957+
link_range: ori_link.range.clone(),
958+
};
959+
952960
let link = ori_link.link.replace("`", "");
953961
let no_backticks_range = range_between_backticks(&ori_link);
954962
let parts = link.split('#').collect::<Vec<_>>();
955963
let (link, extra_fragment) = if parts.len() > 2 {
956964
// A valid link can't have multiple #'s
957-
anchor_failure(
958-
self.cx,
959-
&item,
960-
&link,
961-
dox,
962-
ori_link.range,
963-
AnchorFailure::MultipleAnchors,
964-
);
965+
anchor_failure(self.cx, diag_info, AnchorFailure::MultipleAnchors);
965966
return None;
966967
} else if parts.len() == 2 {
967968
if parts[0].trim().is_empty() {
@@ -1092,20 +1093,14 @@ impl LinkCollector<'_, '_> {
10921093
return None;
10931094
}
10941095

1095-
let diag_info = DiagnosticInfo {
1096-
item,
1097-
dox,
1098-
ori_link: &ori_link.link,
1099-
link_range: ori_link.range.clone(),
1100-
};
11011096
let (mut res, mut fragment) = self.resolve_with_disambiguator_cached(
11021097
ResolutionInfo {
11031098
module_id,
11041099
dis: disambiguator,
11051100
path_str: path_str.to_owned(),
11061101
extra_fragment,
11071102
},
1108-
diag_info,
1103+
diag_info.clone(), // this struct should really be Copy, but Range is not :(
11091104
matches!(ori_link.kind, LinkType::Reference | LinkType::Shortcut),
11101105
)?;
11111106

@@ -1123,10 +1118,7 @@ impl LinkCollector<'_, '_> {
11231118
if fragment.is_some() {
11241119
anchor_failure(
11251120
self.cx,
1126-
&item,
1127-
path_str,
1128-
dox,
1129-
ori_link.range,
1121+
diag_info,
11301122
AnchorFailure::RustdocAnchorConflict(prim),
11311123
);
11321124
return None;
@@ -1360,14 +1352,7 @@ impl LinkCollector<'_, '_> {
13601352
None
13611353
}
13621354
Err(ErrorKind::AnchorFailure(msg)) => {
1363-
anchor_failure(
1364-
self.cx,
1365-
diag.item,
1366-
diag.ori_link,
1367-
diag.dox,
1368-
diag.link_range,
1369-
msg,
1370-
);
1355+
anchor_failure(self.cx, diag, msg);
13711356
None
13721357
}
13731358
}
@@ -1384,29 +1369,15 @@ impl LinkCollector<'_, '_> {
13841369
Ok(res)
13851370
}
13861371
Err(ErrorKind::AnchorFailure(msg)) => {
1387-
anchor_failure(
1388-
self.cx,
1389-
diag.item,
1390-
diag.ori_link,
1391-
diag.dox,
1392-
diag.link_range,
1393-
msg,
1394-
);
1372+
anchor_failure(self.cx, diag, msg);
13951373
return None;
13961374
}
13971375
Err(ErrorKind::Resolve(box kind)) => Err(kind),
13981376
},
13991377
value_ns: match self.resolve(path_str, ValueNS, base_node, extra_fragment) {
14001378
Ok(res) => Ok(res),
14011379
Err(ErrorKind::AnchorFailure(msg)) => {
1402-
anchor_failure(
1403-
self.cx,
1404-
diag.item,
1405-
diag.ori_link,
1406-
diag.dox,
1407-
diag.link_range,
1408-
msg,
1409-
);
1380+
anchor_failure(self.cx, diag, msg);
14101381
return None;
14111382
}
14121383
Err(ErrorKind::Resolve(box kind)) => Err(kind),
@@ -2004,10 +1975,7 @@ fn resolution_failure(
20041975
/// Report an anchor failure.
20051976
fn anchor_failure(
20061977
cx: &DocContext<'_>,
2007-
item: &Item,
2008-
ori_link: &str,
2009-
dox: &str,
2010-
link_range: Range<usize>,
1978+
DiagnosticInfo { item, ori_link, dox, link_range }: DiagnosticInfo<'_>,
20111979
failure: AnchorFailure,
20121980
) {
20131981
let msg = match failure {

src/test/rustdoc-ui/intra-doc/anchors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ pub fn enum_link() {}
4343
/// [u32#hello]
4444
//~^ ERROR `u32#hello` contains an anchor
4545
pub fn x() {}
46+
47+
/// [prim@usize#x]
48+
//~^ ERROR `prim@usize#x` contains an anchor
49+
pub mod usize {}

src/test/rustdoc-ui/intra-doc/anchors.stderr

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
error: `Foo::f#hola` contains an anchor, but links to fields are already anchored
2-
--> $DIR/anchors.rs:25:15
1+
error: `prim@usize#x` contains an anchor, but links to builtin types are already anchored
2+
--> $DIR/anchors.rs:47:6
33
|
4-
LL | /// Or maybe [Foo::f#hola].
5-
| ^^^^^^^^^^^ contains invalid anchor
4+
LL | /// [prim@usize#x]
5+
| ^^^^^^^^^^^^ contains invalid anchor
66
|
77
note: the lint level is defined here
88
--> $DIR/anchors.rs:1:9
99
|
1010
LL | #![deny(rustdoc::broken_intra_doc_links)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
error: `Foo::f#hola` contains an anchor, but links to fields are already anchored
14+
--> $DIR/anchors.rs:25:15
15+
|
16+
LL | /// Or maybe [Foo::f#hola].
17+
| ^^^^^^^^^^^ contains invalid anchor
18+
1319
error: `hello#people#!` contains multiple anchors
1420
--> $DIR/anchors.rs:31:28
1521
|
@@ -28,5 +34,5 @@ error: `u32#hello` contains an anchor, but links to builtin types are already an
2834
LL | /// [u32#hello]
2935
| ^^^^^^^^^ contains invalid anchor
3036

31-
error: aborting due to 4 previous errors
37+
error: aborting due to 5 previous errors
3238

0 commit comments

Comments
 (0)