44use std:: mem;
55
66use base_db:: CrateId ;
7- use either:: Either ;
87use hir_expand:: {
98 name:: { name, AsName , Name } ,
109 ExpandError , InFile ,
@@ -29,7 +28,6 @@ use crate::{
2928 db:: DefDatabase ,
3029 expander:: Expander ,
3130 hir:: {
32- dummy_expr_id,
3331 format_args:: {
3432 self , FormatAlignment , FormatArgs , FormatArgsPiece , FormatArgument , FormatArgumentKind ,
3533 FormatArgumentsCollector , FormatCount , FormatDebugHex , FormatOptions ,
@@ -66,16 +64,7 @@ pub(super) fn lower(
6664 def_map : expander. module . def_map ( db) ,
6765 source_map : BodySourceMap :: default ( ) ,
6866 ast_id_map : db. ast_id_map ( expander. current_file_id ( ) ) ,
69- body : Body {
70- exprs : Default :: default ( ) ,
71- pats : Default :: default ( ) ,
72- bindings : Default :: default ( ) ,
73- binding_owners : Default :: default ( ) ,
74- labels : Default :: default ( ) ,
75- params : Vec :: new ( ) ,
76- body_expr : dummy_expr_id ( ) ,
77- block_scopes : Vec :: new ( ) ,
78- } ,
67+ body : Body :: default ( ) ,
7968 expander,
8069 current_try_block_label : None ,
8170 is_lowering_assignee_expr : false ,
@@ -191,35 +180,35 @@ impl ExprCollector<'_> {
191180 is_async_fn : bool ,
192181 ) -> ( Body , BodySourceMap ) {
193182 if let Some ( ( param_list, mut attr_enabled) ) = param_list {
183+ let mut params = vec ! [ ] ;
194184 if let Some ( self_param) =
195185 param_list. self_param ( ) . filter ( |_| attr_enabled. next ( ) . unwrap_or ( false ) )
196186 {
197187 let is_mutable =
198188 self_param. mut_token ( ) . is_some ( ) && self_param. amp_token ( ) . is_none ( ) ;
199- let ptr = AstPtr :: new ( & Either :: Right ( self_param) ) ;
200189 let binding_id: la_arena:: Idx < Binding > =
201190 self . alloc_binding ( name ! [ self ] , BindingAnnotation :: new ( is_mutable, false ) ) ;
202- let param_pat = self . alloc_pat ( Pat :: Bind { id : binding_id, subpat : None } , ptr) ;
203- self . add_definition_to_binding ( binding_id, param_pat) ;
204- self . body . params . push ( param_pat) ;
191+ self . body . self_param = Some ( binding_id) ;
192+ self . source_map . self_param = Some ( self . expander . in_file ( AstPtr :: new ( & self_param) ) ) ;
205193 }
206194
207195 for ( param, _) in param_list. params ( ) . zip ( attr_enabled) . filter ( |( _, enabled) | * enabled)
208196 {
209197 let param_pat = self . collect_pat_top ( param. pat ( ) ) ;
210- self . body . params . push ( param_pat) ;
198+ params. push ( param_pat) ;
211199 }
200+ self . body . params = params. into_boxed_slice ( ) ;
212201 } ;
213202 self . body . body_expr = self . with_label_rib ( RibKind :: Closure , |this| {
214203 if is_async_fn {
215204 match body {
216205 Some ( e) => {
206+ let syntax_ptr = AstPtr :: new ( & e) ;
217207 let expr = this. collect_expr ( e) ;
218- this. alloc_expr_desugared ( Expr :: Async {
219- id : None ,
220- statements : Box :: new ( [ ] ) ,
221- tail : Some ( expr) ,
222- } )
208+ this. alloc_expr_desugared_with_ptr (
209+ Expr :: Async { id : None , statements : Box :: new ( [ ] ) , tail : Some ( expr) } ,
210+ syntax_ptr,
211+ )
223212 }
224213 None => this. missing_expr ( ) ,
225214 }
@@ -405,7 +394,7 @@ impl ExprCollector<'_> {
405394 }
406395 ast:: Expr :: ParenExpr ( e) => {
407396 let inner = self . collect_expr_opt ( e. expr ( ) ) ;
408- // make the paren expr point to the inner expression as well
397+ // make the paren expr point to the inner expression as well for IDE resolution
409398 let src = self . expander . in_file ( syntax_ptr) ;
410399 self . source_map . expr_map . insert ( src, inner) ;
411400 inner
@@ -707,6 +696,7 @@ impl ExprCollector<'_> {
707696 . alloc_label_desugared ( Label { name : Name :: generate_new_name ( self . body . labels . len ( ) ) } ) ;
708697 let old_label = self . current_try_block_label . replace ( label) ;
709698
699+ let ptr = AstPtr :: new ( & e) . upcast ( ) ;
710700 let ( btail, expr_id) = self . with_labeled_rib ( label, |this| {
711701 let mut btail = None ;
712702 let block = this. collect_block_ ( e, |id, statements, tail| {
@@ -716,23 +706,21 @@ impl ExprCollector<'_> {
716706 ( btail, block)
717707 } ) ;
718708
719- let callee = self . alloc_expr_desugared ( Expr :: Path ( try_from_output) ) ;
709+ let callee = self . alloc_expr_desugared_with_ptr ( Expr :: Path ( try_from_output) , ptr ) ;
720710 let next_tail = match btail {
721- Some ( tail) => self . alloc_expr_desugared ( Expr :: Call {
722- callee,
723- args : Box :: new ( [ tail] ) ,
724- is_assignee_expr : false ,
725- } ) ,
711+ Some ( tail) => self . alloc_expr_desugared_with_ptr (
712+ Expr :: Call { callee, args : Box :: new ( [ tail] ) , is_assignee_expr : false } ,
713+ ptr,
714+ ) ,
726715 None => {
727- let unit = self . alloc_expr_desugared ( Expr :: Tuple {
728- exprs : Box :: new ( [ ] ) ,
729- is_assignee_expr : false ,
730- } ) ;
731- self . alloc_expr_desugared ( Expr :: Call {
732- callee,
733- args : Box :: new ( [ unit] ) ,
734- is_assignee_expr : false ,
735- } )
716+ let unit = self . alloc_expr_desugared_with_ptr (
717+ Expr :: Tuple { exprs : Box :: new ( [ ] ) , is_assignee_expr : false } ,
718+ ptr,
719+ ) ;
720+ self . alloc_expr_desugared_with_ptr (
721+ Expr :: Call { callee, args : Box :: new ( [ unit] ) , is_assignee_expr : false } ,
722+ ptr,
723+ )
736724 }
737725 } ;
738726 let Expr :: Block { tail, .. } = & mut self . body . exprs [ expr_id] else {
@@ -1067,16 +1055,12 @@ impl ExprCollector<'_> {
10671055 None => None ,
10681056 } ,
10691057 ) ;
1070- match expansion {
1071- Some ( tail) => {
1072- // Make the macro-call point to its expanded expression so we can query
1073- // semantics on syntax pointers to the macro
1074- let src = self . expander . in_file ( syntax_ptr) ;
1075- self . source_map . expr_map . insert ( src, tail) ;
1076- Some ( tail)
1077- }
1078- None => None ,
1079- }
1058+ expansion. inspect ( |& tail| {
1059+ // Make the macro-call point to its expanded expression so we can query
1060+ // semantics on syntax pointers to the macro
1061+ let src = self . expander . in_file ( syntax_ptr) ;
1062+ self . source_map . expr_map . insert ( src, tail) ;
1063+ } )
10801064 }
10811065
10821066 fn collect_stmt ( & mut self , statements : & mut Vec < Statement > , s : ast:: Stmt ) {
@@ -1261,7 +1245,7 @@ impl ExprCollector<'_> {
12611245 ( Some ( id) , Pat :: Bind { id, subpat } )
12621246 } ;
12631247
1264- let ptr = AstPtr :: new ( & Either :: Left ( pat) ) ;
1248+ let ptr = AstPtr :: new ( & pat) ;
12651249 let pat = self . alloc_pat ( pattern, ptr) ;
12661250 if let Some ( binding_id) = binding {
12671251 self . add_definition_to_binding ( binding_id, pat) ;
@@ -1359,9 +1343,10 @@ impl ExprCollector<'_> {
13591343 suffix : suffix. into_iter ( ) . map ( |p| self . collect_pat ( p, binding_list) ) . collect ( ) ,
13601344 }
13611345 }
1362- #[ rustfmt:: skip] // https://github.com/rust-lang/rustfmt/issues/5676
13631346 ast:: Pat :: LiteralPat ( lit) => ' b: {
1364- let Some ( ( hir_lit, ast_lit) ) = pat_literal_to_hir ( lit) else { break ' b Pat :: Missing } ;
1347+ let Some ( ( hir_lit, ast_lit) ) = pat_literal_to_hir ( lit) else {
1348+ break ' b Pat :: Missing ;
1349+ } ;
13651350 let expr = Expr :: Literal ( hir_lit) ;
13661351 let expr_ptr = AstPtr :: new ( & ast:: Expr :: Literal ( ast_lit) ) ;
13671352 let expr_id = self . alloc_expr ( expr, expr_ptr) ;
@@ -1397,7 +1382,7 @@ impl ExprCollector<'_> {
13971382 ast:: Pat :: MacroPat ( mac) => match mac. macro_call ( ) {
13981383 Some ( call) => {
13991384 let macro_ptr = AstPtr :: new ( & call) ;
1400- let src = self . expander . in_file ( AstPtr :: new ( & Either :: Left ( pat) ) ) ;
1385+ let src = self . expander . in_file ( AstPtr :: new ( & pat) ) ;
14011386 let pat =
14021387 self . collect_macro_call ( call, macro_ptr, true , |this, expanded_pat| {
14031388 this. collect_pat_opt ( expanded_pat, binding_list)
@@ -1426,7 +1411,7 @@ impl ExprCollector<'_> {
14261411 Pat :: Range { start, end }
14271412 }
14281413 } ;
1429- let ptr = AstPtr :: new ( & Either :: Left ( pat) ) ;
1414+ let ptr = AstPtr :: new ( & pat) ;
14301415 self . alloc_pat ( pattern, ptr)
14311416 }
14321417
@@ -1987,10 +1972,19 @@ impl ExprCollector<'_> {
19871972 self . source_map . expr_map . insert ( src, id) ;
19881973 id
19891974 }
1990- // FIXME: desugared exprs don't have ptr, that's wrong and should be fixed somehow.
1975+ // FIXME: desugared exprs don't have ptr, that's wrong and should be fixed.
1976+ // Migrate to alloc_expr_desugared_with_ptr and then rename back
19911977 fn alloc_expr_desugared ( & mut self , expr : Expr ) -> ExprId {
19921978 self . body . exprs . alloc ( expr)
19931979 }
1980+ fn alloc_expr_desugared_with_ptr ( & mut self , expr : Expr , ptr : ExprPtr ) -> ExprId {
1981+ let src = self . expander . in_file ( ptr) ;
1982+ let id = self . body . exprs . alloc ( expr) ;
1983+ self . source_map . expr_map_back . insert ( id, src) ;
1984+ // We intentionally don't fill this as it could overwrite a non-desugared entry
1985+ // self.source_map.expr_map.insert(src, id);
1986+ id
1987+ }
19941988 fn missing_expr ( & mut self ) -> ExprId {
19951989 self . alloc_expr_desugared ( Expr :: Missing )
19961990 }
0 commit comments