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

Fix mutable parameters of mutable ADTs #153

Merged
merged 6 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .stainless-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
noxt-0.8.0-41-g5e334c2
noxt-0.8.0-41-g6e0a759
6 changes: 4 additions & 2 deletions demo/examples/generic_option.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate stainless;

enum Maybe<T> {
Nothing,
Just(T),
Expand All @@ -21,11 +23,11 @@ fn get_or_else<T>(maybe: Maybe<T>, default: T) -> T {
fn flatten<T>(maybemaybe: Maybe<Maybe<T>>) -> Maybe<T> {
match maybemaybe {
Maybe::Nothing => Maybe::Nothing,
Maybe::Just(maybe) => maybe
Maybe::Just(maybe) => maybe,
}
}

fn main() -> () {
pub fn main() -> () {
let x = 123;
let maybe_x = just(x);
get_or_else(maybe_x, 0);
Expand Down
12 changes: 6 additions & 6 deletions demo/examples/insertion_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl List<i32> {
#[measure(self)]
pub fn contents(&self) -> Set<i32> {
match self {
List::Nil => Set::empty(),
List::Cons(head, tail) => tail.contents().add(*head),
List::Nil => Set::new(),
List::Cons(head, tail) => tail.contents().insert(*head),
}
}

Expand Down Expand Up @@ -69,8 +69,8 @@ impl List<i32> {
#[post(
ret.size() == self.size() + 1 &&
ret.is_sorted() &&
ret.contents().is_subset_of(&self.contents().add(e)) &&
self.contents().add(e).is_subset_of(&ret.contents())
ret.contents().is_subset(&self.contents().insert(e)) &&
self.contents().insert(e).is_subset(&ret.contents())
)]
pub fn sorted_insert(self, e: i32) -> List<i32> {
match self {
Expand All @@ -85,8 +85,8 @@ impl List<i32> {
#[post(
ret.size() == self.size() &&
ret.is_sorted() &&
ret.contents().is_subset_of(&self.contents()) &&
self.contents().is_subset_of(&ret.contents())
ret.contents().is_subset(&self.contents()) &&
self.contents().is_subset(&ret.contents())
)]
pub fn sort(self) -> List<i32> {
match self {
Expand Down
9 changes: 9 additions & 0 deletions demo/examples/list_binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ impl<T> List<T> {
}
}

// Check that we can have a measure where the parameter is consumed.
#[measure(self)]
pub fn consume(self) -> () {
match self {
List::Nil => (),
List::Cons(..) => (),
}
}

#[pre(index < self.size())]
#[post(ret.is_some())]
pub fn get(&self, index: u32) -> Option<&T> {
Expand Down
35 changes: 35 additions & 0 deletions demo/examples/mut_local_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![allow(dead_code, unused_assignments)]

extern crate stainless;
use stainless::*;

#[var(field)]
struct S {
field: i32,
}

fn set_field(s: S) -> S {
// current work-around for anti-aliasing
let mut s = S { ..s };
s.field = 789;
s
}

fn set_int(mut s: i32) -> i32 {
s = 1000;
s
}

pub fn main() {
// field assignment
let mut s = S { field: 123 };
assert!(s.field == 123);
s.field = 456;
assert!(s.field == 456);

let s = set_field(s);
assert!(s.field == 789);

let i = set_int(12);
assert!(i == 1000);
}
26 changes: 26 additions & 0 deletions demo/examples/mut_local_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![allow(unused)]

extern crate stainless;
use stainless::*;

// This should pass because place is only locally mutable.
pub fn change<'a, T>(mut place: &'a T, new: &'a T) -> &'a T {
place = new;
place
}

#[pre(a > 0)]
#[post(ret > 0)]
pub fn with_spec(mut a: i32) -> i32 {
a
}

pub fn main() {
// Here, we don't modify anything locally.
let y = 10;
let z = 5;
assert!(z == 5);
let res = change(&z, &y);
assert!(z == 5);
assert!(*res == y);
}
12 changes: 3 additions & 9 deletions stainless_extraction/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@ pub(super) enum Flag {
Measure,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub(super) struct Flags {
set: HashSet<Flag>,
}

impl Flags {
fn new() -> Self {
Self {
set: HashSet::new(),
}
}

pub(super) fn add(&mut self, flag: Flag) {
self.set.insert(flag);
}
Expand Down Expand Up @@ -124,7 +118,7 @@ pub(super) fn extract_flag(
impl<'l, 'tcx> BaseExtractor<'l, 'tcx> {
pub(super) fn extract_flags(&self, carrier_hid: HirId) -> (Flags, HashMap<Symbol, Flags>) {
let attrs = self.tcx.hir().attrs(carrier_hid);
let mut carrier_flags = Flags::new();
let mut carrier_flags = Flags::default();
let mut flags_by_symbol: HashMap<Symbol, Flags> = HashMap::new();

for attr in attrs {
Expand All @@ -140,7 +134,7 @@ impl<'l, 'tcx> BaseExtractor<'l, 'tcx> {
let mut add_by_symbol = |symbol: Symbol| {
flags_by_symbol
.entry(symbol)
.or_insert_with(Flags::new)
.or_insert_with(Flags::default)
.add(flag)
};

Expand Down
4 changes: 2 additions & 2 deletions stainless_extraction/src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,15 @@ impl<'l, 'tcx> BaseExtractor<'l, 'tcx> {
pub(super) fn get_or_extract_adt(&mut self, def_id: DefId) -> &'l st::ADTSort<'l> {
self
.get_id_from_def(def_id)
.and_then(|id| self.with_extraction(|xt| xt.adts.get(id).copied()))
.and_then(|id| self.get_adt(id))
.unwrap_or_else(|| {
let adt = self.extract_adt(def_id);
self.add_adt(adt);
adt
})
}

pub(super) fn extract_adt(&mut self, def_id: DefId) -> &'l st::ADTSort<'l> {
fn extract_adt(&mut self, def_id: DefId) -> &'l st::ADTSort<'l> {
let f = self.factory();
let adt_id = self.get_or_register_def(def_id);
let adt_def = self.tcx.adt_def(def_id);
Expand Down
4 changes: 4 additions & 0 deletions stainless_extraction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ impl<'l, 'tcx> BaseExtractor<'l, 'tcx> {
})
}

fn get_adt(&self, id: StainlessSymId<'l>) -> Option<&'l st::ADTSort<'l>> {
self.with_extraction(|xt| xt.adts.get(id).copied())
}

fn add_function_ref(&mut self, def_id: DefId) {
self.with_extraction_mut(|xt| {
assert!(xt.function_refs.insert(def_id));
Expand Down
1 change: 1 addition & 0 deletions stainless_frontend/tests/extraction_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ define_tests!(
fail_extraction: double_measure_impl,
fail_verification: liskov_rectangle,
fail_extraction: mut_borrow_ref,
crash_verification: mut_immutable_field,
fail_extraction: mut_params,
crash_verification: return_in_cond,
crash_verification: return_in_guard,
Expand Down
10 changes: 10 additions & 0 deletions stainless_frontend/tests/fail/mut_immutable_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate stainless;

struct A {
b: bool,
}

fn set_false(mut a: A) -> A {
a.b = false;
a
}
20 changes: 20 additions & 0 deletions stainless_frontend/tests/pass/mut_local_fields.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code, unused_assignments)]

extern crate stainless;
use stainless::*;

Expand All @@ -6,10 +8,28 @@ struct S {
field: i32,
}

fn set_field(s: S) -> S {
// current work-around for anti-aliasing
let mut s = S { ..s };
s.field = 789;
s
}

fn set_int(mut s: i32) -> i32 {
s = 1000;
s
}

pub fn main() {
// field assignment
let mut s = S { field: 123 };
assert!(s.field == 123);
s.field = 456;
assert!(s.field == 456);

let s = set_field(s);
assert!(s.field == 789);

let i = set_int(12);
assert!(i == 1000);
}