Skip to content

Commit 7d52715

Browse files
author
zombiefungus
committed
add new ImportAlias enum to differentiate no alias from an _ alias
1 parent dce7dc4 commit 7d52715

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ where
438438
} else {
439439
match import.path.segments.last() {
440440
Some(last_segment) => {
441-
let name = import.alias.clone().unwrap_or_else(|| last_segment.clone());
441+
let name = match &import.alias {
442+
raw::ImportAlias::Alias(name) => name.clone(),
443+
_ => last_segment.clone(), // "use as ;" and "use as _;" are treated the same way
444+
};
442445
log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
443446

444447
// extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658

crates/ra_hir_def/src/nameres/raw.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,21 @@ impl_arena_id!(Import);
145145
#[derive(Debug, Clone, PartialEq, Eq)]
146146
pub struct ImportData {
147147
pub(super) path: ModPath,
148-
pub(super) alias: Option<Name>,
148+
pub(super) alias: ImportAlias,
149149
pub(super) is_glob: bool,
150150
pub(super) is_prelude: bool,
151151
pub(super) is_extern_crate: bool,
152152
pub(super) is_macro_use: bool,
153153
pub(super) visibility: RawVisibility,
154154
}
155155

156+
#[derive(Debug, Clone, PartialEq, Eq)]
157+
pub enum ImportAlias {
158+
NoAlias,
159+
Unnamed, // use Foo as _;
160+
Alias(Name),
161+
}
162+
156163
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
157164
pub(super) struct Def(RawId);
158165
impl_arena_id!(Def);
@@ -353,7 +360,11 @@ impl RawItemsCollector {
353360
let path = ModPath::from_name_ref(&name_ref);
354361
let visibility =
355362
RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene);
356-
let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name());
363+
let alias = extern_crate.alias().map_or(ImportAlias::NoAlias, |a| {
364+
a.name()
365+
.map(|it| it.as_name())
366+
.map_or(ImportAlias::Unnamed, |a| ImportAlias::Alias(a))
367+
});
357368
let attrs = self.parse_attrs(&extern_crate);
358369
// FIXME: cfg_attr
359370
let is_macro_use = extern_crate.has_atom_attr("macro_use");

crates/ra_hir_def/src/path.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ impl ModPath {
5757
pub(crate) fn expand_use_item(
5858
item_src: InFile<ast::UseItem>,
5959
hygiene: &Hygiene,
60-
mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<Name>),
60+
mut cb: impl FnMut(
61+
ModPath,
62+
&ast::UseTree,
63+
/* is_glob */ bool,
64+
crate::nameres::raw::ImportAlias,
65+
),
6166
) {
6267
if let Some(tree) = item_src.value.use_tree() {
6368
lower::lower_use_tree(None, tree, hygiene, &mut cb);

crates/ra_hir_def/src/path/lower/lower_use.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
use std::iter;
55

66
use either::Either;
7-
use hir_expand::{
8-
hygiene::Hygiene,
9-
name::{AsName, Name},
10-
};
7+
use hir_expand::{hygiene::Hygiene, name::AsName};
118
use ra_syntax::ast::{self, NameOwner};
129
use test_utils::tested_by;
1310

11+
use crate::nameres::raw::ImportAlias;
1412
use crate::path::{ModPath, PathKind};
1513

1614
pub(crate) fn lower_use_tree(
1715
prefix: Option<ModPath>,
1816
tree: ast::UseTree,
1917
hygiene: &Hygiene,
20-
cb: &mut dyn FnMut(ModPath, &ast::UseTree, bool, Option<Name>),
18+
cb: &mut dyn FnMut(ModPath, &ast::UseTree, bool, ImportAlias),
2119
) {
2220
if let Some(use_tree_list) = tree.use_tree_list() {
2321
let prefix = match tree.path() {
@@ -34,7 +32,9 @@ pub(crate) fn lower_use_tree(
3432
lower_use_tree(prefix.clone(), child_tree, hygiene, cb);
3533
}
3634
} else {
37-
let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
35+
let alias = tree.alias().map_or(ImportAlias::NoAlias, |a| {
36+
a.name().map(|it| it.as_name()).map_or(ImportAlias::Unnamed, |a| ImportAlias::Alias(a))
37+
});
3838
let is_glob = tree.has_star();
3939
if let Some(ast_path) = tree.path() {
4040
// Handle self in a path.
@@ -57,7 +57,7 @@ pub(crate) fn lower_use_tree(
5757
} else if is_glob {
5858
tested_by!(glob_enum_group);
5959
if let Some(prefix) = prefix {
60-
cb(prefix, &tree, is_glob, None)
60+
cb(prefix, &tree, is_glob, ImportAlias::NoAlias)
6161
}
6262
}
6363
}

0 commit comments

Comments
 (0)