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 598f865

Browse files
committedMar 25, 2025
Combine several test files into one
This makes it a lot easier to add smaller regression tests related to "incorrectly placed" struct literals.
1 parent 9f336ce commit 598f865

16 files changed

+263
-372
lines changed
 

‎tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-for.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-for.stderr

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-if.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-if.stderr

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-match-discriminant.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-match-discriminant.stderr

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-while.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-in-while.stderr

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-restrictions-in-lamda.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-restrictions-in-lamda.stderr

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-variant-in-if.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎tests/ui/parser/struct-literal-variant-in-if.stderr

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
fn main() {
2+
if Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
3+
println!("yo");
4+
}
5+
if let true = Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
6+
println!("yo");
7+
}
8+
9+
for x in Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
10+
//~^ ERROR `bool` is not an iterator
11+
println!("yo");
12+
}
13+
14+
while Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
15+
println!("yo");
16+
}
17+
while let true = Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
18+
println!("yo");
19+
}
20+
21+
match Foo { x: 3 } { //~ ERROR struct literals are not allowed here
22+
Foo { x: x } => {}
23+
}
24+
25+
let _ = |x: E| {
26+
let field = true;
27+
if x == E::V { field } {}
28+
//~^ ERROR expected value, found struct variant `E::V`
29+
//~| ERROR mismatched types
30+
if x == E::I { field1: true, field2: 42 } {}
31+
//~^ ERROR struct literals are not allowed here
32+
if x == E::V { field: false } {}
33+
//~^ ERROR struct literals are not allowed here
34+
if x == E::J { field: -42 } {}
35+
//~^ ERROR struct literals are not allowed here
36+
if x == E::K { field: "" } {}
37+
//~^ ERROR struct literals are not allowed here
38+
let y: usize = ();
39+
//~^ ERROR mismatched types
40+
};
41+
42+
// Regression test for <https://github.com/rust-lang/rust/issues/43412>.
43+
while || Foo { x: 3 }.hi() { //~ ERROR struct literals are not allowed here
44+
//~^ ERROR mismatched types
45+
println!("yo");
46+
}
47+
48+
// Regression test for <https://github.com/rust-lang/rust/issues/82051>.
49+
if Foo { x: one(), }.hi() { //~ ERROR struct literals are not allowed here
50+
println!("Positive!");
51+
}
52+
}
53+
54+
struct Foo {
55+
x: isize,
56+
}
57+
58+
impl Foo {
59+
fn hi(&self) -> bool {
60+
true
61+
}
62+
}
63+
64+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
65+
enum E {
66+
V { field: bool },
67+
I { field1: bool, field2: usize },
68+
J { field: isize },
69+
K { field: &'static str},
70+
}
71+
72+
fn one() -> isize { 1 }
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
error: struct literals are not allowed here
2+
--> $DIR/struct-literals-in-invalid-places.rs:2:8
3+
|
4+
LL | if Foo { x: 3 }.hi() {
5+
| ^^^^^^^^^^^^
6+
|
7+
help: surround the struct literal with parentheses
8+
|
9+
LL | if (Foo { x: 3 }).hi() {
10+
| + +
11+
12+
error: struct literals are not allowed here
13+
--> $DIR/struct-literals-in-invalid-places.rs:5:19
14+
|
15+
LL | if let true = Foo { x: 3 }.hi() {
16+
| ^^^^^^^^^^^^
17+
|
18+
help: surround the struct literal with parentheses
19+
|
20+
LL | if let true = (Foo { x: 3 }).hi() {
21+
| + +
22+
23+
error: struct literals are not allowed here
24+
--> $DIR/struct-literals-in-invalid-places.rs:9:14
25+
|
26+
LL | for x in Foo { x: 3 }.hi() {
27+
| ^^^^^^^^^^^^
28+
|
29+
help: surround the struct literal with parentheses
30+
|
31+
LL | for x in (Foo { x: 3 }).hi() {
32+
| + +
33+
34+
error: struct literals are not allowed here
35+
--> $DIR/struct-literals-in-invalid-places.rs:14:11
36+
|
37+
LL | while Foo { x: 3 }.hi() {
38+
| ^^^^^^^^^^^^
39+
|
40+
help: surround the struct literal with parentheses
41+
|
42+
LL | while (Foo { x: 3 }).hi() {
43+
| + +
44+
45+
error: struct literals are not allowed here
46+
--> $DIR/struct-literals-in-invalid-places.rs:17:22
47+
|
48+
LL | while let true = Foo { x: 3 }.hi() {
49+
| ^^^^^^^^^^^^
50+
|
51+
help: surround the struct literal with parentheses
52+
|
53+
LL | while let true = (Foo { x: 3 }).hi() {
54+
| + +
55+
56+
error: struct literals are not allowed here
57+
--> $DIR/struct-literals-in-invalid-places.rs:21:11
58+
|
59+
LL | match Foo { x: 3 } {
60+
| ^^^^^^^^^^^^
61+
|
62+
help: surround the struct literal with parentheses
63+
|
64+
LL | match (Foo { x: 3 }) {
65+
| + +
66+
67+
error: struct literals are not allowed here
68+
--> $DIR/struct-literals-in-invalid-places.rs:30:17
69+
|
70+
LL | if x == E::I { field1: true, field2: 42 } {}
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
|
73+
help: surround the struct literal with parentheses
74+
|
75+
LL | if x == (E::I { field1: true, field2: 42 }) {}
76+
| + +
77+
78+
error: struct literals are not allowed here
79+
--> $DIR/struct-literals-in-invalid-places.rs:32:17
80+
|
81+
LL | if x == E::V { field: false } {}
82+
| ^^^^^^^^^^^^^^^^^^^^^
83+
|
84+
help: surround the struct literal with parentheses
85+
|
86+
LL | if x == (E::V { field: false }) {}
87+
| + +
88+
89+
error: struct literals are not allowed here
90+
--> $DIR/struct-literals-in-invalid-places.rs:34:17
91+
|
92+
LL | if x == E::J { field: -42 } {}
93+
| ^^^^^^^^^^^^^^^^^^^
94+
|
95+
help: surround the struct literal with parentheses
96+
|
97+
LL | if x == (E::J { field: -42 }) {}
98+
| + +
99+
100+
error: struct literals are not allowed here
101+
--> $DIR/struct-literals-in-invalid-places.rs:36:17
102+
|
103+
LL | if x == E::K { field: "" } {}
104+
| ^^^^^^^^^^^^^^^^^^
105+
|
106+
help: surround the struct literal with parentheses
107+
|
108+
LL | if x == (E::K { field: "" }) {}
109+
| + +
110+
111+
error: struct literals are not allowed here
112+
--> $DIR/struct-literals-in-invalid-places.rs:43:14
113+
|
114+
LL | while || Foo { x: 3 }.hi() {
115+
| ^^^^^^^^^^^^
116+
|
117+
help: surround the struct literal with parentheses
118+
|
119+
LL | while || (Foo { x: 3 }).hi() {
120+
| + +
121+
122+
error: struct literals are not allowed here
123+
--> $DIR/struct-literals-in-invalid-places.rs:49:8
124+
|
125+
LL | if Foo { x: one(), }.hi() {
126+
| ^^^^^^^^^^^^^^^^^
127+
|
128+
help: surround the struct literal with parentheses
129+
|
130+
LL | if (Foo { x: one(), }).hi() {
131+
| + +
132+
133+
error[E0277]: `bool` is not an iterator
134+
--> $DIR/struct-literals-in-invalid-places.rs:9:14
135+
|
136+
LL | for x in Foo { x: 3 }.hi() {
137+
| ^^^^^^^^^^^^^^^^^ `bool` is not an iterator
138+
|
139+
= help: the trait `Iterator` is not implemented for `bool`
140+
= note: required for `bool` to implement `IntoIterator`
141+
142+
error[E0533]: expected value, found struct variant `E::V`
143+
--> $DIR/struct-literals-in-invalid-places.rs:27:17
144+
|
145+
LL | if x == E::V { field } {}
146+
| ^^^^ not a value
147+
|
148+
help: you might have meant to create a new value of the struct
149+
|
150+
LL | if x == (E::V { field }) {}
151+
| + +
152+
153+
error[E0308]: mismatched types
154+
--> $DIR/struct-literals-in-invalid-places.rs:27:24
155+
|
156+
LL | if x == E::V { field } {}
157+
| ---------------^^^^^--
158+
| | |
159+
| | expected `()`, found `bool`
160+
| expected this to be `()`
161+
|
162+
help: you might have meant to return this value
163+
|
164+
LL | if x == E::V { return field; } {}
165+
| ++++++ +
166+
167+
error[E0308]: mismatched types
168+
--> $DIR/struct-literals-in-invalid-places.rs:38:24
169+
|
170+
LL | let y: usize = ();
171+
| ----- ^^ expected `usize`, found `()`
172+
| |
173+
| expected due to this
174+
175+
error[E0308]: mismatched types
176+
--> $DIR/struct-literals-in-invalid-places.rs:43:11
177+
|
178+
LL | while || Foo { x: 3 }.hi() {
179+
| ^^^^^^^^^^^^^^^^^^^^ expected `bool`, found closure
180+
|
181+
= note: expected type `bool`
182+
found closure `{closure@$DIR/struct-literals-in-invalid-places.rs:43:11: 43:13}`
183+
help: use parentheses to call this closure
184+
|
185+
LL | while (|| Foo { x: 3 }.hi())() {
186+
| + +++
187+
188+
error: aborting due to 17 previous errors
189+
190+
Some errors have detailed explanations: E0277, E0308, E0533.
191+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)
Please sign in to comment.