1
1
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2
2
//! representation.
3
3
4
- use std:: { any :: type_name , mem, sync:: Arc } ;
4
+ use std:: { mem, sync:: Arc } ;
5
5
6
6
use either:: Either ;
7
7
use hir_expand:: {
8
8
hygiene:: Hygiene ,
9
9
name:: { name, AsName , Name } ,
10
- ExpandError , HirFileId , MacroDefId , MacroDefKind ,
10
+ ExpandError , HirFileId ,
11
11
} ;
12
12
use la_arena:: Arena ;
13
13
use profile:: Count ;
@@ -32,11 +32,10 @@ use crate::{
32
32
Statement ,
33
33
} ,
34
34
item_scope:: BuiltinShadowMode ,
35
- item_tree:: { ItemTree , ItemTreeId , ItemTreeNode } ,
35
+ item_tree:: ItemTree ,
36
36
path:: { GenericArgs , Path } ,
37
37
type_ref:: { Mutability , Rawness , TypeRef } ,
38
- AdtId , BlockLoc , ConstLoc , ContainerId , DefWithBodyId , EnumLoc , FunctionLoc , Intern ,
39
- ModuleDefId , StaticLoc , StructLoc , TraitLoc , TypeAliasLoc , UnionLoc ,
38
+ AdtId , BlockLoc , ModuleDefId ,
40
39
} ;
41
40
42
41
use super :: { diagnostics:: BodyDiagnostic , ExprSource , PatSource } ;
@@ -60,15 +59,13 @@ impl LowerCtx {
60
59
61
60
pub ( super ) fn lower (
62
61
db : & dyn DefDatabase ,
63
- def : DefWithBodyId ,
64
62
expander : Expander ,
65
63
params : Option < ast:: ParamList > ,
66
64
body : Option < ast:: Expr > ,
67
65
) -> ( Body , BodySourceMap ) {
68
66
let item_tree = db. item_tree ( expander. current_file_id ) ;
69
67
ExprCollector {
70
68
db,
71
- def,
72
69
source_map : BodySourceMap :: default ( ) ,
73
70
body : Body {
74
71
exprs : Arena :: default ( ) ,
@@ -77,7 +74,6 @@ pub(super) fn lower(
77
74
params : Vec :: new ( ) ,
78
75
body_expr : dummy_expr_id ( ) ,
79
76
block_scopes : Vec :: new ( ) ,
80
- item_scope : Default :: default ( ) ,
81
77
_c : Count :: new ( ) ,
82
78
} ,
83
79
item_trees : {
@@ -92,7 +88,6 @@ pub(super) fn lower(
92
88
93
89
struct ExprCollector < ' a > {
94
90
db : & ' a dyn DefDatabase ,
95
- def : DefWithBodyId ,
96
91
expander : Expander ,
97
92
body : Body ,
98
93
source_map : BodySourceMap ,
@@ -606,32 +601,6 @@ impl ExprCollector<'_> {
606
601
}
607
602
}
608
603
609
- fn find_inner_item < N : ItemTreeNode > ( & self , ast : & N :: Source ) -> Option < ItemTreeId < N > > {
610
- let id = self . expander . ast_id ( ast) ;
611
- let tree = & self . item_trees [ & id. file_id ] ;
612
-
613
- // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes
614
-
615
- // Root file (non-macro).
616
- let item_tree_id = tree
617
- . all_inner_items ( )
618
- . chain ( tree. top_level_items ( ) . iter ( ) . copied ( ) )
619
- . filter_map ( |mod_item| mod_item. downcast :: < N > ( ) )
620
- . find ( |tree_id| tree[ * tree_id] . ast_id ( ) . upcast ( ) == id. value . upcast ( ) )
621
- . or_else ( || {
622
- log:: debug!(
623
- "couldn't find inner {} item for {:?} (AST: `{}` - {:?})" ,
624
- type_name:: <N >( ) ,
625
- id,
626
- ast. syntax( ) ,
627
- ast. syntax( ) ,
628
- ) ;
629
- None
630
- } ) ?;
631
-
632
- Some ( ItemTreeId :: new ( id. file_id , item_tree_id) )
633
- }
634
-
635
604
fn collect_expr_opt ( & mut self , expr : Option < ast:: Expr > ) -> ExprId {
636
605
if let Some ( expr) = expr {
637
606
self . collect_expr ( expr)
@@ -663,7 +632,6 @@ impl ExprCollector<'_> {
663
632
match expansion {
664
633
Some ( expansion) => {
665
634
let statements: ast:: MacroStmts = expansion;
666
- this. collect_stmts_items ( statements. statements ( ) ) ;
667
635
668
636
statements. statements ( ) . for_each ( |stmt| {
669
637
if let Some ( mut r) = this. collect_stmt ( stmt) {
@@ -710,7 +678,6 @@ impl ExprCollector<'_> {
710
678
let prev_def_map = mem:: replace ( & mut self . expander . def_map , def_map) ;
711
679
let prev_local_module = mem:: replace ( & mut self . expander . module , module) ;
712
680
713
- self . collect_stmts_items ( block. statements ( ) ) ;
714
681
let statements =
715
682
block. statements ( ) . filter_map ( |s| self . collect_stmt ( s) ) . flatten ( ) . collect ( ) ;
716
683
let tail = block. tail_expr ( ) . map ( |e| self . collect_expr ( e) ) ;
@@ -725,108 +692,6 @@ impl ExprCollector<'_> {
725
692
expr_id
726
693
}
727
694
728
- fn collect_stmts_items ( & mut self , stmts : ast:: AstChildren < ast:: Stmt > ) {
729
- let container = ContainerId :: DefWithBodyId ( self . def ) ;
730
-
731
- let items = stmts
732
- . filter_map ( |stmt| match stmt {
733
- ast:: Stmt :: Item ( it) => Some ( it) ,
734
- ast:: Stmt :: LetStmt ( _) | ast:: Stmt :: ExprStmt ( _) => None ,
735
- } )
736
- . filter_map ( |item| {
737
- let ( def, name) : ( ModuleDefId , Option < ast:: Name > ) = match item {
738
- ast:: Item :: Fn ( def) => {
739
- let id = self . find_inner_item ( & def) ?;
740
- (
741
- FunctionLoc { container : container. into ( ) , id } . intern ( self . db ) . into ( ) ,
742
- def. name ( ) ,
743
- )
744
- }
745
- ast:: Item :: TypeAlias ( def) => {
746
- let id = self . find_inner_item ( & def) ?;
747
- (
748
- TypeAliasLoc { container : container. into ( ) , id } . intern ( self . db ) . into ( ) ,
749
- def. name ( ) ,
750
- )
751
- }
752
- ast:: Item :: Const ( def) => {
753
- let id = self . find_inner_item ( & def) ?;
754
- (
755
- ConstLoc { container : container. into ( ) , id } . intern ( self . db ) . into ( ) ,
756
- def. name ( ) ,
757
- )
758
- }
759
- ast:: Item :: Static ( def) => {
760
- let id = self . find_inner_item ( & def) ?;
761
- ( StaticLoc { container, id } . intern ( self . db ) . into ( ) , def. name ( ) )
762
- }
763
- ast:: Item :: Struct ( def) => {
764
- let id = self . find_inner_item ( & def) ?;
765
- ( StructLoc { container, id } . intern ( self . db ) . into ( ) , def. name ( ) )
766
- }
767
- ast:: Item :: Enum ( def) => {
768
- let id = self . find_inner_item ( & def) ?;
769
- ( EnumLoc { container, id } . intern ( self . db ) . into ( ) , def. name ( ) )
770
- }
771
- ast:: Item :: Union ( def) => {
772
- let id = self . find_inner_item ( & def) ?;
773
- ( UnionLoc { container, id } . intern ( self . db ) . into ( ) , def. name ( ) )
774
- }
775
- ast:: Item :: Trait ( def) => {
776
- let id = self . find_inner_item ( & def) ?;
777
- ( TraitLoc { container, id } . intern ( self . db ) . into ( ) , def. name ( ) )
778
- }
779
- ast:: Item :: ExternBlock ( _) => return None , // FIXME: collect from extern blocks
780
- ast:: Item :: Impl ( _)
781
- | ast:: Item :: Use ( _)
782
- | ast:: Item :: ExternCrate ( _)
783
- | ast:: Item :: Module ( _)
784
- | ast:: Item :: MacroCall ( _) => return None ,
785
- ast:: Item :: MacroRules ( def) => {
786
- return Some ( Either :: Right ( ast:: Macro :: from ( def) ) ) ;
787
- }
788
- ast:: Item :: MacroDef ( def) => {
789
- return Some ( Either :: Right ( ast:: Macro :: from ( def) ) ) ;
790
- }
791
- } ;
792
-
793
- Some ( Either :: Left ( ( def, name) ) )
794
- } )
795
- . collect :: < Vec < _ > > ( ) ;
796
-
797
- for either in items {
798
- match either {
799
- Either :: Left ( ( def, name) ) => {
800
- self . body . item_scope . define_def ( def) ;
801
- if let Some ( name) = name {
802
- let vis = crate :: visibility:: Visibility :: Public ; // FIXME determine correctly
803
- let has_constructor = match def {
804
- ModuleDefId :: AdtId ( AdtId :: StructId ( s) ) => {
805
- self . db . struct_data ( s) . variant_data . kind ( ) != StructKind :: Record
806
- }
807
- _ => true ,
808
- } ;
809
- self . body . item_scope . push_res (
810
- name. as_name ( ) ,
811
- crate :: per_ns:: PerNs :: from_def ( def, vis, has_constructor) ,
812
- ) ;
813
- }
814
- }
815
- Either :: Right ( e) => {
816
- let mac = MacroDefId {
817
- krate : self . expander . def_map . krate ( ) ,
818
- ast_id : Some ( self . expander . ast_id ( & e) ) ,
819
- kind : MacroDefKind :: Declarative ,
820
- local_inner : false ,
821
- } ;
822
- if let Some ( name) = e. name ( ) {
823
- self . body . item_scope . define_legacy_macro ( name. as_name ( ) , mac) ;
824
- }
825
- }
826
- }
827
- }
828
- }
829
-
830
695
fn collect_block_opt ( & mut self , expr : Option < ast:: BlockExpr > ) -> ExprId {
831
696
if let Some ( block) = expr {
832
697
self . collect_block ( block)
0 commit comments