Skip to content

Commit a5b3328

Browse files
committed
Revert to fix in from_ast() with the root :: prefix the name - with as handling enhancement
1 parent 2d52e71 commit a5b3328

File tree

4 files changed

+59
-50
lines changed

4 files changed

+59
-50
lines changed

src/formatting/imports.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::{
1111
use crate::config::lists::*;
1212
use crate::config::{Edition, IndentStyle};
1313
use crate::formatting::{
14-
comment::combine_strs_with_missing_comments,
14+
comment::{combine_strs_with_missing_comments, contains_comment},
1515
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator},
1616
reorder::{compare_as_versions, compare_opt_ident_as_versions},
1717
rewrite::{Rewrite, RewriteContext},
@@ -94,7 +94,6 @@ pub(crate) enum UseSegment {
9494
Super(Option<String>),
9595
Crate(Option<String>),
9696
Glob,
97-
Empty,
9897
List(Vec<UseTree>),
9998
}
10099

@@ -147,18 +146,16 @@ impl UseSegment {
147146
) -> Option<UseSegment> {
148147
let name = rewrite_ident(context, path_seg.ident);
149148
if name.is_empty() || name == "{{root}}" {
150-
//return None;
151-
if modsep {
152-
return Some(UseSegment::Empty);
153-
} else {
154-
return None;
155-
}
149+
return None;
156150
}
157151
Some(match name {
158152
"self" => UseSegment::Slf(None),
159153
"super" => UseSegment::Super(None),
160154
"crate" => UseSegment::Crate(None),
161-
_ => UseSegment::Ident(name.to_string(), None),
155+
_ => {
156+
let mod_sep = if modsep { "::" } else { "" };
157+
UseSegment::Ident(format!("{}{}", mod_sep, name), None)
158+
}
162159
})
163160
}
164161
}
@@ -198,10 +195,7 @@ impl fmt::Display for UseSegment {
198195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199196
match *self {
200197
UseSegment::Glob => write!(f, "*"),
201-
UseSegment::Empty => write!(f, ""),
202-
UseSegment::Ident(ref s, _) => {
203-
write!(f, "{}", s)
204-
}
198+
UseSegment::Ident(ref s, _) => write!(f, "{}", s),
205199
UseSegment::Slf(..) => write!(f, "self"),
206200
UseSegment::Super(..) => write!(f, "super"),
207201
UseSegment::Crate(..) => write!(f, "crate"),
@@ -350,28 +344,25 @@ impl UseTree {
350344
}
351345
}
352346

353-
let segment_from_simple = |path: &ast::Path, rename: &Option<symbol::Ident>| {
354-
let name = rewrite_ident(context, path_to_imported_ident(&path)).to_owned();
355-
let alias = rename.and_then(|ident| {
347+
let rename_to_alias = |rename: &Option<symbol::Ident>| {
348+
rename.and_then(|ident| {
356349
if ident.name == sym::underscore_imports {
357350
// for impl-only-use
358351
Some("_".to_owned())
359-
} else if ident == path_to_imported_ident(&path) {
352+
} else if ident == path_to_imported_ident(&a.prefix) {
360353
None
361354
} else {
362355
Some(rewrite_ident(context, ident).to_owned())
363356
}
364-
});
365-
match name.as_ref() {
366-
"self" => UseSegment::Slf(alias),
367-
"super" => UseSegment::Super(alias),
368-
"crate" => UseSegment::Crate(alias),
369-
_ => UseSegment::Ident(name, alias),
370-
}
357+
})
371358
};
372359

373360
match a.kind {
374361
UseTreeKind::Glob => {
362+
// in case of a global path and the glob starts at the root, e.g., "::*"
363+
if a.prefix.segments.len() == 1 && leading_modsep {
364+
result.path.push(UseSegment::Ident("".to_owned(), None));
365+
}
375366
result.path.push(UseSegment::Glob);
376367
}
377368
UseTreeKind::Nested(ref list) => {
@@ -392,32 +383,42 @@ impl UseTree {
392383
.collect();
393384

394385
// find whether a case of a global path and the nested list starts at the root
395-
// with one item, e.g., "::{foo}", and does not include comments or "as".
396-
let mut path = None;
397-
let mut rename = None;
386+
// with one item, e.g., "::{foo as bar}", and does not include comments.
387+
let mut first_item = None;
388+
let mut first_alias = None;
398389
if a.prefix.segments.len() == 1 && list.len() == 1 && result.to_string().is_empty()
399390
{
400391
let first = &list[0].0;
401392
match first.kind {
402-
UseTreeKind::Simple(ref ren, ..) => {
393+
UseTreeKind::Simple(ref rename, ..) => {
403394
// "-1" for the "}"
404395
let snippet = context
405396
.snippet(mk_sp(first.span.lo(), span.hi() - BytePos(1)))
406397
.trim();
407-
// Ensure that indent includes only the name and not
408-
// "as" clause, comments, etc.
409-
if snippet.eq(&format!("{}", first.prefix.segments[0].ident)) {
410-
path = Some(&first.prefix);
411-
rename = Some(ren);
398+
// Ensure that indent does not include comments
399+
if !contains_comment(&snippet) {
400+
first_item = Some(first);
401+
first_alias = rename_to_alias(rename);
412402
}
413403
}
414404
_ => {}
415405
}
416406
};
417407

418-
if let (Some(path), Some(rename)) = (path, rename) {
419-
result.path.push(segment_from_simple(path, rename));
408+
if let Some(first) = first_item {
409+
// in case of a global path and the nested list starts at the root
410+
// with one item, e.g., "::{foo as bar}"
411+
let tree = Self::from_ast(context, first, None, None, None, None);
412+
let mod_sep = if leading_modsep { "::" } else { "" };
413+
let seg = UseSegment::Ident(format!("{}{}", mod_sep, tree), first_alias);
414+
result.path.pop();
415+
result.path.push(seg);
420416
} else {
417+
// in case of a global path and the nested list starts at the root,
418+
// e.g., "::{foo, bar}"
419+
if a.prefix.segments.len() == 1 && leading_modsep {
420+
result.path.push(UseSegment::Ident("".to_owned(), None));
421+
}
421422
result.path.push(UseSegment::List(
422423
list.iter()
423424
.zip(items.into_iter())
@@ -433,7 +434,19 @@ impl UseTree {
433434
// bypass the call to path_to_imported_ident which would get only the ident and
434435
// lose the path root, e.g., `that` in `::that`.
435436
// The span of `a.prefix` contains the leading colons.
436-
let segment = segment_from_simple(&a.prefix, rename);
437+
let name = if a.prefix.segments.len() == 2 && leading_modsep {
438+
context.snippet(a.prefix.span).to_owned()
439+
} else {
440+
rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned()
441+
};
442+
let alias = rename_to_alias(rename);
443+
let segment = match name.as_ref() {
444+
"self" => UseSegment::Slf(alias),
445+
"super" => UseSegment::Super(alias),
446+
"crate" => UseSegment::Crate(alias),
447+
_ => UseSegment::Ident(name, alias),
448+
};
449+
437450
// `name` is already in result.
438451
result.path.pop();
439452
result.path.push(segment);
@@ -719,9 +732,6 @@ impl Ord for UseSegment {
719732
(_, &Super(_)) => Ordering::Greater,
720733
(&Crate(_), _) => Ordering::Less,
721734
(_, &Crate(_)) => Ordering::Greater,
722-
(&Empty, &Empty) => Ordering::Equal,
723-
(&Empty, _) => Ordering::Less,
724-
(_, &Empty) => Ordering::Greater,
725735
(&Ident(..), _) => Ordering::Less,
726736
(_, &Ident(..)) => Ordering::Greater,
727737
(&Glob, _) => Ordering::Less,
@@ -830,7 +840,6 @@ impl Rewrite for UseSegment {
830840
UseSegment::Crate(Some(ref rename)) => format!("crate as {}", rename),
831841
UseSegment::Crate(None) => "crate".to_owned(),
832842
UseSegment::Glob => "*".to_owned(),
833-
UseSegment::Empty => "".to_owned(),
834843
UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
835844
context,
836845
use_tree_list,

src/formatting/reorder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn group_imports(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
307307
local_imports.push(ut)
308308
}
309309
// These are probably illegal here
310-
UseSegment::Empty | UseSegment::Glob | UseSegment::List(_) => external_imports.push(ut),
310+
UseSegment::Glob | UseSegment::List(_) => external_imports.push(ut),
311311
}
312312
}
313313

tests/source/issue-3943.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use aaaa::{BBBB};
2525
use crate::detect::{Feature, cache};
2626
use super::{auxvec};
2727

28-
// Tests with comments and "as"
28+
// Tests with comments
2929
use a::{/* pre-comment */ item};
3030
use a::{ item /* post-comment */};
3131
use a::{/* pre-comment */ item /* post-comment */ };

tests/target/issue-3943.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// Tests for original #3943 issue
2+
use ::Foo;
23
use ::foo;
34
use ::foo::Bar1;
45
use ::foo::{Bar2, Baz2};
5-
use ::Foo;
66
use ::{Bar3, Baz3};
77

8+
use ::Foo;
89
use ::foo;
910
use ::foo::Bar;
1011
use ::foo::{Bar, Baz};
11-
use ::Foo;
1212
use ::{Bar, Baz};
1313

14+
use ::Foo;
1415
use ::foo;
1516
use ::foo::Bar;
1617
use ::foo::{Bar, Baz};
17-
use ::Foo;
1818
use ::{Bar, Baz};
1919

2020
// Additional tests for signle item `{}` handling
@@ -25,20 +25,20 @@ use ::BBBB;
2525
use aaaa::BBBB;
2626
use bbbbb::AAAA;
2727

28-
// Tests with comments and "as"
28+
// Tests with comments
2929
use a::{/* pre-comment */ item};
3030
use a::{item /* post-comment */};
3131
use a::{/* pre-comment */ item /* post-comment */};
3232

3333
// Misc
3434
use self::std::fs as self_fs;
35-
use ::foo;
36-
use ::foo as bar;
37-
use ::foo::{foo, Foo};
3835
use ::Foo;
3936
use ::Foo as baz;
4037
use ::Foo1;
41-
use ::*;
38+
use ::foo;
39+
use ::foo as bar;
40+
use ::foo::{foo, Foo};
4241
use dummy;
4342
use std;
4443
use Super::foo;
44+
use ::*;

0 commit comments

Comments
 (0)