Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 890d544

Browse files
workingjubileemati865
authored andcommittedNov 12, 2024
Rollup merge of rust-lang#132828 - est31:let_chains_parsing_tests, r=compiler-errors
Additional tests to ensure let is rejected during parsing In the original stabilization PR, @ `compiler-errors` has [pointed out](rust-lang#94927 (comment)) that rust-lang#97295 wasn't enough to address the concerns about having `let` in expressions being rejected at parsing time, instead of later. Thankfully, since then the situation has been greatly improved by rust-lang#115677. This PR adds some additional tests to `disallowed-positions.rs`, and adds two additional revisions to the "normal" case which is now given the `feature` name: * `no_feature`: Added to incorporate `disallowed-positions-without-feature-gate.rs` into the file, reducing duplication. * `nothing`: like feature, but all functions are cfg'd out. Ensures that the errors are really emitted during parsing. cc tracking issue rust-lang#53667
2 parents 34a3fd8 + c8aa6fa commit 890d544

6 files changed

+2842
-652
lines changed
 

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions-without-feature-gate.rs

-340
This file was deleted.

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions-without-feature-gate.stderr ‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.feature.stderr

+287-159
Large diffs are not rendered by default.

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.no_feature.stderr

+1,239
Large diffs are not rendered by default.

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.stderr ‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.nofeature.stderr

+233-135
Large diffs are not rendered by default.

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.nothing.stderr

+990
Large diffs are not rendered by default.

‎tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs

+93-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ revisions: no_feature feature nothing
2+
//@ edition: 2021
13
// Here we test that `lowering` behaves correctly wrt. `let $pats = $expr` expressions.
24
//
35
// We want to make sure that `let` is banned in situations other than:
@@ -17,14 +19,16 @@
1719
//
1820
// To that end, we check some positions which is not part of the language above.
1921

20-
#![feature(let_chains)] // Avoid inflating `.stderr` with overzealous gates in this test.
22+
// Avoid inflating `.stderr` with overzealous gates (or test what happens if you disable the gate)
23+
#![cfg_attr(not(no_feature), feature(let_chains))]
2124

2225
#![allow(irrefutable_let_patterns)]
2326

2427
use std::ops::Range;
2528

2629
fn main() {}
2730

31+
#[cfg(not(nothing))]
2832
fn _if() {
2933
if (let 0 = 1) {}
3034
//~^ ERROR expected expression, found `let` statement
@@ -46,8 +50,11 @@ fn _if() {
4650
//~^ ERROR expected expression, found `let` statement
4751
//~| ERROR expected expression, found `let` statement
4852
//~| ERROR expected expression, found `let` statement
53+
//[no_feature]~| ERROR `let` expressions in this position are unstable
54+
//[no_feature]~| ERROR `let` expressions in this position are unstable
4955
}
5056

57+
#[cfg(not(nothing))]
5158
fn _while() {
5259
while (let 0 = 1) {}
5360
//~^ ERROR expected expression, found `let` statement
@@ -69,8 +76,11 @@ fn _while() {
6976
//~^ ERROR expected expression, found `let` statement
7077
//~| ERROR expected expression, found `let` statement
7178
//~| ERROR expected expression, found `let` statement
79+
//[no_feature]~| ERROR `let` expressions in this position are unstable
80+
//[no_feature]~| ERROR `let` expressions in this position are unstable
7281
}
7382

83+
#[cfg(not(nothing))]
7484
fn _macros() {
7585
macro_rules! use_expr {
7686
($e:expr) => {
@@ -79,11 +89,12 @@ fn _macros() {
7989
}
8090
}
8191
use_expr!((let 0 = 1 && 0 == 0));
82-
//~^ ERROR expected expression, found `let` statement
92+
//[feature,no_feature]~^ ERROR expected expression, found `let` statement
8393
use_expr!((let 0 = 1));
84-
//~^ ERROR expected expression, found `let` statement
94+
//[feature,no_feature]~^ ERROR expected expression, found `let` statement
8595
}
8696

97+
#[cfg(not(nothing))]
8798
fn nested_within_if_expr() {
8899
if &let 0 = 0 {}
89100
//~^ ERROR expected expression, found `let` statement
@@ -97,7 +108,7 @@ fn nested_within_if_expr() {
97108

98109
fn _check_try_binds_tighter() -> Result<(), ()> {
99110
if let 0 = 0? {}
100-
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
111+
//[feature,no_feature]~^ ERROR the `?` operator can only be applied to values that implement `Try`
101112
Ok(())
102113
}
103114
if (let 0 = 0)? {}
@@ -118,7 +129,7 @@ fn nested_within_if_expr() {
118129

119130
if true..(let 0 = 0) {}
120131
//~^ ERROR expected expression, found `let` statement
121-
//~| ERROR mismatched types
132+
//[feature,no_feature]~| ERROR mismatched types
122133
if ..(let 0 = 0) {}
123134
//~^ ERROR expected expression, found `let` statement
124135
if (let 0 = 0).. {}
@@ -127,27 +138,54 @@ fn nested_within_if_expr() {
127138
// Binds as `(let ... = true)..true &&/|| false`.
128139
if let Range { start: _, end: _ } = true..true && false {}
129140
//~^ ERROR expected expression, found `let` statement
130-
//~| ERROR mismatched types
141+
//[feature,no_feature]~| ERROR mismatched types
131142
if let Range { start: _, end: _ } = true..true || false {}
132143
//~^ ERROR expected expression, found `let` statement
133-
//~| ERROR mismatched types
144+
//[feature,no_feature]~| ERROR mismatched types
134145

135146
// Binds as `(let Range { start: F, end } = F)..(|| true)`.
136147
const F: fn() -> bool = || true;
137148
if let Range { start: F, end } = F..|| true {}
138149
//~^ ERROR expected expression, found `let` statement
139-
//~| ERROR mismatched types
150+
//[feature,no_feature]~| ERROR mismatched types
140151

141152
// Binds as `(let Range { start: true, end } = t)..(&&false)`.
142153
let t = &&true;
143154
if let Range { start: true, end } = t..&&false {}
144155
//~^ ERROR expected expression, found `let` statement
145-
//~| ERROR mismatched types
156+
//[feature,no_feature]~| ERROR mismatched types
146157

147158
if let true = let true = true {}
148159
//~^ ERROR expected expression, found `let` statement
160+
161+
if return let 0 = 0 {}
162+
//~^ ERROR expected expression, found `let` statement
163+
164+
loop { if break let 0 = 0 {} }
165+
//~^ ERROR expected expression, found `let` statement
166+
167+
if (match let 0 = 0 { _ => { false } }) {}
168+
//~^ ERROR expected expression, found `let` statement
169+
170+
if (let 0 = 0, false).1 {}
171+
//~^ ERROR expected expression, found `let` statement
172+
173+
if (let 0 = 0,) {}
174+
//~^ ERROR expected expression, found `let` statement
175+
176+
async fn foo() {
177+
if (let 0 = 0).await {}
178+
//~^ ERROR expected expression, found `let` statement
179+
}
180+
181+
if (|| let 0 = 0) {}
182+
//~^ ERROR expected expression, found `let` statement
183+
184+
if (let 0 = 0)() {}
185+
//~^ ERROR expected expression, found `let` statement
149186
}
150187

188+
#[cfg(not(nothing))]
151189
fn nested_within_while_expr() {
152190
while &let 0 = 0 {}
153191
//~^ ERROR expected expression, found `let` statement
@@ -161,7 +199,7 @@ fn nested_within_while_expr() {
161199

162200
fn _check_try_binds_tighter() -> Result<(), ()> {
163201
while let 0 = 0? {}
164-
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
202+
//[feature,no_feature]~^ ERROR the `?` operator can only be applied to values that implement `Try`
165203
Ok(())
166204
}
167205
while (let 0 = 0)? {}
@@ -182,7 +220,7 @@ fn nested_within_while_expr() {
182220

183221
while true..(let 0 = 0) {}
184222
//~^ ERROR expected expression, found `let` statement
185-
//~| ERROR mismatched types
223+
//[feature,no_feature]~| ERROR mismatched types
186224
while ..(let 0 = 0) {}
187225
//~^ ERROR expected expression, found `let` statement
188226
while (let 0 = 0).. {}
@@ -191,27 +229,54 @@ fn nested_within_while_expr() {
191229
// Binds as `(let ... = true)..true &&/|| false`.
192230
while let Range { start: _, end: _ } = true..true && false {}
193231
//~^ ERROR expected expression, found `let` statement
194-
//~| ERROR mismatched types
232+
//[feature,no_feature]~| ERROR mismatched types
195233
while let Range { start: _, end: _ } = true..true || false {}
196234
//~^ ERROR expected expression, found `let` statement
197-
//~| ERROR mismatched types
235+
//[feature,no_feature]~| ERROR mismatched types
198236

199237
// Binds as `(let Range { start: F, end } = F)..(|| true)`.
200238
const F: fn() -> bool = || true;
201239
while let Range { start: F, end } = F..|| true {}
202240
//~^ ERROR expected expression, found `let` statement
203-
//~| ERROR mismatched types
241+
//[feature,no_feature]~| ERROR mismatched types
204242

205243
// Binds as `(let Range { start: true, end } = t)..(&&false)`.
206244
let t = &&true;
207245
while let Range { start: true, end } = t..&&false {}
208246
//~^ ERROR expected expression, found `let` statement
209-
//~| ERROR mismatched types
247+
//[feature,no_feature]~| ERROR mismatched types
210248

211249
while let true = let true = true {}
212250
//~^ ERROR expected expression, found `let` statement
251+
252+
while return let 0 = 0 {}
253+
//~^ ERROR expected expression, found `let` statement
254+
255+
'outer: loop { while break 'outer let 0 = 0 {} }
256+
//~^ ERROR expected expression, found `let` statement
257+
258+
while (match let 0 = 0 { _ => { false } }) {}
259+
//~^ ERROR expected expression, found `let` statement
260+
261+
while (let 0 = 0, false).1 {}
262+
//~^ ERROR expected expression, found `let` statement
263+
264+
while (let 0 = 0,) {}
265+
//~^ ERROR expected expression, found `let` statement
266+
267+
async fn foo() {
268+
while (let 0 = 0).await {}
269+
//~^ ERROR expected expression, found `let` statement
270+
}
271+
272+
while (|| let 0 = 0) {}
273+
//~^ ERROR expected expression, found `let` statement
274+
275+
while (let 0 = 0)() {}
276+
//~^ ERROR expected expression, found `let` statement
213277
}
214278

279+
#[cfg(not(nothing))]
215280
fn not_error_because_clarified_intent() {
216281
if let Range { start: _, end: _ } = (true..true || false) { }
217282

@@ -222,6 +287,7 @@ fn not_error_because_clarified_intent() {
222287
while let Range { start: _, end: _ } = (true..true && false) { }
223288
}
224289

290+
#[cfg(not(nothing))]
225291
fn outside_if_and_while_expr() {
226292
&let 0 = 0;
227293
//~^ ERROR expected expression, found `let` statement
@@ -232,10 +298,12 @@ fn outside_if_and_while_expr() {
232298
//~^ ERROR expected expression, found `let` statement
233299
-let 0 = 0;
234300
//~^ ERROR expected expression, found `let` statement
301+
let _ = let _ = 3;
302+
//~^ ERROR expected expression, found `let` statement
235303

236304
fn _check_try_binds_tighter() -> Result<(), ()> {
237305
let 0 = 0?;
238-
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
306+
//[feature,no_feature]~^ ERROR the `?` operator can only be applied to values that implement `Try`
239307
Ok(())
240308
}
241309
(let 0 = 0)?;
@@ -260,8 +328,8 @@ fn outside_if_and_while_expr() {
260328
//~^ ERROR expected expression, found `let` statement
261329

262330
(let Range { start: _, end: _ } = true..true || false);
263-
//~^ ERROR mismatched types
264-
//~| ERROR expected expression, found `let` statement
331+
//~^ ERROR expected expression, found `let` statement
332+
//[feature,no_feature]~| ERROR mismatched types
265333

266334
(let true = let true = true);
267335
//~^ ERROR expected expression, found `let` statement
@@ -285,6 +353,7 @@ fn outside_if_and_while_expr() {
285353
}
286354

287355
// Let's make sure that `let` inside const generic arguments are considered.
356+
#[cfg(not(nothing))]
288357
fn inside_const_generic_arguments() {
289358
struct A<const B: bool>;
290359
impl<const B: bool> A<{B}> { const O: u32 = 5; }
@@ -317,6 +386,7 @@ fn inside_const_generic_arguments() {
317386
>::O == 5 {}
318387
}
319388

389+
#[cfg(not(nothing))]
320390
fn with_parenthesis() {
321391
let opt = Some(Some(1i32));
322392

@@ -332,6 +402,7 @@ fn with_parenthesis() {
332402
//~| ERROR expected expression, found `let` statement
333403
}
334404
if let Some(a) = opt && (true && true) {
405+
//[no_feature]~^ ERROR `let` expressions in this position are unstable
335406
}
336407

337408
if (let Some(a) = opt && (let Some(b) = a)) && b == 1 {
@@ -347,14 +418,18 @@ fn with_parenthesis() {
347418
}
348419

349420
if (true && (true)) && let Some(a) = opt {
421+
//[no_feature]~^ ERROR `let` expressions in this position are unstable
350422
}
351423
if (true) && let Some(a) = opt {
424+
//[no_feature]~^ ERROR `let` expressions in this position are unstable
352425
}
353426
if true && let Some(a) = opt {
427+
//[no_feature]~^ ERROR `let` expressions in this position are unstable
354428
}
355429

356430
let fun = || true;
357431
if let true = (true && fun()) && (true) {
432+
//[no_feature]~^ ERROR `let` expressions in this position are unstable
358433
}
359434

360435
#[cfg(FALSE)]

0 commit comments

Comments
 (0)
Please sign in to comment.