diff options
author | Jonas Schievink <[email protected]> | 2020-12-15 19:33:05 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-12-15 19:33:05 +0000 |
commit | b238ddd21adf9910769522a21e31c2e14f664396 (patch) | |
tree | 219749056bcef5cd35cfc94826108c4f955b1c8b /crates/hir_def | |
parent | c31c3246a8c87a3639623c30b692a57e728bb046 (diff) |
Make macro def krate mandatory
Refactors builtin derive support to go through proper name resolution
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/item_scope.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 18 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/macros.rs | 31 |
4 files changed, 38 insertions, 15 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index e4bf5603c..23e2fd764 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -803,7 +803,7 @@ impl ExprCollector<'_> { | |||
803 | } | 803 | } |
804 | Either::Right(e) => { | 804 | Either::Right(e) => { |
805 | let mac = MacroDefId { | 805 | let mac = MacroDefId { |
806 | krate: Some(self.expander.module.krate), | 806 | krate: self.expander.module.krate, |
807 | ast_id: Some(self.expander.ast_id(&e)), | 807 | ast_id: Some(self.expander.ast_id(&e)), |
808 | kind: MacroDefKind::Declarative, | 808 | kind: MacroDefKind::Declarative, |
809 | local_inner: false, | 809 | local_inner: false, |
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index a8b3fe844..62ab3b2bd 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs | |||
@@ -363,7 +363,7 @@ impl ItemInNs { | |||
363 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db).krate, | 363 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db).krate, |
364 | ModuleDefId::BuiltinType(_) => return None, | 364 | ModuleDefId::BuiltinType(_) => return None, |
365 | }, | 365 | }, |
366 | ItemInNs::Macros(id) => return id.krate, | 366 | ItemInNs::Macros(id) => return Some(id.krate), |
367 | }) | 367 | }) |
368 | } | 368 | } |
369 | } | 369 | } |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index c2f741060..785895277 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -309,13 +309,13 @@ impl DefCollector<'_> { | |||
309 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { | 309 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { |
310 | Some((_, expander)) => MacroDefId { | 310 | Some((_, expander)) => MacroDefId { |
311 | ast_id: None, | 311 | ast_id: None, |
312 | krate: Some(self.def_map.krate), | 312 | krate: self.def_map.krate, |
313 | kind: MacroDefKind::ProcMacro(*expander), | 313 | kind: MacroDefKind::ProcMacro(*expander), |
314 | local_inner: false, | 314 | local_inner: false, |
315 | }, | 315 | }, |
316 | None => MacroDefId { | 316 | None => MacroDefId { |
317 | ast_id: None, | 317 | ast_id: None, |
318 | krate: Some(self.def_map.krate), | 318 | krate: self.def_map.krate, |
319 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), | 319 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), |
320 | local_inner: false, | 320 | local_inner: false, |
321 | }, | 321 | }, |
@@ -784,14 +784,6 @@ impl DefCollector<'_> { | |||
784 | directive: &DeriveDirective, | 784 | directive: &DeriveDirective, |
785 | path: &ModPath, | 785 | path: &ModPath, |
786 | ) -> Option<MacroDefId> { | 786 | ) -> Option<MacroDefId> { |
787 | if let Some(name) = path.as_ident() { | ||
788 | // FIXME this should actually be handled with the normal name | ||
789 | // resolution; the std lib defines built-in stubs for the derives, | ||
790 | // but these are new-style `macro`s, which we don't support yet | ||
791 | if let Some(def_id) = find_builtin_derive(name) { | ||
792 | return Some(def_id); | ||
793 | } | ||
794 | } | ||
795 | let resolved_res = self.def_map.resolve_path_fp_with_macro( | 787 | let resolved_res = self.def_map.resolve_path_fp_with_macro( |
796 | self.db, | 788 | self.db, |
797 | ResolveMode::Other, | 789 | ResolveMode::Other, |
@@ -984,7 +976,9 @@ impl ModCollector<'_, '_> { | |||
984 | // to define builtin macros, so we support at least that part. | 976 | // to define builtin macros, so we support at least that part. |
985 | if mac.is_builtin { | 977 | if mac.is_builtin { |
986 | let krate = self.def_collector.def_map.krate; | 978 | let krate = self.def_collector.def_map.krate; |
987 | if let Some(macro_id) = find_builtin_macro(&mac.name, krate, ast_id) { | 979 | let macro_id = find_builtin_macro(&mac.name, krate, ast_id) |
980 | .or_else(|| find_builtin_derive(&mac.name, krate, ast_id)); | ||
981 | if let Some(macro_id) = macro_id { | ||
988 | let vis = self | 982 | let vis = self |
989 | .def_collector | 983 | .def_collector |
990 | .def_map | 984 | .def_map |
@@ -1326,7 +1320,7 @@ impl ModCollector<'_, '_> { | |||
1326 | // Case 2: normal `macro_rules!` macro | 1320 | // Case 2: normal `macro_rules!` macro |
1327 | let macro_id = MacroDefId { | 1321 | let macro_id = MacroDefId { |
1328 | ast_id: Some(ast_id), | 1322 | ast_id: Some(ast_id), |
1329 | krate: Some(self.def_collector.def_map.krate), | 1323 | krate: self.def_collector.def_map.krate, |
1330 | kind: MacroDefKind::Declarative, | 1324 | kind: MacroDefKind::Declarative, |
1331 | local_inner: mac.is_local_inner, | 1325 | local_inner: mac.is_local_inner, |
1332 | }; | 1326 | }; |
diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index 305fca0f9..6fe2ee78a 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs | |||
@@ -633,15 +633,44 @@ pub struct bar; | |||
633 | fn expand_derive() { | 633 | fn expand_derive() { |
634 | let map = compute_crate_def_map( | 634 | let map = compute_crate_def_map( |
635 | " | 635 | " |
636 | //- /main.rs | 636 | //- /main.rs crate:main deps:core |
637 | use core::*; | ||
638 | |||
637 | #[derive(Copy, Clone)] | 639 | #[derive(Copy, Clone)] |
638 | struct Foo; | 640 | struct Foo; |
641 | |||
642 | //- /core.rs crate:core | ||
643 | #[rustc_builtin_macro] | ||
644 | pub macro Copy {} | ||
645 | |||
646 | #[rustc_builtin_macro] | ||
647 | pub macro Clone {} | ||
639 | ", | 648 | ", |
640 | ); | 649 | ); |
641 | assert_eq!(map.modules[map.root].scope.impls().len(), 2); | 650 | assert_eq!(map.modules[map.root].scope.impls().len(), 2); |
642 | } | 651 | } |
643 | 652 | ||
644 | #[test] | 653 | #[test] |
654 | fn resolve_builtin_derive() { | ||
655 | check( | ||
656 | r#" | ||
657 | //- /main.rs crate:main deps:core | ||
658 | use core::*; | ||
659 | |||
660 | //- /core.rs crate:core | ||
661 | #[rustc_builtin_macro] | ||
662 | pub macro Clone {} | ||
663 | |||
664 | pub trait Clone {} | ||
665 | "#, | ||
666 | expect![[r#" | ||
667 | crate | ||
668 | Clone: t m | ||
669 | "#]], | ||
670 | ); | ||
671 | } | ||
672 | |||
673 | #[test] | ||
645 | fn macro_expansion_overflow() { | 674 | fn macro_expansion_overflow() { |
646 | mark::check!(macro_expansion_overflow); | 675 | mark::check!(macro_expansion_overflow); |
647 | check( | 676 | check( |