@@ -7,6 +7,7 @@ mod path;
7
7
8
8
use std:: mem;
9
9
10
+ use cfg:: CfgOptions ;
10
11
use either:: Either ;
11
12
use hir_expand:: {
12
13
HirFileId , InFile , Lookup , MacroDefId ,
@@ -81,8 +82,6 @@ pub(super) fn lower_body(
81
82
// even though they should be the same. Also, when the body comes from multiple expansions, their
82
83
// hygiene is different.
83
84
84
- let krate = module. krate ( ) ;
85
-
86
85
let mut self_param = None ;
87
86
let mut source_map_self_param = None ;
88
87
let mut params = vec ! [ ] ;
@@ -100,9 +99,8 @@ 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
104
- . self_param ( )
105
- . filter ( |self_param| collector. expander . is_cfg_enabled ( db, krate, self_param) )
102
+ if let Some ( self_param_syn) =
103
+ param_list. self_param ( ) . filter ( |self_param| collector. check_cfg ( self_param) )
106
104
{
107
105
let is_mutable =
108
106
self_param_syn. mut_token ( ) . is_some ( ) && self_param_syn. amp_token ( ) . is_none ( ) ;
@@ -119,10 +117,7 @@ pub(super) fn lower_body(
119
117
source_map_self_param =
120
118
Some ( collector. expander . in_file ( AstPtr :: new ( & self_param_syn) ) ) ;
121
119
}
122
- let count = param_list
123
- . params ( )
124
- . filter ( |it| collector. expander . is_cfg_enabled ( db, krate, it) )
125
- . count ( ) ;
120
+ let count = param_list. params ( ) . filter ( |it| collector. check_cfg ( it) ) . count ( ) ;
126
121
params = ( 0 ..count) . map ( |_| collector. missing_pat ( ) ) . collect ( ) ;
127
122
} ;
128
123
let body_expr = collector. missing_expr ( ) ;
@@ -138,9 +133,7 @@ pub(super) fn lower_body(
138
133
}
139
134
140
135
if let Some ( param_list) = parameters {
141
- if let Some ( self_param_syn) =
142
- param_list. self_param ( ) . filter ( |it| collector. expander . is_cfg_enabled ( db, krate, it) )
143
- {
136
+ if let Some ( self_param_syn) = param_list. self_param ( ) . filter ( |it| collector. check_cfg ( it) ) {
144
137
let is_mutable =
145
138
self_param_syn. mut_token ( ) . is_some ( ) && self_param_syn. amp_token ( ) . is_none ( ) ;
146
139
let hygiene = self_param_syn
@@ -157,7 +150,7 @@ pub(super) fn lower_body(
157
150
}
158
151
159
152
for param in param_list. params ( ) {
160
- if collector. expander . is_cfg_enabled ( db , krate , & param) {
153
+ if collector. check_cfg ( & param) {
161
154
let param_pat = collector. collect_pat_top ( param. pat ( ) ) ;
162
155
params. push ( param_pat) ;
163
156
}
@@ -346,7 +339,7 @@ pub(crate) fn lower_function(
346
339
collector. collect_impl_trait ( & mut expr_collector, |collector, mut impl_trait_lower_fn| {
347
340
if let Some ( param_list) = fn_. value . param_list ( ) {
348
341
if let Some ( param) = param_list. self_param ( ) {
349
- let enabled = collector. expander . is_cfg_enabled ( db , module . krate ( ) , & param) ;
342
+ let enabled = collector. check_cfg ( & param) ;
350
343
if enabled {
351
344
has_self_param = true ;
352
345
params. push ( match param. ty ( ) {
@@ -381,7 +374,7 @@ pub(crate) fn lower_function(
381
374
}
382
375
let p = param_list
383
376
. params ( )
384
- . filter ( |param| collector. expander . is_cfg_enabled ( db , module . krate ( ) , param) )
377
+ . filter ( |param| collector. check_cfg ( param) )
385
378
. filter ( |param| {
386
379
let is_variadic = param. dotdotdot_token ( ) . is_some ( ) ;
387
380
has_variadic |= is_variadic;
@@ -441,6 +434,7 @@ pub(crate) fn lower_function(
441
434
442
435
pub struct ExprCollector < ' db > {
443
436
db : & ' db dyn DefDatabase ,
437
+ cfg_options : & ' db CfgOptions ,
444
438
expander : Expander ,
445
439
def_map : Arc < DefMap > ,
446
440
local_def_map : Arc < LocalDefMap > ,
@@ -553,6 +547,7 @@ impl ExprCollector<'_> {
553
547
let expander = Expander :: new ( db, current_file_id, & def_map) ;
554
548
ExprCollector {
555
549
db,
550
+ cfg_options : module. krate ( ) . cfg_options ( db) ,
556
551
module,
557
552
def_map,
558
553
local_def_map,
@@ -1026,7 +1021,9 @@ impl ExprCollector<'_> {
1026
1021
/// Returns `None` if and only if the expression is `#[cfg]`d out.
1027
1022
fn maybe_collect_expr ( & mut self , expr : ast:: Expr ) -> Option < ExprId > {
1028
1023
let syntax_ptr = AstPtr :: new ( & expr) ;
1029
- self . check_cfg ( & expr) ?;
1024
+ if !self . check_cfg ( & expr) {
1025
+ return None ;
1026
+ }
1030
1027
1031
1028
// FIXME: Move some of these arms out into separate methods for clarity
1032
1029
Some ( match expr {
@@ -1114,6 +1111,7 @@ impl ExprCollector<'_> {
1114
1111
ast:: Expr :: WhileExpr ( e) => self . collect_while_loop ( syntax_ptr, e) ,
1115
1112
ast:: Expr :: ForExpr ( e) => self . collect_for_loop ( syntax_ptr, e) ,
1116
1113
ast:: Expr :: CallExpr ( e) => {
1114
+ // FIXME: Remove this once we drop support for <1.86, https://github.com/rust-lang/rust/commit/ac9cb908ac4301dfc25e7a2edee574320022ae2c
1117
1115
let is_rustc_box = {
1118
1116
let attrs = e. attrs ( ) ;
1119
1117
attrs. filter_map ( |it| it. as_simple_atom ( ) ) . any ( |it| it == "rustc_box" )
@@ -1156,13 +1154,17 @@ impl ExprCollector<'_> {
1156
1154
match_arm_list
1157
1155
. arms ( )
1158
1156
. filter_map ( |arm| {
1159
- self . check_cfg ( & arm) . map ( |( ) | MatchArm {
1160
- pat : self . collect_pat_top ( arm. pat ( ) ) ,
1161
- expr : self . collect_expr_opt ( arm. expr ( ) ) ,
1162
- guard : arm
1163
- . guard ( )
1164
- . map ( |guard| self . collect_expr_opt ( guard. condition ( ) ) ) ,
1165
- } )
1157
+ if self . check_cfg ( & arm) {
1158
+ Some ( MatchArm {
1159
+ pat : self . collect_pat_top ( arm. pat ( ) ) ,
1160
+ expr : self . collect_expr_opt ( arm. expr ( ) ) ,
1161
+ guard : arm
1162
+ . guard ( )
1163
+ . map ( |guard| self . collect_expr_opt ( guard. condition ( ) ) ) ,
1164
+ } )
1165
+ } else {
1166
+ None
1167
+ }
1166
1168
} )
1167
1169
. collect ( )
1168
1170
} else {
@@ -1230,7 +1232,9 @@ impl ExprCollector<'_> {
1230
1232
let fields = nfl
1231
1233
. fields ( )
1232
1234
. filter_map ( |field| {
1233
- self . check_cfg ( & field) ?;
1235
+ if !self . check_cfg ( & field) {
1236
+ return None ;
1237
+ }
1234
1238
1235
1239
let name = field. field_name ( ) ?. as_name ( ) ;
1236
1240
@@ -1483,7 +1487,9 @@ impl ExprCollector<'_> {
1483
1487
}
1484
1488
1485
1489
fn maybe_collect_expr_as_pat ( & mut self , expr : & ast:: Expr ) -> Option < PatId > {
1486
- self . check_cfg ( expr) ?;
1490
+ if !self . check_cfg ( expr) {
1491
+ return None ;
1492
+ }
1487
1493
let syntax_ptr = AstPtr :: new ( expr) ;
1488
1494
1489
1495
let result = match expr {
@@ -1558,7 +1564,9 @@ impl ExprCollector<'_> {
1558
1564
let args = record_field_list
1559
1565
. fields ( )
1560
1566
. filter_map ( |f| {
1561
- self . check_cfg ( & f) ?;
1567
+ if !self . check_cfg ( & f) {
1568
+ return None ;
1569
+ }
1562
1570
let field_expr = f. expr ( ) ?;
1563
1571
let pat = self . collect_expr_as_pat ( field_expr) ;
1564
1572
let name = f. field_name ( ) ?. as_name ( ) ;
@@ -2044,7 +2052,7 @@ impl ExprCollector<'_> {
2044
2052
fn collect_stmt ( & mut self , statements : & mut Vec < Statement > , s : ast:: Stmt ) {
2045
2053
match s {
2046
2054
ast:: Stmt :: LetStmt ( stmt) => {
2047
- if self . check_cfg ( & stmt) . is_none ( ) {
2055
+ if ! self . check_cfg ( & stmt) {
2048
2056
return ;
2049
2057
}
2050
2058
let pat = self . collect_pat_top ( stmt. pat ( ) ) ;
@@ -2059,7 +2067,7 @@ impl ExprCollector<'_> {
2059
2067
ast:: Stmt :: ExprStmt ( stmt) => {
2060
2068
let expr = stmt. expr ( ) ;
2061
2069
match & expr {
2062
- Some ( expr) if self . check_cfg ( expr) . is_none ( ) => return ,
2070
+ Some ( expr) if ! self . check_cfg ( expr) => return ,
2063
2071
_ => ( ) ,
2064
2072
}
2065
2073
let has_semi = stmt. semicolon_token ( ) . is_some ( ) ;
@@ -2074,7 +2082,7 @@ impl ExprCollector<'_> {
2074
2082
}
2075
2083
}
2076
2084
ast:: Stmt :: Item ( ast:: Item :: MacroDef ( macro_) ) => {
2077
- if self . check_cfg ( & macro_) . is_none ( ) {
2085
+ if ! self . check_cfg ( & macro_) {
2078
2086
return ;
2079
2087
}
2080
2088
let Some ( name) = macro_. name ( ) else {
@@ -2086,7 +2094,7 @@ impl ExprCollector<'_> {
2086
2094
self . collect_macro_def ( statements, macro_id) ;
2087
2095
}
2088
2096
ast:: Stmt :: Item ( ast:: Item :: MacroRules ( macro_) ) => {
2089
- if self . check_cfg ( & macro_) . is_none ( ) {
2097
+ if ! self . check_cfg ( & macro_) {
2090
2098
return ;
2091
2099
}
2092
2100
let Some ( name) = macro_. name ( ) else {
@@ -2360,7 +2368,9 @@ impl ExprCollector<'_> {
2360
2368
let args = record_pat_field_list
2361
2369
. fields ( )
2362
2370
. filter_map ( |f| {
2363
- self . check_cfg ( & f) ?;
2371
+ if !self . check_cfg ( & f) {
2372
+ return None ;
2373
+ }
2364
2374
let ast_pat = f. pat ( ) ?;
2365
2375
let pat = self . collect_pat ( ast_pat, binding_list) ;
2366
2376
let name = f. field_name ( ) ?. as_name ( ) ;
@@ -2536,25 +2546,18 @@ impl ExprCollector<'_> {
2536
2546
2537
2547
/// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
2538
2548
/// not.
2539
- fn check_cfg ( & mut self , owner : & dyn ast:: HasAttrs ) -> Option < ( ) > {
2540
- let attrs = self . expander . attrs ( self . db , self . module . krate ( ) , owner) ;
2541
- match attrs. cfg ( ) {
2542
- Some ( cfg) => {
2543
- let cfg_options = self . module . krate ( ) . cfg_options ( self . db ) ;
2544
-
2545
- if cfg_options. check ( & cfg) != Some ( false ) {
2546
- return Some ( ( ) ) ;
2547
- }
2548
-
2549
+ fn check_cfg ( & mut self , owner : & dyn ast:: HasAttrs ) -> bool {
2550
+ let enabled = self . expander . is_cfg_enabled ( self . db , owner, self . cfg_options ) ;
2551
+ match enabled {
2552
+ Ok ( ( ) ) => true ,
2553
+ Err ( cfg) => {
2549
2554
self . source_map . diagnostics . push ( ExpressionStoreDiagnostics :: InactiveCode {
2550
2555
node : self . expander . in_file ( SyntaxNodePtr :: new ( owner. syntax ( ) ) ) ,
2551
2556
cfg,
2552
- opts : cfg_options. clone ( ) ,
2557
+ opts : self . cfg_options . clone ( ) ,
2553
2558
} ) ;
2554
-
2555
- None
2559
+ false
2556
2560
}
2557
- None => Some ( ( ) ) ,
2558
2561
}
2559
2562
}
2560
2563
0 commit comments