use crate::{logic::ops::Fin, prelude::*};
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
mod fn_ghost;
pub mod invariant;
pub mod resource;
pub use fn_ghost::{FnGhost, FnGhostWrapper};
pub mod perm;
pub use perm::Container;
#[opaque]
#[intrinsic("ghost")]
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<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())
}
}
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 {}
};
}
#[cfg(creusot)]
define_objective! {}
pub(crate) struct NotObjective {}
#[cfg(creusot)]
#[trusted]
impl !Objective for NotObjective {}