Closed
Description
Hi Rust team! First of all, sorry if this has been already reported, but I couldn't find any issue related to this one.
Using a match
expression as a bool
and combining it with other bool
s using boolean operators yields compiler errors which appear to be unexpected.
Example code that reproduces the compiler errors:
fn test_match_bool(bool_slice: &[bool], maybe_bool: Option<bool>) -> bool {
match maybe_bool {
Some(a_bool) => a_bool,
None => true
} && bool_slice[0]
}
fn main() {
println!("{}", test_match_bool(&[false, true], Some(false)));
}
This yields the following compiler errors:
error[E0308]: mismatched types
--> src/main.rs:2:5
|
2 | / match maybe_bool {
3 | | Some(a_bool) => a_bool,
4 | | None => true
5 | | } && bool_slice[0]
| | ^- help: consider using a semicolon here
| |_____|
| expected `()`, found `bool`
error[E0308]: mismatched types
--> src/main.rs:5:7
|
1 | fn test_match_bool(bool_slice: &[bool], maybe_bool: Option<bool>) -> bool {
| ---- expected `bool` because of return type
...
5 | } && bool_slice[0]
| ^^^^^^^^^^^^^^^^ expected `bool`, found `&&bool`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.
It looks like the compiler fails to understand that the expression continues after the match
expression, and then reads && bool_slice[0]
as being a new expression of type &&bool
.
Oddly enough, prepending a return
keyword to the whole expression:
fn test_match_bool(bool_slice: &[bool], maybe_bool: Option<bool>) -> bool {
return match maybe_bool {
Some(a_bool) => a_bool,
None => true
} && bool_slice[0]
}
fn main() {
println!("{}", test_match_bool(&[false, true], Some(false)));
}
causes the compiler to compile the code successfully.
This happens both on stable and nightly compilers.