diff options
Diffstat (limited to 'crates/ra_hir_def/src/body')
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 151 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/scope.rs | 33 |
2 files changed, 133 insertions, 51 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index f159f80af..a7e2e0982 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -27,6 +27,7 @@ use crate::{ | |||
27 | LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, | 27 | LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, |
28 | }, | 28 | }, |
29 | item_scope::BuiltinShadowMode, | 29 | item_scope::BuiltinShadowMode, |
30 | item_tree::{ItemTree, ItemTreeId, ItemTreeNode}, | ||
30 | path::{GenericArgs, Path}, | 31 | path::{GenericArgs, Path}, |
31 | type_ref::{Mutability, Rawness, TypeRef}, | 32 | type_ref::{Mutability, Rawness, TypeRef}, |
32 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, | 33 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, |
@@ -35,6 +36,8 @@ use crate::{ | |||
35 | 36 | ||
36 | use super::{ExprSource, PatSource}; | 37 | use super::{ExprSource, PatSource}; |
37 | use ast::AstChildren; | 38 | use ast::AstChildren; |
39 | use rustc_hash::FxHashMap; | ||
40 | use std::{any::type_name, sync::Arc}; | ||
38 | 41 | ||
39 | pub(crate) struct LowerCtx { | 42 | pub(crate) struct LowerCtx { |
40 | hygiene: Hygiene, | 43 | hygiene: Hygiene, |
@@ -60,10 +63,10 @@ pub(super) fn lower( | |||
60 | params: Option<ast::ParamList>, | 63 | params: Option<ast::ParamList>, |
61 | body: Option<ast::Expr>, | 64 | body: Option<ast::Expr>, |
62 | ) -> (Body, BodySourceMap) { | 65 | ) -> (Body, BodySourceMap) { |
66 | let item_tree = db.item_tree(expander.current_file_id); | ||
63 | ExprCollector { | 67 | ExprCollector { |
64 | db, | 68 | db, |
65 | def, | 69 | def, |
66 | expander, | ||
67 | source_map: BodySourceMap::default(), | 70 | source_map: BodySourceMap::default(), |
68 | body: Body { | 71 | body: Body { |
69 | exprs: Arena::default(), | 72 | exprs: Arena::default(), |
@@ -72,6 +75,12 @@ pub(super) fn lower( | |||
72 | body_expr: dummy_expr_id(), | 75 | body_expr: dummy_expr_id(), |
73 | item_scope: Default::default(), | 76 | item_scope: Default::default(), |
74 | }, | 77 | }, |
78 | item_trees: { | ||
79 | let mut map = FxHashMap::default(); | ||
80 | map.insert(expander.current_file_id, item_tree); | ||
81 | map | ||
82 | }, | ||
83 | expander, | ||
75 | } | 84 | } |
76 | .collect(params, body) | 85 | .collect(params, body) |
77 | } | 86 | } |
@@ -82,6 +91,8 @@ struct ExprCollector<'a> { | |||
82 | expander: Expander, | 91 | expander: Expander, |
83 | body: Body, | 92 | body: Body, |
84 | source_map: BodySourceMap, | 93 | source_map: BodySourceMap, |
94 | |||
95 | item_trees: FxHashMap<HirFileId, Arc<ItemTree>>, | ||
85 | } | 96 | } |
86 | 97 | ||
87 | impl ExprCollector<'_> { | 98 | impl ExprCollector<'_> { |
@@ -533,6 +544,9 @@ impl ExprCollector<'_> { | |||
533 | self.source_map | 544 | self.source_map |
534 | .expansions | 545 | .expansions |
535 | .insert(macro_call, self.expander.current_file_id); | 546 | .insert(macro_call, self.expander.current_file_id); |
547 | |||
548 | let item_tree = self.db.item_tree(self.expander.current_file_id); | ||
549 | self.item_trees.insert(self.expander.current_file_id, item_tree); | ||
536 | let id = self.collect_expr(expansion); | 550 | let id = self.collect_expr(expansion); |
537 | self.expander.exit(self.db, mark); | 551 | self.expander.exit(self.db, mark); |
538 | id | 552 | id |
@@ -547,6 +561,32 @@ impl ExprCollector<'_> { | |||
547 | } | 561 | } |
548 | } | 562 | } |
549 | 563 | ||
564 | fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> Option<ItemTreeId<N>> { | ||
565 | let id = self.expander.ast_id(ast); | ||
566 | let tree = &self.item_trees[&id.file_id]; | ||
567 | |||
568 | // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes | ||
569 | |||
570 | // Root file (non-macro). | ||
571 | let item_tree_id = tree | ||
572 | .all_inner_items() | ||
573 | .chain(tree.top_level_items().iter().copied()) | ||
574 | .filter_map(|mod_item| mod_item.downcast::<N>()) | ||
575 | .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast()) | ||
576 | .or_else(|| { | ||
577 | log::debug!( | ||
578 | "couldn't find inner {} item for {:?} (AST: `{}` - {:?})", | ||
579 | type_name::<N>(), | ||
580 | id, | ||
581 | ast.syntax(), | ||
582 | ast.syntax(), | ||
583 | ); | ||
584 | None | ||
585 | })?; | ||
586 | |||
587 | Some(ItemTreeId::new(id.file_id, item_tree_id)) | ||
588 | } | ||
589 | |||
550 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | 590 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { |
551 | if let Some(expr) = expr { | 591 | if let Some(expr) = expr { |
552 | self.collect_expr(expr) | 592 | self.collect_expr(expr) |
@@ -578,56 +618,65 @@ impl ExprCollector<'_> { | |||
578 | 618 | ||
579 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { | 619 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { |
580 | let container = ContainerId::DefWithBodyId(self.def); | 620 | let container = ContainerId::DefWithBodyId(self.def); |
581 | for item in block.items() { | 621 | |
582 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { | 622 | let items = block |
583 | ast::ModuleItem::FnDef(def) => { | 623 | .items() |
584 | let ast_id = self.expander.ast_id(&def); | 624 | .filter_map(|item| { |
585 | ( | 625 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { |
586 | FunctionLoc { container: container.into(), ast_id }.intern(self.db).into(), | 626 | ast::ModuleItem::FnDef(def) => { |
587 | def.name(), | 627 | let id = self.find_inner_item(&def)?; |
588 | ) | 628 | ( |
589 | } | 629 | FunctionLoc { container: container.into(), id }.intern(self.db).into(), |
590 | ast::ModuleItem::TypeAliasDef(def) => { | 630 | def.name(), |
591 | let ast_id = self.expander.ast_id(&def); | 631 | ) |
592 | ( | 632 | } |
593 | TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into(), | 633 | ast::ModuleItem::TypeAliasDef(def) => { |
594 | def.name(), | 634 | let id = self.find_inner_item(&def)?; |
595 | ) | 635 | ( |
596 | } | 636 | TypeAliasLoc { container: container.into(), id }.intern(self.db).into(), |
597 | ast::ModuleItem::ConstDef(def) => { | 637 | def.name(), |
598 | let ast_id = self.expander.ast_id(&def); | 638 | ) |
599 | ( | 639 | } |
600 | ConstLoc { container: container.into(), ast_id }.intern(self.db).into(), | 640 | ast::ModuleItem::ConstDef(def) => { |
601 | def.name(), | 641 | let id = self.find_inner_item(&def)?; |
602 | ) | 642 | ( |
603 | } | 643 | ConstLoc { container: container.into(), id }.intern(self.db).into(), |
604 | ast::ModuleItem::StaticDef(def) => { | 644 | def.name(), |
605 | let ast_id = self.expander.ast_id(&def); | 645 | ) |
606 | (StaticLoc { container, ast_id }.intern(self.db).into(), def.name()) | 646 | } |
607 | } | 647 | ast::ModuleItem::StaticDef(def) => { |
608 | ast::ModuleItem::StructDef(def) => { | 648 | let id = self.find_inner_item(&def)?; |
609 | let ast_id = self.expander.ast_id(&def); | 649 | (StaticLoc { container, id }.intern(self.db).into(), def.name()) |
610 | (StructLoc { container, ast_id }.intern(self.db).into(), def.name()) | 650 | } |
611 | } | 651 | ast::ModuleItem::StructDef(def) => { |
612 | ast::ModuleItem::EnumDef(def) => { | 652 | let id = self.find_inner_item(&def)?; |
613 | let ast_id = self.expander.ast_id(&def); | 653 | (StructLoc { container, id }.intern(self.db).into(), def.name()) |
614 | (EnumLoc { container, ast_id }.intern(self.db).into(), def.name()) | 654 | } |
615 | } | 655 | ast::ModuleItem::EnumDef(def) => { |
616 | ast::ModuleItem::UnionDef(def) => { | 656 | let id = self.find_inner_item(&def)?; |
617 | let ast_id = self.expander.ast_id(&def); | 657 | (EnumLoc { container, id }.intern(self.db).into(), def.name()) |
618 | (UnionLoc { container, ast_id }.intern(self.db).into(), def.name()) | 658 | } |
619 | } | 659 | ast::ModuleItem::UnionDef(def) => { |
620 | ast::ModuleItem::TraitDef(def) => { | 660 | let id = self.find_inner_item(&def)?; |
621 | let ast_id = self.expander.ast_id(&def); | 661 | (UnionLoc { container, id }.intern(self.db).into(), def.name()) |
622 | (TraitLoc { container, ast_id }.intern(self.db).into(), def.name()) | 662 | } |
623 | } | 663 | ast::ModuleItem::TraitDef(def) => { |
624 | ast::ModuleItem::ExternBlock(_) => continue, // FIXME: collect from extern blocks | 664 | let id = self.find_inner_item(&def)?; |
625 | ast::ModuleItem::ImplDef(_) | 665 | (TraitLoc { container, id }.intern(self.db).into(), def.name()) |
626 | | ast::ModuleItem::UseItem(_) | 666 | } |
627 | | ast::ModuleItem::ExternCrateItem(_) | 667 | ast::ModuleItem::ExternBlock(_) => return None, // FIXME: collect from extern blocks |
628 | | ast::ModuleItem::Module(_) | 668 | ast::ModuleItem::ImplDef(_) |
629 | | ast::ModuleItem::MacroCall(_) => continue, | 669 | | ast::ModuleItem::UseItem(_) |
630 | }; | 670 | | ast::ModuleItem::ExternCrateItem(_) |
671 | | ast::ModuleItem::Module(_) | ||
672 | | ast::ModuleItem::MacroCall(_) => return None, | ||
673 | }; | ||
674 | |||
675 | Some((def, name)) | ||
676 | }) | ||
677 | .collect::<Vec<_>>(); | ||
678 | |||
679 | for (def, name) in items { | ||
631 | self.body.item_scope.define_def(def); | 680 | self.body.item_scope.define_def(def); |
632 | if let Some(name) = name { | 681 | if let Some(name) = name { |
633 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly | 682 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly |
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 0b74199d9..99e876683 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs | |||
@@ -317,6 +317,39 @@ fn foo() { | |||
317 | ); | 317 | ); |
318 | } | 318 | } |
319 | 319 | ||
320 | #[test] | ||
321 | fn macro_inner_item() { | ||
322 | do_check( | ||
323 | r" | ||
324 | macro_rules! mac { | ||
325 | () => {{ | ||
326 | fn inner() {} | ||
327 | inner(); | ||
328 | }}; | ||
329 | } | ||
330 | |||
331 | fn foo() { | ||
332 | mac!(); | ||
333 | <|> | ||
334 | } | ||
335 | ", | ||
336 | &[], | ||
337 | ); | ||
338 | } | ||
339 | |||
340 | #[test] | ||
341 | fn broken_inner_item() { | ||
342 | do_check( | ||
343 | r" | ||
344 | fn foo() { | ||
345 | trait {} | ||
346 | <|> | ||
347 | } | ||
348 | ", | ||
349 | &[], | ||
350 | ); | ||
351 | } | ||
352 | |||
320 | fn do_check_local_name(ra_fixture: &str, expected_offset: u32) { | 353 | fn do_check_local_name(ra_fixture: &str, expected_offset: u32) { |
321 | let (db, position) = TestDB::with_position(ra_fixture); | 354 | let (db, position) = TestDB::with_position(ra_fixture); |
322 | let file_id = position.file_id; | 355 | let file_id = position.file_id; |