Skip to content

Add support for transitive modifies clause #3372

Description

@celinval

Requested feature: Enable contracts for functions that modify memory via encapsulated pointer.
Use case: Kani contracts require that all writeable memory to be specified. However, they may not be visible in the current context
Link to relevant documentation (Rust reference, Nomicon, RFC):

Test case:

mod heap {
#[derive(Default)]
pub struct HeapArray<T, const N: usize> {
  inner: Box<[T; N]>,
}

impl<T, const N: usize> HeapArray<T, N> {
  pub fn set(&mut self, val: T, idx: usize) {
  }
}
}

mod other {
  struct Stack<T> {
     buf: HeapArray,
     next: usize,
  }

  impl<T> Stack<T> {
    #[modifies(buf)] // This is not enough today
    #[kani::ensures(|_| self.contains(val))]
    fn enqueue(&mut self, val: T) {
      assert!(!self.is_full());
      self.buf.set(val, next);
    }
  }
}

Activity

  1. added
    [C] Feature / Enhancement
    on Jul 23, 2024
  2. added this to the Function Contracts milestone on Jul 25, 2024
  3. celinval commented on Jul 25, 2024

    @celinval
    ContributorAuthor

    A good example here is Arc, which contains a pointer to an ArcInner which includes the object itself, but also an atomic counter which is changed in get_mut.

  4. added
    Z-Contracts
    on Aug 1, 2024
  5. self-assigned this
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-Contracts[C] Feature / Enhancement

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions