Skip to content

Commit 5a25751

Browse files
committed
Add new tests for compatible variant diagnostics.
1 parent 4877756 commit 5a25751

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
enum Hey<A, B> {
2+
A(A),
3+
B(B),
4+
}
5+
6+
fn f() {}
7+
8+
fn a() -> Option<()> {
9+
while false {
10+
//~^ ERROR mismatched types
11+
f();
12+
}
13+
//~^ HELP try adding an expression
14+
}
15+
16+
fn b() -> Result<(), ()> {
17+
f()
18+
//~^ ERROR mismatched types
19+
//~| HELP try adding an expression
20+
}
21+
22+
fn main() {
23+
let _: Option<()> = while false {};
24+
//~^ ERROR mismatched types
25+
//~| HELP try wrapping
26+
let _: Option<()> = {
27+
while false {}
28+
//~^ ERROR mismatched types
29+
//~| HELP try adding an expression
30+
};
31+
let _: Result<i32, i32> = 1;
32+
//~^ ERROR mismatched types
33+
//~| HELP try wrapping
34+
let _: Option<i32> = 1;
35+
//~^ ERROR mismatched types
36+
//~| HELP try wrapping
37+
let _: Hey<i32, i32> = 1;
38+
//~^ ERROR mismatched types
39+
//~| HELP try wrapping
40+
let _: Hey<i32, bool> = false;
41+
//~^ ERROR mismatched types
42+
//~| HELP try wrapping
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/compatible-variants.rs:9:5
3+
|
4+
LL | fn a() -> Option<()> {
5+
| ---------- expected `Option<()>` because of return type
6+
LL | / while false {
7+
LL | |
8+
LL | | f();
9+
LL | | }
10+
| |_____^ expected enum `Option`, found `()`
11+
|
12+
= note: expected enum `Option<()>`
13+
found unit type `()`
14+
help: try adding an expression at the end of the block
15+
|
16+
LL ~ }
17+
LL + None
18+
|
19+
LL ~ }
20+
LL + Some(())
21+
|
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/compatible-variants.rs:17:5
25+
|
26+
LL | fn b() -> Result<(), ()> {
27+
| -------------- expected `Result<(), ()>` because of return type
28+
LL | f()
29+
| ^^^ expected enum `Result`, found `()`
30+
|
31+
= note: expected enum `Result<(), ()>`
32+
found unit type `()`
33+
help: try adding an expression at the end of the block
34+
|
35+
LL ~ f();
36+
LL + Ok(())
37+
|
38+
39+
error[E0308]: mismatched types
40+
--> $DIR/compatible-variants.rs:23:25
41+
|
42+
LL | let _: Option<()> = while false {};
43+
| ---------- ^^^^^^^^^^^^^^ expected enum `Option`, found `()`
44+
| |
45+
| expected due to this
46+
|
47+
= note: expected enum `Option<()>`
48+
found unit type `()`
49+
help: try wrapping the expression in `Some`
50+
|
51+
LL | let _: Option<()> = Some(while false {});
52+
| +++++ +
53+
54+
error[E0308]: mismatched types
55+
--> $DIR/compatible-variants.rs:27:9
56+
|
57+
LL | while false {}
58+
| ^^^^^^^^^^^^^^ expected enum `Option`, found `()`
59+
|
60+
= note: expected enum `Option<()>`
61+
found unit type `()`
62+
help: try adding an expression at the end of the block
63+
|
64+
LL ~ while false {}
65+
LL + None
66+
|
67+
LL ~ while false {}
68+
LL + Some(())
69+
|
70+
71+
error[E0308]: mismatched types
72+
--> $DIR/compatible-variants.rs:31:31
73+
|
74+
LL | let _: Result<i32, i32> = 1;
75+
| ---------------- ^ expected enum `Result`, found integer
76+
| |
77+
| expected due to this
78+
|
79+
= note: expected enum `Result<i32, i32>`
80+
found type `{integer}`
81+
help: try wrapping the expression in a variant of `Result`
82+
|
83+
LL | let _: Result<i32, i32> = Ok(1);
84+
| +++ +
85+
LL | let _: Result<i32, i32> = Err(1);
86+
| ++++ +
87+
88+
error[E0308]: mismatched types
89+
--> $DIR/compatible-variants.rs:34:26
90+
|
91+
LL | let _: Option<i32> = 1;
92+
| ----------- ^ expected enum `Option`, found integer
93+
| |
94+
| expected due to this
95+
|
96+
= note: expected enum `Option<i32>`
97+
found type `{integer}`
98+
help: try wrapping the expression in `Some`
99+
|
100+
LL | let _: Option<i32> = Some(1);
101+
| +++++ +
102+
103+
error[E0308]: mismatched types
104+
--> $DIR/compatible-variants.rs:37:28
105+
|
106+
LL | let _: Hey<i32, i32> = 1;
107+
| ------------- ^ expected enum `Hey`, found integer
108+
| |
109+
| expected due to this
110+
|
111+
= note: expected enum `Hey<i32, i32>`
112+
found type `{integer}`
113+
help: try wrapping the expression in a variant of `Hey`
114+
|
115+
LL | let _: Hey<i32, i32> = Hey::A(1);
116+
| +++++++ +
117+
LL | let _: Hey<i32, i32> = Hey::B(1);
118+
| +++++++ +
119+
120+
error[E0308]: mismatched types
121+
--> $DIR/compatible-variants.rs:40:29
122+
|
123+
LL | let _: Hey<i32, bool> = false;
124+
| -------------- ^^^^^ expected enum `Hey`, found `bool`
125+
| |
126+
| expected due to this
127+
|
128+
= note: expected enum `Hey<i32, bool>`
129+
found type `bool`
130+
help: try wrapping the expression in `Hey::B`
131+
|
132+
LL | let _: Hey<i32, bool> = Hey::B(false);
133+
| +++++++ +
134+
135+
error: aborting due to 8 previous errors
136+
137+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)