Skip to content

Commit 5282939

Browse files
remove const_drop and add tests
1 parent 2ee7f02 commit 5282939

File tree

4 files changed

+97
-31
lines changed

4 files changed

+97
-31
lines changed

library/core/src/cell.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -910,10 +910,7 @@ impl<T> RefCell<T> {
910910
#[rustc_confusables("swap")]
911911
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
912912
pub const fn replace(&self, t: T) -> T {
913-
let mut s = self.borrow_mut();
914-
let t = mem::replace(s.as_mut(), t);
915-
s.const_drop();
916-
t
913+
mem::replace(self.borrow_mut().as_mut(), t)
917914
}
918915

919916
/// Replaces the wrapped value with a new one computed from `f`, returning
@@ -965,11 +962,7 @@ impl<T> RefCell<T> {
965962
#[stable(feature = "refcell_swap", since = "1.24.0")]
966963
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
967964
pub const fn swap(&self, other: &Self) {
968-
let mut s = self.borrow_mut();
969-
let mut o = other.borrow_mut();
970-
mem::swap(s.as_mut(), o.as_mut());
971-
s.const_drop();
972-
o.const_drop();
965+
mem::swap(self.borrow_mut().as_mut(), other.borrow_mut().as_mut())
973966
}
974967
}
975968

@@ -1474,12 +1467,13 @@ impl<'b> BorrowRef<'b> {
14741467
}
14751468
}
14761469

1477-
impl Drop for BorrowRef<'_> {
1470+
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
1471+
impl const Drop for BorrowRef<'_> {
14781472
#[inline]
14791473
fn drop(&mut self) {
14801474
let borrow = self.borrow.get();
14811475
debug_assert!(is_reading(borrow));
1482-
self.borrow.set(borrow - 1);
1476+
self.borrow.replace(borrow - 1);
14831477
}
14841478
}
14851479

@@ -1683,15 +1677,6 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
16831677
unsafe { self.value.as_mut() }
16841678
}
16851679

1686-
#[inline]
1687-
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
1688-
const fn const_drop(self) {
1689-
// SAFETY: only `borrow` is `Drop`
1690-
let borrow = unsafe { ptr::read(&self.borrow) };
1691-
mem::forget(self);
1692-
borrow.const_drop();
1693-
}
1694-
16951680
/// Makes a new `RefMut` for a component of the borrowed data, e.g., an enum
16961681
/// variant.
16971682
///
@@ -1853,25 +1838,17 @@ struct BorrowRefMut<'b> {
18531838
borrow: &'b Cell<BorrowFlag>,
18541839
}
18551840

1856-
impl Drop for BorrowRefMut<'_> {
1841+
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
1842+
impl const Drop for BorrowRefMut<'_> {
18571843
#[inline]
18581844
fn drop(&mut self) {
18591845
let borrow = self.borrow.get();
18601846
debug_assert!(is_writing(borrow));
1861-
self.borrow.set(borrow + 1);
1847+
self.borrow.replace(borrow + 1);
18621848
}
18631849
}
18641850

18651851
impl<'b> BorrowRefMut<'b> {
1866-
#[inline]
1867-
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
1868-
const fn const_drop(self) {
1869-
let borrow = self.borrow.get();
1870-
debug_assert!(is_writing(borrow));
1871-
self.borrow.replace(borrow + 1);
1872-
mem::forget(self)
1873-
}
1874-
18751852
#[inline]
18761853
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
18771854
const fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>> {

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
#![feature(cfg_target_has_atomic)]
143143
#![feature(cfg_target_has_atomic_equal_alignment)]
144144
#![feature(cfg_ub_checks)]
145+
#![feature(const_destruct)]
145146
#![feature(const_precise_live_drops)]
146147
#![feature(const_trait_impl)]
147148
#![feature(decl_macro)]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//@ run-pass
2+
//! This tests `cell::Ref` and `cell::RefMut` are usable in `const` contexts
3+
4+
#![feature(const_ref_cell)]
5+
#![feature(const_destruct)]
6+
7+
use std::mem;
8+
use std::sync::atomic::{AtomicU8, Ordering};
9+
use std::cell::RefCell;
10+
11+
static DROP_COUNT: AtomicU8 = AtomicU8::new(0);
12+
13+
struct Dummy;
14+
15+
impl Drop for Dummy {
16+
fn drop(&mut self){
17+
DROP_COUNT.fetch_add(1, Ordering::Relaxed);
18+
}
19+
}
20+
21+
const REF_CELL_TEST: RefCell<Dummy> = {
22+
let a = RefCell::new(Dummy);
23+
24+
// Check that `borrow` guards correctly at compile-time
25+
{
26+
assert!(a.try_borrow().is_ok());
27+
assert!(a.try_borrow_mut().is_ok());
28+
let _a = a.borrow();
29+
assert!(a.try_borrow().is_ok());
30+
assert!(a.try_borrow_mut().is_err());
31+
}
32+
33+
// Check that `borrow_mut` guards correctly at compile-time
34+
{
35+
assert!(a.try_borrow().is_ok());
36+
assert!(a.try_borrow_mut().is_ok());
37+
let _a = a.borrow_mut();
38+
assert!(a.try_borrow().is_err());
39+
assert!(a.try_borrow_mut().is_err());
40+
}
41+
42+
a
43+
};
44+
45+
fn main() {
46+
let dummy = REF_CELL_TEST;
47+
assert_eq!(DROP_COUNT.load(Ordering::Relaxed), 0);
48+
mem::forget(dummy);
49+
}

tests/ui/consts/const-refcell.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ run-pass
2+
//! This tests `RefCell` is usable in `const` contexts
3+
4+
#![feature(const_ref_cell)]
5+
6+
use std::mem;
7+
use std::sync::atomic::{AtomicU8, Ordering};
8+
use std::cell::RefCell;
9+
10+
static DROP_COUNT: AtomicU8 = AtomicU8::new(0);
11+
12+
struct Dummy;
13+
14+
impl Drop for Dummy {
15+
fn drop(&mut self){
16+
DROP_COUNT.fetch_add(1, Ordering::Relaxed);
17+
}
18+
}
19+
20+
const REF_CELL_TEST: RefCell<Dummy> = {
21+
let a = RefCell::new(Dummy);
22+
let b = RefCell::new(Dummy);
23+
24+
// Check that `replace` is usable at compile-time
25+
mem::forget(a.replace(Dummy));
26+
27+
// Check that `swap` is usable at compile-time
28+
a.swap(&b);
29+
30+
// Forget `b` to avoid drop
31+
mem::forget(b);
32+
a
33+
};
34+
35+
fn main() {
36+
let dummy = REF_CELL_TEST;
37+
assert_eq!(DROP_COUNT.load(Ordering::Relaxed), 0);
38+
mem::forget(dummy);
39+
}

0 commit comments

Comments
 (0)