Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,31 @@ impl LinkCollector<'_, '_> {
}
}

/// This method checks specifically if a primitive has a potential ambiguity with a local module.
/// Returns `true` if an error was emitted.
fn check_mod_primitive_ambiguity(
&self,
res: &mut Res,
path_str: &str,
disambiguator: Option<Disambiguator>,
diag_info: &DiagnosticInfo<'_>,
) -> bool {
if !matches!(res, Res::Primitive(_))
&& let Some(prim) = resolve_primitive(path_str, TypeNS)
{
// `prim@char`
if matches!(disambiguator, Some(Disambiguator::Primitive)) {
*res = prim;
} else {
// `[char]` when a `char` module is in scope
let candidates = &[(*res, res.def_id(self.cx.tcx)), (prim, None)];
ambiguity_error(self.cx, diag_info, path_str, candidates, true);
return true;
}
}
false
}

fn compute_link(
&mut self,
mut res: Res,
Expand All @@ -1286,21 +1311,19 @@ impl LinkCollector<'_, '_> {
// Check for a primitive which might conflict with a module
// Report the ambiguity and require that the user specify which one they meant.
// FIXME: could there ever be a primitive not in the type namespace?
if matches!(
disambiguator,
None | Some(Disambiguator::Namespace(Namespace::TypeNS) | Disambiguator::Primitive)
) && !matches!(res, Res::Primitive(_))
&& let Some(prim) = resolve_primitive(path_str, TypeNS)
{
// `prim@char`
if matches!(disambiguator, Some(Disambiguator::Primitive)) {
res = prim;
} else {
// `[char]` when a `char` module is in scope
let candidates = &[(res, res.def_id(self.cx.tcx)), (prim, None)];
ambiguity_error(self.cx, &diag_info, path_str, candidates, true);
return None;
let emitted_error = match disambiguator {
None | Some(Disambiguator::Primitive) => {
self.check_mod_primitive_ambiguity(&mut res, path_str, disambiguator, &diag_info)
}
Some(Disambiguator::Namespace(Namespace::TypeNS))
if !matches!(res, Res::Def(DefKind::TyAlias, _)) =>
{
self.check_mod_primitive_ambiguity(&mut res, path_str, disambiguator, &diag_info)
}
_ => false,
};
if emitted_error {
return None;
}

match res {
Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/intra-doc/type-alias-primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Ensure that no warning is emitted if the disambiguator is used for type alias.
// Regression test for <https://github.com/rust-lang/rust/issues/146855>.

//@ check-pass

#![deny(rustdoc::broken_intra_doc_links)]

pub struct Foo;
#[allow(non_camel_case_types)]
pub type f32 = Foo;

/// This function returns [`type@f32`]" and not [`prim@f32`].
pub fn my_fn() -> f32 {}
14 changes: 14 additions & 0 deletions tests/rustdoc/intra-doc/type-alias-primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Ensure that no warning is emitted if the disambiguator is used for type alias.
// Regression test for <https://github.com/rust-lang/rust/issues/146855>.

#![crate_name = "foo"]

pub struct Foo;
#[allow(non_camel_case_types)]
pub type f32 = Foo;

/// This function returns [`type@f32`] and not [bla][`prim@f32`].
//@ has 'foo/fn.my_fn.html'
//@ has - '//a[@href="type.f32.html"]' "f32"
//@ has - '//a[@href="https://doc.rust-lang.org/nightly/std/primitive.f32.html"]' "bla"
pub fn my_fn() -> f32 {}
Loading