Skip to content

Commit ad497a3

Browse files
committed
Add path changes for 2018 edition.
1 parent b812ed1 commit ad497a3

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

src/items/use-declarations.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ fn main() {
5757
}
5858
```
5959

60+
## `use` Visibility
61+
6062
Like items, `use` declarations are private to the containing module, by
6163
default. Also like items, a `use` declaration can be public, if qualified by
6264
the `pub` keyword. Such a `use` declaration serves to _re-export_ a name. A
@@ -82,32 +84,32 @@ mod quux {
8284
In this example, the module `quux` re-exports two public names defined in
8385
`foo`.
8486

85-
Also note that the paths contained in `use` items are relative to the crate
86-
root. So, in the previous example, the `use` refers to `quux::foo::{bar, baz}`,
87-
and not simply to `foo::{bar, baz}`. This also means that top-level module
88-
declarations should be at the crate root if direct usage of the declared
89-
modules within `use` items is desired. It is also possible to use `self` and
90-
`super` at the beginning of a `use` item to refer to the current and direct
91-
parent modules respectively. All rules regarding accessing declared modules in
92-
`use` declarations apply to both module declarations and `extern crate`
93-
declarations.
87+
## `use` Paths
88+
89+
Paths in `use` items must start with a crate name or one of the [path
90+
qualifiers] `crate`, `self`, `super`, or `::`. `crate` refers to the current
91+
crate. `self` refers to the current module. `super` refers to the parent
92+
module. `::` can be used to explicitly refer to a crate, requiring an extern
93+
crate name to follow.
9494

9595
An example of what will and will not work for `use` items:
96+
<!-- Note: This example works as-is in either 2015 or 2018. -->
9697

9798
```rust
9899
# #![allow(unused_imports)]
99-
use foo::baz::foobaz; // good: foo is at the root of the crate
100+
use std::path::{self, Path, PathBuf}; // good: std is a crate name
101+
use crate::foo::baz::foobaz; // good: foo is at the root of the crate
100102

101103
mod foo {
102104

103105
mod example {
104106
pub mod iter {}
105107
}
106108

107-
use foo::example::iter; // good: foo is at crate root
108-
// use example::iter; // bad: example is not at the crate root
109+
use crate::foo::example::iter; // good: foo is at crate root
110+
// use example::iter; // bad: relative paths are not allowed without `self`
109111
use self::baz::foobaz; // good: self refers to module 'foo'
110-
use foo::bar::foobar; // good: foo is at crate root
112+
use crate::foo::bar::foobar; // good: foo is at crate root
111113

112114
pub mod bar {
113115
pub fn foobar() { }
@@ -122,6 +124,33 @@ mod foo {
122124
fn main() {}
123125
```
124126

127+
> **Edition Differences**: In the 2015 Edition, `use` paths also allow
128+
> accessing items in the crate root. Using the example above, the following
129+
> `use` paths work in 2015 but not 2018:
130+
>
131+
> ```rust,ignore
132+
> use foo::example::iter;
133+
> use ::foo::baz::foobaz;
134+
> ```
135+
>
136+
> In the 2018 Edition, if an in-scope item has the same name as an external
137+
> crate, then `use` of that crate name requires a leading `::` to
138+
> unambiguously select the crate name. This is to retain compatibility with
139+
> potential future changes. <!-- uniform_paths future-proofing -->
140+
>
141+
> ```rust,edition2018
142+
> // use std::fs; // Error, this is ambiguous.
143+
> use ::std::fs; // Imports from the `std` crate, not the module below.
144+
> use self::std::fs as self_fs; // Imports the module below.
145+
>
146+
> mod std {
147+
> pub mod fs {}
148+
> }
149+
> # fn main() {}
150+
> ```
151+
152+
125153
[IDENTIFIER]: identifiers.html
126154
[_SimplePath_]: paths.html#simple-paths
127155
[_Visibility_]: visibility-and-privacy.html
156+
[path qualifiers]: paths.html#path-qualifiers

src/paths.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ x::y::z;
2222
> &nbsp;&nbsp; `::`<sup>?</sup> _SimplePathSegment_ (`::` _SimplePathSegment_)<sup>\*</sup>
2323
>
2424
> _SimplePathSegment_ :\
25-
> &nbsp;&nbsp; [IDENTIFIER] | `super` | `self` | `$crate`
25+
> &nbsp;&nbsp; [IDENTIFIER] | `super` | `self` | `crate` | `$crate`
2626
2727
Simple paths are used in [visibility] markers, [attributes], [macros], and [`use`] items.
2828
Examples:
@@ -45,7 +45,7 @@ mod m {
4545
> &nbsp;&nbsp; _PathIdentSegment_ (`::` _GenericArgs_)<sup>?</sup>
4646
>
4747
> _PathIdentSegment_ :\
48-
> &nbsp;&nbsp; [IDENTIFIER] | `super` | `self` | `Self` | `$crate`
48+
> &nbsp;&nbsp; [IDENTIFIER] | `super` | `self` | `Self` | `crate` | `$crate`
4949
>
5050
> _GenericArgs_ :\
5151
> &nbsp;&nbsp; &nbsp;&nbsp; `<` `>`\
@@ -152,6 +152,12 @@ Paths starting with `::` are considered to be global paths where the segments of
152152
start being resolved from the crate root. Each identifier in the path must resolve to an
153153
item.
154154

155+
> **Edition Differences**: In the 2015 Edition, the crate root contains a variety of
156+
> different items, including external crates, default crates such as `std` and `core`, and
157+
> items in the top level of the crate (including `use` imports).
158+
>
159+
> Beginning with the 2018 Edition, paths starting with `::` can only reference crates.
160+
155161
```rust
156162
mod a {
157163
pub fn foo() {}
@@ -166,8 +172,8 @@ mod b {
166172

167173
### `self`
168174

169-
`self` resolves the path relative to the current module. When referring to an item, `self`
170-
can only be used as the first segment, without a preceding `::`.
175+
`self` resolves the path relative to the current module. `self` can only be used as the
176+
first segment, without a preceding `::`.
171177

172178
```rust
173179
fn foo() {}
@@ -236,13 +242,27 @@ mod a {
236242
# fn main() {}
237243
```
238244

245+
### `crate`
246+
247+
`crate` resolves the path relative to the current crate. `crate` can only be used as the
248+
first segment, without a preceding `::`.
249+
250+
```rust
251+
fn foo() {}
252+
mod a {
253+
fn bar() {
254+
crate::foo();
255+
}
256+
}
257+
# fn main() {}
258+
```
259+
239260
### `$crate`
240261

241262
`$crate` is only used within [macro transcribers], and can only be used as the first
242-
segment, without a preceding `::`. When a macro is imported from a crate named `foo`,
243-
`$crate` will expand to `::foo`. When a macro is used in the same crate where it is
244-
defined, `$crate` will expand to nothing. This allows you to reliably refer to items in
245-
the crate where the macro is defined.
263+
segment, without a preceding `::`. `$crate` will expand to a path to access items from the
264+
top level of the crate where the macro is defined, regardless of which crate the macro is
265+
invoked.
246266

247267
```rust
248268
pub fn increment(x: u32) -> u32 {

0 commit comments

Comments
 (0)