diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2021-03-06 18:29:41 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2021-03-07 13:49:36 -0800 |
| commit | e62a54334445cd806bfc7bb9a7a41b63413fbedc (patch) | |
| tree | 1d3eb2c3b93edfb94e1624ed5f75334574d6b697 /compiler/rustc_ast_lowering/src/expr.rs | |
| parent | 1c77a1fa3ca574f2a40056f64d498db8efe0d8a8 (diff) | |
| download | rust-e62a54334445cd806bfc7bb9a7a41b63413fbedc.tar.gz rust-e62a54334445cd806bfc7bb9a7a41b63413fbedc.zip | |
Account for `if (let pat = expr) {}`
Partially address #82827.
Diffstat (limited to 'compiler/rustc_ast_lowering/src/expr.rs')
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 82b41e13ccc..63fc4e0ceda 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -97,6 +97,23 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::Let(ref pat, ref scrutinee) => { self.lower_expr_if_let(e.span, pat, scrutinee, then, else_opt.as_deref()) } + ExprKind::Paren(ref paren) => match paren.peel_parens().kind { + ExprKind::Let(ref pat, ref scrutinee) => { + // A user has written `if (let Some(x) = foo) {`, we want to avoid + // confusing them with mentions of nightly features. + // If this logic is changed, you will also likely need to touch + // `unused::UnusedParens::check_expr`. + self.if_let_expr_with_parens(cond, &paren.peel_parens()); + self.lower_expr_if_let( + e.span, + pat, + scrutinee, + then, + else_opt.as_deref(), + ) + } + _ => self.lower_expr_if(cond, then, else_opt.as_deref()), + }, _ => self.lower_expr_if(cond, then, else_opt.as_deref()), }, ExprKind::While(ref cond, ref body, opt_label) => self @@ -346,6 +363,32 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::Call(f, self.lower_exprs(&real_args)) } + fn if_let_expr_with_parens(&mut self, cond: &Expr, paren: &Expr) { + let start = cond.span.until(paren.span); + let end = paren.span.shrink_to_hi().until(cond.span.shrink_to_hi()); + let mut err = self.sess.struct_span_err( + vec![start, end], + "invalid parentheses around `let` expression in `if let`", + ); + if self.sess.opts.unstable_features.is_nightly_build() { + err.note( + "only supported directly without parentheses in conditions of `if`- and \ + `while`-expressions, as well as in `let` chains within parentheses", + ); + } else { + err.note("variable declaration using `let` is a statement, not a condition"); + } + err.multipart_suggestion( + "`if let` needs to be written without parentheses", + vec![(start, String::new()), (end, String::new())], + rustc_errors::Applicability::MachineApplicable, + ); + err.emit(); + // Ideally, we'd remove the feature gating of a `let` expression since we are already + // complaining about it here, but `feature_gate::check_crate` has already run by now: + // self.sess.parse_sess.gated_spans.ungate_last(sym::let_chains, paren.span); + } + /// Emit an error and lower `ast::ExprKind::Let(pat, scrutinee)` into: /// ```rust /// match scrutinee { pats => true, _ => false } @@ -356,8 +399,10 @@ impl<'hir> LoweringContext<'_, 'hir> { if self.sess.opts.unstable_features.is_nightly_build() { self.sess .struct_span_err(span, "`let` expressions are not supported here") - .note("only supported directly in conditions of `if`- and `while`-expressions") - .note("as well as when nested within `&&` and parenthesis in those conditions") + .note( + "only supported directly without parentheses in conditions of `if`- and \ + `while`-expressions, as well as in `let` chains within parentheses", + ) .emit(); } else { self.sess |
