Skip to content

Commit 6b4e833

Browse files
committed
let_chains: Add tests for places where let expressions aren't allowed.
1 parent 635021b commit 6b4e833

File tree

2 files changed

+635
-0
lines changed

2 files changed

+635
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Here we test that `ast_validation` behaves correctly wrt. `let $pats = $expr` expressions.
2+
//
3+
// We want to make sure that `let` is banned in situations other than:
4+
//
5+
// expr =
6+
// | ...
7+
// | "if" expr_with_let block {"else" block}?
8+
// | {label ":"}? while" expr_with_let block
9+
// ;
10+
//
11+
// expr_with_let =
12+
// | "let" top_pats "=" expr
13+
// | expr_with_let "&&" expr_with_let
14+
// | "(" expr_with_let ")"
15+
// | expr
16+
// ;
17+
//
18+
// To that end, we check some positions which is not part of the language above.
19+
20+
#![allow(irrefutable_let_patterns)]
21+
22+
use std::ops::Range;
23+
24+
fn main() {}
25+
26+
fn nested_within_if_expr() {
27+
if &let 0 = 0 {} //~ ERROR `let` expressions are not supported here
28+
//~^ ERROR `let` expressions only supported in `if`
29+
30+
if !let 0 = 0 {} //~ ERROR `let` expressions are not supported here
31+
if *let 0 = 0 {} //~ ERROR `let` expressions are not supported here
32+
if -let 0 = 0 {} //~ ERROR `let` expressions are not supported here
33+
34+
fn _check_try_binds_tighter() -> Result<(), ()> {
35+
if let 0 = 0? {}
36+
Ok(())
37+
}
38+
if (let 0 = 0)? {} //~ ERROR `let` expressions are not supported here
39+
40+
if true || let 0 = 0 {} //~ ERROR `let` expressions are not supported here
41+
if (true || let 0 = 0) {} //~ ERROR `let` expressions are not supported here
42+
if true && (true || let 0 = 0) {} //~ ERROR `let` expressions are not supported here
43+
if true || (true && let 0 = 0) {} //~ ERROR `let` expressions are not supported here
44+
45+
let mut x = true;
46+
if x = let 0 = 0 {} //~ ERROR `let` expressions are not supported here
47+
48+
if true..(let 0 = 0) {} //~ ERROR `let` expressions are not supported here
49+
if ..(let 0 = 0) {} //~ ERROR `let` expressions are not supported here
50+
if (let 0 = 0).. {} //~ ERROR `let` expressions are not supported here
51+
52+
// Binds as `(let ... = true)..true &&/|| false`.
53+
if let Range { start: _, end: _ } = true..true && false {}
54+
//~^ ERROR `let` expressions are not supported here
55+
if let Range { start: _, end: _ } = true..true || false {}
56+
//~^ ERROR `let` expressions are not supported here
57+
58+
// Binds as `(let Range { start: F, end } = F)..(|| true)`.
59+
const F: fn() -> bool = || true;
60+
if let Range { start: F, end } = F..|| true {}
61+
//~^ ERROR `let` expressions are not supported here
62+
63+
// Binds as `(let Range { start: true, end } = t)..(&&false)`.
64+
let t = &&true;
65+
if let Range { start: true, end } = t..&&false {}
66+
//~^ ERROR `let` expressions are not supported here
67+
68+
if let true = let true = true {} //~ ERROR `let` expressions are not supported here
69+
}
70+
71+
fn nested_within_while_expr() {
72+
while &let 0 = 0 {} //~ ERROR `let` expressions are not supported here
73+
74+
while !let 0 = 0 {} //~ ERROR `let` expressions are not supported here
75+
while *let 0 = 0 {} //~ ERROR `let` expressions are not supported here
76+
while -let 0 = 0 {} //~ ERROR `let` expressions are not supported here
77+
78+
fn _check_try_binds_tighter() -> Result<(), ()> {
79+
while let 0 = 0? {}
80+
Ok(())
81+
}
82+
while (let 0 = 0)? {} //~ ERROR `let` expressions are not supported here
83+
84+
while true || let 0 = 0 {} //~ ERROR `let` expressions are not supported here
85+
while (true || let 0 = 0) {} //~ ERROR `let` expressions are not supported here
86+
while true && (true || let 0 = 0) {} //~ ERROR `let` expressions are not supported here
87+
while true || (true && let 0 = 0) {} //~ ERROR `let` expressions are not supported here
88+
89+
let mut x = true;
90+
while x = let 0 = 0 {} //~ ERROR `let` expressions are not supported here
91+
92+
while true..(let 0 = 0) {} //~ ERROR `let` expressions are not supported here
93+
while ..(let 0 = 0) {} //~ ERROR `let` expressions are not supported here
94+
while (let 0 = 0).. {} //~ ERROR `let` expressions are not supported here
95+
96+
// Binds as `(let ... = true)..true &&/|| false`.
97+
while let Range { start: _, end: _ } = true..true && false {}
98+
//~^ ERROR `let` expressions are not supported here
99+
while let Range { start: _, end: _ } = true..true || false {}
100+
//~^ ERROR `let` expressions are not supported here
101+
102+
// Binds as `(let Range { start: F, end } = F)..(|| true)`.
103+
const F: fn() -> bool = || true;
104+
while let Range { start: F, end } = F..|| true {}
105+
//~^ ERROR `let` expressions are not supported here
106+
107+
// Binds as `(let Range { start: true, end } = t)..(&&false)`.
108+
let t = &&true;
109+
while let Range { start: true, end } = t..&&false {}
110+
//~^ ERROR `let` expressions are not supported here
111+
112+
while let true = let true = true {} //~ ERROR `let` expressions are not supported here
113+
}
114+
115+
fn not_error_because_clarified_intent() {
116+
if let Range { start: _, end: _ } = (true..true || false) { }
117+
118+
if let Range { start: _, end: _ } = (true..true && false) { }
119+
120+
while let Range { start: _, end: _ } = (true..true || false) { }
121+
122+
while let Range { start: _, end: _ } = (true..true && false) { }
123+
}
124+
125+
fn outside_if_and_while_expr() {
126+
&let 0 = 0; //~ ERROR `let` expressions are not supported here
127+
128+
!let 0 = 0; //~ ERROR `let` expressions are not supported here
129+
*let 0 = 0; //~ ERROR `let` expressions are not supported here
130+
-let 0 = 0; //~ ERROR `let` expressions are not supported here
131+
132+
fn _check_try_binds_tighter() -> Result<(), ()> {
133+
let 0 = 0?;
134+
Ok(())
135+
}
136+
(let 0 = 0)?; //~ ERROR `let` expressions are not supported here
137+
138+
true || let 0 = 0; //~ ERROR `let` expressions are not supported here
139+
(true || let 0 = 0); //~ ERROR `let` expressions are not supported here
140+
true && (true || let 0 = 0); //~ ERROR `let` expressions are not supported here
141+
142+
let mut x = true;
143+
x = let 0 = 0; //~ ERROR `let` expressions are not supported here
144+
145+
true..(let 0 = 0); //~ ERROR `let` expressions are not supported here
146+
..(let 0 = 0); //~ ERROR `let` expressions are not supported here
147+
(let 0 = 0)..; //~ ERROR `let` expressions are not supported here
148+
149+
(let Range { start: _, end: _ } = true..true || false);
150+
//~^ ERROR `let` expressions are not supported here
151+
152+
(let true = let true = true);
153+
//~^ ERROR `let` expressions are not supported here
154+
//~| ERROR `let` expressions are not supported here
155+
156+
// Check function tail position.
157+
&let 0 = 0
158+
//~^ ERROR `let` expressions are not supported here
159+
}

0 commit comments

Comments
 (0)