Skip to content

Commit baa1999

Browse files
Merge #8066
8066: Handle `#[cfg]` on call arguments r=jonas-schievink a=jonas-schievink This resolved the issue reported in this comment: #5649 (comment) bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 0fbfab3 + cb530e7 commit baa1999

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

crates/hir_def/src/body/lower.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,15 @@ impl ExprCollector<'_> {
177177
}
178178

179179
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
180+
self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
181+
}
182+
183+
/// Returns `None` if the expression is `#[cfg]`d out.
184+
fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
180185
let syntax_ptr = AstPtr::new(&expr);
181-
if self.check_cfg(&expr).is_none() {
182-
return self.missing_expr();
183-
}
186+
self.check_cfg(&expr)?;
184187

185-
match expr {
188+
Some(match expr {
186189
ast::Expr::IfExpr(e) => {
187190
let then_branch = self.collect_block_opt(e.then_branch());
188191

@@ -211,8 +214,9 @@ impl ExprCollector<'_> {
211214
guard: None,
212215
},
213216
];
214-
return self
215-
.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
217+
return Some(
218+
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr),
219+
);
216220
}
217221
},
218222
};
@@ -283,8 +287,9 @@ impl ExprCollector<'_> {
283287
];
284288
let match_expr =
285289
self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
286-
return self
287-
.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr);
290+
return Some(
291+
self.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr),
292+
);
288293
}
289294
},
290295
};
@@ -301,7 +306,7 @@ impl ExprCollector<'_> {
301306
ast::Expr::CallExpr(e) => {
302307
let callee = self.collect_expr_opt(e.expr());
303308
let args = if let Some(arg_list) = e.arg_list() {
304-
arg_list.args().map(|e| self.collect_expr(e)).collect()
309+
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
305310
} else {
306311
Vec::new()
307312
};
@@ -310,7 +315,7 @@ impl ExprCollector<'_> {
310315
ast::Expr::MethodCallExpr(e) => {
311316
let receiver = self.collect_expr_opt(e.receiver());
312317
let args = if let Some(arg_list) = e.arg_list() {
313-
arg_list.args().map(|e| self.collect_expr(e)).collect()
318+
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
314319
} else {
315320
Vec::new()
316321
};
@@ -538,7 +543,7 @@ impl ExprCollector<'_> {
538543
self.alloc_expr(Expr::Missing, syntax_ptr)
539544
}
540545
}
541-
}
546+
})
542547
}
543548

544549
fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(

crates/hir_ty/src/diagnostics/expr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,27 @@ fn main() {
690690
"#,
691691
)
692692
}
693+
694+
#[test]
695+
fn cfgd_out_call_arguments() {
696+
check_diagnostics(
697+
r#"
698+
struct C(#[cfg(FALSE)] ());
699+
impl C {
700+
fn new() -> Self {
701+
Self(
702+
#[cfg(FALSE)]
703+
(),
704+
)
705+
}
706+
707+
fn method(&self) {}
708+
}
709+
710+
fn main() {
711+
C::new().method(#[cfg(FALSE)] 0);
712+
}
713+
"#,
714+
);
715+
}
693716
}

0 commit comments

Comments
 (0)