flux-rs / flux

Refinement Types for Rust
MIT License
632 stars 17 forks source link

Global const cannot be referenced in refined function signature #572

Closed enjhnsn2 closed 9 months ago

enjhnsn2 commented 9 months ago

This code verifies correctly:

#[flux::sig(fn() -> i32{r: r == 8})]
fn test() -> i32 {
    8
}

but this code fails:

pub const EIGHT: i32 = 8;

#[flux::sig(fn() -> i32{r: r == EIGHT})]
fn test() -> i32 {
    8
}

with the error:

error[FLUX]: cannot find value `EIGHT` in this scope
 --> src/lib.rs:3:33
  |
3 | #[flux::sig(fn() -> i32{r: r == EIGHT})]
  |                                 ^^^^^ not found in this scope

error: aborting due to previous error
nilehmann commented 9 months ago

This is a known limitation. Constants have to be explicitly marked so they can be used inside refinements like so:

#[flux::constant]
pub const EIGHT: i32 = 8;

#[flux::sig(fn() -> i32{r: r == EIGHT})]
fn test() -> i32 {
    8
}

The annotation will make the constant globally available no matter where it was defined.

The underlying issue is that there's no easy way to resolve names into rust items. The compiler doesn't offer an API for this. So we need to reimplement name resolution or find a good enough hack.

enjhnsn2 commented 9 months ago

Noted, I was not aware of the #[flux::constant] API. I will close the issue