diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 36 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests.rs | 31 |
3 files changed, 66 insertions, 8 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7e6083961..37d0f3093 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_expand::{ | 3 | use hir_expand::{ |
4 | builtin_macro::find_builtin_macro, | ||
4 | name::{self, AsName, Name}, | 5 | name::{self, AsName, Name}, |
5 | HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, | 6 | HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind, |
6 | }; | 7 | }; |
7 | use ra_cfg::CfgOptions; | 8 | use ra_cfg::CfgOptions; |
8 | use ra_db::{CrateId, FileId}; | 9 | use ra_db::{CrateId, FileId}; |
@@ -36,11 +37,12 @@ pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> | |||
36 | ); | 37 | ); |
37 | 38 | ||
38 | // look for the prelude | 39 | // look for the prelude |
39 | if def_map.prelude.is_none() { | 40 | // If the dependency defines a prelude, we overwrite an already defined |
40 | let map = db.crate_def_map(dep.crate_id); | 41 | // prelude. This is necessary to import the "std" prelude if a crate |
41 | if map.prelude.is_some() { | 42 | // depends on both "core" and "std". |
42 | def_map.prelude = map.prelude; | 43 | let dep_def_map = db.crate_def_map(dep.crate_id); |
43 | } | 44 | if dep_def_map.prelude.is_some() { |
45 | def_map.prelude = dep_def_map.prelude; | ||
44 | } | 46 | } |
45 | } | 47 | } |
46 | 48 | ||
@@ -691,10 +693,30 @@ where | |||
691 | fn collect_macro(&mut self, mac: &raw::MacroData) { | 693 | fn collect_macro(&mut self, mac: &raw::MacroData) { |
692 | let ast_id = AstId::new(self.file_id, mac.ast_id); | 694 | let ast_id = AstId::new(self.file_id, mac.ast_id); |
693 | 695 | ||
696 | // Case 0: builtin macros | ||
697 | if mac.builtin { | ||
698 | if let Some(name) = &mac.name { | ||
699 | let krate = self.def_collector.def_map.krate; | ||
700 | if let Some(macro_id) = find_builtin_macro(name, krate, ast_id) { | ||
701 | self.def_collector.define_macro( | ||
702 | self.module_id, | ||
703 | name.clone(), | ||
704 | macro_id, | ||
705 | mac.export, | ||
706 | ); | ||
707 | return; | ||
708 | } | ||
709 | } | ||
710 | } | ||
711 | |||
694 | // Case 1: macro rules, define a macro in crate-global mutable scope | 712 | // Case 1: macro rules, define a macro in crate-global mutable scope |
695 | if is_macro_rules(&mac.path) { | 713 | if is_macro_rules(&mac.path) { |
696 | if let Some(name) = &mac.name { | 714 | if let Some(name) = &mac.name { |
697 | let macro_id = MacroDefId { ast_id, krate: self.def_collector.def_map.krate }; | 715 | let macro_id = MacroDefId { |
716 | ast_id, | ||
717 | krate: self.def_collector.def_map.krate, | ||
718 | kind: MacroDefKind::Declarative, | ||
719 | }; | ||
698 | self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); | 720 | self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); |
699 | } | 721 | } |
700 | return; | 722 | return; |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 369376f30..f52002bc0 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -200,6 +200,7 @@ pub(super) struct MacroData { | |||
200 | pub(super) path: Path, | 200 | pub(super) path: Path, |
201 | pub(super) name: Option<Name>, | 201 | pub(super) name: Option<Name>, |
202 | pub(super) export: bool, | 202 | pub(super) export: bool, |
203 | pub(super) builtin: bool, | ||
203 | } | 204 | } |
204 | 205 | ||
205 | struct RawItemsCollector { | 206 | struct RawItemsCollector { |
@@ -367,7 +368,11 @@ impl RawItemsCollector { | |||
367 | // FIXME: cfg_attr | 368 | // FIXME: cfg_attr |
368 | let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export"); | 369 | let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export"); |
369 | 370 | ||
370 | let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); | 371 | // FIXME: cfg_attr |
372 | let builtin = | ||
373 | m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro"); | ||
374 | |||
375 | let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin }); | ||
371 | self.push_item(current_module, attrs, RawItemKind::Macro(m)); | 376 | self.push_item(current_module, attrs, RawItemKind::Macro(m)); |
372 | } | 377 | } |
373 | 378 | ||
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 52bd0aa91..256f7d4be 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -464,6 +464,37 @@ fn values_dont_shadow_extern_crates() { | |||
464 | } | 464 | } |
465 | 465 | ||
466 | #[test] | 466 | #[test] |
467 | fn std_prelude_takes_precedence_above_core_prelude() { | ||
468 | let map = def_map( | ||
469 | r#" | ||
470 | //- /main.rs crate:main deps:core,std | ||
471 | use {Foo, Bar}; | ||
472 | |||
473 | //- /std.rs crate:std deps:core | ||
474 | #[prelude_import] | ||
475 | pub use self::prelude::*; | ||
476 | mod prelude { | ||
477 | pub struct Foo; | ||
478 | pub use core::prelude::Bar; | ||
479 | } | ||
480 | |||
481 | //- /core.rs crate:core | ||
482 | #[prelude_import] | ||
483 | pub use self::prelude::*; | ||
484 | mod prelude { | ||
485 | pub struct Bar; | ||
486 | } | ||
487 | "#, | ||
488 | ); | ||
489 | |||
490 | assert_snapshot!(map, @r###" | ||
491 | ⋮crate | ||
492 | ⋮Bar: t v | ||
493 | ⋮Foo: t v | ||
494 | "###); | ||
495 | } | ||
496 | |||
497 | #[test] | ||
467 | fn cfg_not_test() { | 498 | fn cfg_not_test() { |
468 | let map = def_map( | 499 | let map = def_map( |
469 | r#" | 500 | r#" |