From 397d84ee292295ce1d19033e586467ef9148e5bc Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 2 Feb 2019 00:36:48 +0100 Subject: Add test for extern crate renames --- crates/ra_hir/src/nameres/tests.rs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'crates/ra_hir/src/nameres') diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 81c8a4f12..42c59f76f 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -334,6 +334,48 @@ fn item_map_across_crates() { ); } +#[test] +fn extern_crate_rename() { + let (mut db, sr) = MockDatabase::with_files( + " + //- /main.rs + extern crate alloc as alloc_crate; + + mod alloc; + + use alloc_crate::Arc; + + //- /lib.rs + struct Arc; + ", + ); + let main_id = sr.files[RelativePath::new("/main.rs")]; + let lib_id = sr.files[RelativePath::new("/lib.rs")]; + + let mut crate_graph = CrateGraph::default(); + let main_crate = crate_graph.add_crate_root(main_id); + let lib_crate = crate_graph.add_crate_root(lib_id); + crate_graph + .add_dep(main_crate, "alloc".into(), lib_crate) + .unwrap(); + + db.set_crate_graph(Arc::new(crate_graph)); + + 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, + " + Arc: t v + alloc: t + alloc_crate: t + ", + ); +} + #[test] fn import_across_source_roots() { let (mut db, sr) = MockDatabase::with_files( -- cgit v1.2.3 From d69023fc72b26e64ebf1f96fc322a2f7377a5f4d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 2 Feb 2019 00:49:20 +0100 Subject: Lower extern crates to imports This is probably not completely correct, but it kind of works. --- crates/ra_hir/src/nameres/lower.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/nameres') diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index df87f520f..db898a782 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, ModuleSource, Name, + SourceItemId, Path, PathKind, ModuleSource, Name, HirFileId, MacroCallLoc, AsName, PerNs, Function, ModuleDef, Module, Struct, Enum, Const, Static, Trait, Type, ids::LocationCtx, PersistentHirDatabase, @@ -186,8 +186,21 @@ impl LoweredModule { ast::ModuleItemKind::UseItem(it) => { self.add_use_item(source_map, it); } - ast::ModuleItemKind::ExternCrateItem(_) => { - // TODO + 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 alias = it.alias().and_then(|a| a.name()).map(AsName::as_name); + self.imports.alloc(ImportData { + path, + alias, + is_glob: false, + }); + } } ast::ModuleItemKind::ConstDef(it) => { if let Some(name) = it.name() { -- cgit v1.2.3 From 1056b480d6235ee72849a416b84e13180f84307c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 3 Feb 2019 23:23:22 +0100 Subject: Make extern crates in the root module add to the extern prelude To accomplish this, separate the extern prelude from the per-module item maps. --- crates/ra_hir/src/nameres/lower.rs | 3 +++ crates/ra_hir/src/nameres/tests.rs | 12 ++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src/nameres') diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index db898a782..7e6e48ae0 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -23,6 +23,7 @@ pub(super) struct ImportData { pub(super) path: Path, pub(super) alias: Option, pub(super) is_glob: bool, + pub(super) is_extern_crate: bool, } /// A set of items and imports declared inside a module, without relation to @@ -199,6 +200,7 @@ impl LoweredModule { path, alias, is_glob: false, + is_extern_crate: true, }); } } @@ -228,6 +230,7 @@ impl LoweredModule { path, alias, is_glob: segment.is_none(), + is_extern_crate: false, }); if let Some(segment) = segment { source_map.insert(import, segment) diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 42c59f76f..0654dbaa1 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -329,7 +329,6 @@ fn item_map_across_crates() { module.module_id, " Baz: t v - test_crate: t ", ); } @@ -342,7 +341,9 @@ fn extern_crate_rename() { extern crate alloc as alloc_crate; mod alloc; + mod sync; + //- /sync.rs use alloc_crate::Arc; //- /lib.rs @@ -350,6 +351,7 @@ fn extern_crate_rename() { ", ); let main_id = sr.files[RelativePath::new("/main.rs")]; + let sync_id = sr.files[RelativePath::new("/sync.rs")]; let lib_id = sr.files[RelativePath::new("/lib.rs")]; let mut crate_graph = CrateGraph::default(); @@ -361,7 +363,7 @@ fn extern_crate_rename() { db.set_crate_graph(Arc::new(crate_graph)); - let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); + 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); @@ -370,8 +372,6 @@ fn extern_crate_rename() { module.module_id, " Arc: t v - alloc: t - alloc_crate: t ", ); } @@ -403,8 +403,6 @@ fn import_across_source_roots() { let main_id = sr2.files[RelativePath::new("/main.rs")]; - eprintln!("lib = {:?}, main = {:?}", lib_id, main_id); - let mut crate_graph = CrateGraph::default(); let main_crate = crate_graph.add_crate_root(main_id); let lib_crate = crate_graph.add_crate_root(lib_id); @@ -423,7 +421,6 @@ fn import_across_source_roots() { module.module_id, " C: t v - test_crate: t ", ); } @@ -465,7 +462,6 @@ fn reexport_across_crates() { module.module_id, " Baz: t v - test_crate: t ", ); } -- cgit v1.2.3