Skip to content

Add tests for mutability #574

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

Merged
merged 12 commits into from
May 9, 2025
2 changes: 1 addition & 1 deletion kmir/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "kmir"
version = "0.3.156"
version = "0.3.157"
description = ""
requires-python = "~=3.10"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion kmir/src/kmir/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from typing import Final

VERSION: Final = '0.3.156'
VERSION: Final = '0.3.157'
31 changes: 31 additions & 0 deletions kmir/src/tests/integration/data/prove-rs/interior-mut-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::cell::RefCell;

struct Counter {
value: RefCell<i32>,
}

impl Counter {
fn new(start: i32) -> Self {
Counter { value: RefCell::new(start) }
}

fn increment(&self) {
*self.value.borrow_mut() += 1;
}

fn get(&self) -> i32 {
*self.value.borrow()
}
}

fn main() {
let counter = Counter::new(0);
// println!("Before: {}", counter.get());

// We only have &counter, but can still mutate inside
counter.increment();
counter.increment();

assert!(2 == counter.get());
// println!("After: {}", counter.get());
}
31 changes: 31 additions & 0 deletions kmir/src/tests/integration/data/prove-rs/interior-mut2-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::cell::Cell;

struct Counter {
value: Cell<i32>,
}

impl Counter {
fn new(start: i32) -> Self {
Counter { value: Cell::new(start) }
}

fn increment(&self) {
self.value.set(self.get() + 1);
}

fn get(&self) -> i32 {
self.value.get()
}
}

fn main() {
let counter = Counter::new(0);
// println!("Before: {}", counter.get());

// We only have &counter, but can still mutate inside
counter.increment();
counter.increment();

assert!(2 == counter.get());
// println!("After: {}", counter.get());
}
11 changes: 11 additions & 0 deletions kmir/src/tests/integration/data/prove-rs/interior-mut3-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::cell::UnsafeCell;

fn main() {
let data = UnsafeCell::new(0);

unsafe {
*data.get() += 42;
}

assert!(data.into_inner() == 42)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

┌─ 1 (root, init)
│ #init ( symbol ( "interior_mut_fail" ) globalAllocEntry ( 2 , Memory ( allocatio
│ function: main
│ span: 288
│ (138 steps)
└─ 3 (stuck, leaf)
rvalueAddressOf ( mutabilityNot , place ( ... local: local ( 1 ) , projection: p
span: 88


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

┌─ 1 (root, init)
│ #init ( symbol ( "interior_mut2_fail" ) globalAllocEntry ( 1 , Memory ( allocati
│ function: main
│ span: 120
│ (129 steps)
└─ 3 (stuck, leaf)
rvalueAddressOf ( mutabilityNot , place ( ... local: local ( 1 ) , projection: p
span: 50


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

┌─ 1 (root, init)
│ #init ( symbol ( "interior_mut3_fail" ) globalAllocEntry ( 1 , Memory ( allocati
│ function: main
│ span: 67
│ (48 steps)
└─ 3 (stuck, leaf)
rvalueAddressOf ( mutabilityNot , place ( ... local: local ( 1 ) , projection: p
span: 52


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram


16 changes: 16 additions & 0 deletions kmir/src/tests/integration/data/ub/mut_const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
let mut prior = 1;
let x = 10;

// println!("Before: {}", x);
let _y = &x; // <-- need something else on the stack for the offset to work
let prior_mut = &mut prior as *mut i32;

unsafe {
let prior_alias = prior_mut.add(1);
*prior_alias = 20;
}

// println!("After: {}", x);
assert!(x == 20);
}
3 changes: 3 additions & 0 deletions kmir/src/tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ def test_prove(spec: Path, tmp_path: Path, kmir: KMIR) -> None:
PROVING_FILES = list(PROVING_DIR.glob('*.rs'))
PROVE_RS_SHOW_SPECS = [
'local-raw-fail',
'interior-mut-fail',
'interior-mut2-fail',
'interior-mut3-fail',
'assert_eq_exp-fail',
'bitwise-fail',
]
Expand Down
2 changes: 1 addition & 1 deletion kmir/uv.lock

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

2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.156
0.3.157