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

Fixes signature of enter #9

Merged
merged 4 commits into from
Mar 16, 2024
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
4 changes: 2 additions & 2 deletions src/box_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ where
/// - If the passed function panics.
/// - If the underlying future panics.
/// - If the underlying future awaits for a future other than the [`crate::FrozenFuture`].
pub fn enter<'borrow, Output: 'borrow, G>(&'borrow mut self, f: G) -> Output
pub fn enter<'borrow, Output, G>(&'borrow mut self, f: G) -> Output
where
G: for<'a> FnOnce(&'a mut <T as Family<'a>>::Family) -> Output,
G: for<'a> FnOnce(&'borrow mut <T as Family<'a>>::Family) -> Output,
{
// SAFETY:
// 1. `self.0` is valid as a post-condition of `new_typed`.
Expand Down
49 changes: 48 additions & 1 deletion src/counterexamples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//!
//! ## Covariant escapes to outer
//!
//! ```compile_fail,E0521
//! ```compile_fail,E0597
//! use nolife::{scope, BoxScope, Family};
//! use std::cell::Cell;
//!
Expand Down Expand Up @@ -263,6 +263,53 @@
//! }
//! ```
//!
//! # Signature of `enter` is not sound, #7 <https://github.com/dureuill/nolife/issues/7>
//!
//! Example lifted as-is from [@steffahn](https://github.com/steffahn)
//!
//! ```compile_fail,E0597,E0499
//! use nolife::{BoxScope, Family, TimeCapsule, scope};
//!
//! struct Foo<'a> {
//! s: String,
//! r: Option<&'a mut String>,
//! }
//!
//! struct FooFamily;
//!
//! impl<'a> Family<'a> for FooFamily {
//! type Family = Foo<'a>;
//! }
//!
//! fn storing_own_reference() {
//! {
//! let mut scope: BoxScope<FooFamily, _> = BoxScope::new_typed(scope!({
//! let mut f = Foo {
//! s: String::from("Hello World!"),
//! r: None,
//! };
//! freeze_forever!(&mut f)
//! }));
//!
//! scope.enter(|foo| {
//! foo.r = Some(&mut foo.s);
//! });
//! scope.enter(|foo| {
//! let alias1: &mut String = &mut foo.s;
//! let alias2: &mut String = foo.r.as_deref_mut().unwrap(); // miri will complain here already
//! // two aliasing mutable references!!
//!
//! let s: &str = alias1;
//! let owner: String = std::mem::take(alias2);
//!
//! println!("Now it exists: {s}");
//! drop(owner);
//! println!("Now it's gone: {s}");
//! })
//! }
//! }
//! ```
//!
//! # Recursion is not allowed
//!
//! ```compile_fail,E0733
Expand Down
4 changes: 2 additions & 2 deletions src/raw_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ where
/// 2. `this` verifies the guarantees of `Pin` (one of its fields is pinned in this function)
/// 3. No other exclusive reference to the frozen value. In particular, no concurrent calls to this function.
#[allow(unused_unsafe)]
pub(crate) unsafe fn enter<'borrow, Output: 'borrow, G>(this: NonNull<Self>, f: G) -> Output
pub(crate) unsafe fn enter<'borrow, Output, G>(this: NonNull<Self>, f: G) -> Output
where
G: for<'a> FnOnce(&'a mut <T as Family<'a>>::Family) -> Output,
G: for<'a> FnOnce(&'borrow mut <T as Family<'a>>::Family) -> Output,
{
// SAFETY: precondition (1)
let RawScopeFields { state, active_fut } = unsafe { Self::fields(this.as_ptr()) };
Expand Down
Loading