Skip to content

Commit efd3733

Browse files
committed
add test checking that 'if cond { .. }' where 'cond: &mut? bool' isn't accepted.
1 parent 8d1e5b8 commit efd3733

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Checks for `if` expressions with respect to default match bindings.
2+
// Specifically, we do not accept `if cond { ... }` where `cond: &mut? bool`.
3+
// Meanwhile, `match cond { true => ..., _ => ... }` does accept that.
4+
5+
// FIXME(@rust-lang/lang-team): consider relaxing this?
6+
7+
fn b_ref<'a>() -> &'a bool { &true }
8+
fn b_mut_ref<'a>() -> &'a mut bool { &mut true }
9+
10+
fn main() {
11+
// This is OK:
12+
match b_ref() { true => {}, _ => {} }
13+
match b_mut_ref() { true => {}, _ => {} }
14+
match &true { true => {}, _ => {} }
15+
match &mut true { true => {}, _ => {} }
16+
17+
// This is NOT:
18+
if b_ref() {} //~ ERROR mismatched types [E0308]
19+
if b_mut_ref() {} //~ ERROR mismatched types [E0308]
20+
if &true {} //~ ERROR mismatched types [E0308]
21+
if &mut true {} //~ ERROR mismatched types [E0308]
22+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/if-no-match-bindings.rs:18:8
3+
|
4+
LL | if b_ref() {}
5+
| ^^^^^^^ expected bool, found &bool
6+
|
7+
= note: expected type `bool`
8+
found type `&bool`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/if-no-match-bindings.rs:19:8
12+
|
13+
LL | if b_mut_ref() {}
14+
| ^^^^^^^^^^^ expected bool, found &mut bool
15+
|
16+
= note: expected type `bool`
17+
found type `&mut bool`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/if-no-match-bindings.rs:20:8
21+
|
22+
LL | if &true {}
23+
| ^^^^^ expected bool, found &bool
24+
|
25+
= note: expected type `bool`
26+
found type `&bool`
27+
28+
error[E0308]: mismatched types
29+
--> $DIR/if-no-match-bindings.rs:21:8
30+
|
31+
LL | if &mut true {}
32+
| ^^^^^^^^^ expected bool, found &mut bool
33+
|
34+
= note: expected type `bool`
35+
found type `&mut bool`
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)