Skip to content

Commit 0483d34

Browse files
committed
Update intra_rustdoc_links
- Resolving paths - Path ambiguities - Linking to external crates
1 parent 98d06f2 commit 0483d34

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

text/0000-intra-rustdoc-links.md

+68
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,80 @@ will be rendered as
120120
You can use <a href="../bars/struct.Bar.html"><code>bars::Bar</code></a> for that.
121121
```
122122

123+
## Resolving paths
124+
125+
The Rust paths used in links are resolved
126+
relative to the item in whose documentation they appear.
127+
128+
For example:
129+
130+
```rust
131+
/// Container for a [Dolor](ipsum::Dolor).
132+
struct Lorem(ipsum::Dolor);
133+
134+
/// Contains various things, mostly [Dolor](ipsum::Dolor) and a helper function,
135+
/// [sit](ipsum::sit).
136+
mod ipsum {
137+
struct Dolor;
138+
139+
/// Takes a <Dolor> and does things.
140+
fn sit(d: Dolor) {}
141+
}
142+
143+
mod amet {
144+
//! Helper types, can be used with the [ipsum](super::ipsum) module.
145+
}
146+
```
147+
148+
## Path ambiguities
149+
150+
Rust allows items to have the same name. A short evaluation revealed that
151+
152+
- unit and tuple struct names
153+
can conflict with
154+
function names,
155+
- unit and tuple struct names
156+
can conflict with
157+
enum names,
158+
- and regular struct, enum, and trait names
159+
can conflict with
160+
each other
161+
(but not function names).
162+
163+
It appears the ambiguity can resolved
164+
by being able to restrict the path to function names.
165+
We propose that in case of ambiguity,
166+
you can add `()` as a suffix to path
167+
to only search for functions.
168+
(Additionally, to link to macros, you must add `!` to the path.)
169+
170+
This was originally proposed by
171+
[@kennytm](https://github.com/kennytm)
172+
[here](https://github.com/rust-lang/rfcs/pull/1946#issuecomment-284719684),
173+
going into more details:
174+
175+
> `<syntax::ptr::P>` — First search for type-like items. If not found, search for value-like items
176+
> `<syntax::ptr::P()>` — Only search for functions.
177+
> `<std::stringify!>` — Only search for macros.
178+
123179
## Linking to methods
124180

125181
To link to methods, it may be necessary to use fully-qualified paths,
126182
like `<Foo as Bar>::bar`.
127183
We have yet to analyse in which cases this is necessary,
128184
and this syntax is currently not described in [the reference's section on paths][ref-paths].
129185

186+
## Linking to external crates
187+
188+
Rustdoc is already able to link to external crates,
189+
and renders documentation for all dependencies by default.
190+
Referencing the standard library (or `core`)
191+
should generate links with a well-known base path,
192+
e.g. `https://doc.rust-lang.org/nightly/`.
193+
Referencing other external crates
194+
links to the pages Rustdoc has already rendered (or will render) for them.
195+
Special flags (e.g. `cargo doc --no-deps`) will not change this behavior.
196+
130197
## Errors
131198

132199
Ideally, Rustdoc would be able to recognize Rust path syntax,
@@ -200,6 +267,7 @@ Maybe present an example use case of a module whose documentation links to sever
200267
- The Rust paths might conflict with other valid links,
201268
though we could not think of any.
202269

270+
203271
# Alternatives
204272
[alternatives]: #alternatives
205273

0 commit comments

Comments
 (0)