From 1526eb25c98fd16a9c0d114d0ed44e8fec1cc19c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Feb 2019 20:44:34 +0100 Subject: Import the prelude --- crates/ra_hir/src/marks.rs | 1 + crates/ra_hir/src/nameres.rs | 50 +++++++++++++++++++++++++++++++++++--- crates/ra_hir/src/nameres/lower.rs | 7 +++++- crates/ra_hir/src/nameres/tests.rs | 37 ++++++++++++++++++++++++++++ crates/ra_hir/src/resolve.rs | 12 ++++----- 5 files changed, 96 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 50d4e824c..16852a6a1 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -6,4 +6,5 @@ test_utils::marks!( type_var_resolves_to_int_var glob_enum glob_across_crates + std_prelude ); diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index ffb20d564..2ba6038c6 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -34,6 +34,10 @@ use crate::{ /// module, the set of visible items. #[derive(Default, Debug, PartialEq, Eq)] pub struct ItemMap { + /// The prelude module for this crate. This either comes from an import + /// marked with the `prelude_import` attribute, or (in the normal case) from + /// a dependency (`std` or `core`). + prelude: Option, pub(crate) extern_prelude: FxHashMap, per_module: ArenaMap, } @@ -211,6 +215,13 @@ where if let Some(module) = dep.krate.root_module(self.db) { self.result.extern_prelude.insert(dep.name.clone(), module.into()); } + // look for the prelude + if self.result.prelude.is_none() { + let item_map = self.db.item_map(dep.krate); + if item_map.prelude.is_some() { + self.result.prelude = item_map.prelude; + } + } } } @@ -279,7 +290,10 @@ where log::debug!("glob import: {:?}", import); match def.take_types() { Some(ModuleDef::Module(m)) => { - if m.krate != self.krate { + if import.is_prelude { + tested_by!(std_prelude); + self.result.prelude = Some(m); + } else if m.krate != self.krate { tested_by!(glob_across_crates); // glob import from other crate => we can just import everything once let item_map = self.db.item_map(m.krate); @@ -434,12 +448,40 @@ impl ItemMap { self.resolve_path_fp(db, original_module, path).0 } - pub(crate) fn resolve_name_in_module(&self, module: Module, name: &Name) -> PerNs { + fn resolve_in_prelude( + &self, + db: &impl PersistentHirDatabase, + original_module: Module, + name: &Name, + ) -> PerNs { + if let Some(prelude) = self.prelude { + let resolution = if prelude.krate == original_module.krate { + self[prelude.module_id].items.get(name).cloned() + } else { + db.item_map(prelude.krate)[prelude.module_id].items.get(name).cloned() + }; + resolution.map(|r| r.def).unwrap_or_else(PerNs::none) + } else { + PerNs::none() + } + } + + pub(crate) fn resolve_name_in_module( + &self, + db: &impl PersistentHirDatabase, + module: Module, + name: &Name, + ) -> PerNs { + // Resolve in: + // - current module / scope + // - extern prelude + // - std prelude let from_scope = self[module.module_id].items.get(name).map_or(PerNs::none(), |it| it.def); let from_extern_prelude = self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); + let from_prelude = self.resolve_in_prelude(db, module, name); - from_scope.or(from_extern_prelude) + from_scope.or(from_extern_prelude).or(from_prelude) } // Returns Yes if we are sure that additions to `ItemMap` wouldn't change @@ -459,7 +501,7 @@ impl ItemMap { Some((_, segment)) => segment, None => return (PerNs::none(), ReachedFixedPoint::Yes), }; - self.resolve_name_in_module(original_module, &segment.name) + self.resolve_name_in_module(db, original_module, &segment.name) } PathKind::Super => { if let Some(p) = original_module.parent(db) { diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 3cd496d7f..922dbe9c1 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use ra_syntax::{ AstNode, SourceFile, TreeArc, AstPtr, - ast::{self, ModuleItemOwner, NameOwner}, + ast::{self, ModuleItemOwner, NameOwner, AttrsOwner}, }; use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; use rustc_hash::FxHashMap; @@ -23,6 +23,7 @@ pub(super) struct ImportData { pub(super) path: Path, pub(super) alias: Option, pub(super) is_glob: bool, + pub(super) is_prelude: bool, pub(super) is_extern_crate: bool, } @@ -191,6 +192,7 @@ impl LoweredModule { path, alias, is_glob: false, + is_prelude: false, is_extern_crate: true, }); } @@ -214,11 +216,14 @@ impl LoweredModule { } fn add_use_item(&mut self, source_map: &mut ImportSourceMap, item: &ast::UseItem) { + let is_prelude = + item.attrs().any(|attr| attr.as_atom().map(|s| s == "prelude_import").unwrap_or(false)); Path::expand_use_item(item, |path, segment, alias| { let import = self.imports.alloc(ImportData { path, alias, is_glob: segment.is_none(), + is_prelude, is_extern_crate: false, }); if let Some(segment) = segment { diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 6dbe759d1..68ebe963a 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -296,6 +296,43 @@ fn module_resolution_works_for_non_standard_filenames() { ); } +#[test] +fn std_prelude() { + covers!(std_prelude); + let mut db = MockDatabase::with_files( + " + //- /main.rs + use Foo::*; + + //- /lib.rs + mod prelude; + #[prelude_import] + use prelude::*; + + //- /prelude.rs + pub enum Foo { Bar, Baz }; + ", + ); + db.set_crate_graph_from_fixture(crate_graph! { + "main": ("/main.rs", ["test_crate"]), + "test_crate": ("/lib.rs", []), + }); + let main_id = db.file_id_of("/main.rs"); + + let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); + let krate = module.krate(&db).unwrap(); + let item_map = db.item_map(krate); + + check_module_item_map( + &item_map, + module.module_id, + " + Bar: t v + Baz: t v + ", + ); +} + #[test] fn name_res_works_for_broken_modules() { covers!(name_res_works_for_broken_modules); diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 40c860cf4..fde4d6580 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -56,10 +56,10 @@ pub enum Resolution { } impl Resolver { - pub fn resolve_name(&self, name: &Name) -> PerNs { + pub fn resolve_name(&self, db: &impl HirDatabase, name: &Name) -> PerNs { let mut resolution = PerNs::none(); for scope in self.scopes.iter().rev() { - resolution = resolution.or(scope.resolve_name(name)); + resolution = resolution.or(scope.resolve_name(db, name)); if resolution.is_both() { return resolution; } @@ -69,9 +69,9 @@ impl Resolver { pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { if let Some(name) = path.as_ident() { - self.resolve_name(name) + self.resolve_name(db, name) } else if path.is_self() { - self.resolve_name(&Name::self_param()) + self.resolve_name(db, &Name::self_param()) } else { let (item_map, module) = match self.module() { Some(m) => m, @@ -143,13 +143,13 @@ impl Resolver { } impl Scope { - fn resolve_name(&self, name: &Name) -> PerNs { + fn resolve_name(&self, db: &impl HirDatabase, name: &Name) -> PerNs { match self { Scope::ModuleScope(m) => { if let Some(KnownName::SelfParam) = name.as_known_name() { PerNs::types(Resolution::Def(m.module.into())) } else { - m.item_map.resolve_name_in_module(m.module, name).map(Resolution::Def) + m.item_map.resolve_name_in_module(db, m.module, name).map(Resolution::Def) } } Scope::GenericParams(gp) => match gp.find_by_name(name) { -- cgit v1.2.3 From 3a9934e2c3280864877a90c5ced777bad898d73a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Feb 2019 22:34:29 +0100 Subject: Keep track of crate edition --- crates/ra_hir/src/mock.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 5ca870867..145ed39a1 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -3,6 +3,7 @@ use std::{sync::Arc, panic}; use parking_lot::Mutex; use ra_db::{ FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, + Edition, }; use relative_path::RelativePathBuf; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; @@ -60,7 +61,7 @@ impl MockDatabase { let mut crate_graph = CrateGraph::default(); for (crate_name, (crate_root, _)) in graph.0.iter() { let crate_root = self.file_id_of(&crate_root); - let crate_id = crate_graph.add_crate_root(crate_root); + let crate_id = crate_graph.add_crate_root(crate_root, Edition::Edition2018); ids.insert(crate_name, crate_id); } for (crate_name, (_, deps)) in graph.0.iter() { @@ -144,7 +145,7 @@ impl MockDatabase { if is_crate_root { let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(file_id); + crate_graph.add_crate_root(file_id, Edition::Edition2018); self.set_crate_graph(Arc::new(crate_graph)); } file_id -- cgit v1.2.3 From d5ad38cbb87103d8713855e0ec705fd957249afd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 11 Feb 2019 23:11:12 +0100 Subject: Resolve 2015 style imports --- crates/ra_hir/src/code_model_api.rs | 9 ++++- crates/ra_hir/src/nameres.rs | 68 +++++++++++++++++++++++++++++++++---- crates/ra_hir/src/nameres/tests.rs | 45 ++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 2ac05c836..fb7ad0867 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use relative_path::RelativePathBuf; -use ra_db::{CrateId, FileId, SourceRootId}; +use ra_db::{CrateId, FileId, SourceRootId, Edition}; use ra_syntax::{ast::self, TreeArc, SyntaxNode}; use crate::{ @@ -38,13 +38,20 @@ impl Crate { pub fn crate_id(&self) -> CrateId { self.crate_id } + pub fn dependencies(&self, db: &impl PersistentHirDatabase) -> Vec { self.dependencies_impl(db) } + pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option { self.root_module_impl(db) } + pub fn edition(&self, db: &impl PersistentHirDatabase) -> Edition { + let crate_graph = db.crate_graph(); + crate_graph.edition(self.crate_id) + } + // TODO: should this be in source_binder? pub fn source_root_crates( db: &impl PersistentHirDatabase, diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 2ba6038c6..028e2bee3 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -18,9 +18,11 @@ pub(crate) mod lower; use std::{time, sync::Arc}; +use rustc_hash::{FxHashMap, FxHashSet}; + use ra_arena::map::ArenaMap; +use ra_db::Edition; use test_utils::tested_by; -use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ Module, ModuleDef, @@ -32,8 +34,9 @@ use crate::{ /// `ItemMap` is the result of module name resolution. It contains, for each /// module, the set of visible items. -#[derive(Default, Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub struct ItemMap { + edition: Edition, /// The prelude module for this crate. This either comes from an import /// marked with the `prelude_import` attribute, or (in the normal case) from /// a dependency (`std` or `core`). @@ -180,7 +183,12 @@ where module_tree, processed_imports: FxHashSet::default(), glob_imports: FxHashMap::default(), - result: ItemMap::default(), + result: ItemMap { + edition: krate.edition(db), + prelude: None, + extern_prelude: FxHashMap::default(), + per_module: ArenaMap::default(), + }, } } @@ -277,10 +285,14 @@ where import_id: ImportId, import: &ImportData, ) -> ReachedFixedPoint { - log::debug!("resolving import: {:?}", import); + log::debug!("resolving import: {:?} ({:?})", import, self.result.edition); let original_module = Module { krate: self.krate, module_id }; - let (def, reached_fixedpoint) = - self.result.resolve_path_fp(self.db, original_module, &import.path); + let (def, reached_fixedpoint) = self.result.resolve_path_fp( + self.db, + ResolveMode::Import, + original_module, + &import.path, + ); if reached_fixedpoint != ReachedFixedPoint::Yes { return reached_fixedpoint; @@ -417,6 +429,12 @@ where } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ResolveMode { + Import, + Other, +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum ReachedFixedPoint { Yes, @@ -445,7 +463,7 @@ impl ItemMap { original_module: Module, path: &Path, ) -> PerNs { - self.resolve_path_fp(db, original_module, path).0 + self.resolve_path_fp(db, ResolveMode::Other, original_module, path).0 } fn resolve_in_prelude( @@ -484,11 +502,27 @@ impl ItemMap { from_scope.or(from_extern_prelude).or(from_prelude) } + fn resolve_name_in_crate_root_or_extern_prelude( + &self, + db: &impl PersistentHirDatabase, + module: Module, + name: &Name, + ) -> PerNs { + let crate_root = module.crate_root(db); + let from_crate_root = + self[crate_root.module_id].items.get(name).map_or(PerNs::none(), |it| it.def); + let from_extern_prelude = + self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); + + from_crate_root.or(from_extern_prelude) + } + // Returns Yes if we are sure that additions to `ItemMap` wouldn't change // the result. fn resolve_path_fp( &self, db: &impl PersistentHirDatabase, + mode: ResolveMode, original_module: Module, path: &Path, ) -> (PerNs, ReachedFixedPoint) { @@ -496,11 +530,31 @@ impl ItemMap { let mut curr_per_ns: PerNs = match path.kind { PathKind::Crate => PerNs::types(original_module.crate_root(db).into()), PathKind::Self_ => PerNs::types(original_module.into()), + // plain import or absolute path in 2015: crate-relative with + // fallback to extern prelude (with the simplification in + // rust-lang/rust#57745) + // TODO there must be a nicer way to write this condition + PathKind::Plain | PathKind::Abs + if self.edition == Edition::Edition2015 + && (path.kind == PathKind::Abs || mode == ResolveMode::Import) => + { + let segment = match segments.next() { + Some((_, segment)) => segment, + None => return (PerNs::none(), ReachedFixedPoint::Yes), + }; + log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); + self.resolve_name_in_crate_root_or_extern_prelude( + db, + original_module, + &segment.name, + ) + } PathKind::Plain => { let segment = match segments.next() { Some((_, segment)) => segment, None => return (PerNs::none(), ReachedFixedPoint::Yes), }; + log::debug!("resolving {:?} in module", segment); self.resolve_name_in_module(db, original_module, &segment.name) } PathKind::Super => { diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 68ebe963a..bee475c3a 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -265,6 +265,51 @@ fn glob_across_crates() { ); } +#[test] +fn edition_2015_imports() { + use ra_db::{CrateGraph, Edition}; + let mut db = MockDatabase::with_files( + " + //- /main.rs + mod foo; + mod bar; + + //- /bar.rs + struct Bar; + + //- /foo.rs + use bar::Bar; + use other_crate::FromLib; + + //- /lib.rs + struct FromLib; + ", + ); + let main_id = db.file_id_of("/main.rs"); + let lib_id = db.file_id_of("/lib.rs"); + let foo_id = db.file_id_of("/foo.rs"); + + let mut crate_graph = CrateGraph::default(); + let main_crate = crate_graph.add_crate_root(main_id, Edition::Edition2015); + let lib_crate = crate_graph.add_crate_root(lib_id, Edition::Edition2018); + crate_graph.add_dep(main_crate, "other_crate".into(), lib_crate).unwrap(); + + db.set_crate_graph(Arc::new(crate_graph)); + + let module = crate::source_binder::module_from_file_id(&db, foo_id).unwrap(); + let krate = module.krate(&db).unwrap(); + let item_map = db.item_map(krate); + + check_module_item_map( + &item_map, + module.module_id, + " + Bar: t v + FromLib: t v + ", + ); +} + #[test] fn module_resolution_works_for_non_standard_filenames() { let mut db = MockDatabase::with_files( -- cgit v1.2.3 From 70839b7ef8217fd019ce5bd3a643a8a16f5fa829 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 13 Feb 2019 20:31:27 +0100 Subject: Make edition handling a bit nicer and allow specifying edition in crate_graph macro --- crates/ra_hir/src/mock.rs | 15 +++++++++------ crates/ra_hir/src/nameres/tests.rs | 14 ++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 145ed39a1..f1cad77c5 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -59,12 +59,12 @@ impl MockDatabase { pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) { let mut ids = FxHashMap::default(); let mut crate_graph = CrateGraph::default(); - for (crate_name, (crate_root, _)) in graph.0.iter() { + for (crate_name, (crate_root, edition, _)) in graph.0.iter() { let crate_root = self.file_id_of(&crate_root); - let crate_id = crate_graph.add_crate_root(crate_root, Edition::Edition2018); + let crate_id = crate_graph.add_crate_root(crate_root, *edition); ids.insert(crate_name, crate_id); } - for (crate_name, (_, deps)) in graph.0.iter() { + for (crate_name, (_, _, deps)) in graph.0.iter() { let from = ids[crate_name]; for dep in deps { let to = ids[dep]; @@ -233,16 +233,19 @@ impl MockDatabase { } #[derive(Default)] -pub struct CrateGraphFixture(pub FxHashMap)>); +pub struct CrateGraphFixture(pub FxHashMap)>); #[macro_export] macro_rules! crate_graph { - ($($crate_name:literal: ($crate_path:literal, [$($dep:literal),*]),)*) => {{ + ($($crate_name:literal: ($crate_path:literal, $($edition:literal,)? [$($dep:literal),*]),)*) => {{ let mut res = $crate::mock::CrateGraphFixture::default(); $( + #[allow(unused_mut, unused_assignments)] + let mut edition = ra_db::Edition::Edition2018; + $(edition = ra_db::Edition::from_string($edition);)? res.0.insert( $crate_name.to_string(), - ($crate_path.to_string(), vec![$($dep.to_string()),*]) + ($crate_path.to_string(), edition, vec![$($dep.to_string()),*]) ); )* res diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index bee475c3a..e764e0855 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -267,7 +267,6 @@ fn glob_across_crates() { #[test] fn edition_2015_imports() { - use ra_db::{CrateGraph, Edition}; let mut db = MockDatabase::with_files( " //- /main.rs @@ -285,17 +284,12 @@ fn edition_2015_imports() { struct FromLib; ", ); - let main_id = db.file_id_of("/main.rs"); - let lib_id = db.file_id_of("/lib.rs"); + db.set_crate_graph_from_fixture(crate_graph! { + "main": ("/main.rs", "2015", ["other_crate"]), + "other_crate": ("/lib.rs", "2018", []), + }); let foo_id = db.file_id_of("/foo.rs"); - let mut crate_graph = CrateGraph::default(); - let main_crate = crate_graph.add_crate_root(main_id, Edition::Edition2015); - let lib_crate = crate_graph.add_crate_root(lib_id, Edition::Edition2018); - crate_graph.add_dep(main_crate, "other_crate".into(), lib_crate).unwrap(); - - db.set_crate_graph(Arc::new(crate_graph)); - let module = crate::source_binder::module_from_file_id(&db, foo_id).unwrap(); let krate = module.krate(&db).unwrap(); let item_map = db.item_map(krate); -- cgit v1.2.3 From 92c595a6a6f7624092432d28ffd7e0ffd189cbda Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 13 Feb 2019 20:42:43 +0100 Subject: Handle extern crates better, so they work correctly in 2015 edition (see the removed comment.) --- crates/ra_hir/src/nameres.rs | 25 +++++++++++++++++-------- crates/ra_hir/src/nameres/lower.rs | 9 ++------- crates/ra_hir/src/nameres/tests.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 028e2bee3..cd7b41cff 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -287,12 +287,18 @@ where ) -> ReachedFixedPoint { log::debug!("resolving import: {:?} ({:?})", import, self.result.edition); let original_module = Module { krate: self.krate, module_id }; - let (def, reached_fixedpoint) = self.result.resolve_path_fp( - self.db, - ResolveMode::Import, - original_module, - &import.path, - ); + + let (def, reached_fixedpoint) = if import.is_extern_crate { + let res = self.result.resolve_name_in_extern_prelude( + &import + .path + .as_ident() + .expect("extern crate should have been desugared to one-element path"), + ); + (res, if res.is_none() { ReachedFixedPoint::No } else { ReachedFixedPoint::Yes }) + } else { + self.result.resolve_path_fp(self.db, ResolveMode::Import, original_module, &import.path) + }; if reached_fixedpoint != ReachedFixedPoint::Yes { return reached_fixedpoint; @@ -502,6 +508,10 @@ impl ItemMap { from_scope.or(from_extern_prelude).or(from_prelude) } + fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { + self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) + } + fn resolve_name_in_crate_root_or_extern_prelude( &self, db: &impl PersistentHirDatabase, @@ -511,8 +521,7 @@ impl ItemMap { let crate_root = module.crate_root(db); let from_crate_root = self[crate_root.module_id].items.get(name).map_or(PerNs::none(), |it| it.def); - let from_extern_prelude = - self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); + let from_extern_prelude = self.resolve_name_in_extern_prelude(name); from_crate_root.or(from_extern_prelude) } diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 922dbe9c1..81d80654c 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -8,7 +8,7 @@ use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; use rustc_hash::FxHashMap; use crate::{ - SourceItemId, Path, PathKind, ModuleSource, Name, + SourceItemId, Path, ModuleSource, Name, HirFileId, MacroCallLoc, AsName, PerNs, Function, ModuleDef, Module, Struct, Enum, Const, Static, Trait, Type, ids::LocationCtx, PersistentHirDatabase, @@ -180,13 +180,8 @@ impl LoweredModule { self.add_use_item(source_map, it); } ast::ModuleItemKind::ExternCrateItem(it) => { - // Lower `extern crate x` to `use ::x`. This is kind of cheating - // and only works if we always interpret absolute paths in the - // 2018 style; otherwise `::x` could also refer to a module in - // the crate root. if let Some(name_ref) = it.name_ref() { - let mut path = Path::from_name_ref(name_ref); - path.kind = PathKind::Abs; + let path = Path::from_name_ref(name_ref); let alias = it.alias().and_then(|a| a.name()).map(AsName::as_name); self.imports.alloc(ImportData { path, diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index e764e0855..6402c89c0 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -542,6 +542,42 @@ fn extern_crate_rename() { ); } +#[test] +fn extern_crate_rename_2015_edition() { + let mut db = MockDatabase::with_files( + " + //- /main.rs + extern crate alloc as alloc_crate; + + mod alloc; + mod sync; + + //- /sync.rs + use alloc_crate::Arc; + + //- /lib.rs + struct Arc; + ", + ); + db.set_crate_graph_from_fixture(crate_graph! { + "main": ("/main.rs", "2015", ["alloc"]), + "alloc": ("/lib.rs", []), + }); + let sync_id = db.file_id_of("/sync.rs"); + + let module = crate::source_binder::module_from_file_id(&db, sync_id).unwrap(); + let krate = module.krate(&db).unwrap(); + let item_map = db.item_map(krate); + + check_module_item_map( + &item_map, + module.module_id, + " + Arc: t v + ", + ); +} + #[test] fn import_across_source_roots() { let mut db = MockDatabase::with_files( -- cgit v1.2.3 From 911e32bca9b73e66eceb6bbee3768c82e94597d5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 13 Feb 2019 20:53:42 +0100 Subject: Complete names from prelude --- crates/ra_hir/src/nameres.rs | 2 +- crates/ra_hir/src/resolve.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index cd7b41cff..e35b4b129 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -40,7 +40,7 @@ pub struct ItemMap { /// The prelude module for this crate. This either comes from an import /// marked with the `prelude_import` attribute, or (in the normal case) from /// a dependency (`std` or `core`). - prelude: Option, + pub(crate) prelude: Option, pub(crate) extern_prelude: FxHashMap, per_module: ArenaMap, } diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index fde4d6580..91a531801 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -82,10 +82,10 @@ impl Resolver { } } - pub fn all_names(&self) -> FxHashMap> { + pub fn all_names(&self, db: &impl HirDatabase) -> FxHashMap> { let mut names = FxHashMap::default(); for scope in self.scopes.iter().rev() { - scope.collect_names(&mut |name, res| { + scope.collect_names(db, &mut |name, res| { let current: &mut PerNs = names.entry(name).or_default(); if current.types.is_none() { current.types = res.types; @@ -174,7 +174,7 @@ impl Scope { } } - fn collect_names(&self, f: &mut dyn FnMut(Name, PerNs)) { + fn collect_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, PerNs)) { match self { Scope::ModuleScope(m) => { // TODO: should we provide `self` here? @@ -190,6 +190,12 @@ impl Scope { m.item_map.extern_prelude.iter().for_each(|(name, def)| { f(name.clone(), PerNs::types(Resolution::Def(*def))); }); + if let Some(prelude) = m.item_map.prelude { + let prelude_item_map = db.item_map(prelude.krate); + prelude_item_map[prelude.module_id].entries().for_each(|(name, res)| { + f(name.clone(), res.def.map(Resolution::Def)); + }); + } } Scope::GenericParams(gp) => { for param in &gp.params { -- cgit v1.2.3