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

Commit b784f23

Browse files
authored
Merge pull request rust-lang#2952 from crw5996/fix-use-bug
Fixed modsep operator for various rustlang editions
2 parents 4f3c20c + 18cd0c4 commit b784f23

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

src/imports.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ast::{self, UseTreeKind};
1515
use syntax::source_map::{self, BytePos, Span, DUMMY_SP};
1616

1717
use comment::combine_strs_with_missing_comments;
18-
use config::IndentStyle;
18+
use config::{Edition, IndentStyle};
1919
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
2020
use rewrite::{Rewrite, RewriteContext};
2121
use shape::Shape;
@@ -144,6 +144,7 @@ impl UseSegment {
144144
fn from_path_segment(
145145
context: &RewriteContext,
146146
path_seg: &ast::PathSegment,
147+
modsep: bool,
147148
) -> Option<UseSegment> {
148149
let name = rewrite_ident(context, path_seg.ident);
149150
if name.is_empty() || name == "{{root}}" {
@@ -152,7 +153,10 @@ impl UseSegment {
152153
Some(match name {
153154
"self" => UseSegment::Slf(None),
154155
"super" => UseSegment::Super(None),
155-
_ => UseSegment::Ident((*name).to_owned(), None),
156+
_ => {
157+
let mod_sep = if modsep { "::" } else { "" };
158+
UseSegment::Ident(format!("{}{}", mod_sep, name), None)
159+
}
156160
})
157161
}
158162
}
@@ -313,8 +317,13 @@ impl UseTree {
313317
visibility,
314318
attrs,
315319
};
320+
321+
let leading_modsep = context.config.edition() == Edition::Edition2018
322+
&& a.prefix.to_string().len() > 2
323+
&& a.prefix.to_string().starts_with("::");
324+
316325
for p in &a.prefix.segments {
317-
if let Some(use_segment) = UseSegment::from_path_segment(context, p) {
326+
if let Some(use_segment) = UseSegment::from_path_segment(context, p, leading_modsep) {
318327
result.path.push(use_segment);
319328
}
320329
}

tests/source/issue-2927-2.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-edition: Edition2015
2+
#![feature(rust_2018_preview, uniform_paths)]
3+
use futures::prelude::*;
4+
use http_03::cli::Cli;
5+
use hyper::{service::service_fn_ok, Body, Response, Server};
6+
use ::log::{error, info, log};
7+
use structopt::StructOpt;

tests/source/issue-2927.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-edition: Edition2018
2+
#![feature(rust_2018_preview, uniform_paths)]
3+
use futures::prelude::*;
4+
use http_03::cli::Cli;
5+
use hyper::{service::service_fn_ok, Body, Response, Server};
6+
use ::log::{error, info, log};
7+
use structopt::StructOpt;

tests/target/issue-2927-2.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-edition: Edition2015
2+
#![feature(rust_2018_preview, uniform_paths)]
3+
use futures::prelude::*;
4+
use http_03::cli::Cli;
5+
use hyper::{service::service_fn_ok, Body, Response, Server};
6+
use log::{error, info, log};
7+
use structopt::StructOpt;

tests/target/issue-2927.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-edition: Edition2018
2+
#![feature(rust_2018_preview, uniform_paths)]
3+
use ::log::{error, info, log};
4+
use futures::prelude::*;
5+
use http_03::cli::Cli;
6+
use hyper::{service::service_fn_ok, Body, Response, Server};
7+
use structopt::StructOpt;

0 commit comments

Comments
 (0)