diff options
| author | Seiichi Uchida <seuchida@gmail.com> | 2019-01-27 23:14:57 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-27 23:14:57 +0900 |
| commit | 36c9dc646cb01bedfbb49269ff96e94cf1397eee (patch) | |
| tree | 68147c9d8bb073ed01220ec79dc8ef0f49ed04c3 /src | |
| parent | 5b6f6431a3d81026a1987f5081e64262ac96af82 (diff) | |
| parent | 2125ad272e036c13547e6c6ff8cdfd0188c91fd7 (diff) | |
Merge pull request #3293 from scampi/issue-3241
Keep leading colons for global paths
Diffstat (limited to 'src')
| -rw-r--r-- | src/imports.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/imports.rs b/src/imports.rs index f8721456aa7..dea8fdf313f 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -324,9 +324,8 @@ impl UseTree { attrs, }; - let leading_modsep = context.config.edition() == Edition::Edition2018 - && a.prefix.to_string().len() > 2 - && a.prefix.to_string().starts_with("::"); + let leading_modsep = + context.config.edition() == Edition::Edition2018 && a.prefix.is_global(); let mut modsep = leading_modsep; @@ -339,6 +338,10 @@ impl UseTree { match a.kind { UseTreeKind::Glob => { + // in case of a global path and the glob starts at the root, e.g., "::*" + if a.prefix.segments.len() == 1 && leading_modsep { + result.path.push(UseSegment::Ident("".to_owned(), None)); + } result.path.push(UseSegment::Glob); } UseTreeKind::Nested(ref list) => { @@ -357,6 +360,11 @@ impl UseTree { false, ) .collect(); + // in case of a global path and the nested list starts at the root, + // e.g., "::{foo, bar}" + if a.prefix.segments.len() == 1 && leading_modsep { + result.path.push(UseSegment::Ident("".to_owned(), None)); + } result.path.push(UseSegment::List( list.iter() .zip(items.into_iter()) @@ -367,7 +375,15 @@ impl UseTree { )); } UseTreeKind::Simple(ref rename, ..) => { - let name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned(); + // If the path has leading double colons and is composed of only 2 segments, then we + // bypass the call to path_to_imported_ident which would get only the ident and + // lose the path root, e.g., `that` in `::that`. + // The span of `a.prefix` contains the leading colons. + let name = if a.prefix.segments.len() == 2 && leading_modsep { + context.snippet(a.prefix.span).to_owned() + } else { + rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned() + }; let alias = rename.and_then(|ident| { if ident.name == "_" { // for impl-only-use |
