Skip to content

Commit 3858c0f

Browse files
bugadaniJoshua Nelson
and
Joshua Nelson
committed
Apply suggestions from code review
Co-authored-by: Joshua Nelson <[email protected]>
1 parent 3a640f2 commit 3858c0f

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -305,26 +305,19 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
305305
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => {
306306
if ns != ValueNS {
307307
return Err(ResolutionFailure::WrongNamespace(res, ns).into());
308-
} else {
309-
// In case this is a trait item, skip the
310-
// early return and try looking for the trait.
311308
}
309+
// Fall through: In case this is a trait item, skip the
310+
// early return and try looking for the trait.
312311
}
313312
Res::Def(DefKind::AssocTy, _) => {
314-
if ns == ValueNS {
313+
if ns != TypeNS {
315314
return Err(ResolutionFailure::WrongNamespace(res, ns).into());
316-
} else {
317-
// In case this is a trait item, skip the
318-
// early return and try looking for the trait.
319315
}
316+
// Fall through: In case this is a trait item, skip the
317+
// early return and try looking for the trait.
320318
}
321319
Res::Def(DefKind::Variant, _) => {
322-
if extra_fragment.is_some() {
323-
return Err(ErrorKind::AnchorFailure(
324-
AnchorFailure::RustdocAnchorConflict(res),
325-
));
326-
}
327-
return handle_variant(cx, res);
320+
return handle_variant(cx, res, extra_fragment);
328321
}
329322
// Not a trait item; just return what we found.
330323
Res::PrimTy(ty) => {
@@ -581,7 +574,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
581574
current_item: &Option<String>,
582575
extra_fragment: &Option<String>,
583576
) -> Option<Res> {
584-
// resolve can't be used for macro namespace
577+
// resolve() can't be used for macro namespace
585578
let result = match ns {
586579
Namespace::MacroNS => self.macro_resolve(path_str, module_id).map_err(ErrorKind::from),
587580
Namespace::TypeNS | Namespace::ValueNS => self
@@ -1946,9 +1939,13 @@ fn privacy_error(
19461939
fn handle_variant(
19471940
cx: &DocContext<'_>,
19481941
res: Res,
1942+
extra_fragment: &Option<String>,
19491943
) -> Result<(Res, Option<String>), ErrorKind<'static>> {
19501944
use rustc_middle::ty::DefIdTree;
19511945

1946+
if extra_fragment.is_some() {
1947+
return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res)));
1948+
}
19521949
cx.tcx.parent(res.def_id()).map_or_else(
19531950
|| Err(ResolutionFailure::NoParentItem.into()),
19541951
|parent| {
@@ -1980,7 +1977,9 @@ const PRIMITIVES: &[(&str, Res)] = &[
19801977
];
19811978

19821979
fn is_primitive(path_str: &str, ns: Namespace) -> Option<(&'static str, Res)> {
1983-
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).copied() } else { None }
1980+
is_bool_value(path_str, ns).or_else(|| {
1981+
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).copied() } else { None }
1982+
})
19841983
}
19851984

19861985
fn is_bool_value(path_str: &str, ns: Namespace) -> Option<(&'static str, Res)> {

src/test/rustdoc-ui/intra-links-ambiguity.rs

+4
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ pub mod foo {
3434
///
3535
/// Ambiguous non-implied shortcut link [`foo::bar`]. //~ERROR `foo::bar`
3636
pub struct Docs {}
37+
38+
/// [true] //~ ERROR `true` is both a module and a builtin type
39+
/// [primitive@true]
40+
pub mod r#true {}

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,20 @@ help: to link to the function, add parentheses
8282
LL | /// Ambiguous non-implied shortcut link [`foo::bar()`].
8383
| ^^^^^^^^^^^^
8484

85-
error: aborting due to 5 previous errors
85+
error: `true` is both a module and a builtin type
86+
--> $DIR/intra-links-ambiguity.rs:38:6
87+
|
88+
LL | /// [true]
89+
| ^^^^ ambiguous link
90+
|
91+
help: to link to the module, prefix with `mod@`
92+
|
93+
LL | /// [mod@true]
94+
| ^^^^^^^^
95+
help: to link to the builtin type, prefix with `prim@`
96+
|
97+
LL | /// [prim@true]
98+
| ^^^^^^^^^
99+
100+
error: aborting due to 6 previous errors
86101

0 commit comments

Comments
 (0)