From cb530e7c97508c55bc5553ea7d9fe1e07d4a1b36 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Mar 2021 15:08:46 +0100 Subject: Handle `#[cfg]` on call arguments --- crates/hir_def/src/body/lower.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 60b25db56..19f5065d1 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -177,12 +177,15 @@ impl ExprCollector<'_> { } fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { + self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_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); - if self.check_cfg(&expr).is_none() { - return self.missing_expr(); - } + self.check_cfg(&expr)?; - match expr { + Some(match expr { ast::Expr::IfExpr(e) => { let then_branch = self.collect_block_opt(e.then_branch()); @@ -211,8 +214,9 @@ impl ExprCollector<'_> { 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 @@ impl ExprCollector<'_> { ]; 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 @@ impl ExprCollector<'_> { 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 @@ impl ExprCollector<'_> { 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 @@ impl ExprCollector<'_> { self.alloc_expr(Expr::Missing, syntax_ptr) } } - } + }) } fn collect_macro_call), T: ast::AstNode>( -- cgit v1.2.3