Skip to content

Commit 092352f

Browse files
authored
Rollup merge of #111841 - matthewjasper:validate-match-guards, r=compiler-errors
Run AST validation on match guards correctly AST validation was being skipped on match guards other than `if let` guards.
2 parents 52890cc + 72d41f3 commit 092352f

File tree

6 files changed

+240
-38
lines changed

6 files changed

+240
-38
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
736736
this.visit_expr(&arm.body);
737737
this.visit_pat(&arm.pat);
738738
walk_list!(this, visit_attribute, &arm.attrs);
739-
if let Some(guard) = &arm.guard && let ExprKind::Let(_, guard_expr, _) = &guard.kind {
739+
if let Some(guard) = &arm.guard {
740740
this.with_let_management(None, |this, _| {
741-
this.visit_expr(guard_expr)
741+
this.visit_expr(guard)
742742
});
743-
return;
744743
}
745744
}
746745
}

compiler/rustc_mir_build/src/build/expr/as_operand.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
118118
let category = Category::of(&expr.kind).unwrap();
119119
debug!(?category, ?expr.kind);
120120
match category {
121-
Category::Constant if let NeedsTemporary::No = needs_temporary || !expr.ty.needs_drop(this.tcx, this.param_env) => {
121+
Category::Constant
122+
if matches!(needs_temporary, NeedsTemporary::No)
123+
|| !expr.ty.needs_drop(this.tcx, this.param_env) =>
124+
{
122125
let constant = this.as_constant(expr);
123126
block.and(Operand::Constant(Box::new(constant)))
124127
}
125128
Category::Constant | Category::Place | Category::Rvalue(..) => {
126129
let operand = unpack!(block = this.as_temp(block, scope, expr, Mutability::Mut));
127130
// Overwrite temp local info if we have something more interesting to record.
128131
if !matches!(local_info, LocalInfo::Boring) {
129-
let decl_info = this.local_decls[operand].local_info.as_mut().assert_crate_local();
132+
let decl_info =
133+
this.local_decls[operand].local_info.as_mut().assert_crate_local();
130134
if let LocalInfo::Boring | LocalInfo::BlockTailTemp(_) = **decl_info {
131135
**decl_info = local_info;
132136
}

tests/ui/rfc-2294-if-let-guard/feature-gate.rs

+12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ fn _if_let_guard() {
1010
() if (let 0 = 1) => {}
1111
//~^ ERROR `let` expressions in this position are unstable
1212
//~| ERROR expected expression, found `let` statement
13+
//~| ERROR `let` expressions are not supported here
1314

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

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

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

3438
() if (let 0 = 1) && (let 0 = 1) => {}
3539
//~^ ERROR `let` expressions in this position are unstable
3640
//~| ERROR `let` expressions in this position are unstable
3741
//~| ERROR expected expression, found `let` statement
3842
//~| ERROR expected expression, found `let` statement
43+
//~| ERROR `let` expressions are not supported here
44+
//~| ERROR `let` expressions are not supported here
3945

4046
() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
4147
//~^ ERROR `if let` guards are experimental
@@ -47,6 +53,10 @@ fn _if_let_guard() {
4753
//~| ERROR expected expression, found `let` statement
4854
//~| ERROR expected expression, found `let` statement
4955
//~| ERROR expected expression, found `let` statement
56+
//~| ERROR `let` expressions are not supported here
57+
//~| ERROR `let` expressions are not supported here
58+
//~| ERROR `let` expressions are not supported here
59+
5060

5161
() if let Range { start: _, end: _ } = (true..true) && false => {}
5262
//~^ ERROR `if let` guards are experimental
@@ -68,9 +78,11 @@ fn _macros() {
6878
use_expr!((let 0 = 1 && 0 == 0));
6979
//~^ ERROR `let` expressions in this position are unstable
7080
//~| ERROR expected expression, found `let` statement
81+
//~| ERROR `let` expressions are not supported here
7182
use_expr!((let 0 = 1));
7283
//~^ ERROR `let` expressions in this position are unstable
7384
//~| ERROR expected expression, found `let` statement
85+
//~| ERROR `let` expressions are not supported here
7486
match () {
7587
#[cfg(FALSE)]
7688
() if let 0 = 1 => {}

0 commit comments

Comments
 (0)