diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 398 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/mod_resolution.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 482 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests.rs | 25 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/globs.rs | 47 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/incremental.rs | 38 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | 28 |
7 files changed, 334 insertions, 691 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 77baa4c69..2ced4f66b 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -4,6 +4,7 @@ | |||
4 | //! resolves imports and expands macros. | 4 | //! resolves imports and expands macros. |
5 | 5 | ||
6 | use hir_expand::{ | 6 | use hir_expand::{ |
7 | ast_id_map::FileAstId, | ||
7 | builtin_derive::find_builtin_derive, | 8 | builtin_derive::find_builtin_derive, |
8 | builtin_macro::find_builtin_macro, | 9 | builtin_macro::find_builtin_macro, |
9 | name::{name, AsName, Name}, | 10 | name::{name, AsName, Name}, |
@@ -19,13 +20,16 @@ use test_utils::mark; | |||
19 | use crate::{ | 20 | use crate::{ |
20 | attr::Attrs, | 21 | attr::Attrs, |
21 | db::DefDatabase, | 22 | db::DefDatabase, |
23 | item_tree::{ | ||
24 | self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind, | ||
25 | }, | ||
22 | nameres::{ | 26 | nameres::{ |
23 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, | 27 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, |
24 | raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, | 28 | BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, |
25 | }, | 29 | }, |
26 | path::{ImportAlias, ModPath, PathKind}, | 30 | path::{ImportAlias, ModPath, PathKind}, |
27 | per_ns::PerNs, | 31 | per_ns::PerNs, |
28 | visibility::Visibility, | 32 | visibility::{RawVisibility, Visibility}, |
29 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, | 33 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, |
30 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, | 34 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, |
31 | TraitLoc, TypeAliasLoc, UnionLoc, | 35 | TraitLoc, TypeAliasLoc, UnionLoc, |
@@ -36,8 +40,8 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr | |||
36 | 40 | ||
37 | // populate external prelude | 41 | // populate external prelude |
38 | for dep in &crate_graph[def_map.krate].dependencies { | 42 | for dep in &crate_graph[def_map.krate].dependencies { |
39 | let dep_def_map = db.crate_def_map(dep.crate_id); | ||
40 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); | 43 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); |
44 | let dep_def_map = db.crate_def_map(dep.crate_id); | ||
41 | def_map.extern_prelude.insert( | 45 | def_map.extern_prelude.insert( |
42 | dep.as_name(), | 46 | dep.as_name(), |
43 | ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(), | 47 | ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(), |
@@ -102,10 +106,50 @@ impl PartialResolvedImport { | |||
102 | } | 106 | } |
103 | 107 | ||
104 | #[derive(Clone, Debug, Eq, PartialEq)] | 108 | #[derive(Clone, Debug, Eq, PartialEq)] |
109 | struct Import { | ||
110 | pub path: ModPath, | ||
111 | pub alias: Option<ImportAlias>, | ||
112 | pub visibility: RawVisibility, | ||
113 | pub is_glob: bool, | ||
114 | pub is_prelude: bool, | ||
115 | pub is_extern_crate: bool, | ||
116 | pub is_macro_use: bool, | ||
117 | } | ||
118 | |||
119 | impl Import { | ||
120 | fn from_use(tree: &ItemTree, id: FileItemTreeId<item_tree::Import>) -> Self { | ||
121 | let it = &tree[id]; | ||
122 | let visibility = &tree[it.visibility]; | ||
123 | Self { | ||
124 | path: it.path.clone(), | ||
125 | alias: it.alias.clone(), | ||
126 | visibility: visibility.clone(), | ||
127 | is_glob: it.is_glob, | ||
128 | is_prelude: it.is_prelude, | ||
129 | is_extern_crate: false, | ||
130 | is_macro_use: false, | ||
131 | } | ||
132 | } | ||
133 | |||
134 | fn from_extern_crate(tree: &ItemTree, id: FileItemTreeId<item_tree::ExternCrate>) -> Self { | ||
135 | let it = &tree[id]; | ||
136 | let visibility = &tree[it.visibility]; | ||
137 | Self { | ||
138 | path: it.path.clone(), | ||
139 | alias: it.alias.clone(), | ||
140 | visibility: visibility.clone(), | ||
141 | is_glob: false, | ||
142 | is_prelude: false, | ||
143 | is_extern_crate: true, | ||
144 | is_macro_use: it.is_macro_use, | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
105 | struct ImportDirective { | 150 | struct ImportDirective { |
106 | module_id: LocalModuleId, | 151 | module_id: LocalModuleId, |
107 | import_id: raw::Import, | 152 | import: Import, |
108 | import: raw::ImportData, | ||
109 | status: PartialResolvedImport, | 153 | status: PartialResolvedImport, |
110 | } | 154 | } |
111 | 155 | ||
@@ -123,6 +167,13 @@ struct DeriveDirective { | |||
123 | ast_id: AstIdWithPath<ast::ModuleItem>, | 167 | ast_id: AstIdWithPath<ast::ModuleItem>, |
124 | } | 168 | } |
125 | 169 | ||
170 | struct DefData<'a> { | ||
171 | id: ModuleDefId, | ||
172 | name: &'a Name, | ||
173 | visibility: &'a RawVisibility, | ||
174 | has_constructor: bool, | ||
175 | } | ||
176 | |||
126 | /// Walks the tree of module recursively | 177 | /// Walks the tree of module recursively |
127 | struct DefCollector<'a> { | 178 | struct DefCollector<'a> { |
128 | db: &'a dyn DefDatabase, | 179 | db: &'a dyn DefDatabase, |
@@ -140,7 +191,7 @@ struct DefCollector<'a> { | |||
140 | impl DefCollector<'_> { | 191 | impl DefCollector<'_> { |
141 | fn collect(&mut self) { | 192 | fn collect(&mut self) { |
142 | let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id; | 193 | let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id; |
143 | let raw_items = self.db.raw_items(file_id.into()); | 194 | let item_tree = self.db.item_tree(file_id.into()); |
144 | let module_id = self.def_map.root; | 195 | let module_id = self.def_map.root; |
145 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; | 196 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; |
146 | ModCollector { | 197 | ModCollector { |
@@ -148,10 +199,10 @@ impl DefCollector<'_> { | |||
148 | macro_depth: 0, | 199 | macro_depth: 0, |
149 | module_id, | 200 | module_id, |
150 | file_id: file_id.into(), | 201 | file_id: file_id.into(), |
151 | raw_items: &raw_items, | 202 | item_tree: &item_tree, |
152 | mod_dir: ModDir::root(), | 203 | mod_dir: ModDir::root(), |
153 | } | 204 | } |
154 | .collect(raw_items.items()); | 205 | .collect(item_tree.top_level_items()); |
155 | 206 | ||
156 | // main name resolution fixed-point loop. | 207 | // main name resolution fixed-point loop. |
157 | let mut i = 0; | 208 | let mut i = 0; |
@@ -286,7 +337,7 @@ impl DefCollector<'_> { | |||
286 | fn import_macros_from_extern_crate( | 337 | fn import_macros_from_extern_crate( |
287 | &mut self, | 338 | &mut self, |
288 | current_module_id: LocalModuleId, | 339 | current_module_id: LocalModuleId, |
289 | import: &raw::ImportData, | 340 | import: &item_tree::ExternCrate, |
290 | ) { | 341 | ) { |
291 | log::debug!( | 342 | log::debug!( |
292 | "importing macros from extern crate: {:?} ({:?})", | 343 | "importing macros from extern crate: {:?} ({:?})", |
@@ -352,11 +403,7 @@ impl DefCollector<'_> { | |||
352 | } | 403 | } |
353 | } | 404 | } |
354 | 405 | ||
355 | fn resolve_import( | 406 | fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialResolvedImport { |
356 | &self, | ||
357 | module_id: LocalModuleId, | ||
358 | import: &raw::ImportData, | ||
359 | ) -> PartialResolvedImport { | ||
360 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); | 407 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); |
361 | if import.is_extern_crate { | 408 | if import.is_extern_crate { |
362 | let res = self.def_map.resolve_name_in_extern_prelude( | 409 | let res = self.def_map.resolve_name_in_extern_prelude( |
@@ -649,17 +696,17 @@ impl DefCollector<'_> { | |||
649 | depth: usize, | 696 | depth: usize, |
650 | ) { | 697 | ) { |
651 | let file_id: HirFileId = macro_call_id.as_file(); | 698 | let file_id: HirFileId = macro_call_id.as_file(); |
652 | let raw_items = self.db.raw_items(file_id); | 699 | let item_tree = self.db.item_tree(file_id); |
653 | let mod_dir = self.mod_dirs[&module_id].clone(); | 700 | let mod_dir = self.mod_dirs[&module_id].clone(); |
654 | ModCollector { | 701 | ModCollector { |
655 | def_collector: &mut *self, | 702 | def_collector: &mut *self, |
656 | macro_depth: depth, | 703 | macro_depth: depth, |
657 | file_id, | 704 | file_id, |
658 | module_id, | 705 | module_id, |
659 | raw_items: &raw_items, | 706 | item_tree: &item_tree, |
660 | mod_dir, | 707 | mod_dir, |
661 | } | 708 | } |
662 | .collect(raw_items.items()); | 709 | .collect(item_tree.top_level_items()); |
663 | } | 710 | } |
664 | 711 | ||
665 | fn finish(self) -> CrateDefMap { | 712 | fn finish(self) -> CrateDefMap { |
@@ -673,12 +720,12 @@ struct ModCollector<'a, 'b> { | |||
673 | macro_depth: usize, | 720 | macro_depth: usize, |
674 | module_id: LocalModuleId, | 721 | module_id: LocalModuleId, |
675 | file_id: HirFileId, | 722 | file_id: HirFileId, |
676 | raw_items: &'a raw::RawItems, | 723 | item_tree: &'a ItemTree, |
677 | mod_dir: ModDir, | 724 | mod_dir: ModDir, |
678 | } | 725 | } |
679 | 726 | ||
680 | impl ModCollector<'_, '_> { | 727 | impl ModCollector<'_, '_> { |
681 | fn collect(&mut self, items: &[raw::RawItem]) { | 728 | fn collect(&mut self, items: &[ModItem]) { |
682 | // Note: don't assert that inserted value is fresh: it's simply not true | 729 | // Note: don't assert that inserted value is fresh: it's simply not true |
683 | // for macros. | 730 | // for macros. |
684 | self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone()); | 731 | self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone()); |
@@ -695,64 +742,204 @@ impl ModCollector<'_, '_> { | |||
695 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 742 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
696 | // any other items. | 743 | // any other items. |
697 | for item in items { | 744 | for item in items { |
698 | if self.is_cfg_enabled(&item.attrs) { | 745 | if self.is_cfg_enabled(self.item_tree.attrs((*item).into())) { |
699 | if let raw::RawItemKind::Import(import_id) = item.kind { | 746 | if let ModItem::ExternCrate(id) = item { |
700 | let import = self.raw_items[import_id].clone(); | 747 | let import = self.item_tree[*id].clone(); |
701 | if import.is_extern_crate && import.is_macro_use { | 748 | if import.is_macro_use { |
702 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); | 749 | self.def_collector.import_macros_from_extern_crate(self.module_id, &import); |
703 | } | 750 | } |
704 | } | 751 | } |
705 | } | 752 | } |
706 | } | 753 | } |
707 | 754 | ||
708 | for item in items { | 755 | for &item in items { |
709 | if self.is_cfg_enabled(&item.attrs) { | 756 | let attrs = self.item_tree.attrs(item.into()); |
710 | match item.kind { | 757 | if self.is_cfg_enabled(attrs) { |
711 | raw::RawItemKind::Module(m) => { | 758 | let module = |
712 | self.collect_module(&self.raw_items[m], &item.attrs) | 759 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; |
713 | } | 760 | let container = ContainerId::ModuleId(module); |
714 | raw::RawItemKind::Import(import_id) => { | 761 | |
762 | let mut def = None; | ||
763 | match item { | ||
764 | ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs), | ||
765 | ModItem::Import(import_id) => { | ||
715 | self.def_collector.unresolved_imports.push(ImportDirective { | 766 | self.def_collector.unresolved_imports.push(ImportDirective { |
716 | module_id: self.module_id, | 767 | module_id: self.module_id, |
717 | import_id, | 768 | import: Import::from_use(&self.item_tree, import_id), |
718 | import: self.raw_items[import_id].clone(), | ||
719 | status: PartialResolvedImport::Unresolved, | 769 | status: PartialResolvedImport::Unresolved, |
720 | }) | 770 | }) |
721 | } | 771 | } |
722 | raw::RawItemKind::Def(def) => { | 772 | ModItem::ExternCrate(import_id) => { |
723 | self.define_def(&self.raw_items[def], &item.attrs) | 773 | self.def_collector.unresolved_imports.push(ImportDirective { |
774 | module_id: self.module_id, | ||
775 | import: Import::from_extern_crate(&self.item_tree, import_id), | ||
776 | status: PartialResolvedImport::Unresolved, | ||
777 | }) | ||
724 | } | 778 | } |
725 | raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]), | 779 | ModItem::MacroCall(mac) => self.collect_macro(&self.item_tree[mac]), |
726 | raw::RawItemKind::Impl(imp) => { | 780 | ModItem::Impl(imp) => { |
727 | let module = ModuleId { | 781 | let module = ModuleId { |
728 | krate: self.def_collector.def_map.krate, | 782 | krate: self.def_collector.def_map.krate, |
729 | local_id: self.module_id, | 783 | local_id: self.module_id, |
730 | }; | 784 | }; |
731 | let container = ContainerId::ModuleId(module); | 785 | let container = ContainerId::ModuleId(module); |
732 | let ast_id = self.raw_items[imp].ast_id; | 786 | let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) } |
733 | let impl_id = | 787 | .intern(self.def_collector.db); |
734 | ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
735 | .intern(self.def_collector.db); | ||
736 | self.def_collector.def_map.modules[self.module_id] | 788 | self.def_collector.def_map.modules[self.module_id] |
737 | .scope | 789 | .scope |
738 | .define_impl(impl_id) | 790 | .define_impl(impl_id) |
739 | } | 791 | } |
792 | ModItem::Function(id) => { | ||
793 | let func = &self.item_tree[id]; | ||
794 | def = Some(DefData { | ||
795 | id: FunctionLoc { | ||
796 | container: container.into(), | ||
797 | id: ItemTreeId::new(self.file_id, id), | ||
798 | } | ||
799 | .intern(self.def_collector.db) | ||
800 | .into(), | ||
801 | name: &func.name, | ||
802 | visibility: &self.item_tree[func.visibility], | ||
803 | has_constructor: false, | ||
804 | }); | ||
805 | } | ||
806 | ModItem::Struct(id) => { | ||
807 | let it = &self.item_tree[id]; | ||
808 | |||
809 | // FIXME: check attrs to see if this is an attribute macro invocation; | ||
810 | // in which case we don't add the invocation, just a single attribute | ||
811 | // macro invocation | ||
812 | self.collect_derives(attrs, it.ast_id.upcast()); | ||
813 | |||
814 | def = Some(DefData { | ||
815 | id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) } | ||
816 | .intern(self.def_collector.db) | ||
817 | .into(), | ||
818 | name: &it.name, | ||
819 | visibility: &self.item_tree[it.visibility], | ||
820 | has_constructor: it.kind != StructDefKind::Record, | ||
821 | }); | ||
822 | } | ||
823 | ModItem::Union(id) => { | ||
824 | let it = &self.item_tree[id]; | ||
825 | |||
826 | // FIXME: check attrs to see if this is an attribute macro invocation; | ||
827 | // in which case we don't add the invocation, just a single attribute | ||
828 | // macro invocation | ||
829 | self.collect_derives(attrs, it.ast_id.upcast()); | ||
830 | |||
831 | def = Some(DefData { | ||
832 | id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) } | ||
833 | .intern(self.def_collector.db) | ||
834 | .into(), | ||
835 | name: &it.name, | ||
836 | visibility: &self.item_tree[it.visibility], | ||
837 | has_constructor: false, | ||
838 | }); | ||
839 | } | ||
840 | ModItem::Enum(id) => { | ||
841 | let it = &self.item_tree[id]; | ||
842 | |||
843 | // FIXME: check attrs to see if this is an attribute macro invocation; | ||
844 | // in which case we don't add the invocation, just a single attribute | ||
845 | // macro invocation | ||
846 | self.collect_derives(attrs, it.ast_id.upcast()); | ||
847 | |||
848 | def = Some(DefData { | ||
849 | id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) } | ||
850 | .intern(self.def_collector.db) | ||
851 | .into(), | ||
852 | name: &it.name, | ||
853 | visibility: &self.item_tree[it.visibility], | ||
854 | has_constructor: false, | ||
855 | }); | ||
856 | } | ||
857 | ModItem::Const(id) => { | ||
858 | let it = &self.item_tree[id]; | ||
859 | |||
860 | if let Some(name) = &it.name { | ||
861 | def = Some(DefData { | ||
862 | id: ConstLoc { | ||
863 | container: container.into(), | ||
864 | id: ItemTreeId::new(self.file_id, id), | ||
865 | } | ||
866 | .intern(self.def_collector.db) | ||
867 | .into(), | ||
868 | name, | ||
869 | visibility: &self.item_tree[it.visibility], | ||
870 | has_constructor: false, | ||
871 | }); | ||
872 | } | ||
873 | } | ||
874 | ModItem::Static(id) => { | ||
875 | let it = &self.item_tree[id]; | ||
876 | |||
877 | def = Some(DefData { | ||
878 | id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) } | ||
879 | .intern(self.def_collector.db) | ||
880 | .into(), | ||
881 | name: &it.name, | ||
882 | visibility: &self.item_tree[it.visibility], | ||
883 | has_constructor: false, | ||
884 | }); | ||
885 | } | ||
886 | ModItem::Trait(id) => { | ||
887 | let it = &self.item_tree[id]; | ||
888 | |||
889 | def = Some(DefData { | ||
890 | id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) } | ||
891 | .intern(self.def_collector.db) | ||
892 | .into(), | ||
893 | name: &it.name, | ||
894 | visibility: &self.item_tree[it.visibility], | ||
895 | has_constructor: false, | ||
896 | }); | ||
897 | } | ||
898 | ModItem::TypeAlias(id) => { | ||
899 | let it = &self.item_tree[id]; | ||
900 | |||
901 | def = Some(DefData { | ||
902 | id: TypeAliasLoc { | ||
903 | container: container.into(), | ||
904 | id: ItemTreeId::new(self.file_id, id), | ||
905 | } | ||
906 | .intern(self.def_collector.db) | ||
907 | .into(), | ||
908 | name: &it.name, | ||
909 | visibility: &self.item_tree[it.visibility], | ||
910 | has_constructor: false, | ||
911 | }); | ||
912 | } | ||
913 | } | ||
914 | |||
915 | if let Some(DefData { id, name, visibility, has_constructor }) = def { | ||
916 | self.def_collector.def_map.modules[self.module_id].scope.define_def(id); | ||
917 | let vis = self | ||
918 | .def_collector | ||
919 | .def_map | ||
920 | .resolve_visibility(self.def_collector.db, self.module_id, visibility) | ||
921 | .unwrap_or(Visibility::Public); | ||
922 | self.def_collector.update( | ||
923 | self.module_id, | ||
924 | &[(name.clone(), PerNs::from_def(id, vis, has_constructor))], | ||
925 | vis, | ||
926 | ) | ||
740 | } | 927 | } |
741 | } | 928 | } |
742 | } | 929 | } |
743 | } | 930 | } |
744 | 931 | ||
745 | fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { | 932 | fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { |
746 | let path_attr = attrs.by_key("path").string_value(); | 933 | let path_attr = attrs.by_key("path").string_value(); |
747 | let is_macro_use = attrs.by_key("macro_use").exists(); | 934 | let is_macro_use = attrs.by_key("macro_use").exists(); |
748 | match module { | 935 | match &module.kind { |
749 | // inline module, just recurse | 936 | // inline module, just recurse |
750 | raw::ModuleData::Definition { name, visibility, items, ast_id } => { | 937 | ModKind::Inline { items } => { |
751 | let module_id = self.push_child_module( | 938 | let module_id = self.push_child_module( |
752 | name.clone(), | 939 | module.name.clone(), |
753 | AstId::new(self.file_id, *ast_id), | 940 | AstId::new(self.file_id, module.ast_id), |
754 | None, | 941 | None, |
755 | &visibility, | 942 | &self.item_tree[module.visibility], |
756 | ); | 943 | ); |
757 | 944 | ||
758 | ModCollector { | 945 | ModCollector { |
@@ -760,8 +947,8 @@ impl ModCollector<'_, '_> { | |||
760 | macro_depth: self.macro_depth, | 947 | macro_depth: self.macro_depth, |
761 | module_id, | 948 | module_id, |
762 | file_id: self.file_id, | 949 | file_id: self.file_id, |
763 | raw_items: self.raw_items, | 950 | item_tree: self.item_tree, |
764 | mod_dir: self.mod_dir.descend_into_definition(name, path_attr), | 951 | mod_dir: self.mod_dir.descend_into_definition(&module.name, path_attr), |
765 | } | 952 | } |
766 | .collect(&*items); | 953 | .collect(&*items); |
767 | if is_macro_use { | 954 | if is_macro_use { |
@@ -769,31 +956,31 @@ impl ModCollector<'_, '_> { | |||
769 | } | 956 | } |
770 | } | 957 | } |
771 | // out of line module, resolve, parse and recurse | 958 | // out of line module, resolve, parse and recurse |
772 | raw::ModuleData::Declaration { name, visibility, ast_id } => { | 959 | ModKind::Outline {} => { |
773 | let ast_id = AstId::new(self.file_id, *ast_id); | 960 | let ast_id = AstId::new(self.file_id, module.ast_id); |
774 | match self.mod_dir.resolve_declaration( | 961 | match self.mod_dir.resolve_declaration( |
775 | self.def_collector.db, | 962 | self.def_collector.db, |
776 | self.file_id, | 963 | self.file_id, |
777 | name, | 964 | &module.name, |
778 | path_attr, | 965 | path_attr, |
779 | ) { | 966 | ) { |
780 | Ok((file_id, mod_dir)) => { | 967 | Ok((file_id, is_mod_rs, mod_dir)) => { |
781 | let module_id = self.push_child_module( | 968 | let module_id = self.push_child_module( |
782 | name.clone(), | 969 | module.name.clone(), |
783 | ast_id, | 970 | ast_id, |
784 | Some(file_id), | 971 | Some((file_id, is_mod_rs)), |
785 | &visibility, | 972 | &self.item_tree[module.visibility], |
786 | ); | 973 | ); |
787 | let raw_items = self.def_collector.db.raw_items(file_id.into()); | 974 | let item_tree = self.def_collector.db.item_tree(file_id.into()); |
788 | ModCollector { | 975 | ModCollector { |
789 | def_collector: &mut *self.def_collector, | 976 | def_collector: &mut *self.def_collector, |
790 | macro_depth: self.macro_depth, | 977 | macro_depth: self.macro_depth, |
791 | module_id, | 978 | module_id, |
792 | file_id: file_id.into(), | 979 | file_id: file_id.into(), |
793 | raw_items: &raw_items, | 980 | item_tree: &item_tree, |
794 | mod_dir, | 981 | mod_dir, |
795 | } | 982 | } |
796 | .collect(raw_items.items()); | 983 | .collect(item_tree.top_level_items()); |
797 | if is_macro_use { | 984 | if is_macro_use { |
798 | self.import_all_legacy_macros(module_id); | 985 | self.import_all_legacy_macros(module_id); |
799 | } | 986 | } |
@@ -814,7 +1001,7 @@ impl ModCollector<'_, '_> { | |||
814 | &mut self, | 1001 | &mut self, |
815 | name: Name, | 1002 | name: Name, |
816 | declaration: AstId<ast::Module>, | 1003 | declaration: AstId<ast::Module>, |
817 | definition: Option<FileId>, | 1004 | definition: Option<(FileId, bool)>, |
818 | visibility: &crate::visibility::RawVisibility, | 1005 | visibility: &crate::visibility::RawVisibility, |
819 | ) -> LocalModuleId { | 1006 | ) -> LocalModuleId { |
820 | let vis = self | 1007 | let vis = self |
@@ -827,7 +1014,9 @@ impl ModCollector<'_, '_> { | |||
827 | modules[res].parent = Some(self.module_id); | 1014 | modules[res].parent = Some(self.module_id); |
828 | modules[res].origin = match definition { | 1015 | modules[res].origin = match definition { |
829 | None => ModuleOrigin::Inline { definition: declaration }, | 1016 | None => ModuleOrigin::Inline { definition: declaration }, |
830 | Some(definition) => ModuleOrigin::File { declaration, definition }, | 1017 | Some((definition, is_mod_rs)) => { |
1018 | ModuleOrigin::File { declaration, definition, is_mod_rs } | ||
1019 | } | ||
831 | }; | 1020 | }; |
832 | for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() { | 1021 | for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() { |
833 | modules[res].scope.define_legacy_macro(name, mac) | 1022 | modules[res].scope.define_legacy_macro(name, mac) |
@@ -840,77 +1029,7 @@ impl ModCollector<'_, '_> { | |||
840 | res | 1029 | res |
841 | } | 1030 | } |
842 | 1031 | ||
843 | fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) { | 1032 | fn collect_derives(&mut self, attrs: &Attrs, ast_id: FileAstId<ast::ModuleItem>) { |
844 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; | ||
845 | // FIXME: check attrs to see if this is an attribute macro invocation; | ||
846 | // in which case we don't add the invocation, just a single attribute | ||
847 | // macro invocation | ||
848 | self.collect_derives(attrs, def); | ||
849 | |||
850 | let name = def.name.clone(); | ||
851 | let container = ContainerId::ModuleId(module); | ||
852 | let vis = &def.visibility; | ||
853 | let mut has_constructor = false; | ||
854 | |||
855 | let def: ModuleDefId = match def.kind { | ||
856 | raw::DefKind::Function(ast_id) => FunctionLoc { | ||
857 | container: container.into(), | ||
858 | ast_id: AstId::new(self.file_id, ast_id), | ||
859 | } | ||
860 | .intern(self.def_collector.db) | ||
861 | .into(), | ||
862 | raw::DefKind::Struct(ast_id, mode) => { | ||
863 | has_constructor = mode != raw::StructDefKind::Record; | ||
864 | StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
865 | .intern(self.def_collector.db) | ||
866 | .into() | ||
867 | } | ||
868 | raw::DefKind::Union(ast_id) => { | ||
869 | UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
870 | .intern(self.def_collector.db) | ||
871 | .into() | ||
872 | } | ||
873 | raw::DefKind::Enum(ast_id) => { | ||
874 | EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
875 | .intern(self.def_collector.db) | ||
876 | .into() | ||
877 | } | ||
878 | raw::DefKind::Const(ast_id) => { | ||
879 | ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) } | ||
880 | .intern(self.def_collector.db) | ||
881 | .into() | ||
882 | } | ||
883 | raw::DefKind::Static(ast_id) => { | ||
884 | StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
885 | .intern(self.def_collector.db) | ||
886 | .into() | ||
887 | } | ||
888 | raw::DefKind::Trait(ast_id) => { | ||
889 | TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | ||
890 | .intern(self.def_collector.db) | ||
891 | .into() | ||
892 | } | ||
893 | raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc { | ||
894 | container: container.into(), | ||
895 | ast_id: AstId::new(self.file_id, ast_id), | ||
896 | } | ||
897 | .intern(self.def_collector.db) | ||
898 | .into(), | ||
899 | }; | ||
900 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | ||
901 | let vis = self | ||
902 | .def_collector | ||
903 | .def_map | ||
904 | .resolve_visibility(self.def_collector.db, self.module_id, vis) | ||
905 | .unwrap_or(Visibility::Public); | ||
906 | self.def_collector.update( | ||
907 | self.module_id, | ||
908 | &[(name, PerNs::from_def(def, vis, has_constructor))], | ||
909 | vis, | ||
910 | ) | ||
911 | } | ||
912 | |||
913 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { | ||
914 | for derive_subtree in attrs.by_key("derive").tt_values() { | 1033 | for derive_subtree in attrs.by_key("derive").tt_values() { |
915 | // for #[derive(Copy, Clone)], `derive_subtree` is the `(Copy, Clone)` subtree | 1034 | // for #[derive(Copy, Clone)], `derive_subtree` is the `(Copy, Clone)` subtree |
916 | for tt in &derive_subtree.token_trees { | 1035 | for tt in &derive_subtree.token_trees { |
@@ -921,7 +1040,7 @@ impl ModCollector<'_, '_> { | |||
921 | }; | 1040 | }; |
922 | let path = ModPath::from_tt_ident(ident); | 1041 | let path = ModPath::from_tt_ident(ident); |
923 | 1042 | ||
924 | let ast_id = AstIdWithPath::new(self.file_id, def.kind.ast_id(), path); | 1043 | let ast_id = AstIdWithPath::new(self.file_id, ast_id, path); |
925 | self.def_collector | 1044 | self.def_collector |
926 | .unexpanded_attribute_macros | 1045 | .unexpanded_attribute_macros |
927 | .push(DeriveDirective { module_id: self.module_id, ast_id }); | 1046 | .push(DeriveDirective { module_id: self.module_id, ast_id }); |
@@ -929,11 +1048,11 @@ impl ModCollector<'_, '_> { | |||
929 | } | 1048 | } |
930 | } | 1049 | } |
931 | 1050 | ||
932 | fn collect_macro(&mut self, mac: &raw::MacroData) { | 1051 | fn collect_macro(&mut self, mac: &MacroCall) { |
933 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); | 1052 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); |
934 | 1053 | ||
935 | // Case 0: builtin macros | 1054 | // Case 0: builtin macros |
936 | if mac.builtin { | 1055 | if mac.is_builtin { |
937 | if let Some(name) = &mac.name { | 1056 | if let Some(name) = &mac.name { |
938 | let krate = self.def_collector.def_map.krate; | 1057 | let krate = self.def_collector.def_map.krate; |
939 | if let Some(macro_id) = find_builtin_macro(name, krate, ast_id.ast_id) { | 1058 | if let Some(macro_id) = find_builtin_macro(name, krate, ast_id.ast_id) { |
@@ -941,7 +1060,7 @@ impl ModCollector<'_, '_> { | |||
941 | self.module_id, | 1060 | self.module_id, |
942 | name.clone(), | 1061 | name.clone(), |
943 | macro_id, | 1062 | macro_id, |
944 | mac.export, | 1063 | mac.is_export, |
945 | ); | 1064 | ); |
946 | return; | 1065 | return; |
947 | } | 1066 | } |
@@ -955,9 +1074,14 @@ impl ModCollector<'_, '_> { | |||
955 | ast_id: Some(ast_id.ast_id), | 1074 | ast_id: Some(ast_id.ast_id), |
956 | krate: Some(self.def_collector.def_map.krate), | 1075 | krate: Some(self.def_collector.def_map.krate), |
957 | kind: MacroDefKind::Declarative, | 1076 | kind: MacroDefKind::Declarative, |
958 | local_inner: mac.local_inner, | 1077 | local_inner: mac.is_local_inner, |
959 | }; | 1078 | }; |
960 | self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); | 1079 | self.def_collector.define_macro( |
1080 | self.module_id, | ||
1081 | name.clone(), | ||
1082 | macro_id, | ||
1083 | mac.is_export, | ||
1084 | ); | ||
961 | } | 1085 | } |
962 | return; | 1086 | return; |
963 | } | 1087 | } |
diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs index 19fe0615a..39e9a6d97 100644 --- a/crates/ra_hir_def/src/nameres/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/mod_resolution.rs | |||
@@ -44,7 +44,7 @@ impl ModDir { | |||
44 | file_id: HirFileId, | 44 | file_id: HirFileId, |
45 | name: &Name, | 45 | name: &Name, |
46 | attr_path: Option<&SmolStr>, | 46 | attr_path: Option<&SmolStr>, |
47 | ) -> Result<(FileId, ModDir), String> { | 47 | ) -> Result<(FileId, bool, ModDir), String> { |
48 | let file_id = file_id.original_file(db.upcast()); | 48 | let file_id = file_id.original_file(db.upcast()); |
49 | 49 | ||
50 | let mut candidate_files = Vec::new(); | 50 | let mut candidate_files = Vec::new(); |
@@ -64,11 +64,12 @@ impl ModDir { | |||
64 | if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) { | 64 | if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) { |
65 | let mut root_non_dir_owner = false; | 65 | let mut root_non_dir_owner = false; |
66 | let mut mod_path = RelativePathBuf::new(); | 66 | let mut mod_path = RelativePathBuf::new(); |
67 | if !(candidate.ends_with("mod.rs") || attr_path.is_some()) { | 67 | let is_mod_rs = candidate.ends_with("mod.rs"); |
68 | if !(is_mod_rs || attr_path.is_some()) { | ||
68 | root_non_dir_owner = true; | 69 | root_non_dir_owner = true; |
69 | mod_path.push(&name.to_string()); | 70 | mod_path.push(&name.to_string()); |
70 | } | 71 | } |
71 | return Ok((file_id, ModDir { path: mod_path, root_non_dir_owner })); | 72 | return Ok((file_id, is_mod_rs, ModDir { path: mod_path, root_non_dir_owner })); |
72 | } | 73 | } |
73 | } | 74 | } |
74 | Err(candidate_files.remove(0)) | 75 | Err(candidate_files.remove(0)) |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs deleted file mode 100644 index f44baa579..000000000 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ /dev/null | |||
@@ -1,482 +0,0 @@ | |||
1 | //! Lowers syntax tree of a rust file into a raw representation of containing | ||
2 | //! items, *without* attaching them to a module structure. | ||
3 | //! | ||
4 | //! That is, raw items don't have semantics, just as syntax, but, unlike syntax, | ||
5 | //! they don't change with trivial source code edits, making them a great tool | ||
6 | //! for building salsa recomputation firewalls. | ||
7 | |||
8 | use std::{ops::Index, sync::Arc}; | ||
9 | |||
10 | use hir_expand::{ | ||
11 | ast_id_map::AstIdMap, | ||
12 | hygiene::Hygiene, | ||
13 | name::{AsName, Name}, | ||
14 | }; | ||
15 | use ra_arena::{Arena, Idx}; | ||
16 | use ra_prof::profile; | ||
17 | use ra_syntax::{ | ||
18 | ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, | ||
19 | AstNode, | ||
20 | }; | ||
21 | use test_utils::mark; | ||
22 | |||
23 | use crate::{ | ||
24 | attr::Attrs, | ||
25 | db::DefDatabase, | ||
26 | path::{ImportAlias, ModPath}, | ||
27 | visibility::RawVisibility, | ||
28 | FileAstId, HirFileId, InFile, | ||
29 | }; | ||
30 | |||
31 | /// `RawItems` is a set of top-level items in a file (except for impls). | ||
32 | /// | ||
33 | /// It is the input to name resolution algorithm. `RawItems` are not invalidated | ||
34 | /// on most edits. | ||
35 | #[derive(Debug, Default, PartialEq, Eq)] | ||
36 | pub struct RawItems { | ||
37 | modules: Arena<ModuleData>, | ||
38 | imports: Arena<ImportData>, | ||
39 | defs: Arena<DefData>, | ||
40 | macros: Arena<MacroData>, | ||
41 | impls: Arena<ImplData>, | ||
42 | /// items for top-level module | ||
43 | items: Vec<RawItem>, | ||
44 | } | ||
45 | |||
46 | impl RawItems { | ||
47 | pub(crate) fn raw_items_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<RawItems> { | ||
48 | let _p = profile("raw_items_query"); | ||
49 | let mut collector = RawItemsCollector { | ||
50 | raw_items: RawItems::default(), | ||
51 | source_ast_id_map: db.ast_id_map(file_id), | ||
52 | file_id, | ||
53 | hygiene: Hygiene::new(db.upcast(), file_id), | ||
54 | }; | ||
55 | if let Some(node) = db.parse_or_expand(file_id) { | ||
56 | if let Some(source_file) = ast::SourceFile::cast(node.clone()) { | ||
57 | collector.process_module(None, source_file); | ||
58 | } else if let Some(item_list) = ast::MacroItems::cast(node) { | ||
59 | collector.process_module(None, item_list); | ||
60 | } | ||
61 | } | ||
62 | let raw_items = collector.raw_items; | ||
63 | Arc::new(raw_items) | ||
64 | } | ||
65 | |||
66 | pub(super) fn items(&self) -> &[RawItem] { | ||
67 | &self.items | ||
68 | } | ||
69 | } | ||
70 | |||
71 | impl Index<Idx<ModuleData>> for RawItems { | ||
72 | type Output = ModuleData; | ||
73 | fn index(&self, idx: Idx<ModuleData>) -> &ModuleData { | ||
74 | &self.modules[idx] | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl Index<Import> for RawItems { | ||
79 | type Output = ImportData; | ||
80 | fn index(&self, idx: Import) -> &ImportData { | ||
81 | &self.imports[idx] | ||
82 | } | ||
83 | } | ||
84 | |||
85 | impl Index<Idx<DefData>> for RawItems { | ||
86 | type Output = DefData; | ||
87 | fn index(&self, idx: Idx<DefData>) -> &DefData { | ||
88 | &self.defs[idx] | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl Index<Idx<MacroData>> for RawItems { | ||
93 | type Output = MacroData; | ||
94 | fn index(&self, idx: Idx<MacroData>) -> &MacroData { | ||
95 | &self.macros[idx] | ||
96 | } | ||
97 | } | ||
98 | |||
99 | impl Index<Idx<ImplData>> for RawItems { | ||
100 | type Output = ImplData; | ||
101 | fn index(&self, idx: Idx<ImplData>) -> &ImplData { | ||
102 | &self.impls[idx] | ||
103 | } | ||
104 | } | ||
105 | |||
106 | #[derive(Debug, PartialEq, Eq, Clone)] | ||
107 | pub(super) struct RawItem { | ||
108 | pub(super) attrs: Attrs, | ||
109 | pub(super) kind: RawItemKind, | ||
110 | } | ||
111 | |||
112 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
113 | pub(super) enum RawItemKind { | ||
114 | Module(Idx<ModuleData>), | ||
115 | Import(Import), | ||
116 | Def(Idx<DefData>), | ||
117 | Macro(Idx<MacroData>), | ||
118 | Impl(Idx<ImplData>), | ||
119 | } | ||
120 | |||
121 | #[derive(Debug, PartialEq, Eq)] | ||
122 | pub(super) enum ModuleData { | ||
123 | Declaration { | ||
124 | name: Name, | ||
125 | visibility: RawVisibility, | ||
126 | ast_id: FileAstId<ast::Module>, | ||
127 | }, | ||
128 | Definition { | ||
129 | name: Name, | ||
130 | visibility: RawVisibility, | ||
131 | ast_id: FileAstId<ast::Module>, | ||
132 | items: Vec<RawItem>, | ||
133 | }, | ||
134 | } | ||
135 | |||
136 | pub(crate) type Import = Idx<ImportData>; | ||
137 | |||
138 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
139 | pub struct ImportData { | ||
140 | pub(super) path: ModPath, | ||
141 | pub(super) alias: Option<ImportAlias>, | ||
142 | pub(super) is_glob: bool, | ||
143 | pub(super) is_prelude: bool, | ||
144 | pub(super) is_extern_crate: bool, | ||
145 | pub(super) is_macro_use: bool, | ||
146 | pub(super) visibility: RawVisibility, | ||
147 | } | ||
148 | |||
149 | // type Def = Idx<DefData>; | ||
150 | |||
151 | #[derive(Debug, PartialEq, Eq)] | ||
152 | pub(super) struct DefData { | ||
153 | pub(super) name: Name, | ||
154 | pub(super) kind: DefKind, | ||
155 | pub(super) visibility: RawVisibility, | ||
156 | } | ||
157 | |||
158 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
159 | pub(super) enum StructDefKind { | ||
160 | Record, | ||
161 | Tuple, | ||
162 | Unit, | ||
163 | } | ||
164 | |||
165 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
166 | pub(super) enum DefKind { | ||
167 | Function(FileAstId<ast::FnDef>), | ||
168 | Struct(FileAstId<ast::StructDef>, StructDefKind), | ||
169 | Union(FileAstId<ast::UnionDef>), | ||
170 | Enum(FileAstId<ast::EnumDef>), | ||
171 | Const(FileAstId<ast::ConstDef>), | ||
172 | Static(FileAstId<ast::StaticDef>), | ||
173 | Trait(FileAstId<ast::TraitDef>), | ||
174 | TypeAlias(FileAstId<ast::TypeAliasDef>), | ||
175 | } | ||
176 | |||
177 | impl DefKind { | ||
178 | pub fn ast_id(self) -> FileAstId<ast::ModuleItem> { | ||
179 | match self { | ||
180 | DefKind::Function(it) => it.upcast(), | ||
181 | DefKind::Struct(it, _) => it.upcast(), | ||
182 | DefKind::Union(it) => it.upcast(), | ||
183 | DefKind::Enum(it) => it.upcast(), | ||
184 | DefKind::Const(it) => it.upcast(), | ||
185 | DefKind::Static(it) => it.upcast(), | ||
186 | DefKind::Trait(it) => it.upcast(), | ||
187 | DefKind::TypeAlias(it) => it.upcast(), | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | #[derive(Debug, PartialEq, Eq)] | ||
193 | pub(super) struct MacroData { | ||
194 | pub(super) ast_id: FileAstId<ast::MacroCall>, | ||
195 | pub(super) path: ModPath, | ||
196 | pub(super) name: Option<Name>, | ||
197 | pub(super) export: bool, | ||
198 | pub(super) local_inner: bool, | ||
199 | pub(super) builtin: bool, | ||
200 | } | ||
201 | |||
202 | #[derive(Debug, PartialEq, Eq)] | ||
203 | pub(super) struct ImplData { | ||
204 | pub(super) ast_id: FileAstId<ast::ImplDef>, | ||
205 | } | ||
206 | |||
207 | struct RawItemsCollector { | ||
208 | raw_items: RawItems, | ||
209 | source_ast_id_map: Arc<AstIdMap>, | ||
210 | file_id: HirFileId, | ||
211 | hygiene: Hygiene, | ||
212 | } | ||
213 | |||
214 | impl RawItemsCollector { | ||
215 | fn process_module( | ||
216 | &mut self, | ||
217 | current_module: Option<Idx<ModuleData>>, | ||
218 | body: impl ast::ModuleItemOwner, | ||
219 | ) { | ||
220 | for item in body.items() { | ||
221 | self.add_item(current_module, item) | ||
222 | } | ||
223 | } | ||
224 | |||
225 | fn add_item(&mut self, current_module: Option<Idx<ModuleData>>, item: ast::ModuleItem) { | ||
226 | let attrs = self.parse_attrs(&item); | ||
227 | let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); | ||
228 | let (kind, name) = match item { | ||
229 | ast::ModuleItem::Module(module) => { | ||
230 | self.add_module(current_module, module); | ||
231 | return; | ||
232 | } | ||
233 | ast::ModuleItem::UseItem(use_item) => { | ||
234 | self.add_use_item(current_module, use_item); | ||
235 | return; | ||
236 | } | ||
237 | ast::ModuleItem::ExternCrateItem(extern_crate) => { | ||
238 | self.add_extern_crate_item(current_module, extern_crate); | ||
239 | return; | ||
240 | } | ||
241 | ast::ModuleItem::ImplDef(it) => { | ||
242 | self.add_impl(current_module, it); | ||
243 | return; | ||
244 | } | ||
245 | ast::ModuleItem::StructDef(it) => { | ||
246 | let kind = match it.kind() { | ||
247 | ast::StructKind::Record(_) => StructDefKind::Record, | ||
248 | ast::StructKind::Tuple(_) => StructDefKind::Tuple, | ||
249 | ast::StructKind::Unit => StructDefKind::Unit, | ||
250 | }; | ||
251 | let id = self.source_ast_id_map.ast_id(&it); | ||
252 | let name = it.name(); | ||
253 | (DefKind::Struct(id, kind), name) | ||
254 | } | ||
255 | ast::ModuleItem::UnionDef(it) => { | ||
256 | let id = self.source_ast_id_map.ast_id(&it); | ||
257 | let name = it.name(); | ||
258 | (DefKind::Union(id), name) | ||
259 | } | ||
260 | ast::ModuleItem::EnumDef(it) => { | ||
261 | (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
262 | } | ||
263 | ast::ModuleItem::FnDef(it) => { | ||
264 | (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
265 | } | ||
266 | ast::ModuleItem::TraitDef(it) => { | ||
267 | (DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
268 | } | ||
269 | ast::ModuleItem::TypeAliasDef(it) => { | ||
270 | (DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
271 | } | ||
272 | ast::ModuleItem::ConstDef(it) => { | ||
273 | (DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
274 | } | ||
275 | ast::ModuleItem::StaticDef(it) => { | ||
276 | (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
277 | } | ||
278 | ast::ModuleItem::MacroCall(it) => { | ||
279 | self.add_macro(current_module, it); | ||
280 | return; | ||
281 | } | ||
282 | ast::ModuleItem::ExternBlock(it) => { | ||
283 | self.add_extern_block(current_module, it); | ||
284 | return; | ||
285 | } | ||
286 | }; | ||
287 | if let Some(name) = name { | ||
288 | let name = name.as_name(); | ||
289 | let def = self.raw_items.defs.alloc(DefData { name, kind, visibility }); | ||
290 | self.push_item(current_module, attrs, RawItemKind::Def(def)); | ||
291 | } | ||
292 | } | ||
293 | |||
294 | fn add_extern_block( | ||
295 | &mut self, | ||
296 | current_module: Option<Idx<ModuleData>>, | ||
297 | block: ast::ExternBlock, | ||
298 | ) { | ||
299 | if let Some(items) = block.extern_item_list() { | ||
300 | for item in items.extern_items() { | ||
301 | let attrs = self.parse_attrs(&item); | ||
302 | let visibility = | ||
303 | RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); | ||
304 | let (kind, name) = match item { | ||
305 | ast::ExternItem::FnDef(it) => { | ||
306 | (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
307 | } | ||
308 | ast::ExternItem::StaticDef(it) => { | ||
309 | (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) | ||
310 | } | ||
311 | }; | ||
312 | |||
313 | if let Some(name) = name { | ||
314 | let name = name.as_name(); | ||
315 | let def = self.raw_items.defs.alloc(DefData { name, kind, visibility }); | ||
316 | self.push_item(current_module, attrs, RawItemKind::Def(def)); | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | } | ||
321 | |||
322 | fn add_module(&mut self, current_module: Option<Idx<ModuleData>>, module: ast::Module) { | ||
323 | let name = match module.name() { | ||
324 | Some(it) => it.as_name(), | ||
325 | None => return, | ||
326 | }; | ||
327 | let attrs = self.parse_attrs(&module); | ||
328 | let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); | ||
329 | |||
330 | let ast_id = self.source_ast_id_map.ast_id(&module); | ||
331 | if module.semicolon_token().is_some() { | ||
332 | let item = | ||
333 | self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); | ||
334 | self.push_item(current_module, attrs, RawItemKind::Module(item)); | ||
335 | return; | ||
336 | } | ||
337 | |||
338 | if let Some(item_list) = module.item_list() { | ||
339 | let item = self.raw_items.modules.alloc(ModuleData::Definition { | ||
340 | name, | ||
341 | visibility, | ||
342 | ast_id, | ||
343 | items: Vec::new(), | ||
344 | }); | ||
345 | self.process_module(Some(item), item_list); | ||
346 | self.push_item(current_module, attrs, RawItemKind::Module(item)); | ||
347 | return; | ||
348 | } | ||
349 | mark::hit!(name_res_works_for_broken_modules); | ||
350 | } | ||
351 | |||
352 | fn add_use_item(&mut self, current_module: Option<Idx<ModuleData>>, use_item: ast::UseItem) { | ||
353 | // FIXME: cfg_attr | ||
354 | let is_prelude = use_item.has_atom_attr("prelude_import"); | ||
355 | let attrs = self.parse_attrs(&use_item); | ||
356 | let visibility = RawVisibility::from_ast_with_hygiene(use_item.visibility(), &self.hygiene); | ||
357 | |||
358 | let mut buf = Vec::new(); | ||
359 | ModPath::expand_use_item( | ||
360 | InFile { value: use_item, file_id: self.file_id }, | ||
361 | &self.hygiene, | ||
362 | |path, _use_tree, is_glob, alias| { | ||
363 | let import_data = ImportData { | ||
364 | path, | ||
365 | alias, | ||
366 | is_glob, | ||
367 | is_prelude, | ||
368 | is_extern_crate: false, | ||
369 | is_macro_use: false, | ||
370 | visibility: visibility.clone(), | ||
371 | }; | ||
372 | buf.push(import_data); | ||
373 | }, | ||
374 | ); | ||
375 | for import_data in buf { | ||
376 | self.push_import(current_module, attrs.clone(), import_data); | ||
377 | } | ||
378 | } | ||
379 | |||
380 | fn add_extern_crate_item( | ||
381 | &mut self, | ||
382 | current_module: Option<Idx<ModuleData>>, | ||
383 | extern_crate: ast::ExternCrateItem, | ||
384 | ) { | ||
385 | if let Some(name_ref) = extern_crate.name_ref() { | ||
386 | let path = ModPath::from_name_ref(&name_ref); | ||
387 | let visibility = | ||
388 | RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene); | ||
389 | let alias = extern_crate.alias().map(|a| { | ||
390 | a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias) | ||
391 | }); | ||
392 | let attrs = self.parse_attrs(&extern_crate); | ||
393 | // FIXME: cfg_attr | ||
394 | let is_macro_use = extern_crate.has_atom_attr("macro_use"); | ||
395 | let import_data = ImportData { | ||
396 | path, | ||
397 | alias, | ||
398 | is_glob: false, | ||
399 | is_prelude: false, | ||
400 | is_extern_crate: true, | ||
401 | is_macro_use, | ||
402 | visibility, | ||
403 | }; | ||
404 | self.push_import(current_module, attrs, import_data); | ||
405 | } | ||
406 | } | ||
407 | |||
408 | fn add_macro(&mut self, current_module: Option<Idx<ModuleData>>, m: ast::MacroCall) { | ||
409 | let attrs = self.parse_attrs(&m); | ||
410 | let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { | ||
411 | Some(it) => it, | ||
412 | _ => return, | ||
413 | }; | ||
414 | |||
415 | let name = m.name().map(|it| it.as_name()); | ||
416 | let ast_id = self.source_ast_id_map.ast_id(&m); | ||
417 | |||
418 | // FIXME: cfg_attr | ||
419 | let export_attr = attrs.by_key("macro_export"); | ||
420 | |||
421 | let export = export_attr.exists(); | ||
422 | let local_inner = if export { | ||
423 | export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it { | ||
424 | tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => { | ||
425 | ident.text.contains("local_inner_macros") | ||
426 | } | ||
427 | _ => false, | ||
428 | }) | ||
429 | } else { | ||
430 | false | ||
431 | }; | ||
432 | |||
433 | let builtin = attrs.by_key("rustc_builtin_macro").exists(); | ||
434 | |||
435 | let m = self.raw_items.macros.alloc(MacroData { | ||
436 | ast_id, | ||
437 | path, | ||
438 | name, | ||
439 | export, | ||
440 | local_inner, | ||
441 | builtin, | ||
442 | }); | ||
443 | self.push_item(current_module, attrs, RawItemKind::Macro(m)); | ||
444 | } | ||
445 | |||
446 | fn add_impl(&mut self, current_module: Option<Idx<ModuleData>>, imp: ast::ImplDef) { | ||
447 | let attrs = self.parse_attrs(&imp); | ||
448 | let ast_id = self.source_ast_id_map.ast_id(&imp); | ||
449 | let imp = self.raw_items.impls.alloc(ImplData { ast_id }); | ||
450 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | ||
451 | } | ||
452 | |||
453 | fn push_import( | ||
454 | &mut self, | ||
455 | current_module: Option<Idx<ModuleData>>, | ||
456 | attrs: Attrs, | ||
457 | data: ImportData, | ||
458 | ) { | ||
459 | let import = self.raw_items.imports.alloc(data); | ||
460 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | ||
461 | } | ||
462 | |||
463 | fn push_item( | ||
464 | &mut self, | ||
465 | current_module: Option<Idx<ModuleData>>, | ||
466 | attrs: Attrs, | ||
467 | kind: RawItemKind, | ||
468 | ) { | ||
469 | match current_module { | ||
470 | Some(module) => match &mut self.raw_items.modules[module] { | ||
471 | ModuleData::Definition { items, .. } => items, | ||
472 | ModuleData::Declaration { .. } => unreachable!(), | ||
473 | }, | ||
474 | None => &mut self.raw_items.items, | ||
475 | } | ||
476 | .push(RawItem { attrs, kind }) | ||
477 | } | ||
478 | |||
479 | fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { | ||
480 | Attrs::new(item, &self.hygiene) | ||
481 | } | ||
482 | } | ||
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 05cd0297d..503099fb7 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -424,31 +424,6 @@ fn extern_crate_rename_2015_edition() { | |||
424 | } | 424 | } |
425 | 425 | ||
426 | #[test] | 426 | #[test] |
427 | fn import_across_source_roots() { | ||
428 | let map = def_map( | ||
429 | " | ||
430 | //- /main.rs crate:main deps:test_crate | ||
431 | use test_crate::a::b::C; | ||
432 | |||
433 | //- root /test_crate/ | ||
434 | |||
435 | //- /test_crate/lib.rs crate:test_crate | ||
436 | pub mod a { | ||
437 | pub mod b { | ||
438 | pub struct C; | ||
439 | } | ||
440 | } | ||
441 | |||
442 | ", | ||
443 | ); | ||
444 | |||
445 | assert_snapshot!(map, @r###" | ||
446 | ⋮crate | ||
447 | ⋮C: t v | ||
448 | "###); | ||
449 | } | ||
450 | |||
451 | #[test] | ||
452 | fn reexport_across_crates() { | 427 | fn reexport_across_crates() { |
453 | let map = def_map( | 428 | let map = def_map( |
454 | " | 429 | " |
diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index 2b12c0daa..2f440975a 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs | |||
@@ -229,3 +229,50 @@ fn glob_enum_group() { | |||
229 | "### | 229 | "### |
230 | ); | 230 | ); |
231 | } | 231 | } |
232 | |||
233 | #[test] | ||
234 | fn glob_shadowed_def() { | ||
235 | mark::check!(import_shadowed); | ||
236 | let map = def_map( | ||
237 | r###" | ||
238 | //- /lib.rs | ||
239 | mod foo; | ||
240 | mod bar; | ||
241 | |||
242 | use foo::*; | ||
243 | use bar::baz; | ||
244 | |||
245 | use baz::Bar; | ||
246 | |||
247 | //- /foo.rs | ||
248 | pub mod baz { | ||
249 | pub struct Foo; | ||
250 | } | ||
251 | |||
252 | //- /bar.rs | ||
253 | pub mod baz { | ||
254 | pub struct Bar; | ||
255 | } | ||
256 | "###, | ||
257 | ); | ||
258 | assert_snapshot!(map, @r###" | ||
259 | ⋮crate | ||
260 | ⋮Bar: t v | ||
261 | ⋮bar: t | ||
262 | ⋮baz: t | ||
263 | ⋮foo: t | ||
264 | ⋮ | ||
265 | ⋮crate::bar | ||
266 | ⋮baz: t | ||
267 | ⋮ | ||
268 | ⋮crate::bar::baz | ||
269 | ⋮Bar: t v | ||
270 | ⋮ | ||
271 | ⋮crate::foo | ||
272 | ⋮baz: t | ||
273 | ⋮ | ||
274 | ⋮crate::foo::baz | ||
275 | ⋮Foo: t v | ||
276 | "### | ||
277 | ); | ||
278 | } | ||
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index 87165ac33..0c288a108 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs | |||
@@ -58,44 +58,6 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { | |||
58 | } | 58 | } |
59 | 59 | ||
60 | #[test] | 60 | #[test] |
61 | fn adding_inner_items_should_not_invalidate_def_map() { | ||
62 | check_def_map_is_not_recomputed( | ||
63 | r" | ||
64 | //- /lib.rs | ||
65 | struct S { a: i32} | ||
66 | enum E { A } | ||
67 | trait T { | ||
68 | fn a() {} | ||
69 | } | ||
70 | mod foo;<|> | ||
71 | impl S { | ||
72 | fn a() {} | ||
73 | } | ||
74 | use crate::foo::bar::Baz; | ||
75 | //- /foo/mod.rs | ||
76 | pub mod bar; | ||
77 | |||
78 | //- /foo/bar.rs | ||
79 | pub struct Baz; | ||
80 | ", | ||
81 | r" | ||
82 | struct S { a: i32, b: () } | ||
83 | enum E { A, B } | ||
84 | trait T { | ||
85 | fn a() {} | ||
86 | fn b() {} | ||
87 | } | ||
88 | mod foo;<|> | ||
89 | impl S { | ||
90 | fn a() {} | ||
91 | fn b() {} | ||
92 | } | ||
93 | use crate::foo::bar::Baz; | ||
94 | ", | ||
95 | ); | ||
96 | } | ||
97 | |||
98 | #[test] | ||
99 | fn typing_inside_a_macro_should_not_invalidate_def_map() { | 61 | fn typing_inside_a_macro_should_not_invalidate_def_map() { |
100 | let (mut db, pos) = TestDB::with_position( | 62 | let (mut db, pos) = TestDB::with_position( |
101 | r" | 63 | r" |
diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index b43b294ca..753684201 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | |||
@@ -20,8 +20,11 @@ fn name_res_works_for_broken_modules() { | |||
20 | ", | 20 | ", |
21 | ); | 21 | ); |
22 | assert_snapshot!(map, @r###" | 22 | assert_snapshot!(map, @r###" |
23 | ⋮crate | 23 | crate |
24 | ⋮Baz: _ | 24 | Baz: _ |
25 | foo: t | ||
26 | |||
27 | crate::foo | ||
25 | "###); | 28 | "###); |
26 | } | 29 | } |
27 | 30 | ||
@@ -332,6 +335,22 @@ fn module_resolution_relative_path_2() { | |||
332 | } | 335 | } |
333 | 336 | ||
334 | #[test] | 337 | #[test] |
338 | fn module_resolution_relative_path_outside_root() { | ||
339 | let map = def_map( | ||
340 | r###" | ||
341 | //- /main.rs | ||
342 | |||
343 | #[path="../../../../../outside.rs"] | ||
344 | mod foo; | ||
345 | "###, | ||
346 | ); | ||
347 | |||
348 | assert_snapshot!(map, @r###" | ||
349 | ⋮crate | ||
350 | "###); | ||
351 | } | ||
352 | |||
353 | #[test] | ||
335 | fn module_resolution_explicit_path_mod_rs_2() { | 354 | fn module_resolution_explicit_path_mod_rs_2() { |
336 | let map = def_map( | 355 | let map = def_map( |
337 | r###" | 356 | r###" |
@@ -719,10 +738,7 @@ fn unresolved_module_diagnostics() { | |||
719 | ), | 738 | ), |
720 | ), | 739 | ), |
721 | ), | 740 | ), |
722 | value: FileAstId { | 741 | value: FileAstId::<ra_syntax::ast::generated::nodes::Module>(1), |
723 | raw: Idx::<SyntaxNodePtr>(1), | ||
724 | _ty: PhantomData, | ||
725 | }, | ||
726 | }, | 742 | }, |
727 | candidate: "bar.rs", | 743 | candidate: "bar.rs", |
728 | }, | 744 | }, |