prusti-dev icon indicating copy to clipboard operation
prusti-dev copied to clipboard

Failing parsing of `... && if let ...`

Open fpoli opened this issue 1 year ago • 1 comments

Prusti fails to parse the following contract.

use prusti_contracts::*;

#[requires(
    true &&
    if let Some(_) = x { // <-- ERROR: expected `=`
        true
    } else {
        true
    }
)]
fn foo(x: &Option<i32>) { }

fpoli avatar Apr 11 '23 09:04 fpoli

This example shows another issue when parsing &&, predicate accepts syntax that is not allowed in normal Rust code:

use prusti_contracts::*;

predicate!{
    fn test_pred<T>(x: &Option<T>) -> bool {
        match x {
            _ => true,
        } && true
    }
}

fn test_fn<T>(x: &Option<T>) -> bool {
    match x {
        _ => true,
    } && true
}

Prusti accepts test_pred, but test_fn fails to compile with:

mismatched types
expected `bool`, found `&&bool`
predicate_parsing.rs(11, 33): expected `bool` because of return type
predicate_parsing.rs(12, 5): parentheses are required to parse this as an expression: `(`, `)`

Adding brackets around the match works in both cases:

predicate!{
    fn test_pred_fixed<T>(x: &Option<T>) -> bool {
        (match x {
            _ => true,
        }) && true
    }
}

fn test_fn_fixed<T>(x: &Option<T>) -> bool {
    (match x {
        _ => true,
    }) && true
}

Patrick-6 avatar Apr 13 '23 12:04 Patrick-6