From 7d527159457420d55f1ee2f70615098a10176b91 Mon Sep 17 00:00:00 2001 From: zombiefungus Date: Thu, 30 Jan 2020 23:14:20 -0500 Subject: add new ImportAlias enum to differentiate no alias from an _ alias --- crates/ra_hir_def/src/nameres/collector.rs | 5 ++++- crates/ra_hir_def/src/nameres/raw.rs | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7499dff31..193067f73 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -438,7 +438,10 @@ where } else { match import.path.segments.last() { Some(last_segment) => { - let name = import.alias.clone().unwrap_or_else(|| last_segment.clone()); + let name = match &import.alias { + raw::ImportAlias::Alias(name) => name.clone(), + _ => last_segment.clone(), // "use as ;" and "use as _;" are treated the same way + }; log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index fac1169ef..f068b4d4e 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -145,7 +145,7 @@ impl_arena_id!(Import); #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { pub(super) path: ModPath, - pub(super) alias: Option, + pub(super) alias: ImportAlias, pub(super) is_glob: bool, pub(super) is_prelude: bool, pub(super) is_extern_crate: bool, @@ -153,6 +153,13 @@ pub struct ImportData { pub(super) visibility: RawVisibility, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ImportAlias { + NoAlias, + Unnamed, // use Foo as _; + Alias(Name), +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(super) struct Def(RawId); impl_arena_id!(Def); @@ -353,7 +360,11 @@ impl RawItemsCollector { let path = ModPath::from_name_ref(&name_ref); let visibility = RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene); - let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); + let alias = extern_crate.alias().map_or(ImportAlias::NoAlias, |a| { + a.name() + .map(|it| it.as_name()) + .map_or(ImportAlias::Unnamed, |a| ImportAlias::Alias(a)) + }); let attrs = self.parse_attrs(&extern_crate); // FIXME: cfg_attr let is_macro_use = extern_crate.has_atom_attr("macro_use"); -- cgit v1.2.3 From f4f71e361ee632a7a09b633934a9c0a11f4a9be7 Mon Sep 17 00:00:00 2001 From: zombiefungus Date: Sun, 2 Feb 2020 08:04:06 -0500 Subject: include requested changes --- crates/ra_hir_def/src/nameres/collector.rs | 7 ++++--- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++------------ 2 files changed, 12 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 193067f73..6352c71ef 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -22,7 +22,7 @@ use crate::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, }, - path::{ModPath, PathKind}, + path::{ImportAlias, ModPath, PathKind}, per_ns::PerNs, visibility::Visibility, AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, @@ -439,8 +439,9 @@ where match import.path.segments.last() { Some(last_segment) => { let name = match &import.alias { - raw::ImportAlias::Alias(name) => name.clone(), - _ => last_segment.clone(), // "use as ;" and "use as _;" are treated the same way + Some(ImportAlias::Alias(name)) => name.clone(), + Some(ImportAlias::Underscore) => last_segment.clone(), // FIXME rust-analyzer#2736 + None => last_segment.clone(), }; log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index f068b4d4e..650cf1f98 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -22,8 +22,11 @@ use ra_syntax::{ use test_utils::tested_by; use crate::{ - attr::Attrs, db::DefDatabase, path::ModPath, visibility::RawVisibility, FileAstId, HirFileId, - InFile, + attr::Attrs, + db::DefDatabase, + path::{ImportAlias, ModPath}, + visibility::RawVisibility, + FileAstId, HirFileId, InFile, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -145,7 +148,7 @@ impl_arena_id!(Import); #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { pub(super) path: ModPath, - pub(super) alias: ImportAlias, + pub(super) alias: Option, pub(super) is_glob: bool, pub(super) is_prelude: bool, pub(super) is_extern_crate: bool, @@ -153,13 +156,6 @@ pub struct ImportData { pub(super) visibility: RawVisibility, } -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum ImportAlias { - NoAlias, - Unnamed, // use Foo as _; - Alias(Name), -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(super) struct Def(RawId); impl_arena_id!(Def); @@ -360,10 +356,10 @@ impl RawItemsCollector { let path = ModPath::from_name_ref(&name_ref); let visibility = RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene); - let alias = extern_crate.alias().map_or(ImportAlias::NoAlias, |a| { + let alias = extern_crate.alias().map(|a| { a.name() .map(|it| it.as_name()) - .map_or(ImportAlias::Unnamed, |a| ImportAlias::Alias(a)) + .map_or(ImportAlias::Underscore, |a| ImportAlias::Alias(a)) }); let attrs = self.parse_attrs(&extern_crate); // FIXME: cfg_attr -- cgit v1.2.3