@@ -11,7 +11,7 @@ use rustc_span::{
11
11
use crate :: config:: lists:: * ;
12
12
use crate :: config:: { Edition , IndentStyle } ;
13
13
use crate :: formatting:: {
14
- comment:: combine_strs_with_missing_comments,
14
+ comment:: { combine_strs_with_missing_comments, contains_comment } ,
15
15
lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator } ,
16
16
reorder:: { compare_as_versions, compare_opt_ident_as_versions} ,
17
17
rewrite:: { Rewrite , RewriteContext } ,
@@ -94,7 +94,6 @@ pub(crate) enum UseSegment {
94
94
Super ( Option < String > ) ,
95
95
Crate ( Option < String > ) ,
96
96
Glob ,
97
- Empty ,
98
97
List ( Vec < UseTree > ) ,
99
98
}
100
99
@@ -147,18 +146,16 @@ impl UseSegment {
147
146
) -> Option < UseSegment > {
148
147
let name = rewrite_ident ( context, path_seg. ident ) ;
149
148
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 ;
156
150
}
157
151
Some ( match name {
158
152
"self" => UseSegment :: Slf ( None ) ,
159
153
"super" => UseSegment :: Super ( None ) ,
160
154
"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
+ }
162
159
} )
163
160
}
164
161
}
@@ -198,10 +195,7 @@ impl fmt::Display for UseSegment {
198
195
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
199
196
match * self {
200
197
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) ,
205
199
UseSegment :: Slf ( ..) => write ! ( f, "self" ) ,
206
200
UseSegment :: Super ( ..) => write ! ( f, "super" ) ,
207
201
UseSegment :: Crate ( ..) => write ! ( f, "crate" ) ,
@@ -350,28 +344,25 @@ impl UseTree {
350
344
}
351
345
}
352
346
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| {
356
349
if ident. name == sym:: underscore_imports {
357
350
// for impl-only-use
358
351
Some ( "_" . to_owned ( ) )
359
- } else if ident == path_to_imported_ident ( & path ) {
352
+ } else if ident == path_to_imported_ident ( & a . prefix ) {
360
353
None
361
354
} else {
362
355
Some ( rewrite_ident ( context, ident) . to_owned ( ) )
363
356
}
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
+ } )
371
358
} ;
372
359
373
360
match a. kind {
374
361
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
+ }
375
366
result. path . push ( UseSegment :: Glob ) ;
376
367
}
377
368
UseTreeKind :: Nested ( ref list) => {
@@ -392,32 +383,42 @@ impl UseTree {
392
383
. collect ( ) ;
393
384
394
385
// 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 ;
398
389
if a. prefix . segments . len ( ) == 1 && list. len ( ) == 1 && result. to_string ( ) . is_empty ( )
399
390
{
400
391
let first = & list[ 0 ] . 0 ;
401
392
match first. kind {
402
- UseTreeKind :: Simple ( ref ren , ..) => {
393
+ UseTreeKind :: Simple ( ref rename , ..) => {
403
394
// "-1" for the "}"
404
395
let snippet = context
405
396
. snippet ( mk_sp ( first. span . lo ( ) , span. hi ( ) - BytePos ( 1 ) ) )
406
397
. 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) ;
412
402
}
413
403
}
414
404
_ => { }
415
405
}
416
406
} ;
417
407
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) ;
420
416
} 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
+ }
421
422
result. path . push ( UseSegment :: List (
422
423
list. iter ( )
423
424
. zip ( items. into_iter ( ) )
@@ -433,7 +434,19 @@ impl UseTree {
433
434
// bypass the call to path_to_imported_ident which would get only the ident and
434
435
// lose the path root, e.g., `that` in `::that`.
435
436
// 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
+
437
450
// `name` is already in result.
438
451
result. path . pop ( ) ;
439
452
result. path . push ( segment) ;
@@ -719,9 +732,6 @@ impl Ord for UseSegment {
719
732
( _, & Super ( _) ) => Ordering :: Greater ,
720
733
( & Crate ( _) , _) => Ordering :: Less ,
721
734
( _, & Crate ( _) ) => Ordering :: Greater ,
722
- ( & Empty , & Empty ) => Ordering :: Equal ,
723
- ( & Empty , _) => Ordering :: Less ,
724
- ( _, & Empty ) => Ordering :: Greater ,
725
735
( & Ident ( ..) , _) => Ordering :: Less ,
726
736
( _, & Ident ( ..) ) => Ordering :: Greater ,
727
737
( & Glob , _) => Ordering :: Less ,
@@ -830,7 +840,6 @@ impl Rewrite for UseSegment {
830
840
UseSegment :: Crate ( Some ( ref rename) ) => format ! ( "crate as {}" , rename) ,
831
841
UseSegment :: Crate ( None ) => "crate" . to_owned ( ) ,
832
842
UseSegment :: Glob => "*" . to_owned ( ) ,
833
- UseSegment :: Empty => "" . to_owned ( ) ,
834
843
UseSegment :: List ( ref use_tree_list) => rewrite_nested_use_tree (
835
844
context,
836
845
use_tree_list,
0 commit comments