Skip to content

Commit ae1e7da

Browse files
committed
path-clarity: Terminology change to avoid misusing "relative" and "absolute"
Describe the new variant as "uniform paths", and the previous variant as "anchored use paths". These names both highlight the primary benefit of each variant, and avoid describing paths including "self" or "super" as "absolute".
1 parent d165681 commit ae1e7da

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

src/rust-2018/path-clarity.md

+27-31
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ features, but they end up *simplifying* the module system, to make it more
1313
clear as to what is going on.
1414

1515
Note: During the 2018 edition preview, there are two variants of the module
16-
system under consideration, the "absolute use paths" variant and the "relative
16+
system under consideration, the "uniform paths" variant and the "anchored use
1717
paths" variant. Most of these changes apply to both variants; the two variant
1818
sections call out the differences between the two. We encourage testing of the
19-
new "relative paths" variant introduced in edition preview 2. The release of
19+
new "uniform paths" variant introduced in edition preview 2. The release of
2020
the 2018 edition will use one of these two variants.
2121

2222
Here's a brief summary:
2323

2424
* `extern crate` is no longer needed
2525
* The `crate` keyword refers to the current crate.
26-
* Relative paths variant: Paths work uniformly in both `use` declarations and
27-
in other code, both in the top-level module and in submodules, and may use
28-
either absolute paths or local names relative to the current module.
29-
* Absolute use paths variant: Paths in `use` declarations are always absolute
30-
and begin with a crate name (or `crate`); paths in other code may use
31-
absolute paths or local names relative to the current module.
26+
* Uniform paths variant: Paths work uniformly in both `use` declarations and in
27+
other code. Paths work uniformly both in the top-level module and in
28+
submodules. Any path may start with a crate, with `crate`, `super`, or
29+
`self`, or with a local name relative to the current module.
30+
* Anchored use paths variant: Paths in `use` declarations always start with a
31+
crate name, or with `crate`, `super`, or `self`. Paths in code other than
32+
`use` declarations may also start with names relative to the current module.
3233
* A `foo.rs` and `foo/` subdirectory may coexist; `mod.rs` is no longer needed
3334
when placing submodules in a subdirectory.
3435

@@ -83,21 +84,21 @@ The prefix `::` previously referred to either the crate root or an external
8384
crate; it now unambiguously refers to an external crate. For instance,
8485
`::foo::bar` always refers to the name `bar` inside the external crate `foo`.
8586

86-
### Relative paths variant
87+
### Uniform paths variant
8788

88-
The relative paths variant of Rust 2018 simplifies and unifies path handling
89+
The uniform paths variant of Rust 2018 simplifies and unifies path handling
8990
compared to Rust 2015. In Rust 2015, paths work differently in `use`
9091
declarations than they do elsewhere. In particular, paths in `use`
9192
declarations would always start from the crate root, while paths in other code
9293
implicitly started from the current module. Those differences didn't have any
9394
effect in the top-level module, which meant that everything would seem
9495
straightforward until working on a project large enough to have submodules.
9596

96-
In the relative paths variant of Rust 2018, paths in `use` declarations and in
97+
In the uniform paths variant of Rust 2018, paths in `use` declarations and in
9798
other code always work the same way, both in the top-level module and in any
98-
submodule. You can either use a relative path from the current module, an
99-
absolute path from the top of the current crate (starting with `crate::`), or
100-
an absolute path starting from an external crate name.
99+
submodule. You can always use a relative path from the current module, a path
100+
starting from an external crate name, or a path starting with `crate`, `super`,
101+
or `self`.
101102

102103
Code that looked like this:
103104

@@ -135,7 +136,7 @@ will look exactly the same in Rust 2018, except that you can delete the `extern
135136
crate` line:
136137

137138
```rust,ignore
138-
// Rust 2018 (relative paths variant)
139+
// Rust 2018 (uniform paths variant)
139140
140141
use futures::Future;
141142
@@ -166,7 +167,7 @@ With Rust 2018, however, the same code will also work completely unmodified in
166167
a submodule:
167168

168169
```rust,ignore
169-
// Rust 2018 (relative paths variant)
170+
// Rust 2018 (uniform paths variant)
170171
171172
mod submodule {
172173
use futures::Future;
@@ -204,15 +205,10 @@ either rename one of the conflicting names or explicitly disambiguate the path.
204205
To explicitly disambiguate a path, use `::name` for an external crate name, or
205206
`self::name` for a local module or item.
206207

207-
### Absolute use paths variant
208+
### Anchored use paths variant
208209

209-
In the absolute use paths variant of Rust 2018, paths in `use` declarations
210-
*must* begin with one of:
211-
212-
- A crate name
213-
- `crate` for the current crate's root
214-
- `self` for the current module's root
215-
- `super` for the current module's parent
210+
In the anchored use paths variant of Rust 2018, paths in `use` declarations
211+
*must* begin with a crate name, `crate`, `self`, or `super`.
216212

217213
Code that looked like this:
218214

@@ -233,7 +229,7 @@ use foo::Bar;
233229
Now looks like this:
234230

235231
```rust,ignore
236-
// Rust 2018 (absolute use paths variant)
232+
// Rust 2018 (anchored use paths variant)
237233
238234
// 'futures' is the name of a crate
239235
use futures::Future;
@@ -278,7 +274,7 @@ mod submodule {
278274

279275
In the `futures` example, the `my_poll` function signature is incorrect, because `submodule`
280276
contains no items named `futures`; that is, this path is considered relative. But because
281-
`use` is absolute, `use futures::` works even though a lone `futures::` doesn't! With `std`
277+
`use` is anchored, `use futures::` works even though a lone `futures::` doesn't! With `std`
282278
it can be even more confusing, as you never wrote the `extern crate std;` line at all. So
283279
why does it work in `main` but not in a submodule? Same thing: it's a relative path because
284280
it's not in a `use` declaration. `extern crate std;` is inserted at the crate root, so
@@ -287,26 +283,26 @@ it's fine in `main`, but it doesn't exist in the submodule at all.
287283
Let's look at how this change affects things:
288284

289285
```rust,ignore
290-
// Rust 2018 (absolute use paths variant)
286+
// Rust 2018 (anchored use paths variant)
291287
292288
// no more `extern crate futures;`
293289
294290
mod submodule {
295-
// 'futures' is the name of a crate, so this is absolute and works
291+
// 'futures' is the name of a crate, so this is anchored and works
296292
use futures::Future;
297293
298-
// 'futures' is the name of a crate, so this is absolute and works
294+
// 'futures' is the name of a crate, so this is anchored and works
299295
fn my_poll() -> futures::Poll { ... }
300296
}
301297
302298
fn main() {
303-
// 'std' is the name of a crate, so this is absolute and works
299+
// 'std' is the name of a crate, so this is anchored and works
304300
let five = std::sync::Arc::new(5);
305301
}
306302
307303
mod submodule {
308304
fn function() {
309-
// 'std' is the name of a crate, so this is absolute and works
305+
// 'std' is the name of a crate, so this is anchored and works
310306
let five = std::sync::Arc::new(5);
311307
}
312308
}

0 commit comments

Comments
 (0)