Skip to content

Commit cbb0fff

Browse files
committed
Fix let_chains and if_let_guard feature flags
1 parent ecf7299 commit cbb0fff

File tree

5 files changed

+112
-23
lines changed

5 files changed

+112
-23
lines changed

compiler/rustc_parse/src/parser/expr.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -2383,16 +2383,30 @@ impl<'a> Parser<'a> {
23832383
}
23842384

23852385
pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
2386+
fn check_let_expr(expr: &Expr) -> (bool, bool) {
2387+
match expr.kind {
2388+
ExprKind::Binary(_, ref lhs, ref rhs) => {
2389+
let lhs_rslt = check_let_expr(lhs);
2390+
let rhs_rslt = check_let_expr(rhs);
2391+
(lhs_rslt.0 || rhs_rslt.0, false)
2392+
}
2393+
ExprKind::Let(..) => (true, true),
2394+
_ => (false, true),
2395+
}
2396+
}
23862397
let attrs = self.parse_outer_attributes()?;
23872398
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
23882399
let lo = this.token.span;
23892400
let pat = this.parse_pat_allow_top_alt(None, RecoverComma::Yes, RecoverColon::Yes)?;
23902401
let guard = if this.eat_keyword(kw::If) {
23912402
let if_span = this.prev_token.span;
23922403
let cond = this.parse_expr()?;
2393-
if let ExprKind::Let(..) = cond.kind {
2394-
// Remove the last feature gating of a `let` expression since it's stable.
2395-
this.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
2404+
let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond);
2405+
if has_let_expr {
2406+
if does_not_have_bin_op {
2407+
// Remove the last feature gating of a `let` expression since it's stable.
2408+
this.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
2409+
}
23962410
let span = if_span.to(cond.span);
23972411
this.sess.gated_spans.gate(sym::if_let_guard, span);
23982412
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ fn _if_let_guard() {
1414
//~^ ERROR `let` expressions in this position are unstable
1515

1616
() if true && let 0 = 1 => {}
17-
//~^ ERROR `let` expressions in this position are unstable
17+
//~^ ERROR `if let` guards are experimental
18+
//~| ERROR `let` expressions in this position are unstable
1819

1920
() if let 0 = 1 && true => {}
20-
//~^ ERROR `let` expressions in this position are unstable
21+
//~^ ERROR `if let` guards are experimental
22+
//~| ERROR `let` expressions in this position are unstable
2123

2224
() if (let 0 = 1) && true => {}
2325
//~^ ERROR `let` expressions in this position are unstable
@@ -30,14 +32,17 @@ fn _if_let_guard() {
3032
//~| ERROR `let` expressions in this position are unstable
3133

3234
() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
33-
//~^ ERROR `let` expressions in this position are unstable
35+
//~^ ERROR `if let` guards are experimental
36+
//~| ERROR `let` expressions in this position are unstable
3437
//~| ERROR `let` expressions in this position are unstable
3538
//~| ERROR `let` expressions in this position are unstable
3639
//~| ERROR `let` expressions in this position are unstable
3740
//~| ERROR `let` expressions in this position are unstable
3841

3942
() if let Range { start: _, end: _ } = (true..true) && false => {}
40-
//~^ ERROR `let` expressions in this position are unstable
43+
//~^ ERROR `if let` guards are experimental
44+
//~| ERROR `let` expressions in this position are unstable
45+
4146
_ => {}
4247
}
4348
}

src/test/ui/rfc-2294-if-let-guard/feature-gate.stderr

+56-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: no rules expected the token `let`
2-
--> $DIR/feature-gate.rs:64:15
2+
--> $DIR/feature-gate.rs:69:15
33
|
44
LL | macro_rules! use_expr {
55
| --------------------- when calling this macro
@@ -18,7 +18,47 @@ LL | () if let 0 = 1 => {}
1818
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
1919

2020
error[E0658]: `if let` guards are experimental
21-
--> $DIR/feature-gate.rs:60:12
21+
--> $DIR/feature-gate.rs:16:12
22+
|
23+
LL | () if true && let 0 = 1 => {}
24+
| ^^^^^^^^^^^^^^^^^^^^
25+
|
26+
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
27+
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
28+
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
29+
30+
error[E0658]: `if let` guards are experimental
31+
--> $DIR/feature-gate.rs:20:12
32+
|
33+
LL | () if let 0 = 1 && true => {}
34+
| ^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
37+
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
38+
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
39+
40+
error[E0658]: `if let` guards are experimental
41+
--> $DIR/feature-gate.rs:34:12
42+
|
43+
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
|
46+
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
47+
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
48+
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
49+
50+
error[E0658]: `if let` guards are experimental
51+
--> $DIR/feature-gate.rs:42:12
52+
|
53+
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
|
56+
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
57+
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
58+
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
59+
60+
error[E0658]: `if let` guards are experimental
61+
--> $DIR/feature-gate.rs:65:12
2262
|
2363
LL | () if let 0 = 1 => {}
2464
| ^^^^^^^^^^^^
@@ -55,7 +95,7 @@ LL | () if true && let 0 = 1 => {}
5595
= help: add `#![feature(let_chains)]` to the crate attributes to enable
5696

5797
error[E0658]: `let` expressions in this position are unstable
58-
--> $DIR/feature-gate.rs:19:15
98+
--> $DIR/feature-gate.rs:20:15
5999
|
60100
LL | () if let 0 = 1 && true => {}
61101
| ^^^^^^^^^
@@ -64,7 +104,7 @@ LL | () if let 0 = 1 && true => {}
64104
= help: add `#![feature(let_chains)]` to the crate attributes to enable
65105

66106
error[E0658]: `let` expressions in this position are unstable
67-
--> $DIR/feature-gate.rs:22:16
107+
--> $DIR/feature-gate.rs:24:16
68108
|
69109
LL | () if (let 0 = 1) && true => {}
70110
| ^^^^^^^^^
@@ -73,7 +113,7 @@ LL | () if (let 0 = 1) && true => {}
73113
= help: add `#![feature(let_chains)]` to the crate attributes to enable
74114

75115
error[E0658]: `let` expressions in this position are unstable
76-
--> $DIR/feature-gate.rs:25:24
116+
--> $DIR/feature-gate.rs:27:24
77117
|
78118
LL | () if true && (let 0 = 1) => {}
79119
| ^^^^^^^^^
@@ -82,7 +122,7 @@ LL | () if true && (let 0 = 1) => {}
82122
= help: add `#![feature(let_chains)]` to the crate attributes to enable
83123

84124
error[E0658]: `let` expressions in this position are unstable
85-
--> $DIR/feature-gate.rs:28:16
125+
--> $DIR/feature-gate.rs:30:16
86126
|
87127
LL | () if (let 0 = 1) && (let 0 = 1) => {}
88128
| ^^^^^^^^^
@@ -91,7 +131,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
91131
= help: add `#![feature(let_chains)]` to the crate attributes to enable
92132

93133
error[E0658]: `let` expressions in this position are unstable
94-
--> $DIR/feature-gate.rs:28:31
134+
--> $DIR/feature-gate.rs:30:31
95135
|
96136
LL | () if (let 0 = 1) && (let 0 = 1) => {}
97137
| ^^^^^^^^^
@@ -100,7 +140,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
100140
= help: add `#![feature(let_chains)]` to the crate attributes to enable
101141

102142
error[E0658]: `let` expressions in this position are unstable
103-
--> $DIR/feature-gate.rs:32:15
143+
--> $DIR/feature-gate.rs:34:15
104144
|
105145
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
106146
| ^^^^^^^^^
@@ -109,7 +149,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
109149
= help: add `#![feature(let_chains)]` to the crate attributes to enable
110150

111151
error[E0658]: `let` expressions in this position are unstable
112-
--> $DIR/feature-gate.rs:32:28
152+
--> $DIR/feature-gate.rs:34:28
113153
|
114154
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
115155
| ^^^^^^^^^
@@ -118,7 +158,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
118158
= help: add `#![feature(let_chains)]` to the crate attributes to enable
119159

120160
error[E0658]: `let` expressions in this position are unstable
121-
--> $DIR/feature-gate.rs:32:42
161+
--> $DIR/feature-gate.rs:34:42
122162
|
123163
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
124164
| ^^^^^^^^^
@@ -127,7 +167,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
127167
= help: add `#![feature(let_chains)]` to the crate attributes to enable
128168

129169
error[E0658]: `let` expressions in this position are unstable
130-
--> $DIR/feature-gate.rs:32:55
170+
--> $DIR/feature-gate.rs:34:55
131171
|
132172
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
133173
| ^^^^^^^^^
@@ -136,7 +176,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
136176
= help: add `#![feature(let_chains)]` to the crate attributes to enable
137177

138178
error[E0658]: `let` expressions in this position are unstable
139-
--> $DIR/feature-gate.rs:32:68
179+
--> $DIR/feature-gate.rs:34:68
140180
|
141181
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
142182
| ^^^^^^^^^
@@ -145,7 +185,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
145185
= help: add `#![feature(let_chains)]` to the crate attributes to enable
146186

147187
error[E0658]: `let` expressions in this position are unstable
148-
--> $DIR/feature-gate.rs:39:15
188+
--> $DIR/feature-gate.rs:42:15
149189
|
150190
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
151191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -154,7 +194,7 @@ LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
154194
= help: add `#![feature(let_chains)]` to the crate attributes to enable
155195

156196
error[E0658]: `let` expressions in this position are unstable
157-
--> $DIR/feature-gate.rs:54:16
197+
--> $DIR/feature-gate.rs:59:16
158198
|
159199
LL | use_expr!((let 0 = 1 && 0 == 0));
160200
| ^^^^^^^^^
@@ -163,14 +203,14 @@ LL | use_expr!((let 0 = 1 && 0 == 0));
163203
= help: add `#![feature(let_chains)]` to the crate attributes to enable
164204

165205
error[E0658]: `let` expressions in this position are unstable
166-
--> $DIR/feature-gate.rs:56:16
206+
--> $DIR/feature-gate.rs:61:16
167207
|
168208
LL | use_expr!((let 0 = 1));
169209
| ^^^^^^^^^
170210
|
171211
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
172212
= help: add `#![feature(let_chains)]` to the crate attributes to enable
173213

174-
error: aborting due to 19 previous errors
214+
error: aborting due to 23 previous errors
175215

176216
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match true {
3+
_ if let true = true && true => {}
4+
//~^ ERROR `if let` guards are
5+
//~| ERROR `let` expressions in this
6+
_ => {}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0658]: `if let` guards are experimental
2+
--> $DIR/issue-93150.rs:3:11
3+
|
4+
LL | _ if let true = true && true => {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
8+
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
9+
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`
10+
11+
error[E0658]: `let` expressions in this position are unstable
12+
--> $DIR/issue-93150.rs:3:14
13+
|
14+
LL | _ if let true = true && true => {}
15+
| ^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
18+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
19+
20+
error: aborting due to 2 previous errors
21+
22+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)