Skip to content

Commit b1af43b

Browse files
committed
Auto merge of rust-lang#76934 - camelid:rustdoc-allow-generic-params, r=jyn514
Allow generic parameters in intra-doc links Fixes rust-lang#62834. --- The contents of the generics will be mostly ignored (except for warning if fully-qualified syntax is used, which is currently unsupported in intra-doc links - see issue rust-lang#74563). * Allow links like `Vec<T>`, `Result<T, E>`, and `Option<Box<T>>` * Allow links like `Vec::<T>::new()` * Warn on * Unbalanced angle brackets (e.g. `Vec<T` or `Vec<T>>`) * Missing type to apply generics to (`<T>` or `<Box<T>>`) * Use of fully-qualified syntax (`<Vec as IntoIterator>::into_iter`) * Invalid path separator (`Vec:<T>:new`) * Too many angle brackets (`Vec<<T>>`) * Empty angle brackets (`Vec<>`) Note that this implementation *does* allow some constructs that aren't valid in the actual Rust syntax, for example `Box::<T>new()`. That may not be supported in rustdoc in the future; it is an implementation detail.
2 parents 790d19c + e2424a2 commit b1af43b

12 files changed

+441
-20
lines changed

compiler/rustc_middle/src/query/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ rustc_queries! {
9292
/// Computes the `DefId` of the corresponding const parameter in case the `key` is a
9393
/// const argument and returns `None` otherwise.
9494
///
95-
/// ```rust
95+
/// ```ignore (incomplete)
9696
/// let a = foo::<7>();
9797
/// // ^ Calling `opt_const_param_of` for this argument,
9898
///
@@ -162,10 +162,12 @@ rustc_queries! {
162162
/// Specifically this is the bounds written on the trait's type
163163
/// definition, or those after the `impl` keyword
164164
///
165+
/// ```ignore (incomplete)
165166
/// type X: Bound + 'lt
166-
/// ^^^^^^^^^^^
167+
/// // ^^^^^^^^^^^
167168
/// impl Debug + Display
168-
/// ^^^^^^^^^^^^^^^
169+
/// // ^^^^^^^^^^^^^^^
170+
/// ```
169171
///
170172
/// `key` is the `DefId` of the associated type or opaque type.
171173
///
@@ -176,18 +178,22 @@ rustc_queries! {
176178

177179
/// Elaborated version of the predicates from `explicit_item_bounds`.
178180
///
179-
/// Example for
181+
/// For example:
180182
///
183+
/// ```
181184
/// trait MyTrait {
182-
/// type MyAType: Eq + ?Sized`
185+
/// type MyAType: Eq + ?Sized;
183186
/// }
187+
/// ```
184188
///
185189
/// `explicit_item_bounds` returns `[<Self as MyTrait>::MyAType: Eq]`,
186190
/// and `item_bounds` returns
191+
/// ```text
187192
/// [
188193
/// <Self as Trait>::MyAType: Eq,
189194
/// <Self as Trait>::MyAType: PartialEq<<Self as Trait>::MyAType>
190195
/// ]
196+
/// ```
191197
///
192198
/// Bounds from the parent (e.g. with nested impl trait) are not included.
193199
query item_bounds(key: DefId) -> &'tcx ty::List<ty::Predicate<'tcx>> {

library/core/src/option.rs

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
//! ```
142142
//!
143143
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
144+
//! [`Box<U>`]: ../../std/boxed/struct.Box.html
145+
//! [`num::NonZero*`]: crate::num
146+
//! [`ptr::NonNull<U>`]: crate::ptr::NonNull
144147
145148
#![stable(feature = "rust1", since = "1.0.0")]
146149

src/doc/rustdoc/src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
- [Linking to items by name](linking-to-items-by-name.md)
99
- [Lints](lints.md)
1010
- [Passes](passes.md)
11-
- [Advanced Features](advanced-features.md)
11+
- [Advanced features](advanced-features.md)
1212
- [Unstable features](unstable-features.md)

src/doc/rustdoc/src/advanced-features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Advanced Features
1+
# Advanced features
22

33
The features listed on this page fall outside the rest of the main categories.
44

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Linking to items by name
22

3-
Rustdoc is capable of directly linking to other rustdoc pages in Markdown documentation using the path of item as a link.
3+
Rustdoc is capable of directly linking to other rustdoc pages using the path of
4+
the item as a link.
45

56
For example, in the following code all of the links will link to the rustdoc page for `Bar`:
67

@@ -19,15 +20,26 @@ pub struct Foo3;
1920
/// This struct is also not [`Bar`]
2021
pub struct Foo4;
2122

23+
/// This struct *is* [`Bar`]!
2224
pub struct Bar;
2325
```
2426

25-
You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and `crate`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros respectively. Backticks around the link will be stripped.
27+
Backticks around the link will be stripped, so ``[`Option`]`` will correctly
28+
link to `Option`.
29+
30+
You can refer to anything in scope, and use paths, including `Self`, `self`,
31+
`super`, and `crate`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros, respectively.
32+
33+
You can also refer to items with generic parameters like `Vec<T>`. The link will
34+
resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
35+
`<Vec as IntoIterator>::into_iter()`) is [not yet supported][fqs-issue], however.
36+
37+
[fqs-issue]: https://github.com/rust-lang/rust/issues/74563
2638

2739
```rust,edition2018
2840
use std::sync::mpsc::Receiver;
2941
30-
/// This is an version of [`Receiver`], with support for [`std::future`].
42+
/// This is a version of [`Receiver<T>`] with support for [`std::future`].
3143
///
3244
/// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
3345
pub struct AsyncReceiver<T> {
@@ -44,13 +56,15 @@ impl<T> AsyncReceiver<T> {
4456
You can also link to sections using URL fragment specifiers:
4557

4658
```rust
47-
/// This is a special implementation of [positional parameters]
59+
/// This is a special implementation of [positional parameters].
4860
///
4961
/// [positional parameters]: std::fmt#formatting-parameters
5062
struct MySpecialFormatter;
5163
```
5264

53-
Paths in Rust have three namespaces: type, value, and macro. Items from these namespaces are allowed to overlap. In case of ambiguity, rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`, `function@`, `mod@`, `fn@`, `module@`, `method@`, `prim@`, `primitive@`, `macro@`, or `derive@`:
65+
Paths in Rust have three namespaces: type, value, and macro. Item names must be
66+
unique within their namespace, but can overlap with items outside of their
67+
namespace. In case of ambiguity, rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`, `fn@`, `function@`, `mod@`, `module@`, `method@`, `prim@`, `primitive@`, `macro@`, or `derive@`:
5468

5569
```rust
5670
/// See also: [`Foo`](struct@Foo)
@@ -62,4 +76,19 @@ struct Foo {}
6276
fn Foo() {}
6377
```
6478

65-
Note: Because of how `macro_rules` macros are scoped in Rust, the intra-doc links of a `macro_rules` macro will be resolved relative to the crate root, as opposed to the module it is defined in.
79+
You can also disambiguate for functions by adding `()` after the function name,
80+
or for macros by adding `!` after the macro name:
81+
82+
```rust
83+
/// See also: [`Foo`](struct@Foo)
84+
struct Bar;
85+
86+
/// This is different from [`Foo()`]
87+
struct Foo {}
88+
89+
fn Foo() {}
90+
```
91+
92+
Note: Because of how `macro_rules!` macros are scoped in Rust, the intra-doc links of a `macro_rules!` macro will be resolved [relative to the crate root][#72243], as opposed to the module it is defined in.
93+
94+
[#72243]: https://github.com/rust-lang/rust/issues/72243

src/doc/rustdoc/src/lints.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
can use them like any other lints by doing this:
55

66
```rust,ignore
7-
#![allow(missing_docs)] // allowing the lint, no message
8-
#![warn(missing_docs)] // warn if there is missing docs
9-
#![deny(missing_docs)] // rustdoc will fail if there is missing docs
7+
#![allow(missing_docs)] // allows the lint, no diagnostics will be reported
8+
#![warn(missing_docs)] // warn if there are missing docs
9+
#![deny(missing_docs)] // error if there are missing docs
1010
```
1111

1212
Here is the list of the lints provided by `rustdoc`:
1313

1414
## broken_intra_doc_links
1515

16-
This lint **warns by default**. This lint detects when an [intra-doc link] fails to get resolved. For example:
16+
This lint **warns by default**. This lint detects when an [intra-doc link] fails to be resolved. For example:
1717

18-
[intra-doc link]: linking-to-items-by-name.html
18+
[intra-doc link]: linking-to-items-by-name.md
1919

2020
```rust
2121
/// I want to link to [`Nonexistent`] but it doesn't exist!

src/librustdoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
html_playground_url = "https://play.rust-lang.org/"
44
)]
55
#![feature(rustc_private)]
6+
#![feature(array_methods)]
67
#![feature(box_patterns)]
78
#![feature(box_syntax)]
89
#![feature(in_band_lifetimes)]
910
#![feature(nll)]
1011
#![feature(or_patterns)]
12+
#![feature(peekable_next_if)]
1113
#![feature(test)]
1214
#![feature(crate_visibility_modifier)]
1315
#![feature(never_type)]

0 commit comments

Comments
 (0)