Skip to content

Commit aef3f5f

Browse files
committed
removed gate and added test for drop order
1 parent 5796073 commit aef3f5f

File tree

9 files changed

+94
-29
lines changed

9 files changed

+94
-29
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,31 +3407,19 @@ impl<'a> Parser<'a> {
34073407
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<P<Expr>>> {
34083408
// Used to check the `if_let_guard` feature mostly by scanning
34093409
// `&&` tokens.
3410-
fn has_let_expr(expr: &Expr) -> bool {
3411-
match &expr.kind {
3412-
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
3413-
let lhs_rslt = has_let_expr(lhs);
3414-
let rhs_rslt = has_let_expr(rhs);
3415-
lhs_rslt || rhs_rslt
3416-
}
3417-
ExprKind::Let(..) => true,
3418-
_ => false,
3419-
}
3420-
}
34213410
if !self.eat_keyword(exp!(If)) {
34223411
// No match arm guard present.
34233412
return Ok(None);
34243413
}
34253414

3426-
let if_span = self.prev_token.span;
34273415
let mut cond = self.parse_match_guard_condition()?;
34283416

3429-
CondChecker::new(self, LetChainsPolicy::AlwaysAllowed).visit_expr(&mut cond);
3417+
CondChecker::new(
3418+
self,
3419+
LetChainsPolicy::EditionDependent { current_edition: Edition::Edition2024 },
3420+
)
3421+
.visit_expr(&mut cond);
34303422

3431-
if has_let_expr(&cond) {
3432-
let span = if_span.to(cond.span);
3433-
self.psess.gated_spans.gate(sym::if_let_guard, span);
3434-
}
34353423
Ok(Some(cond))
34363424
}
34373425

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//@ run-pass
2+
//@revisions: edition2015 edition2018 edition2021 edition2024
3+
//@[edition2015] edition:2015
4+
//@[edition2018] edition:2018
5+
//@[edition2021] edition:2021
6+
//@[edition2024] edition:2024
7+
8+
#![allow(irrefutable_let_patterns)]
9+
10+
use std::sync::Mutex;
11+
12+
static A: Mutex<Vec<i32>> = Mutex::new(Vec::new());
13+
14+
struct D(i32);
15+
16+
fn make_d(x: i32) -> D {
17+
A.lock().unwrap().push(x);
18+
D(x)
19+
}
20+
21+
impl Drop for D {
22+
fn drop(&mut self) {
23+
A.lock().unwrap().push(!self.0);
24+
}
25+
}
26+
27+
fn if_let_guard(num: i32) {
28+
let _d = make_d(1);
29+
match num {
30+
1 | 2 if let D(ref _x) = make_d(2) => {
31+
make_d(3);
32+
}
33+
_ => {}
34+
}
35+
}
36+
37+
fn if_let(num: i32) {
38+
let _d = make_d(1);
39+
match num {
40+
1 | 2 => {
41+
if let D(ref _x) = make_d(2) {
42+
make_d(3);
43+
}
44+
}
45+
_ => {}
46+
}
47+
}
48+
49+
fn main() {
50+
if_let(1);
51+
if_let(2);
52+
if_let_guard(1);
53+
if_let_guard(2);
54+
let expected =
55+
[1, 2, 3, !3, !2, !1, 1, 2, 3, !3, !2, !1,
56+
// Here is two parts, first one is for basic if let inside the match arm
57+
// And second part is for if let guard
58+
1, 2, 3, !3, !2, !1, 1, 2, 3, !3, !2, !1];
59+
assert_eq!(*A.lock().unwrap(), expected);
60+
}

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.edition2021.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: use of moved value: `x`
2-
--> $DIR/move-guard-if-let-chain.rs:14:23
2+
--> $DIR/move-guard-if-let-chain.rs:15:23
33
|
44
LL | let x: Box<_> = Box::new(1);
55
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -15,7 +15,7 @@ LL | (1, 2) if let ref y = x && c => (),
1515
| +++
1616

1717
error[E0382]: use of moved value: `x`
18-
--> $DIR/move-guard-if-let-chain.rs:38:23
18+
--> $DIR/move-guard-if-let-chain.rs:39:23
1919
|
2020
LL | let x: Box<_> = Box::new(1);
2121
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -31,7 +31,7 @@ LL | (1, _) if let ref y = x && c => (),
3131
| +++
3232

