Skip to content

Commit 6875220

Browse files
committed
Use rustc_resolve's descr() instead of rewriting it
1 parent ebc8cb4 commit 6875220

File tree

2 files changed

+38
-54
lines changed

2 files changed

+38
-54
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ impl ResolutionFailure<'a> {
110110

111111
enum AnchorFailure {
112112
MultipleAnchors,
113-
Primitive,
114-
Variant,
115-
AssocConstant,
116-
AssocType,
117-
Field,
118-
Method,
113+
RustdocAnchorConflict(Res),
119114
}
120115

121116
struct LinkCollector<'a, 'tcx> {
@@ -288,7 +283,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
288283
// Not a trait item; just return what we found.
289284
Res::PrimTy(..) => {
290285
if extra_fragment.is_some() {
291-
return Err(ErrorKind::AnchorFailure(AnchorFailure::Primitive));
286+
return Err(ErrorKind::AnchorFailure(
287+
AnchorFailure::RustdocAnchorConflict(res),
288+
));
292289
}
293290
return Ok((res, Some(path_str.to_owned())));
294291
}
@@ -305,7 +302,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
305302
}
306303
} else if let Some((path, prim)) = is_primitive(path_str, ns) {
307304
if extra_fragment.is_some() {
308-
return Err(ErrorKind::AnchorFailure(AnchorFailure::Primitive));
305+
return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(
306+
prim,
307+
)));
309308
}
310309
return Ok((prim, Some(path.to_owned())));
311310
}
@@ -444,11 +443,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
444443
ty::AssocKind::Type => "associatedtype",
445444
};
446445
Some(if extra_fragment.is_some() {
447-
Err(ErrorKind::AnchorFailure(if kind == ty::AssocKind::Fn {
448-
AnchorFailure::Method
449-
} else {
450-
AnchorFailure::AssocConstant
451-
}))
446+
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(
447+
ty_res,
448+
)))
452449
} else {
453450
// HACK(jynelson): `clean` expects the type, not the associated item.
454451
// but the disambiguator logic expects the associated item.
@@ -470,11 +467,17 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
470467
};
471468
field.map(|item| {
472469
if extra_fragment.is_some() {
473-
Err(ErrorKind::AnchorFailure(if def.is_enum() {
474-
AnchorFailure::Variant
475-
} else {
476-
AnchorFailure::Field
477-
}))
470+
let res = Res::Def(
471+
if def.is_enum() {
472+
DefKind::Variant
473+
} else {
474+
DefKind::Field
475+
},
476+
item.did,
477+
);
478+
Err(ErrorKind::AnchorFailure(
479+
AnchorFailure::RustdocAnchorConflict(res),
480+
))
478481
} else {
479482
Ok((
480483
ty_res,
@@ -518,13 +521,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
518521
};
519522

520523
if extra_fragment.is_some() {
521-
Err(ErrorKind::AnchorFailure(if item.kind == ty::AssocKind::Const {
522-
AnchorFailure::AssocConstant
523-
} else if item.kind == ty::AssocKind::Type {
524-
AnchorFailure::AssocType
525-
} else {
526-
AnchorFailure::Method
527-
}))
524+
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(
525+
ty_res,
526+
)))
528527
} else {
529528
let res = Res::Def(item.kind.as_def_kind(), item.def_id);
530529
Ok((res, Some(format!("{}.{}", kind, item_name))))
@@ -889,8 +888,10 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
889888
Some(res.0)
890889
}
891890
Err(ErrorKind::Resolve(kind)) => kind.full_res(),
892-
// TODO: add `Res` to AnchorFailure
893-
Err(ErrorKind::AnchorFailure(_)) => None,
891+
Err(ErrorKind::AnchorFailure(
892+
AnchorFailure::RustdocAnchorConflict(res),
893+
)) => Some(res),
894+
Err(ErrorKind::AnchorFailure(AnchorFailure::MultipleAnchors)) => None,
894895
};
895896
this.kind_side_channel.take().map(|(kind, id)| Res::Def(kind, id)).or(res)
896897
};
@@ -1070,7 +1071,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
10701071
path_str,
10711072
&dox,
10721073
link_range,
1073-
AnchorFailure::Primitive,
1074+
AnchorFailure::RustdocAnchorConflict(prim),
10741075
);
10751076
continue;
10761077
}
@@ -1555,28 +1556,11 @@ fn anchor_failure(
15551556
) {
15561557
let msg = match failure {
15571558
AnchorFailure::MultipleAnchors => format!("`{}` contains multiple anchors", path_str),
1558-
AnchorFailure::Primitive
1559-
| AnchorFailure::Variant
1560-
| AnchorFailure::AssocConstant
1561-
| AnchorFailure::AssocType
1562-
| AnchorFailure::Field
1563-
| AnchorFailure::Method => {
1564-
let kind = match failure {
1565-
AnchorFailure::Primitive => "primitive type",
1566-
AnchorFailure::Variant => "enum variant",
1567-
AnchorFailure::AssocConstant => "associated constant",
1568-
AnchorFailure::AssocType => "associated type",
1569-
AnchorFailure::Field => "struct field",
1570-
AnchorFailure::Method => "method",
1571-
AnchorFailure::MultipleAnchors => unreachable!("should be handled already"),
1572-
};
1573-
1574-
format!(
1575-
"`{}` contains an anchor, but links to {kind}s are already anchored",
1576-
path_str,
1577-
kind = kind
1578-
)
1579-
}
1559+
AnchorFailure::RustdocAnchorConflict(res) => format!(
1560+
"`{}` contains an anchor, but links to {kind}s are already anchored",
1561+
path_str,
1562+
kind = res.descr(),
1563+
),
15801564
};
15811565

15821566
report_diagnostic(cx, &msg, item, dox, &link_range, |diag, sp| {
@@ -1689,7 +1673,7 @@ fn handle_variant(
16891673
use rustc_middle::ty::DefIdTree;
16901674

16911675
if extra_fragment.is_some() {
1692-
return Err(ErrorKind::AnchorFailure(AnchorFailure::Variant));
1676+
return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res)));
16931677
}
16941678
let parent = if let Some(parent) = cx.tcx.parent(res.def_id()) {
16951679
parent

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `Foo::f#hola` contains an anchor, but links to struct fields are already anchored
1+
error: `Foo::f#hola` contains an anchor, but links to fields are already anchored
22
--> $DIR/intra-links-anchors.rs:25:15
33
|
44
LL | /// Or maybe [Foo::f#hola].
@@ -16,13 +16,13 @@ error: `hello#people#!` contains multiple anchors
1616
LL | /// Another anchor error: [hello#people#!].
1717
| ^^^^^^^^^^^^^^ contains invalid anchor
1818

19-
error: `Enum::A#whatever` contains an anchor, but links to enum variants are already anchored
19+
error: `Enum::A#whatever` contains an anchor, but links to variants are already anchored
2020
--> $DIR/intra-links-anchors.rs:37:28
2121
|
2222
LL | /// Damn enum's variants: [Enum::A#whatever].
2323
| ^^^^^^^^^^^^^^^^ contains invalid anchor
2424

25-
error: `u32#hello` contains an anchor, but links to primitive types are already anchored
25+
error: `u32#hello` contains an anchor, but links to builtin types are already anchored
2626
--> $DIR/intra-links-anchors.rs:43:6
2727
|
2828
LL | /// [u32#hello]

0 commit comments

Comments
 (0)