Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using boolean operations with match expressions yields unexpected compiler errors #75632

Closed
utoalex opened this issue Aug 17, 2020 · 2 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@utoalex
Copy link

utoalex commented Aug 17, 2020

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 bools 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.

@utoalex utoalex added the C-bug Category: This is a bug. label Aug 17, 2020
@estebank
Copy link
Contributor

Output in nightly after #74650:

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`
  |
help: parentheses are required to parse this as an expression
  |
2 |     (match maybe_bool {
3 |         Some(a_bool) => a_bool,
4 |         None => true
5 |     }) && bool_slice[0]
  |

@utoalex
Copy link
Author

utoalex commented Aug 17, 2020

Oh my, my nightly compiler was not up-to-date, sorry.
Closing this issue then.

@utoalex utoalex closed this as completed Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants