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.
This commit is contained in:
Matthias Krüger 2023-05-24 21:36:57 +02:00 committed by GitHub
commit 092352f6fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 240 additions and 38 deletions

View File

@ -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;
}
}
}

View File

@ -118,7 +118,10 @@ 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)))
}
@ -126,7 +129,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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;
}

View File

@ -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
@ -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
@ -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
@ -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 => {}

View File

@ -5,67 +5,67 @@ LL | () if (let 0 = 1) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:14:18
--> $DIR/feature-gate.rs:15:18
|
LL | () if (((let 0 = 1))) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:26:16
--> $DIR/feature-gate.rs:28:16
|
LL | () if (let 0 = 1) && true => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:30:24
--> $DIR/feature-gate.rs:33:24
|
LL | () if true && (let 0 = 1) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:34:16
--> $DIR/feature-gate.rs:38:16
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:34:31
--> $DIR/feature-gate.rs:38:31
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:40:42
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:40:55
--> $DIR/feature-gate.rs:46:55
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:40:68
--> $DIR/feature-gate.rs:46:68
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:68:16
--> $DIR/feature-gate.rs:78:16
|
LL | use_expr!((let 0 = 1 && 0 == 0));
| ^^^
error: expected expression, found `let` statement
--> $DIR/feature-gate.rs:71:16
--> $DIR/feature-gate.rs:82:16
|
LL | use_expr!((let 0 = 1));
| ^^^
error: no rules expected the token `let`
--> $DIR/feature-gate.rs:80:15
--> $DIR/feature-gate.rs:92:15
|
LL | macro_rules! use_expr {
| --------------------- when calling this macro
@ -74,11 +74,154 @@ LL | use_expr!(let 0 = 1);
| ^^^ no rules expected this token in macro call
|
note: while trying to match meta-variable `$e:expr`
--> $DIR/feature-gate.rs:61:10
--> $DIR/feature-gate.rs:71:10
|
LL | ($e:expr) => {
| ^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:10:16
|
LL | () if (let 0 = 1) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:10:16
|
LL | () if (let 0 = 1) => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:15:18
|
LL | () if (((let 0 = 1))) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:15:18
|
LL | () if (((let 0 = 1))) => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:28:16
|
LL | () if (let 0 = 1) && true => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:28:16
|
LL | () if (let 0 = 1) && true => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:33:24
|
LL | () if true && (let 0 = 1) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:33:24
|
LL | () if true && (let 0 = 1) => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:38:16
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:38:16
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:38:31
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:38:31
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:46:55
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:46:68
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:78:16
|
LL | use_expr!((let 0 = 1 && 0 == 0));
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:78:16
|
LL | use_expr!((let 0 = 1 && 0 == 0));
| ^^^^^^^^^^^^^^^^^^^
error: `let` expressions are not supported here
--> $DIR/feature-gate.rs:82:16
|
LL | use_expr!((let 0 = 1));
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
--> $DIR/feature-gate.rs:82:16
|
LL | use_expr!((let 0 = 1));
| ^^^^^^^^^
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:7:12
|
@ -90,7 +233,7 @@ LL | () if let 0 = 1 => {}
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:18:12
--> $DIR/feature-gate.rs:20:12
|
LL | () if true && let 0 = 1 => {}
| ^^^^^^^^^^^^^^^^^^^^
@ -100,7 +243,7 @@ LL | () if true && let 0 = 1 => {}
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:22:12
--> $DIR/feature-gate.rs:24:12
|
LL | () if let 0 = 1 && true => {}
| ^^^^^^^^^^^^^^^^^^^^
@ -110,7 +253,7 @@ LL | () if let 0 = 1 && true => {}
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:40:12
--> $DIR/feature-gate.rs:46:12
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -120,7 +263,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:51:12
--> $DIR/feature-gate.rs:61:12
|
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -130,7 +273,7 @@ LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:76:12
--> $DIR/feature-gate.rs:88:12
|
LL | () if let 0 = 1 => {}
| ^^^^^^^^^^^^
@ -149,7 +292,7 @@ LL | () if (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:14:18
--> $DIR/feature-gate.rs:15:18
|
LL | () if (((let 0 = 1))) => {}
| ^^^^^^^^^
@ -158,7 +301,7 @@ LL | () if (((let 0 = 1))) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:18:23
--> $DIR/feature-gate.rs:20:23
|
LL | () if true && let 0 = 1 => {}
| ^^^^^^^^^
@ -167,7 +310,7 @@ LL | () if true && let 0 = 1 => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:22:15
--> $DIR/feature-gate.rs:24:15
|
LL | () if let 0 = 1 && true => {}
| ^^^^^^^^^
@ -176,7 +319,7 @@ LL | () if let 0 = 1 && true => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:26:16
--> $DIR/feature-gate.rs:28:16
|
LL | () if (let 0 = 1) && true => {}
| ^^^^^^^^^
@ -185,7 +328,7 @@ LL | () if (let 0 = 1) && true => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:30:24
--> $DIR/feature-gate.rs:33:24
|
LL | () if true && (let 0 = 1) => {}
| ^^^^^^^^^
@ -194,7 +337,7 @@ LL | () if true && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:34:16
--> $DIR/feature-gate.rs:38:16
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
@ -203,7 +346,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:34:31
--> $DIR/feature-gate.rs:38:31
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
@ -212,7 +355,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:40:15
--> $DIR/feature-gate.rs:46:15
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
@ -221,7 +364,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:40:28
--> $DIR/feature-gate.rs:46:28
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
@ -230,7 +373,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:40:42
--> $DIR/feature-gate.rs:46:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
@ -239,7 +382,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:40:55
--> $DIR/feature-gate.rs:46:55
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
@ -248,7 +391,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:40:68
--> $DIR/feature-gate.rs:46:68
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
@ -257,7 +400,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:51:15
--> $DIR/feature-gate.rs:61:15
|
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -266,7 +409,7 @@ LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:68:16
--> $DIR/feature-gate.rs:78:16
|
LL | use_expr!((let 0 = 1 && 0 == 0));
| ^^^^^^^^^
@ -275,7 +418,7 @@ LL | use_expr!((let 0 = 1 && 0 == 0));
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:71:16
--> $DIR/feature-gate.rs:82:16
|
LL | use_expr!((let 0 = 1));
| ^^^^^^^^^
@ -283,6 +426,6 @@ LL | use_expr!((let 0 = 1));
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable
error: aborting due to 34 previous errors
error: aborting due to 45 previous errors
For more information about this error, try `rustc --explain E0658`.

View File

@ -0,0 +1,23 @@
#![feature(let_chains)]
fn let_or_guard(x: Result<Option<i32>, ()>) {
match x {
Ok(opt) if let Some(4) = opt || false => {}
//~^ ERROR `let` expressions are not supported here
_ => {}
}
}
fn hiding_unsafe_mod(x: Result<Option<i32>, ()>) {
match x {
Ok(opt)
if {
unsafe mod a {};
//~^ ERROR module cannot be declared unsafe
false
} => {}
_ => {}
}
}
fn main() {}

View File

@ -0,0 +1,21 @@
error: `let` expressions are not supported here
--> $DIR/ast-validate-guards.rs:5:20
|
LL | Ok(opt) if let Some(4) = opt || false => {}
| ^^^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
--> $DIR/ast-validate-guards.rs:5:38
|
LL | Ok(opt) if let Some(4) = opt || false => {}
| ^^
error: module cannot be declared unsafe
--> $DIR/ast-validate-guards.rs:15:17
|
LL | unsafe mod a {};
| ^^^^^^
error: aborting due to 2 previous errors