Skip to content

Commit 73c36fd

Browse files
bors[bot]zombiefungus
andauthored
Merge #2962
2962: Differentiate underscore alias from named aliases r=matklad a=zombiefungus pre for Fixing Issue 2736 edited to avoid autoclosing the issue Co-authored-by: zombiefungus <[email protected]>
2 parents 918547d + f4f71e3 commit 73c36fd

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
2323
raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode,
2424
},
25-
path::{ModPath, PathKind},
25+
path::{ImportAlias, ModPath, PathKind},
2626
per_ns::PerNs,
2727
visibility::Visibility,
2828
AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
@@ -438,7 +438,11 @@ 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+
Some(ImportAlias::Alias(name)) => name.clone(),
443+
Some(ImportAlias::Underscore) => last_segment.clone(), // FIXME rust-analyzer#2736
444+
None => last_segment.clone(),
445+
};
442446
log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
443447

444448
// 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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ use ra_syntax::{
2222
use test_utils::tested_by;
2323

2424
use crate::{
25-
attr::Attrs, db::DefDatabase, path::ModPath, visibility::RawVisibility, FileAstId, HirFileId,
26-
InFile,
25+
attr::Attrs,
26+
db::DefDatabase,
27+
path::{ImportAlias, ModPath},
28+
visibility::RawVisibility,
29+
FileAstId, HirFileId, InFile,
2730
};
2831

2932
/// `RawItems` is a set of top-level items in a file (except for impls).
@@ -145,7 +148,7 @@ impl_arena_id!(Import);
145148
#[derive(Debug, Clone, PartialEq, Eq)]
146149
pub struct ImportData {
147150
pub(super) path: ModPath,
148-
pub(super) alias: Option<Name>,
151+
pub(super) alias: Option<ImportAlias>,
149152
pub(super) is_glob: bool,
150153
pub(super) is_prelude: bool,
151154
pub(super) is_extern_crate: bool,
@@ -353,7 +356,11 @@ impl RawItemsCollector {
353356
let path = ModPath::from_name_ref(&name_ref);
354357
let visibility =
355358
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());
359+
let alias = extern_crate.alias().map(|a| {
360+
a.name()
361+
.map(|it| it.as_name())
362+
.map_or(ImportAlias::Underscore, |a| ImportAlias::Alias(a))
363+
});
357364
let attrs = self.parse_attrs(&extern_crate);
358365
// FIXME: cfg_attr
359366
let is_macro_use = extern_crate.has_atom_attr("macro_use");

crates/ra_hir_def/src/path.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ pub enum PathKind {
3434
DollarCrate(CrateId),
3535
}
3636

37+
#[derive(Debug, Clone, PartialEq, Eq)]
38+
pub enum ImportAlias {
39+
/// Unnamed alias, as in `use Foo as _;`
40+
Underscore,
41+
/// Named alias
42+
Alias(Name),
43+
}
44+
3745
impl ModPath {
3846
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
3947
lower::lower_path(path, hygiene).map(|it| it.mod_path)
@@ -57,7 +65,7 @@ impl ModPath {
5765
pub(crate) fn expand_use_item(
5866
item_src: InFile<ast::UseItem>,
5967
hygiene: &Hygiene,
60-
mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<Name>),
68+
mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<ImportAlias>),
6169
) {
6270
if let Some(tree) = item_src.value.use_tree() {
6371
lower::lower_use_tree(None, tree, hygiene, &mut cb);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
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

14-
use crate::path::{ModPath, PathKind};
11+
use crate::path::{ImportAlias, ModPath, PathKind};
1512

1613
pub(crate) fn lower_use_tree(
1714
prefix: Option<ModPath>,
1815
tree: ast::UseTree,
1916
hygiene: &Hygiene,
20-
cb: &mut dyn FnMut(ModPath, &ast::UseTree, bool, Option<Name>),
17+
cb: &mut dyn FnMut(ModPath, &ast::UseTree, bool, Option<ImportAlias>),
2118
) {
2219
if let Some(use_tree_list) = tree.use_tree_list() {
2320
let prefix = match tree.path() {
@@ -34,7 +31,11 @@ pub(crate) fn lower_use_tree(
3431
lower_use_tree(prefix.clone(), child_tree, hygiene, cb);
3532
}
3633
} else {
37-
let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
34+
let alias = tree.alias().map(|a| {
35+
a.name()
36+
.map(|it| it.as_name())
37+
.map_or(ImportAlias::Underscore, |a| ImportAlias::Alias(a))
38+
});
3839
let is_glob = tree.has_star();
3940
if let Some(ast_path) = tree.path() {
4041
// Handle self in a path.

0 commit comments

Comments
 (0)