From 06710fce596371b2130c6e7aebe2472dd935b4ab Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Sat, 2 Jan 2021 17:37:58 -0500 Subject: [PATCH] [rt] Change destructor decref to use immutable slices It should be safe since the destructor should have exclusive access to the object, but this avoids making potentially aliased references. --- rt/src/lam.rs | 20 ++++++++++++++++---- rt/src/lib.rs | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/rt/src/lam.rs b/rt/src/lam.rs index b998fa8..ffdb5f6 100644 --- a/rt/src/lam.rs +++ b/rt/src/lam.rs @@ -98,17 +98,29 @@ impl ObjLam { ObjLam::size_of(self.params, self.upvars) } - pub(crate) unsafe fn raw_fields_mut(&mut self) -> *mut Obj { - (self as *mut ObjLam).add(1) as *mut Obj + fn raw_fields_mut(&self) -> *mut Obj { + unsafe { (self as *const ObjLam).add(1) as *mut Obj } } - pub(crate) unsafe fn params_mut(&mut self) -> &mut [Obj] { + unsafe fn params_mut(&mut self) -> &mut [Obj] { let ptr = self.raw_fields_mut(); core::slice::from_raw_parts_mut(ptr, self.params as usize) } - pub(crate) unsafe fn upvars_mut(&mut self) -> &mut [Obj] { + pub(crate) fn params(&self) -> &[Obj] { + let ptr = self.raw_fields_mut(); + unsafe { core::slice::from_raw_parts(ptr, self.params as usize) } + } + + unsafe fn upvars_mut(&mut self) -> &mut [Obj] { let ptr = self.raw_fields_mut().add(self.params as usize); core::slice::from_raw_parts_mut(ptr, self.upvars as usize) } + + pub(crate) fn upvars(&self) -> &[Obj] { + unsafe { + let ptr = self.raw_fields_mut().add(self.params as usize); + core::slice::from_raw_parts(ptr, self.upvars as usize) + } + } } diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 626a65d..2bd2386 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -143,10 +143,10 @@ impl Obj { } if self.is_lam() { let lam = &mut *self.box_lam; - for param in lam.params_mut() { + for param in lam.params() { param.decref(); } - for upvar in lam.upvars_mut() { + for upvar in lam.upvars() { upvar.decref(); } } -- 2.47.0