diff options
author | Florian Diebold <[email protected]> | 2019-02-10 19:44:34 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-02-13 19:10:09 +0000 |
commit | 1526eb25c98fd16a9c0d114d0ed44e8fec1cc19c (patch) | |
tree | 1acf0f556de14a2fac88929db9af96025a47f79f /crates/ra_hir/src/nameres | |
parent | 65266c644a31e6b321e5afb3c5a2ee75be76cb0c (diff) |
Import the prelude
Diffstat (limited to 'crates/ra_hir/src/nameres')
-rw-r--r-- | crates/ra_hir/src/nameres/lower.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 37 |
2 files changed, 43 insertions, 1 deletions
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; | |||
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | AstNode, SourceFile, TreeArc, AstPtr, | 4 | AstNode, SourceFile, TreeArc, AstPtr, |
5 | ast::{self, ModuleItemOwner, NameOwner}, | 5 | ast::{self, ModuleItemOwner, NameOwner, AttrsOwner}, |
6 | }; | 6 | }; |
7 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 7 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
8 | use rustc_hash::FxHashMap; | 8 | use rustc_hash::FxHashMap; |
@@ -23,6 +23,7 @@ pub(super) struct ImportData { | |||
23 | pub(super) path: Path, | 23 | pub(super) path: Path, |
24 | pub(super) alias: Option<Name>, | 24 | pub(super) alias: Option<Name>, |
25 | pub(super) is_glob: bool, | 25 | pub(super) is_glob: bool, |
26 | pub(super) is_prelude: bool, | ||
26 | pub(super) is_extern_crate: bool, | 27 | pub(super) is_extern_crate: bool, |
27 | } | 28 | } |
28 | 29 | ||
@@ -191,6 +192,7 @@ impl LoweredModule { | |||
191 | path, | 192 | path, |
192 | alias, | 193 | alias, |
193 | is_glob: false, | 194 | is_glob: false, |
195 | is_prelude: false, | ||
194 | is_extern_crate: true, | 196 | is_extern_crate: true, |
195 | }); | 197 | }); |
196 | } | 198 | } |
@@ -214,11 +216,14 @@ impl LoweredModule { | |||
214 | } | 216 | } |
215 | 217 | ||
216 | fn add_use_item(&mut self, source_map: &mut ImportSourceMap, item: &ast::UseItem) { | 218 | fn add_use_item(&mut self, source_map: &mut ImportSourceMap, item: &ast::UseItem) { |
219 | let is_prelude = | ||
220 | item.attrs().any(|attr| attr.as_atom().map(|s| s == "prelude_import").unwrap_or(false)); | ||
217 | Path::expand_use_item(item, |path, segment, alias| { | 221 | Path::expand_use_item(item, |path, segment, alias| { |
218 | let import = self.imports.alloc(ImportData { | 222 | let import = self.imports.alloc(ImportData { |
219 | path, | 223 | path, |
220 | alias, | 224 | alias, |
221 | is_glob: segment.is_none(), | 225 | is_glob: segment.is_none(), |
226 | is_prelude, | ||
222 | is_extern_crate: false, | 227 | is_extern_crate: false, |
223 | }); | 228 | }); |
224 | if let Some(segment) = segment { | 229 | 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 | |||
@@ -297,6 +297,43 @@ fn module_resolution_works_for_non_standard_filenames() { | |||
297 | } | 297 | } |
298 | 298 | ||
299 | #[test] | 299 | #[test] |
300 | fn std_prelude() { | ||
301 | covers!(std_prelude); | ||
302 | let mut db = MockDatabase::with_files( | ||
303 | " | ||
304 | //- /main.rs | ||
305 | use Foo::*; | ||
306 | |||
307 | //- /lib.rs | ||
308 | mod prelude; | ||
309 | #[prelude_import] | ||
310 | use prelude::*; | ||
311 | |||
312 | //- /prelude.rs | ||
313 | pub enum Foo { Bar, Baz }; | ||
314 | ", | ||
315 | ); | ||
316 | db.set_crate_graph_from_fixture(crate_graph! { | ||
317 | "main": ("/main.rs", ["test_crate"]), | ||
318 | "test_crate": ("/lib.rs", []), | ||
319 | }); | ||
320 | let main_id = db.file_id_of("/main.rs"); | ||
321 | |||
322 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | ||
323 | let krate = module.krate(&db).unwrap(); | ||
324 | let item_map = db.item_map(krate); | ||
325 | |||
326 | check_module_item_map( | ||
327 | &item_map, | ||
328 | module.module_id, | ||
329 | " | ||
330 | Bar: t v | ||
331 | Baz: t v | ||
332 | ", | ||
333 | ); | ||
334 | } | ||
335 | |||
336 | #[test] | ||
300 | fn name_res_works_for_broken_modules() { | 337 | fn name_res_works_for_broken_modules() { |
301 | covers!(name_res_works_for_broken_modules); | 338 | covers!(name_res_works_for_broken_modules); |
302 | let (item_map, module_id) = item_map( | 339 | let (item_map, module_id) = item_map( |