Skip to content

Commit 67d88f9

Browse files
authored
Rollup merge of #147027 - GuillaumeGomez:tyalias-disambiguator, r=lolbinarycat
Add new `tyalias` intra-doc link disambiguator Fixes #146855. Alternative to #146866. This adds support for a new disambiguator: `tyalias`. I think it's common enough to have type aliases nowaday, so no reason to not be able to have a disambiguator for them. cc ``@fmease`` r? lolbinarycat
2 parents e504c10 + 62c457b commit 67d88f9

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

src/doc/rustdoc/src/write-documentation/linking-to-items-by-name.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn Foo() {}
9090
These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]` will be
9191
rendered as `Foo`. The following prefixes are available: `struct`, `enum`, `trait`, `union`,
9292
`mod`, `module`, `const`, `constant`, `fn`, `function`, `field`, `variant`, `method`, `derive`,
93-
`type`, `value`, `macro`, `prim` or `primitive`.
93+
`type`, `value`, `macro`, `tyalias`, `typealias`, `prim` or `primitive`.
9494

9595
You can also disambiguate for functions by adding `()` after the function name,
9696
or for macros by adding `!` after the macro name. The macro `!` can be followed by `()`, `{}`,

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl Res {
130130
DefKind::Static { .. } => "static",
131131
DefKind::Field => "field",
132132
DefKind::Variant | DefKind::Ctor(..) => "variant",
133+
DefKind::TyAlias => "tyalias",
133134
// Now handle things that don't have a specific disambiguator
134135
_ => match kind
135136
.ns()
@@ -1708,6 +1709,7 @@ impl Disambiguator {
17081709
"value" => NS(Namespace::ValueNS),
17091710
"macro" => NS(Namespace::MacroNS),
17101711
"prim" | "primitive" => Primitive,
1712+
"tyalias" | "typealias" => Kind(DefKind::TyAlias),
17111713
_ => return Err((format!("unknown disambiguator `{prefix}`"), 0..idx)),
17121714
};
17131715

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#![deny(rustdoc::broken_intra_doc_links)]
5+
6+
pub struct Foo;
7+
8+
#[allow(non_camel_case_types)]
9+
pub type f32 = Foo;
10+
11+
/// This function returns [`f32`].
12+
//~^ ERROR: `f32` is both a type alias and a primitive type
13+
//~| HELP: to link to the type alias, prefix with `tyalias@`
14+
//~| HELP: to link to the primitive type, prefix with `prim@`
15+
pub fn my_fn() -> f32 {}
16+
17+
/// This function returns [type@f32].
18+
//~^ ERROR: `f32` is both a type alias and a primitive type
19+
//~| HELP: to link to the type alias, prefix with `tyalias@`
20+
//~| HELP: to link to the primitive type, prefix with `prim@`
21+
pub fn my_fn2() -> f32 {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: `f32` is both a type alias and a primitive type
2+
--> $DIR/type-alias-primitive-suggestion.rs:11:29
3+
|
4+
LL | /// This function returns [`f32`].
5+
| ^^^ ambiguous link
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/type-alias-primitive-suggestion.rs:4:9
9+
|
10+
LL | #![deny(rustdoc::broken_intra_doc_links)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
help: to link to the type alias, prefix with `tyalias@`
13+
|
14+
LL | /// This function returns [`tyalias@f32`].
15+
| ++++++++
16+
help: to link to the primitive type, prefix with `prim@`
17+
|
18+
LL | /// This function returns [`prim@f32`].
19+
| +++++
20+
21+
error: `f32` is both a type alias and a primitive type
22+
--> $DIR/type-alias-primitive-suggestion.rs:17:28
23+
|
24+
LL | /// This function returns [type@f32].
25+
| ^^^^^^^^ ambiguous link
26+
|
27+
help: to link to the type alias, prefix with `tyalias@`
28+
|
29+
LL - /// This function returns [type@f32].
30+
LL + /// This function returns [tyalias@f32].
31+
|
32+
help: to link to the primitive type, prefix with `prim@`
33+
|
34+
LL - /// This function returns [type@f32].
35+
LL + /// This function returns [prim@f32].
36+
|
37+
38+
error: aborting due to 2 previous errors
39+
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+
//@ check-pass
5+
6+
#![deny(rustdoc::broken_intra_doc_links)]
7+
8+
pub struct Foo;
9+
10+
#[allow(non_camel_case_types)]
11+
pub type f32 = Foo;
12+
13+
/// This function returns [`tyalias@f32`] and not [`prim@f32`].
14+
pub fn my_fn() -> f32 {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#![deny(rustdoc::broken_intra_doc_links)]
6+
7+
pub struct Foo;
8+
9+
#[allow(non_camel_case_types)]
10+
pub type f32 = Foo;
11+
12+
/// This function returns [`tyalias@f32`] and not [bla][`prim@f32`].
13+
//@ has 'foo/fn.my_fn.html'
14+
//@ has - '//a[@href="type.f32.html"]' "f32"
15+
//@ has - '//a[@href="{{channel}}/std/primitive.f32.html"]' "bla"
16+
pub fn my_fn() -> f32 { 0. }
17+
18+
/// This function returns [`typealias@f32`].
19+
//@ has 'foo/fn.my_other_fn.html'
20+
//@ has - '//a[@href="type.f32.html"]' "f32"
21+
pub fn my_other_fn() -> f32 { 0. }

0 commit comments

Comments
 (0)