From 578e7143936158a0130f17bedcc946cae62583f3 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 5 Sep 2020 14:09:33 +0200 Subject: [PATCH 1/2] Fix dropck issue of SyncOnceCell. Fixes #76367. --- library/std/src/lazy.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index 845e9d76a772b..fc485f0cd47ba 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -6,6 +6,7 @@ mod tests; use crate::{ cell::{Cell, UnsafeCell}, fmt, + marker::PhantomData, mem::{self, MaybeUninit}, ops::{Deref, Drop}, panic::{RefUnwindSafe, UnwindSafe}, @@ -46,6 +47,8 @@ pub struct SyncOnceCell { once: Once, // Whether or not the value is initialized is tracked by `state_and_queue`. value: UnsafeCell>, + // Make sure dropck understands we're dropping T in our Drop impl. + _marker: PhantomData, } // Why do we need `T: Send`? @@ -119,7 +122,11 @@ impl SyncOnceCell { /// Creates a new empty cell. #[unstable(feature = "once_cell", issue = "74465")] pub const fn new() -> SyncOnceCell { - SyncOnceCell { once: Once::new(), value: UnsafeCell::new(MaybeUninit::uninit()) } + SyncOnceCell { + once: Once::new(), + value: UnsafeCell::new(MaybeUninit::uninit()), + _marker: PhantomData, + } } /// Gets the reference to the underlying value. From e56ea68db50d23f4a7efa712c53ba02e506fd61a Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 5 Sep 2020 15:55:20 +0200 Subject: [PATCH 2/2] Add compile_fail test for SyncOnceCell's dropck issue. --- library/std/src/lazy.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index fc485f0cd47ba..d171231b0f1e6 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -47,7 +47,25 @@ pub struct SyncOnceCell { once: Once, // Whether or not the value is initialized is tracked by `state_and_queue`. value: UnsafeCell>, - // Make sure dropck understands we're dropping T in our Drop impl. + /// `PhantomData` to make sure dropck understands we're dropping T in our Drop impl. + /// + /// ```compile_fail,E0597 + /// #![feature(once_cell)] + /// + /// use std::lazy::SyncOnceCell; + /// + /// struct A<'a>(&'a str); + /// + /// impl<'a> Drop for A<'a> { + /// fn drop(&mut self) {} + /// } + /// + /// let cell = SyncOnceCell::new(); + /// { + /// let s = String::new(); + /// let _ = cell.set(A(&s)); + /// } + /// ``` _marker: PhantomData, }