From b11e2f23411ec9aea4aa261c644f2c9e0df9457a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 9 Aug 2020 00:02:08 -0400 Subject: [PATCH] Give precedence to primitives over modules This has less surprising behavior when there is a module with the same name as a primitive in scope. --- src/librustdoc/passes/collect_intra_doc_links.rs | 8 +++++--- src/test/rustdoc/intra-link-prim-precedence.rs | 7 ++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index f84486347af..062bd61a7d0 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -217,9 +217,11 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { return Ok((res, Some(path_str.to_owned()))); } Res::Def(DefKind::Mod, _) => { - // This resolved to a module, but if we were passed `type@`, - // we want primitive types to take precedence instead. - if disambiguator == Some(Disambiguator::Namespace(Namespace::TypeNS)) { + // This resolved to a module, but we want primitive types to take precedence instead. + if matches!( + disambiguator, + None | Some(Disambiguator::Namespace(Namespace::TypeNS)) + ) { if let Some(prim) = is_primitive(path_str, ns) { if extra_fragment.is_some() { return Err(ErrorKind::AnchorFailure(AnchorFailure::Primitive)); diff --git a/src/test/rustdoc/intra-link-prim-precedence.rs b/src/test/rustdoc/intra-link-prim-precedence.rs index 5f10c1ec4a7..15ea1232fd6 100644 --- a/src/test/rustdoc/intra-link-prim-precedence.rs +++ b/src/test/rustdoc/intra-link-prim-precedence.rs @@ -8,5 +8,10 @@ pub mod char {} pub struct MyString; /// See also [char] -// @has intra_link_prim_precedence/struct.MyString2.html '//a/@href' 'intra_link_prim_precedence/char/index.html' +// @has intra_link_prim_precedence/struct.MyString2.html '//a/@href' 'https://doc.rust-lang.org/nightly/std/primitive.char.html' pub struct MyString2; + +/// See also [crate::char] and [mod@char] +// @has intra_link_prim_precedence/struct.MyString3.html '//*[@href="../intra_link_prim_precedence/char/index.html"]' 'crate::char' +// @has - '//*[@href="../intra_link_prim_precedence/char/index.html"]' 'mod@char' +pub struct MyString3;