4
4
use std:: mem;
5
5
6
6
use base_db:: CrateId ;
7
- use either:: Either ;
8
7
use hir_expand:: {
9
8
name:: { name, AsName , Name } ,
10
9
ExpandError , InFile ,
@@ -29,7 +28,6 @@ use crate::{
29
28
db:: DefDatabase ,
30
29
expander:: Expander ,
31
30
hir:: {
32
- dummy_expr_id,
33
31
format_args:: {
34
32
self , FormatAlignment , FormatArgs , FormatArgsPiece , FormatArgument , FormatArgumentKind ,
35
33
FormatArgumentsCollector , FormatCount , FormatDebugHex , FormatOptions ,
@@ -66,16 +64,7 @@ pub(super) fn lower(
66
64
def_map : expander. module . def_map ( db) ,
67
65
source_map : BodySourceMap :: default ( ) ,
68
66
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 ( ) ,
79
68
expander,
80
69
current_try_block_label : None ,
81
70
is_lowering_assignee_expr : false ,
@@ -191,35 +180,35 @@ impl ExprCollector<'_> {
191
180
is_async_fn : bool ,
192
181
) -> ( Body , BodySourceMap ) {
193
182
if let Some ( ( param_list, mut attr_enabled) ) = param_list {
183
+ let mut params = vec ! [ ] ;
194
184
if let Some ( self_param) =
195
185
param_list. self_param ( ) . filter ( |_| attr_enabled. next ( ) . unwrap_or ( false ) )
196
186
{
197
187
let is_mutable =
198
188
self_param. mut_token ( ) . is_some ( ) && self_param. amp_token ( ) . is_none ( ) ;
199
- let ptr = AstPtr :: new ( & Either :: Right ( self_param) ) ;
200
189
let binding_id: la_arena:: Idx < Binding > =
201
190
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) ) ) ;
205
193
}
206
194
207
195
for ( param, _) in param_list. params ( ) . zip ( attr_enabled) . filter ( |( _, enabled) | * enabled)
208
196
{
209
197
let param_pat = self . collect_pat_top ( param. pat ( ) ) ;
210
- self . body . params . push ( param_pat) ;
198
+ params. push ( param_pat) ;
211
199
}
200
+ self . body . params = params. into_boxed_slice ( ) ;
212
201
} ;
213
202
self . body . body_expr = self . with_label_rib ( RibKind :: Closure , |this| {
214
203
if is_async_fn {
215
204
match body {
216
205
Some ( e) => {
206
+ let syntax_ptr = AstPtr :: new ( & e) ;
217
207
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
+ )
223
212
}
224
213
None => this. missing_expr ( ) ,
225
214
}
@@ -405,7 +394,7 @@ impl ExprCollector<'_> {
405
394
}
406
395
ast:: Expr :: ParenExpr ( e) => {
407
396
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
409
398
let src = self . expander . in_file ( syntax_ptr) ;
410
399
self . source_map . expr_map . insert ( src, inner) ;
411
400
inner
@@ -707,6 +696,7 @@ impl ExprCollector<'_> {
707
696
. alloc_label_desugared ( Label { name : Name :: generate_new_name ( self . body . labels . len ( ) ) } ) ;
708
697
let old_label = self . current_try_block_label . replace ( label) ;
709
698
699
+ let ptr = AstPtr :: new ( & e) . upcast ( ) ;
710
700
let ( btail, expr_id) = self . with_labeled_rib ( label, |this| {
711
701
let mut btail = None ;
712
702
let block = this. collect_block_ ( e, |id, statements, tail| {
@@ -716,23 +706,21 @@ impl ExprCollector<'_> {
716
706
( btail, block)
717
707
} ) ;
718
708
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 ) ;
720
710
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
+ ) ,
726
715
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
+ )
736
724
}
737
725
} ;
738
726
let Expr :: Block { tail, .. } = & mut self . body . exprs [ expr_id] else {
@@ -1067,16 +1055,12 @@ impl ExprCollector<'_> {
1067
1055
None => None ,
1068
1056
} ,
1069
1057
) ;
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
+ } )
1080
1064
}
1081
1065
1082
1066
fn collect_stmt ( & mut self , statements : & mut Vec < Statement > , s : ast:: Stmt ) {
@@ -1261,7 +1245,7 @@ impl ExprCollector<'_> {
1261
1245
( Some ( id) , Pat :: Bind { id, subpat } )
1262
1246
} ;
1263
1247
1264
- let ptr = AstPtr :: new ( & Either :: Left ( pat) ) ;
1248
+ let ptr = AstPtr :: new ( & pat) ;
1265
1249
let pat = self . alloc_pat ( pattern, ptr) ;
1266
1250
if let Some ( binding_id) = binding {
1267
1251
self . add_definition_to_binding ( binding_id, pat) ;
@@ -1359,9 +1343,10 @@ impl ExprCollector<'_> {
1359
1343
suffix : suffix. into_iter ( ) . map ( |p| self . collect_pat ( p, binding_list) ) . collect ( ) ,
1360
1344
}
1361
1345
}
1362
- #[ rustfmt:: skip] // https://github.com/rust-lang/rustfmt/issues/5676
1363
1346
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
+ } ;
1365
1350
let expr = Expr :: Literal ( hir_lit) ;
1366
1351
let expr_ptr = AstPtr :: new ( & ast:: Expr :: Literal ( ast_lit) ) ;
1367
1352
let expr_id = self . alloc_expr ( expr, expr_ptr) ;
@@ -1397,7 +1382,7 @@ impl ExprCollector<'_> {
1397
1382
ast:: Pat :: MacroPat ( mac) => match mac. macro_call ( ) {
1398
1383
Some ( call) => {
1399
1384
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) ) ;
1401
1386
let pat =
1402
1387
self . collect_macro_call ( call, macro_ptr, true , |this, expanded_pat| {
1403
1388
this. collect_pat_opt ( expanded_pat, binding_list)
@@ -1426,7 +1411,7 @@ impl ExprCollector<'_> {
1426
1411
Pat :: Range { start, end }
1427
1412
}
1428
1413
} ;
1429
- let ptr = AstPtr :: new ( & Either :: Left ( pat) ) ;
1414
+ let ptr = AstPtr :: new ( & pat) ;
1430
1415
self . alloc_pat ( pattern, ptr)
1431
1416
}
1432
1417
@@ -1987,10 +1972,19 @@ impl ExprCollector<'_> {
1987
1972
self . source_map . expr_map . insert ( src, id) ;
1988
1973
id
1989
1974
}
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
1991
1977
fn alloc_expr_desugared ( & mut self , expr : Expr ) -> ExprId {
1992
1978
self . body . exprs . alloc ( expr)
1993
1979
}
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
+ }
1994
1988
fn missing_expr ( & mut self ) -> ExprId {
1995
1989
self . alloc_expr_desugared ( Expr :: Missing )
1996
1990
}
0 commit comments