mod fn_ghost;
pub mod invariant;
pub mod lifetime_logic;
pub mod perm;
pub mod resource;
mod shared;
pub use self::{
fn_ghost::{FnGhost, FnGhostWrapper},
perm::Perm,
shared::GhostShared,
};
#[cfg(creusot)]
use crate::resolve::{resolve, structural_resolve};
use crate::{logic::ops::Fin, prelude::*};
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
#[intrinsic("ghost")]
#[builtin("identity")]
pub struct Ghost<T: ?Sized>(PhantomData<T>);
impl<T: Copy> Copy for Ghost<T> {}
impl<T: Clone + Copy> Clone for Ghost<T> {
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
ghost!(**self)
}
}
impl<T: ?Sized> Deref for Ghost<T> {
type Target = T;
#[trusted]
#[check(ghost)]
#[requires(false)] #[ensures(result == &**self)]
#[intrinsic("ghost_deref")]
fn deref(&self) -> &Self::Target {
panic!()
}
}
impl<T: ?Sized> DerefMut for Ghost<T> {
#[trusted]
#[check(ghost)]
#[requires(false)] #[ensures(result == &mut **self)]
#[intrinsic("ghost_deref_mut")]
fn deref_mut(&mut self) -> &mut Self::Target {
panic!()
}
}
impl<T: ?Sized + Fin> Fin for Ghost<T> {
type Target = T::Target;
#[logic(open, prophetic, inline)]
fn fin<'a>(self) -> &'a Self::Target {
pearlite! { &^*self }
}
}
impl<T: ?Sized> Invariant for Ghost<T> {
#[logic(open, prophetic, inline)]
#[creusot::trusted_trivial_if_param_trivial]
fn invariant(self) -> bool {
inv(*self)
}
}
impl<'a, T: ?Sized> Resolve for Ghost<T> {
#[logic(open, inline, prophetic)]
#[creusot::trusted_trivial_if_param_trivial]
fn resolve(self) -> bool {
resolve(*self)
}
#[trusted]
#[logic]
#[requires(structural_resolve(self))]
#[ensures(self.resolve())]
fn resolve_coherence(self) {}
}
impl<T: ?Sized> Ghost<T> {
#[trusted]
#[erasure(_)]
#[check(ghost)]
#[ensures(**result == **self)]
pub fn borrow(&self) -> Ghost<&T> {
Ghost::conjure()
}
#[trusted]
#[erasure(_)]
#[check(ghost)]
#[ensures(*result == &mut **self)]
pub fn borrow_mut(&mut self) -> Ghost<&mut T> {
Ghost::conjure()
}
#[trusted]
#[erasure(_)]
#[check(ghost)]
#[requires(false)]
pub fn conjure() -> Self {
Ghost(PhantomData)
}
#[cfg(not(creusot))]
#[doc(hidden)]
pub fn from_fn(_: impl FnOnce() -> T) -> Self {
Ghost::conjure()
}
}
impl<T: ?Sized> Ghost<T> {
#[trusted]
#[check(ghost)]
#[ensures(*result == x)]
#[intrinsic("ghost_new")]
pub fn new(x: T) -> Self
where
T: Sized,
{
let _ = x;
panic!()
}
#[trusted]
#[logic(opaque)]
#[ensures(*result == x)]
pub fn new_logic(x: T) -> Self {
dead
}
#[trusted]
#[check(ghost)]
#[ensures(result == *self)]
#[intrinsic("ghost_into_inner")]
pub fn into_inner(self) -> T
where
T: Sized,
{
panic!()
}
#[logic]
#[builtin("identity")]
pub fn inner_logic(self) -> T
where
T: Sized,
{
dead
}
}
impl<T, U: ?Sized> Ghost<(T, U)> {
#[check(ghost)]
#[trusted]
#[erasure(_)]
#[ensures((*self).0 == *result.0)]
#[ensures((*self).1 == *result.1)]
pub fn split(self) -> (Ghost<T>, Ghost<U>) {
(Ghost::conjure(), Ghost::conjure())
}
}
macro_rules! split_ghost {
( $( ( $t:ident , $n:literal ) )+ ) => {
impl<$( $t , )+> Ghost<($( $t ),+)> {
#[check(ghost)]
#[trusted]
$(
#[ensures((*self).$n == *result.$n)]
)+
pub fn split(self) -> ($( Ghost<$t> ),+) {
($( { split_ghost!(@ignore $t); Ghost::conjure() } ),+)
}
}
};
(@ignore $($t:tt)*) => {};
}
split_ghost! { (T1, 0) (T2, 1) (T3, 2) }
split_ghost! { (T1, 0) (T2, 1) (T3, 2) (T4, 3) }
split_ghost! { (T1, 0) (T2, 1) (T3, 2) (T4, 3) (T5, 4) }
split_ghost! { (T1, 0) (T2, 1) (T3, 2) (T4, 3) (T5, 4) (T6, 5) }
split_ghost! { (T1, 0) (T2, 1) (T3, 2) (T4, 3) (T5, 4) (T6, 5) (T7, 6) }
split_ghost! { (T1, 0) (T2, 1) (T3, 2) (T4, 3) (T5, 4) (T6, 5) (T7, 6) (T8, 7) }
pub trait Plain: Copy {
#[ensures(*result == *snap)]
#[check(ghost)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self>;
}
impl Plain for bool {
#[trusted]
#[ensures(*result == *snap)]
#[check(ghost)]
#[allow(unused_variables)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self> {
Ghost::conjure()
}
}
impl<T> Plain for *const T {
#[trusted]
#[ensures(*result == *snap)]
#[check(ghost)]
#[allow(unused_variables)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self> {
Ghost::conjure()
}
}
impl<T> Plain for *mut T {
#[trusted]
#[ensures(*result == *snap)]
#[check(ghost)]
#[allow(unused_variables)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self> {
Ghost::conjure()
}
}
#[cfg(creusot)]
macro_rules! define_objective {
() => {
#[trusted]
pub auto trait Objective {}
#[trusted]
impl !Objective for NotObjective {}
};
}
#[cfg(creusot)]
define_objective! {}
#[derive(Copy)]
pub struct NotObjective {}
impl Clone for NotObjective {
#[ensures(result == *self)]
fn clone(&self) -> Self {
*self
}
}