3333
error[E0382]: use of moved value: `x`
34-
--> $DIR/move-guard-if-let-chain.rs:61:32
34+
--> $DIR/move-guard-if-let-chain.rs:62:32
3535
|
3636
LL | let x: Box<_> = Box::new(1);
3737
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -45,7 +45,7 @@ LL | (1, _) | (_, 2) if let ref y = x && c => (),
4545
| +++
4646

4747
error[E0382]: use of moved value: `x`
48-
--> $DIR/move-guard-if-let-chain.rs:84:16
48+
--> $DIR/move-guard-if-let-chain.rs:85:16
4949
|
5050
LL | let x: Box<_> = Box::new(1);
5151
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -61,7 +61,7 @@ LL | (1, 2) if let ref y = x && c => false,
6161
| +++
6262

6363
error[E0382]: use of moved value: `x`
64-
--> $DIR/move-guard-if-let-chain.rs:105:41
64+
--> $DIR/move-guard-if-let-chain.rs:106:41
6565
|
6666
LL | let x: Box<_> = Box::new(1);
6767
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.edition2024.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: use of moved value: `x`
2-
--> $DIR/move-guard-if-let-chain.rs:14:23
2+
--> $DIR/move-guard-if-let-chain.rs:15:23
33
|
44
LL | let x: Box<_> = Box::new(1);
55
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -15,7 +15,7 @@ LL | (1, 2) if let ref y = x && c => (),
1515
| +++
1616

1717
error[E0382]: use of moved value: `x`
18-
--> $DIR/move-guard-if-let-chain.rs:38:23
18+
--> $DIR/move-guard-if-let-chain.rs:39:23
1919
|
2020
LL | let x: Box<_> = Box::new(1);
2121
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -31,7 +31,7 @@ LL | (1, _) if let ref y = x && c => (),
3131
| +++
3232

3333
error[E0382]: use of moved value: `x`
34-
--> $DIR/move-guard-if-let-chain.rs:61:32
34+
--> $DIR/move-guard-if-let-chain.rs:62:32
3535
|
3636
LL | let x: Box<_> = Box::new(1);
3737
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -45,7 +45,7 @@ LL | (1, _) | (_, 2) if let ref y = x && c => (),
4545
| +++
4646

4747
error[E0382]: use of moved value: `x`
48-
--> $DIR/move-guard-if-let-chain.rs:84:16
48+
--> $DIR/move-guard-if-let-chain.rs:85:16
4949
|
5050
LL | let x: Box<_> = Box::new(1);
5151
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
@@ -61,7 +61,7 @@ LL | (1, 2) if let ref y = x && c => false,
6161
| +++
6262

6363
error[E0382]: use of moved value: `x`
64-
--> $DIR/move-guard-if-let-chain.rs:105:41
64+
--> $DIR/move-guard-if-let-chain.rs:106:41
6565
|
6666
LL | let x: Box<_> = Box::new(1);
6767
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//@[edition2021] edition:2021
33
//@[edition2024] edition:2024
44

5+
#![cfg_attr(edition2021, feature(let_chains))]
56
#![allow(irrefutable_let_patterns)]
67

78
fn same_pattern(c: bool) {

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ fn main() {
3838

3939
match v {
4040
(1, 2) if let y = x => false,
41-
_ => { *x == 1 },
41+
_ => *x == 1,
4242
};
4343
}

tests/ui/rfcs/rfc-2294-if-let-guard/shadowing.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//@[edition2021] edition:2021
66
//@[edition2024] edition:2024
77

8+
#![cfg_attr(edition2021, feature(let_chains))]
9+
810
fn main() {
911
let x: Option<Option<i32>> = Some(Some(6));
1012
match x {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//@ run-pass
1+
#[rustfmt::skip]
22
fn main() {
33
match true {
44
_ if let true = true && true => {}
5+
//~^ ERROR `let` expressions in this position are unstable
56
_ => {}
67
}
78
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: `let` expressions in this position are unstable
2+
--> $DIR/issue-93150.rs:4:14
3+
|
4+
LL | _ if let true = true && true => {}
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
8+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)