Skip to content

Commit

Permalink
Rollup merge of #111841 - matthewjasper:validate-match-guards, r=comp…
Browse files Browse the repository at this point in the history
…iler-errors

Run AST validation on match guards correctly

AST validation was being skipped on match guards other than `if let` guards.
  • Loading branch information
matthiaskrgr committed May 24, 2023
2 parents 52890cc + 72d41f3 commit 092352f
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 38 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
this.visit_expr(&arm.body);
this.visit_pat(&arm.pat);
walk_list!(this, visit_attribute, &arm.attrs);
if let Some(guard) = &arm.guard && let ExprKind::Let(_, guard_expr, _) = &guard.kind {
if let Some(guard) = &arm.guard {
this.with_let_management(None, |this, _| {
this.visit_expr(guard_expr)
this.visit_expr(guard)
});
return;
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let category = Category::of(&expr.kind).unwrap();
debug!(?category, ?expr.kind);
match category {
Category::Constant if let NeedsTemporary::No = needs_temporary || !expr.ty.needs_drop(this.tcx, this.param_env) => {
Category::Constant
if matches!(needs_temporary, NeedsTemporary::No)
|| !expr.ty.needs_drop(this.tcx, this.param_env) =>
{
let constant = this.as_constant(expr);
block.and(Operand::Constant(Box::new(constant)))
}
Category::Constant | Category::Place | Category::Rvalue(..) => {
let operand = unpack!(block = this.as_temp(block, scope, expr, Mutability::Mut));
// Overwrite temp local info if we have something more interesting to record.
if !matches!(local_info, LocalInfo::Boring) {
let decl_info = this.local_decls[operand].local_info.as_mut().assert_crate_local();
let decl_info =
this.local_decls[operand].local_info.as_mut().assert_crate_local();
if let LocalInfo::Boring | LocalInfo::BlockTailTemp(_) = **decl_info {
**decl_info = local_info;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/rfc-2294-if-let-guard/feature-gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ fn _if_let_guard() {
() if (let 0 = 1) => {}
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here

() if (((let 0 = 1))) => {}
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here

() if true && let 0 = 1 => {}
//~^ ERROR `if let` guards are experimental
Expand All @@ -26,16 +28,20 @@ fn _if_let_guard() {
() if (let 0 = 1) && true => {}
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here

() if true && (let 0 = 1) => {}
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here

() if (let 0 = 1) && (let 0 = 1) => {}
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here
//~| ERROR `let` expressions are not supported here

() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
//~^ ERROR `if let` guards are experimental
Expand All @@ -47,6 +53,10 @@ fn _if_let_guard() {
//~| ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here
//~| ERROR `let` expressions are not supported here
//~| ERROR `let` expressions are not supported here


() if let Range { start: _, end: _ } = (true..true) && false => {}
//~^ ERROR `if let` guards are experimental
Expand All @@ -68,9 +78,11 @@ fn _macros() {
use_expr!((let 0 = 1 && 0 == 0));
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here
use_expr!((let 0 = 1));
//~^ ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions are not supported here
match () {
#[cfg(FALSE)]
() if let 0 = 1 => {}
Expand Down
Loading

0 comments on commit 092352f

Please sign in to comment.