Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 36c9dc6

Browse files
authored
Merge pull request rust-lang#3293 from scampi/issue-3241
Keep leading colons for global paths
2 parents 5b6f643 + 2125ad2 commit 36c9dc6

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,33 @@ the "travis example" badge above.
1515

1616
You can run `rustfmt` with Rust 1.24 and above.
1717

18+
### On the Stable toolchain
19+
1820
To install:
1921

20-
```
22+
```sh
2123
rustup component add rustfmt
2224
```
2325

24-
to run on a cargo project in the current working directory:
26+
To run on a cargo project in the current working directory:
2527

26-
```
28+
```sh
2729
cargo fmt
2830
```
2931

30-
For the latest and greatest `rustfmt` (nightly required):
31-
```
32+
### On the Nightly toolchain
33+
34+
For the latest and greatest `rustfmt`, nightly is required.
35+
36+
To install:
37+
38+
```sh
3239
rustup component add rustfmt --toolchain nightly
3340
```
34-
To run:
35-
```
41+
42+
To run on a cargo project in the current working directory:
43+
44+
```sh
3645
cargo +nightly fmt
3746
```
3847

@@ -67,15 +76,15 @@ because in the future Rustfmt might work on code where it currently does not):
6776

6877
## Installation
6978

70-
```
79+
```sh
7180
rustup component add rustfmt
7281
```
7382

7483
## Installing from source
7584

7685
To install from source (nightly required), first checkout to the tag or branch you want to install, then issue
7786

78-
```
87+
```sh
7988
cargo install --path .
8089
```
8190

@@ -161,6 +170,11 @@ Configuration options are either stable or unstable. Stable options can always
161170
be used, while unstable ones are only available on a nightly toolchain, and opt-in.
162171
See [Configurations.md](Configurations.md) for details.
163172

173+
### Rust's Editions
174+
175+
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
176+
executed through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition
177+
needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
164178

165179
## Tips
166180

@@ -178,7 +192,7 @@ See [Configurations.md](Configurations.md) for details.
178192

179193
Example:
180194

181-
```
195+
```sh
182196
cargo fmt -- --emit files
183197
```
184198

src/imports.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,8 @@ impl UseTree {
324324
attrs,
325325
};
326326

327-
let leading_modsep = context.config.edition() == Edition::Edition2018
328-
&& a.prefix.to_string().len() > 2
329-
&& a.prefix.to_string().starts_with("::");
327+
let leading_modsep =
328+
context.config.edition() == Edition::Edition2018 && a.prefix.is_global();
330329

331330
let mut modsep = leading_modsep;
332331

@@ -339,6 +338,10 @@ impl UseTree {
339338

340339
match a.kind {
341340
UseTreeKind::Glob => {
341+
// in case of a global path and the glob starts at the root, e.g., "::*"
342+
if a.prefix.segments.len() == 1 && leading_modsep {
343+
result.path.push(UseSegment::Ident("".to_owned(), None));
344+
}
342345
result.path.push(UseSegment::Glob);
343346
}
344347
UseTreeKind::Nested(ref list) => {
@@ -357,6 +360,11 @@ impl UseTree {
357360
false,
358361
)
359362
.collect();
363+
// in case of a global path and the nested list starts at the root,
364+
// e.g., "::{foo, bar}"
365+
if a.prefix.segments.len() == 1 && leading_modsep {
366+
result.path.push(UseSegment::Ident("".to_owned(), None));
367+
}
360368
result.path.push(UseSegment::List(
361369
list.iter()
362370
.zip(items.into_iter())
@@ -367,7 +375,15 @@ impl UseTree {
367375
));
368376
}
369377
UseTreeKind::Simple(ref rename, ..) => {
370-
let name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned();
378+
// If the path has leading double colons and is composed of only 2 segments, then we
379+
// bypass the call to path_to_imported_ident which would get only the ident and
380+
// lose the path root, e.g., `that` in `::that`.
381+
// The span of `a.prefix` contains the leading colons.
382+
let name = if a.prefix.segments.len() == 2 && leading_modsep {
383+
context.snippet(a.prefix.span).to_owned()
384+
} else {
385+
rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned()
386+
};
371387
let alias = rename.and_then(|ident| {
372388
if ident.name == "_" {
373389
// for impl-only-use

tests/source/issue-3241.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-edition: 2018
2+
3+
use ::ignore;
4+
use ::ignore::some::more;
5+
use ::{foo, bar};
6+
use ::*;
7+
use ::baz::{foo, bar};
8+
9+
fn main() {
10+
println!("Hello, world!");
11+
}

tests/target/issue-3241.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-edition: 2018
2+
3+
use ::baz::{bar, foo};
4+
use ::ignore;
5+
use ::ignore::some::more;
6+
use ::*;
7+
use ::{bar, foo};
8+
9+
fn main() {
10+
println!("Hello, world!");
11+
}

0 commit comments

Comments
 (0)