aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-26 16:30:27 +0100
committerJonas Schievink <[email protected]>2020-06-26 16:30:27 +0100
commit5d7172f17e5b39737dc8d692112cf969926b7f05 (patch)
tree33fa4a9946ee4700884cc129817851344df7f97e /crates
parent4f60b4f2a32190830215defe6cf81836f34d99c9 (diff)
Simplify inner item lowering
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs91
1 files changed, 30 insertions, 61 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 3ced648e5..2e433528d 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -5,7 +5,7 @@ use either::Either;
5use hir_expand::{ 5use hir_expand::{
6 hygiene::Hygiene, 6 hygiene::Hygiene,
7 name::{name, AsName, Name}, 7 name::{name, AsName, Name},
8 AstId, HirFileId, MacroDefId, MacroDefKind, 8 HirFileId, MacroDefId, MacroDefKind,
9}; 9};
10use ra_arena::Arena; 10use ra_arena::Arena;
11use ra_syntax::{ 11use ra_syntax::{
@@ -27,7 +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::{FileItemTreeId, ItemTree, ItemTreeNode}, 30 item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
31 path::{GenericArgs, Path}, 31 path::{GenericArgs, Path},
32 type_ref::{Mutability, Rawness, TypeRef}, 32 type_ref::{Mutability, Rawness, TypeRef},
33 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, 33 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
@@ -37,7 +37,7 @@ use crate::{
37use super::{ExprSource, PatSource}; 37use super::{ExprSource, PatSource};
38use ast::AstChildren; 38use ast::AstChildren;
39use rustc_hash::FxHashMap; 39use rustc_hash::FxHashMap;
40use std::sync::Arc; 40use std::{any::type_name, sync::Arc};
41 41
42pub(crate) struct LowerCtx { 42pub(crate) struct LowerCtx {
43 hygiene: Hygiene, 43 hygiene: Hygiene,
@@ -561,17 +561,23 @@ impl ExprCollector<'_> {
561 } 561 }
562 } 562 }
563 563
564 fn find_inner_item<S: ItemTreeNode>(&self, id: AstId<ast::ModuleItem>) -> FileItemTreeId<S> { 564 fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> ItemTreeId<N> {
565 let id = self.expander.ast_id(ast);
565 let tree = &self.item_trees[&id.file_id]; 566 let tree = &self.item_trees[&id.file_id];
566 567
567 // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes 568 // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes
568 569
569 // Root file (non-macro). 570 // Root file (non-macro).
570 tree.all_inner_items() 571 let item_tree_id = tree
572 .all_inner_items()
571 .chain(tree.top_level_items().iter().copied()) 573 .chain(tree.top_level_items().iter().copied())
572 .filter_map(|mod_item| mod_item.downcast::<S>()) 574 .filter_map(|mod_item| mod_item.downcast::<N>())
573 .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value) 575 .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast())
574 .unwrap_or_else(|| panic!("couldn't find inner item for {:?}", id)) 576 .unwrap_or_else(|| {
577 panic!("couldn't find inner {} item for {:?}", type_name::<N>(), id)
578 });
579
580 ItemTreeId::new(id.file_id, item_tree_id)
575 } 581 }
576 582
577 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { 583 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
@@ -611,82 +617,45 @@ impl ExprCollector<'_> {
611 .filter_map(|item| { 617 .filter_map(|item| {
612 let (def, name): (ModuleDefId, Option<ast::Name>) = match item { 618 let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
613 ast::ModuleItem::FnDef(def) => { 619 ast::ModuleItem::FnDef(def) => {
614 let ast_id = self.expander.ast_id(&def); 620 let id = self.find_inner_item(&def);
615 let id = self.find_inner_item(ast_id.map(|id| id.upcast()));
616 ( 621 (
617 FunctionLoc { container: container.into(), id: ast_id.with_value(id) } 622 FunctionLoc { container: container.into(), id }.intern(self.db).into(),
618 .intern(self.db)
619 .into(),
620 def.name(), 623 def.name(),
621 ) 624 )
622 } 625 }
623 ast::ModuleItem::TypeAliasDef(def) => { 626 ast::ModuleItem::TypeAliasDef(def) => {
624 let ast_id = self.expander.ast_id(&def); 627 let id = self.find_inner_item(&def);
625 let id = self.find_inner_item(ast_id.map(|id| id.upcast()));
626 ( 628 (
627 TypeAliasLoc { container: container.into(), id: ast_id.with_value(id) } 629 TypeAliasLoc { container: container.into(), id }.intern(self.db).into(),
628 .intern(self.db)
629 .into(),
630 def.name(), 630 def.name(),
631 ) 631 )
632 } 632 }
633 ast::ModuleItem::ConstDef(def) => { 633 ast::ModuleItem::ConstDef(def) => {
634 let ast_id = self.expander.ast_id(&def); 634 let id = self.find_inner_item(&def);
635 let id = self.find_inner_item(ast_id.map(|id| id.upcast()));
636 ( 635 (
637 ConstLoc { container: container.into(), id: ast_id.with_value(id) } 636 ConstLoc { container: container.into(), id }.intern(self.db).into(),
638 .intern(self.db)
639 .into(),
640 def.name(), 637 def.name(),
641 ) 638 )
642 } 639 }
643 ast::ModuleItem::StaticDef(def) => { 640 ast::ModuleItem::StaticDef(def) => {
644 let ast_id = self.expander.ast_id(&def); 641 let id = self.find_inner_item(&def);
645 let id = self.find_inner_item(ast_id.map(|id| id.upcast())); 642 (StaticLoc { container, id }.intern(self.db).into(), def.name())
646 (
647 StaticLoc { container, id: ast_id.with_value(id) }
648 .intern(self.db)
649 .into(),
650 def.name(),
651 )
652 } 643 }
653 ast::ModuleItem::StructDef(def) => { 644 ast::ModuleItem::StructDef(def) => {
654 let ast_id = self.expander.ast_id(&def); 645 let id = self.find_inner_item(&def);
655 let id = self.find_inner_item(ast_id.map(|id| id.upcast())); 646 (StructLoc { container, id }.intern(self.db).into(), def.name())
656 (
657 StructLoc { container, id: ast_id.with_value(id) }
658 .intern(self.db)
659 .into(),
660 def.name(),
661 )
662 } 647 }
663 ast::ModuleItem::EnumDef(def) => { 648 ast::ModuleItem::EnumDef(def) => {
664 let ast_id = self.expander.ast_id(&def); 649 let id = self.find_inner_item(&def);
665 let id = self.find_inner_item(ast_id.map(|id| id.upcast())); 650 (EnumLoc { container, id }.intern(self.db).into(), def.name())
666 (
667 EnumLoc { container, id: ast_id.with_value(id) }.intern(self.db).into(),
668 def.name(),
669 )
670 } 651 }
671 ast::ModuleItem::UnionDef(def) => { 652 ast::ModuleItem::UnionDef(def) => {
672 let ast_id = self.expander.ast_id(&def); 653 let id = self.find_inner_item(&def);
673 let id = self.find_inner_item(ast_id.map(|id| id.upcast())); 654 (UnionLoc { container, id }.intern(self.db).into(), def.name())
674 (
675 UnionLoc { container, id: ast_id.with_value(id) }
676 .intern(self.db)
677 .into(),
678 def.name(),
679 )
680 } 655 }
681 ast::ModuleItem::TraitDef(def) => { 656 ast::ModuleItem::TraitDef(def) => {
682 let ast_id = self.expander.ast_id(&def); 657 let id = self.find_inner_item(&def);
683 let id = self.find_inner_item(ast_id.map(|id| id.upcast())); 658 (TraitLoc { container, id }.intern(self.db).into(), def.name())
684 (
685 TraitLoc { container, id: ast_id.with_value(id) }
686 .intern(self.db)
687 .into(),
688 def.name(),
689 )
690 } 659 }
691 ast::ModuleItem::ExternBlock(_) => return None, // FIXME: collect from extern blocks 660 ast::ModuleItem::ExternBlock(_) => return None, // FIXME: collect from extern blocks
692 ast::ModuleItem::ImplDef(_) 661 ast::ModuleItem::ImplDef(_)