Skip to content

Commit 763790f

Browse files
authored
Add SendPerm and SyncPerm to allow implementing Send and Sync on Perm in external libraries. (#2030)
2 parents dc5a196 + 3a92e8e commit 763790f

5 files changed

Lines changed: 32 additions & 29 deletions

File tree

creusot-std/src/cell/permcell.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! track of the logical value.
55
66
use crate::{
7-
ghost::perm::{Container, Perm},
7+
ghost::perm::{Container, Perm, SendPerm, SyncPerm},
88
prelude::*,
99
};
1010
use core::cell::UnsafeCell;
@@ -28,18 +28,13 @@ pub struct PermCell<T: ?Sized>(UnsafeCell<T>);
2828

2929
impl<T: Sized> Container for PermCell<T> {
3030
type Value = T;
31-
32-
#[logic(open, inline)]
33-
fn is_disjoint(&self, _: &T, other: &Self, _: &T) -> bool {
34-
self != other
35-
}
3631
}
3732

3833
unsafe impl<T> Send for PermCell<T> {}
3934
unsafe impl<T> Sync for PermCell<T> {}
4035

41-
unsafe impl<T: Send> Send for Perm<PermCell<T>> {}
42-
unsafe impl<T: Sync> Sync for Perm<PermCell<T>> {}
36+
impl<T> SendPerm for PermCell<T> {}
37+
impl<T> SyncPerm for PermCell<T> {}
4338

4439
impl<T: Sized> Invariant for Perm<PermCell<T>> {
4540
#[logic(open, prophetic, inline)]

creusot-std/src/ghost/perm.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ use core::marker::PhantomData;
99
pub trait Container {
1010
type Value: ?Sized;
1111

12-
#[logic]
13-
fn is_disjoint(&self, self_val: &Self::Value, other: &Self, other_val: &Self::Value) -> bool;
12+
#[logic(open, inline)]
13+
fn is_disjoint(&self, _self_val: &Self::Value, other: &Self, _other_val: &Self::Value) -> bool {
14+
self != other
15+
}
1416
}
1517

18+
pub trait SendPerm: Container {}
19+
pub trait SyncPerm: Container {}
20+
1621
/// Token that represents the ownership of the contents of a container object. The container is
1722
/// either an interrior mutable type (e.g., `Perm` or atomic types) or a raw pointer.
1823
///
@@ -59,7 +64,14 @@ pub trait Container {
5964
/// Certain facts about the layout and alignment of pointers can be made available
6065
/// through the type invariant of [`crate::std::ptr::PtrLive`] by calling [`Perm::live`].
6166
#[opaque]
62-
pub struct Perm<C: ?Sized + Container>(NotObjective, #[allow(unused)] [PhantomData<C::Value>]);
67+
pub struct Perm<C: ?Sized + Container>(
68+
NotObjective,
69+
#[allow(unused)] *mut (),
70+
#[allow(unused)] [PhantomData<C::Value>],
71+
);
72+
73+
unsafe impl<C: ?Sized + SendPerm> Send for Perm<C> {}
74+
unsafe impl<C: ?Sized + SyncPerm> Sync for Perm<C> {}
6375

6476
impl<C: ?Sized + Container> Perm<C> {
6577
/// Returns the underlying container that is managed by this permission.

creusot-std/src/std/sync/atomic.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
use crate::ghost::Objective;
33

44
use crate::{
5-
ghost::{Container, FnGhost, perm::Perm},
5+
ghost::{
6+
Container, FnGhost,
7+
perm::{Perm, SendPerm, SyncPerm},
8+
},
69
logic::FMap,
710
prelude::*,
811
std::sync::{
@@ -45,20 +48,15 @@ macro_rules! impl_atomic {
4548
#[doc = concat!("Creusot wrapper around [`std::sync::atomic::", stringify!($atomic_type), "`].")]
4649
pub struct $atomic_type $(< $T >)?(::std::sync::atomic::$atomic_type $(< $T >)?);
4750

48-
unsafe impl $(< $T >)? Send for Perm<$atomic_type $(< $T >)?> {}
49-
unsafe impl $(< $T >)? Sync for Perm<$atomic_type $(< $T >)?> {}
51+
impl $(< $T >)? SendPerm for $atomic_type $(< $T >)? {}
52+
impl $(< $T >)? SyncPerm for $atomic_type $(< $T >)? {}
5053

5154
#[cfg(creusot)]
5255
#[trusted]
5356
impl $(< $T >)? Objective for Perm<$atomic_type $(< $T >)?> {}
5457

5558
impl $(< $T >)? Container for $atomic_type $(< $T >)? {
5659
type Value = FMap<Timestamp, ($type, SyncView)>;
57-
58-
#[logic(open, inline)]
59-
fn is_disjoint(&self, _: &Self::Value, other: &Self, _: &Self::Value) -> bool {
60-
self != other
61-
}
6260
}
6361

6462
impl $(< $T >)? HasTimestamp for $atomic_type $(< $T >)? {

creusot-std/src/std/sync/atomic_sc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::{
2-
ghost::{Container, FnGhost, perm::Perm},
2+
ghost::{
3+
Container, FnGhost,
4+
perm::{Perm, SendPerm, SyncPerm},
5+
},
36
prelude::*,
47
std::sync::{
58
atomic::{Ordering, Ordering::Ordering as _},
@@ -14,18 +17,13 @@ macro_rules! impl_atomic {
1417
#[doc = concat!("Creusot wrapper around [`std::sync::atomic::", stringify!($atomic_type), "`].")]
1518
pub struct $atomic_type $(< $T >)?(::std::sync::atomic::$atomic_type $(< $T >)?);
1619

17-
unsafe impl $(< $T >)? Send for Perm<$atomic_type $(< $T >)?> {}
18-
unsafe impl $(< $T >)? Sync for Perm<$atomic_type $(< $T >)?> {}
19-
2020
impl $(< $T >)? Container for $atomic_type $(< $T >)? {
2121
type Value = $type;
22-
23-
#[logic(open, inline)]
24-
fn is_disjoint(&self, _: &Self::Value, other: &Self, _: &Self::Value) -> bool {
25-
self != other
26-
}
2722
}
2823

24+
impl $(< $T >)? SendPerm for $atomic_type $(< $T >)? {}
25+
impl $(< $T >)? SyncPerm for $atomic_type $(< $T >)? {}
26+
2927
impl $(< $T >)? $atomic_type $(< $T >)? {
3028
#[ensures(*result.1.val() == val)]
3129
#[ensures(*result.1.ward() == result.0)]

examples/persistent_array.coma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ module M_implementation__impl_PersistentArray_T__set (* implementation::Persiste
20652065
[ return (result: t_Perm_PermCell_Inner_T) -> {[@stop_split] [@expl:deref ensures] result = self}
20662066
(! return {result}) ]
20672067

2068-
predicate is_disjoint_PermCell_Inner_T [@inline:trivial] (self: t_PermCell_Inner_T) (_2: t_Inner_T) (other: t_PermCell_Inner_T) (_4: t_Inner_T) =
2068+
predicate is_disjoint_PermCell_Inner_T [@inline:trivial] (self: t_PermCell_Inner_T) (_self_val: t_Inner_T) (other: t_PermCell_Inner_T) (_other_val: t_Inner_T) =
20692069
self <> other
20702070

20712071
meta "rewrite_def" predicate is_disjoint_PermCell_Inner_T

0 commit comments

Comments
 (0)