Skip to content

add loop-invariants and harnesses #16

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/flux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

env:
FIXPOINT_VERSION: "556104ba5508891c357b0bdf819ce706e93d9349"
FLUX_VERSION: "f6bdf90c54ad6eed51b25c125f251cac01cfe170"
FLUX_VERSION: "ebafb8d0ca32d8c0fcd2a0cfef6b1b4bd4dc4a6f"

jobs:
check-flux-on-core:
Expand Down
44 changes: 19 additions & 25 deletions library/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ safety = { path = "../contracts/safety" }
[features]
compiler-builtins-mem = ['compiler_builtins/mem']
compiler-builtins-c = ["compiler_builtins/c"]
compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
compiler-builtins-no-f16-f128 = ["compiler_builtins/no-f16-f128"]
compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
# Make panics and failed asserts immediately abort without formatting any message
panic_immediate_abort = ["core/panic_immediate_abort"]
# Choose algorithms that are optimized for binary size instead of runtime performance
Expand Down
7 changes: 4 additions & 3 deletions library/alloc/src/boxed/thin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::error::Error;
use core::fmt::{self, Debug, Display, Formatter};
#[cfg(not(no_global_oom_handling))]
use core::intrinsics::const_allocate;
use core::intrinsics::{const_allocate, const_make_global};
use core::marker::PhantomData;
#[cfg(not(no_global_oom_handling))]
use core::marker::Unsize;
Expand Down Expand Up @@ -340,9 +340,10 @@ impl<H> WithHeader<H> {
alloc.add(metadata_offset).cast();
// SAFETY: `*metadata_ptr` is within the allocation.
metadata_ptr.write(ptr::metadata::<Dyn>(ptr::dangling::<T>() as *const Dyn));

// SAFETY: valid heap allocation
const_make_global(alloc);
// SAFETY: we have just written the metadata.
&*(metadata_ptr)
&*metadata_ptr
}
};

Expand Down
54 changes: 50 additions & 4 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
unsafe { self.tail.as_mut().map(|node| &mut node.as_mut().element) }
}

/// Adds an element first in the list.
/// Adds an element to the front of the list.
///
/// This operation should compute in *O*(1) time.
///
Expand All @@ -844,11 +844,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_front(&mut self, elt: T) {
let _ = self.push_front_mut(elt);
}

/// Adds an element to the front of the list, returning a reference to it.
///
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
/// ```
/// #![feature(push_mut)]
/// use std::collections::LinkedList;
///
/// let mut dl = LinkedList::from([1, 2, 3]);
///
/// let ptr = dl.push_front_mut(2);
/// *ptr += 4;
/// assert_eq!(dl.front().unwrap(), &6);
/// ```
#[unstable(feature = "push_mut", issue = "135974")]
#[must_use = "if you don't need a reference to the value, use `LinkedList::push_front` instead"]
pub fn push_front_mut(&mut self, elt: T) -> &mut T {
let node = Box::new_in(Node::new(elt), &self.alloc);
let node_ptr = NonNull::from(Box::leak(node));
let mut node_ptr = NonNull::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
unsafe {
self.push_front_node(node_ptr);
&mut node_ptr.as_mut().element
}
}

Expand Down Expand Up @@ -876,7 +899,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
self.pop_front_node().map(Node::into_element)
}

/// Appends an element to the back of a list.
/// Adds an element to the back of the list.
///
/// This operation should compute in *O*(1) time.
///
Expand All @@ -893,11 +916,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_confusables("push", "append")]
pub fn push_back(&mut self, elt: T) {
let _ = self.push_back_mut(elt);
}

/// Adds an element to the back of the list, returning a reference to it.
///
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
/// ```
/// #![feature(push_mut)]
/// use std::collections::LinkedList;
///
/// let mut dl = LinkedList::from([1, 2, 3]);
///
/// let ptr = dl.push_back_mut(2);
/// *ptr += 4;
/// assert_eq!(dl.back().unwrap(), &6);
/// ```
#[unstable(feature = "push_mut", issue = "135974")]
#[must_use = "if you don't need a reference to the value, use `LinkedList::push_back` instead"]
pub fn push_back_mut(&mut self, elt: T) -> &mut T {
let node = Box::new_in(Node::new(elt), &self.alloc);
let node_ptr = NonNull::from(Box::leak(node));
let mut node_ptr = NonNull::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
unsafe {
self.push_back_node(node_ptr);
&mut node_ptr.as_mut().element
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
// this branch is never taken.
// We use `#[cold]` instead of `#[inline(never)]`, because inlining this
// function into the general case (`.drain(n..m)`) is fine.
// See `tests/codegen/vecdeque-drain.rs` for a test.
// See `tests/codegen-llvm/vecdeque-drain.rs` for a test.
#[cold]
fn join_head_and_tail_wrapping<T, A: Allocator>(
source_deque: &mut VecDeque<T, A>,
Expand Down
6 changes: 6 additions & 0 deletions library/alloc/src/collections/vec_deque/iter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
#[cfg(kani)]
use core::kani;
use core::num::NonZero;
use core::ops::Try;
use core::{fmt, mem, slice};

use safety::requires;

/// An iterator over the elements of a `VecDeque`.
///
/// This `struct` is created by the [`iter`] method on [`super::VecDeque`]. See its
Expand Down Expand Up @@ -144,6 +148,8 @@ impl<'a, T> Iterator for Iter<'a, T> {
}

#[inline]
#[requires(idx < self.len())]
#[cfg_attr(kani, kani::modifies(self))]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
// Safety: The TrustedRandomAccess contract requires that callers only pass an index
// that is in bounds.
Expand Down
6 changes: 6 additions & 0 deletions library/alloc/src/collections/vec_deque/iter_mut.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
#[cfg(kani)]
use core::kani;
use core::num::NonZero;
use core::ops::Try;
use core::{fmt, mem, slice};

use safety::requires;

/// A mutable iterator over the elements of a `VecDeque`.
///
/// This `struct` is created by the [`iter_mut`] method on [`super::VecDeque`]. See its
Expand Down Expand Up @@ -208,6 +212,8 @@ impl<'a, T> Iterator for IterMut<'a, T> {
}

#[inline]
#[requires(idx < self.len())]
#[cfg_attr(kani, kani::modifies(self))]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
// Safety: The TrustedRandomAccess contract requires that callers only pass an index
// that is in bounds.
Expand Down
Loading
Loading