Skip to content

Commit 1da35f0

Browse files
authored
Rollup merge of rust-lang#146866 - GuillaumeGomez:intra-doc-link-disambiguator-tyalias-prim, r=lolbinarycat
[rustdoc] Correctly handle intra doc link when type alias disambiguator is passed for primitive Fixes rust-lang#146855. r? `@lolbinarycat`
2 parents 225e847 + 5b154a7 commit 1da35f0

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,31 @@ impl LinkCollector<'_, '_> {
12741274
}
12751275
}
12761276

1277+
/// This method checks specifically if a primitive has a potential ambiguity with a local module.
1278+
/// Returns `true` if an error was emitted.
1279+
fn check_mod_primitive_ambiguity(
1280+
&self,
1281+
res: &mut Res,
1282+
path_str: &str,
1283+
disambiguator: Option<Disambiguator>,
1284+
diag_info: &DiagnosticInfo<'_>,
1285+
) -> bool {
1286+
if !matches!(res, Res::Primitive(_))
1287+
&& let Some(prim) = resolve_primitive(path_str, TypeNS)
1288+
{
1289+
// `prim@char`
1290+
if matches!(disambiguator, Some(Disambiguator::Primitive)) {
1291+
*res = prim;
1292+
} else {
1293+
// `[char]` when a `char` module is in scope
1294+
let candidates = &[(*res, res.def_id(self.cx.tcx)), (prim, None)];
1295+
ambiguity_error(self.cx, diag_info, path_str, candidates, true);
1296+
return true;
1297+
}
1298+
}
1299+
false
1300+
}
1301+
12771302
fn compute_link(
12781303
&mut self,
12791304
mut res: Res,
@@ -1286,21 +1311,19 @@ impl LinkCollector<'_, '_> {
12861311
// Check for a primitive which might conflict with a module
12871312
// Report the ambiguity and require that the user specify which one they meant.
12881313
// FIXME: could there ever be a primitive not in the type namespace?
1289-
if matches!(
1290-
disambiguator,
1291-
None | Some(Disambiguator::Namespace(Namespace::TypeNS) | Disambiguator::Primitive)
1292-
) && !matches!(res, Res::Primitive(_))
1293-
&& let Some(prim) = resolve_primitive(path_str, TypeNS)
1294-
{
1295-
// `prim@char`
1296-
if matches!(disambiguator, Some(Disambiguator::Primitive)) {
1297-
res = prim;
1298-
} else {
1299-
// `[char]` when a `char` module is in scope
1300-
let candidates = &[(res, res.def_id(self.cx.tcx)), (prim, None)];
1301-
ambiguity_error(self.cx, &diag_info, path_str, candidates, true);
1302-
return None;
1314+
let emitted_error = match disambiguator {
1315+
None | Some(Disambiguator::Primitive) => {
1316+
self.check_mod_primitive_ambiguity(&mut res, path_str, disambiguator, &diag_info)
1317+
}
1318+
Some(Disambiguator::Namespace(Namespace::TypeNS))
1319+
if !matches!(res, Res::Def(DefKind::TyAlias, _)) =>
1320+
{
1321+
self.check_mod_primitive_ambiguity(&mut res, path_str, disambiguator, &diag_info)
13031322
}
1323+
_ => false,
1324+
};
1325+
if emitted_error {
1326+
return None;
13041327
}
13051328

13061329
match res {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Ensure that no warning is emitted if the disambiguator is used for type alias.
2+
// Regression test for <https://github.com/rust-lang/rust/issues/146855>.
3+
4+
//@ check-pass
5+
6+
#![deny(rustdoc::broken_intra_doc_links)]
7+
8+
pub struct Foo;
9+
#[allow(non_camel_case_types)]
10+
pub type f32 = Foo;
11+
12+
/// This function returns [`type@f32`]" and not [`prim@f32`].
13+
pub fn my_fn() -> f32 {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure that no warning is emitted if the disambiguator is used for type alias.
2+
// Regression test for <https://github.com/rust-lang/rust/issues/146855>.
3+
4+
#![crate_name = "foo"]
5+
6+
pub struct Foo;
7+
#[allow(non_camel_case_types)]
8+
pub type f32 = Foo;
9+
10+
/// This function returns [`type@f32`] and not [bla][`prim@f32`].
11+
//@ has 'foo/fn.my_fn.html'
12+
//@ has - '//a[@href="type.f32.html"]' "f32"
13+
//@ has - '//a[@href="https://doc.rust-lang.org/nightly/std/primitive.f32.html"]' "bla"
14+
pub fn my_fn() -> f32 {}

0 commit comments

Comments
 (0)