@@ -10,10 +10,8 @@ use std::mem;
10
10
use either:: Either ;
11
11
use hir_expand:: {
12
12
InFile , Lookup , MacroDefId ,
13
- attrs:: RawAttrs ,
14
13
mod_path:: tool_path,
15
14
name:: { AsName , Name } ,
16
- span_map:: SpanMap ,
17
15
} ;
18
16
use intern:: { Symbol , sym} ;
19
17
use rustc_hash:: FxHashMap ;
@@ -27,14 +25,13 @@ use syntax::{
27
25
SlicePatComponents ,
28
26
} ,
29
27
} ;
30
- use text_size:: TextSize ;
31
28
use thin_vec:: ThinVec ;
32
29
use triomphe:: Arc ;
30
+ use tt:: TextRange ;
33
31
34
32
use crate :: {
35
33
AdtId , BlockId , BlockLoc , DefWithBodyId , FunctionId , GenericDefId , ImplId , ItemTreeLoc ,
36
34
MacroId , ModuleDefId , ModuleId , TraitAliasId , TraitId , TypeAliasId , UnresolvedMacro ,
37
- attr:: Attrs ,
38
35
builtin_type:: BuiltinUint ,
39
36
db:: DefDatabase ,
40
37
expr_store:: {
@@ -83,6 +80,8 @@ pub(super) fn lower_body(
83
80
// even though they should be the same. Also, when the body comes from multiple expansions, their
84
81
// hygiene is different.
85
82
83
+ let krate = module. krate ( ) ;
84
+
86
85
let mut self_param = None ;
87
86
let mut source_map_self_param = None ;
88
87
let mut params = vec ! [ ] ;
@@ -100,14 +99,10 @@ pub(super) fn lower_body(
100
99
// and skip the body.
101
100
if skip_body {
102
101
if let Some ( param_list) = parameters {
103
- if let Some ( self_param_syn) = param_list. self_param ( ) . filter ( |self_param| {
104
- Attrs :: filter (
105
- db,
106
- module. krate ( ) ,
107
- RawAttrs :: new ( db. upcast ( ) , self_param, collector. expander . span_map ( ) . as_ref ( ) ) ,
108
- )
109
- . is_cfg_enabled ( module. krate ( ) . cfg_options ( db) )
110
- } ) {
102
+ if let Some ( self_param_syn) = param_list
103
+ . self_param ( )
104
+ . filter ( |self_param| collector. expander . is_cfg_enabled ( db, krate, self_param) )
105
+ {
111
106
let is_mutable =
112
107
self_param_syn. mut_token ( ) . is_some ( ) && self_param_syn. amp_token ( ) . is_none ( ) ;
113
108
let binding_id: la_arena:: Idx < Binding > = collector. alloc_binding (
@@ -120,14 +115,7 @@ pub(super) fn lower_body(
120
115
}
121
116
let count = param_list
122
117
. params ( )
123
- . filter ( |it| {
124
- Attrs :: filter (
125
- db,
126
- module. krate ( ) ,
127
- RawAttrs :: new ( db. upcast ( ) , it, collector. expander . span_map ( ) . as_ref ( ) ) ,
128
- )
129
- . is_cfg_enabled ( module. krate ( ) . cfg_options ( db) )
130
- } )
118
+ . filter ( |it| collector. expander . is_cfg_enabled ( db, krate, it) )
131
119
. count ( ) ;
132
120
params = ( 0 ..count) . map ( |_| collector. missing_pat ( ) ) . collect ( ) ;
133
121
} ;
@@ -144,14 +132,9 @@ pub(super) fn lower_body(
144
132
}
145
133
146
134
if let Some ( param_list) = parameters {
147
- if let Some ( self_param_syn) = param_list. self_param ( ) . filter ( |it| {
148
- Attrs :: filter (
149
- db,
150
- module. krate ( ) ,
151
- RawAttrs :: new ( db. upcast ( ) , it, collector. expander . span_map ( ) . as_ref ( ) ) ,
152
- )
153
- . is_cfg_enabled ( module. krate ( ) . cfg_options ( db) )
154
- } ) {
135
+ if let Some ( self_param_syn) =
136
+ param_list. self_param ( ) . filter ( |it| collector. expander . is_cfg_enabled ( db, krate, it) )
137
+ {
155
138
let is_mutable =
156
139
self_param_syn. mut_token ( ) . is_some ( ) && self_param_syn. amp_token ( ) . is_none ( ) ;
157
140
let binding_id: la_arena:: Idx < Binding > = collector. alloc_binding (
@@ -160,7 +143,7 @@ pub(super) fn lower_body(
160
143
) ;
161
144
let hygiene = self_param_syn
162
145
. name ( )
163
- . map ( |name| collector. hygiene_id_for ( name. syntax ( ) . text_range ( ) . start ( ) ) )
146
+ . map ( |name| collector. hygiene_id_for ( name. syntax ( ) . text_range ( ) ) )
164
147
. unwrap_or ( HygieneId :: ROOT ) ;
165
148
if !hygiene. is_root ( ) {
166
149
collector. store . binding_hygiene . insert ( binding_id, hygiene) ;
@@ -170,13 +153,7 @@ pub(super) fn lower_body(
170
153
}
171
154
172
155
for param in param_list. params ( ) {
173
- if Attrs :: filter (
174
- db,
175
- module. krate ( ) ,
176
- RawAttrs :: new ( db. upcast ( ) , & param, collector. expander . span_map ( ) . as_ref ( ) ) ,
177
- )
178
- . is_cfg_enabled ( module. krate ( ) . cfg_options ( db) )
179
- {
156
+ if collector. expander . is_cfg_enabled ( db, krate, & param) {
180
157
let param_pat = collector. collect_pat_top ( param. pat ( ) ) ;
181
158
params. push ( param_pat) ;
182
159
}
@@ -336,66 +313,45 @@ pub(crate) fn lower_function(
336
313
let mut params = vec ! [ ] ;
337
314
let mut has_self_param = false ;
338
315
let mut has_variadic = false ;
339
- collector. collect_impl_trait ( |expr_collector , mut impl_trait_lower_fn| {
316
+ collector. collect_impl_trait ( |collector , mut impl_trait_lower_fn| {
340
317
if let Some ( param_list) = fn_. value . param_list ( ) {
341
318
if let Some ( param) = param_list. self_param ( ) {
342
- let enabled = Attrs :: filter (
343
- expr_collector. db ,
344
- expr_collector. module . krate ( ) ,
345
- RawAttrs :: new (
346
- expr_collector. db . upcast ( ) ,
347
- & param,
348
- expr_collector. expander . span_map ( ) . as_ref ( ) ,
349
- ) ,
350
- )
351
- . is_cfg_enabled ( expr_collector. module . krate ( ) . cfg_options ( expr_collector. db ) ) ;
319
+ let enabled = collector. expander . is_cfg_enabled ( db, module. krate ( ) , & param) ;
352
320
if enabled {
353
321
has_self_param = true ;
354
322
params. push ( match param. ty ( ) {
355
- Some ( ty) => expr_collector . lower_type_ref ( ty, & mut impl_trait_lower_fn) ,
323
+ Some ( ty) => collector . lower_type_ref ( ty, & mut impl_trait_lower_fn) ,
356
324
None => {
357
- let self_type = expr_collector . alloc_type_ref_desugared ( TypeRef :: Path (
325
+ let self_type = collector . alloc_type_ref_desugared ( TypeRef :: Path (
358
326
Name :: new_symbol_root ( sym:: Self_ . clone ( ) ) . into ( ) ,
359
327
) ) ;
360
328
let lifetime = param
361
329
. lifetime ( )
362
- . map ( |lifetime| expr_collector . lower_lifetime_ref ( lifetime) ) ;
330
+ . map ( |lifetime| collector . lower_lifetime_ref ( lifetime) ) ;
363
331
match param. kind ( ) {
364
332
ast:: SelfParamKind :: Owned => self_type,
365
- ast:: SelfParamKind :: Ref => expr_collector . alloc_type_ref_desugared (
333
+ ast:: SelfParamKind :: Ref => collector . alloc_type_ref_desugared (
366
334
TypeRef :: Reference ( Box :: new ( RefType {
367
335
ty : self_type,
368
336
lifetime,
369
337
mutability : Mutability :: Shared ,
370
338
} ) ) ,
371
339
) ,
372
- ast:: SelfParamKind :: MutRef => expr_collector
373
- . alloc_type_ref_desugared ( TypeRef :: Reference ( Box :: new (
374
- RefType {
375
- ty : self_type,
376
- lifetime,
377
- mutability : Mutability :: Mut ,
378
- } ,
379
- ) ) ) ,
340
+ ast:: SelfParamKind :: MutRef => collector. alloc_type_ref_desugared (
341
+ TypeRef :: Reference ( Box :: new ( RefType {
342
+ ty : self_type,
343
+ lifetime,
344
+ mutability : Mutability :: Mut ,
345
+ } ) ) ,
346
+ ) ,
380
347
}
381
348
}
382
349
} ) ;
383
350
}
384
351
}
385
352
let p = param_list
386
353
. params ( )
387
- . filter ( |param| {
388
- Attrs :: filter (
389
- expr_collector. db ,
390
- expr_collector. module . krate ( ) ,
391
- RawAttrs :: new (
392
- expr_collector. db . upcast ( ) ,
393
- param,
394
- expr_collector. expander . span_map ( ) . as_ref ( ) ,
395
- ) ,
396
- )
397
- . is_cfg_enabled ( expr_collector. module . krate ( ) . cfg_options ( expr_collector. db ) )
398
- } )
354
+ . filter ( |param| collector. expander . is_cfg_enabled ( db, module. krate ( ) , param) )
399
355
. filter ( |param| {
400
356
let is_variadic = param. dotdotdot_token ( ) . is_some ( ) ;
401
357
has_variadic |= is_variadic;
@@ -405,7 +361,7 @@ pub(crate) fn lower_function(
405
361
// FIXME
406
362
. collect :: < Vec < _ > > ( ) ;
407
363
for p in p {
408
- params. push ( expr_collector . lower_type_ref_opt ( p, & mut impl_trait_lower_fn) ) ;
364
+ params. push ( collector . lower_type_ref_opt ( p, & mut impl_trait_lower_fn) ) ;
409
365
}
410
366
}
411
367
} ) ;
@@ -1026,7 +982,7 @@ impl ExprCollector<'_> {
1026
982
} )
1027
983
}
1028
984
Some ( ast:: BlockModifier :: Label ( label) ) => {
1029
- let label_hygiene = self . hygiene_id_for ( label. syntax ( ) . text_range ( ) . start ( ) ) ;
985
+ let label_hygiene = self . hygiene_id_for ( label. syntax ( ) . text_range ( ) ) ;
1030
986
let label_id = self . collect_label ( label) ;
1031
987
self . with_labeled_rib ( label_id, label_hygiene, |this| {
1032
988
this. collect_block_ ( e, |id, statements, tail| Expr :: Block {
@@ -1072,10 +1028,7 @@ impl ExprCollector<'_> {
1072
1028
} ,
1073
1029
ast:: Expr :: LoopExpr ( e) => {
1074
1030
let label = e. label ( ) . map ( |label| {
1075
- (
1076
- self . hygiene_id_for ( label. syntax ( ) . text_range ( ) . start ( ) ) ,
1077
- self . collect_label ( label) ,
1078
- )
1031
+ ( self . hygiene_id_for ( label. syntax ( ) . text_range ( ) ) , self . collect_label ( label) )
1079
1032
} ) ;
1080
1033
let body = self . collect_labelled_block_opt ( label, e. loop_body ( ) ) ;
1081
1034
self . alloc_expr ( Expr :: Loop { body, label : label. map ( |it| it. 1 ) } , syntax_ptr)
@@ -1423,7 +1376,7 @@ impl ExprCollector<'_> {
1423
1376
// Need to enable `mod_path.len() < 1` for `self`.
1424
1377
let may_be_variable = matches ! ( & path, Path :: BarePath ( mod_path) if mod_path. len( ) <= 1 ) ;
1425
1378
let hygiene = if may_be_variable {
1426
- self . hygiene_id_for ( e. syntax ( ) . text_range ( ) . start ( ) )
1379
+ self . hygiene_id_for ( e. syntax ( ) . text_range ( ) )
1427
1380
} else {
1428
1381
HygieneId :: ROOT
1429
1382
} ;
@@ -1697,7 +1650,7 @@ impl ExprCollector<'_> {
1697
1650
/// to preserve drop semantics. We should probably do the same in future.
1698
1651
fn collect_while_loop ( & mut self , syntax_ptr : AstPtr < ast:: Expr > , e : ast:: WhileExpr ) -> ExprId {
1699
1652
let label = e. label ( ) . map ( |label| {
1700
- ( self . hygiene_id_for ( label. syntax ( ) . text_range ( ) . start ( ) ) , self . collect_label ( label) )
1653
+ ( self . hygiene_id_for ( label. syntax ( ) . text_range ( ) ) , self . collect_label ( label) )
1701
1654
} ) ;
1702
1655
let body = self . collect_labelled_block_opt ( label, e. loop_body ( ) ) ;
1703
1656
@@ -1765,7 +1718,7 @@ impl ExprCollector<'_> {
1765
1718
ellipsis : None ,
1766
1719
} ;
1767
1720
let label = e. label ( ) . map ( |label| {
1768
- ( self . hygiene_id_for ( label. syntax ( ) . text_range ( ) . start ( ) ) , self . collect_label ( label) )
1721
+ ( self . hygiene_id_for ( label. syntax ( ) . text_range ( ) ) , self . collect_label ( label) )
1769
1722
} ) ;
1770
1723
let some_arm = MatchArm {
1771
1724
pat : self . alloc_pat_desugared ( some_pat) ,
@@ -2191,7 +2144,7 @@ impl ExprCollector<'_> {
2191
2144
let name = bp. name ( ) . map ( |nr| nr. as_name ( ) ) . unwrap_or_else ( Name :: missing) ;
2192
2145
let hygiene = bp
2193
2146
. name ( )
2194
- . map ( |name| self . hygiene_id_for ( name. syntax ( ) . text_range ( ) . start ( ) ) )
2147
+ . map ( |name| self . hygiene_id_for ( name. syntax ( ) . text_range ( ) ) )
2195
2148
. unwrap_or ( HygieneId :: ROOT ) ;
2196
2149
2197
2150
let annotation =
@@ -2498,11 +2451,7 @@ impl ExprCollector<'_> {
2498
2451
/// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
2499
2452
/// not.
2500
2453
fn check_cfg ( & mut self , owner : & dyn ast:: HasAttrs ) -> Option < ( ) > {
2501
- let attrs = Attrs :: filter (
2502
- self . db ,
2503
- self . module . krate ( ) ,
2504
- RawAttrs :: new ( self . db . upcast ( ) , owner, self . expander . span_map ( ) . as_ref ( ) ) ,
2505
- ) ;
2454
+ let attrs = self . expander . attrs ( self . db , self . module . krate ( ) , owner) ;
2506
2455
match attrs. cfg ( ) {
2507
2456
Some ( cfg) => {
2508
2457
let cfg_options = self . module . krate ( ) . cfg_options ( self . db ) ;
@@ -2541,14 +2490,16 @@ impl ExprCollector<'_> {
2541
2490
lifetime : Option < ast:: Lifetime > ,
2542
2491
) -> Result < Option < LabelId > , ExpressionStoreDiagnostics > {
2543
2492
let Some ( lifetime) = lifetime else { return Ok ( None ) } ;
2544
- let span_map = self . expander . span_map ( ) ;
2545
- let span = span_map. span_for_range ( lifetime. syntax ( ) . text_range ( ) ) ;
2546
- let ctx = span. ctx ;
2547
- let mut hygiene_id = HygieneId :: new ( ctx. opaque_and_semitransparent ( self . db ) ) ;
2548
- let mut hygiene_info = ctx. outer_expn ( self . db ) . map ( |expansion| {
2549
- let expansion = self . db . lookup_intern_macro_call ( expansion) ;
2550
- ( ctx. parent ( self . db ) , expansion. def )
2551
- } ) ;
2493
+ let mut hygiene_id =
2494
+ self . expander . hygiene_for_range ( self . db , lifetime. syntax ( ) . text_range ( ) ) ;
2495
+ let mut hygiene_info = if hygiene_id. is_root ( ) {
2496
+ None
2497
+ } else {
2498
+ hygiene_id. lookup ( ) . outer_expn ( self . db ) . map ( |expansion| {
2499
+ let expansion = self . db . lookup_intern_macro_call ( expansion) ;
2500
+ ( hygiene_id. lookup ( ) . parent ( self . db ) , expansion. def )
2501
+ } )
2502
+ } ;
2552
2503
let name = Name :: new_lifetime ( & lifetime) ;
2553
2504
2554
2505
for ( rib_idx, rib) in self . label_ribs . iter ( ) . enumerate ( ) . rev ( ) {
@@ -2686,7 +2637,7 @@ impl ExprCollector<'_> {
2686
2637
} ) {
2687
2638
Some ( ( ( s, is_direct_literal) , template) ) => {
2688
2639
let call_ctx = self . expander . call_syntax_ctx ( ) ;
2689
- let hygiene = self . hygiene_id_for ( s. syntax ( ) . text_range ( ) . start ( ) ) ;
2640
+ let hygiene = self . hygiene_id_for ( s. syntax ( ) . text_range ( ) ) ;
2690
2641
let fmt = format_args:: parse (
2691
2642
& s,
2692
2643
fmt_snippet,
@@ -3196,15 +3147,8 @@ impl ExprCollector<'_> {
3196
3147
res
3197
3148
}
3198
3149
3199
- /// If this returns `HygieneId::ROOT`, do not allocate to save space.
3200
- fn hygiene_id_for ( & self , span_start : TextSize ) -> HygieneId {
3201
- match self . expander . span_map ( ) {
3202
- SpanMap :: RealSpanMap ( _) => HygieneId :: ROOT ,
3203
- SpanMap :: ExpansionSpanMap ( span_map) => {
3204
- let ctx = span_map. span_at ( span_start) . ctx ;
3205
- HygieneId :: new ( ctx. opaque_and_semitransparent ( self . db ) )
3206
- }
3207
- }
3150
+ fn hygiene_id_for ( & self , range : TextRange ) -> HygieneId {
3151
+ self . expander . hygiene_for_range ( self . db , range)
3208
3152
}
3209
3153
}
3210
3154
0 commit comments