[go: up one dir, main page]

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

Refining struct with no invariants causes assignment might be unsafe error #755

Open
enjhnsn2 opened this issue Aug 26, 2024 · 0 comments
Open
Labels
mut-ref-unfolding Related to unfolding of mutable references

Comments

@enjhnsn2
Copy link
Collaborator

Hello, when I verify the following code implementing a SubSlice data structure:

pub struct FluxRange {
    pub start: usize,
    pub end: usize,
}

pub struct SubSlice<'a, T> {
    internal: &'a [T],
    active_range: FluxRange,
}

impl<'a, T> SubSlice<'a, T> {
    pub fn reset(&mut self) {
        self.active_range = FluxRange{ start: 0, end: self.internal.len()};
    }
}

it passes.

However, when I try to add indexes to this data structure:

#[flux_rs::refined_by(start: int, end: int)]
pub struct FluxRange {
    #[field(usize[start])]
    pub start: usize,
    #[field(usize[end])]
    pub end: usize,
}

#[flux_rs::refined_by(lo: int, hi: int, len: int)]
pub struct SubSlice<'a, T> {
    #[field(&[T][len])]
    internal: &'a [T],
    #[field(FluxRange[lo, hi])]
    active_range: FluxRange,
}

impl<'a, T> SubSlice<'a, T> {
    pub fn reset(&mut self) {
        self.active_range = FluxRange{ start: 0, end: self.internal.len()};
    }
}

it fails with:

error[E0999]: assignment might be unsafe
   --> src/main.rs:251:9
    |
251 |         self.active_range = FluxRange{ start: 0, end: self.internal.len()};
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a bit confusing to me, as I would assume that if you add indexes to a struct, it should behave the same until you try to state invariants about it (e.g. lo < hi && hi < len). By adding indexes to the fields, did I assert something about the struct's behavior?

@nilehmann nilehmann added the mut-ref-unfolding Related to unfolding of mutable references label Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mut-ref-unfolding Related to unfolding of mutable references
Projects
None yet
Development

No branches or pull requests

3 participants
@nilehmann @enjhnsn2 and others