From cb530e7c97508c55bc5553ea7d9fe1e07d4a1b36 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Mar 2021 15:08:46 +0100 Subject: [PATCH] Handle `#[cfg]` on call arguments --- crates/hir_def/src/body/lower.rs | 29 ++++++++++++++++----------- crates/hir_ty/src/diagnostics/expr.rs | 23 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 60b25db5632..19f5065d138 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -177,12 +177,15 @@ fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId { } fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { - let syntax_ptr = AstPtr::new(&expr); - if self.check_cfg(&expr).is_none() { - return self.missing_expr(); - } + self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr()) + } - match expr { + /// Returns `None` if the expression is `#[cfg]`d out. + fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option { + let syntax_ptr = AstPtr::new(&expr); + self.check_cfg(&expr)?; + + Some(match expr { ast::Expr::IfExpr(e) => { let then_branch = self.collect_block_opt(e.then_branch()); @@ -211,8 +214,9 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { guard: None, }, ]; - return self - .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr); + return Some( + self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr), + ); } }, }; @@ -283,8 +287,9 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { ]; let match_expr = self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); - return self - .alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr); + return Some( + self.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr), + ); } }, }; @@ -301,7 +306,7 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { ast::Expr::CallExpr(e) => { let callee = self.collect_expr_opt(e.expr()); let args = if let Some(arg_list) = e.arg_list() { - arg_list.args().map(|e| self.collect_expr(e)).collect() + arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect() } else { Vec::new() }; @@ -310,7 +315,7 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { ast::Expr::MethodCallExpr(e) => { let receiver = self.collect_expr_opt(e.receiver()); let args = if let Some(arg_list) = e.arg_list() { - arg_list.args().map(|e| self.collect_expr(e)).collect() + arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect() } else { Vec::new() }; @@ -538,7 +543,7 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { self.alloc_expr(Expr::Missing, syntax_ptr) } } - } + }) } fn collect_macro_call), T: ast::AstNode>( diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 71b2cade088..50dc40335a6 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -690,4 +690,27 @@ fn main() { "#, ) } + + #[test] + fn cfgd_out_call_arguments() { + check_diagnostics( + r#" +struct C(#[cfg(FALSE)] ()); +impl C { + fn new() -> Self { + Self( + #[cfg(FALSE)] + (), + ) + } + + fn method(&self) {} +} + +fn main() { + C::new().method(#[cfg(FALSE)] 0); +} + "#, + ); + } }