From 9698221f919a80f2a0810e17c8ee8e80da8cefeb Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 24 May 2014 21:35:53 +1000 Subject: Paper over privacy issues with Deref by changing field names. Types that implement Deref can cause weird error messages due to their private fields conflicting with a field of the type they deref to, e.g., previously struct Foo { x: int } let a: Arc = ...; println!("{}", a.x); would complain the the `x` field of `Arc` was private (since Arc has a private field called `x`) rather than just ignoring it. This patch doesn't fix that issue, but does mean one would have to write `a._ptr` to hit the same error message, which seems far less common. (This patch `_`-prefixes all private fields of `Deref`-implementing types.) cc #12808 --- src/libcore/cell.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 3b1322cdc1b..3a9894a4c65 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -251,7 +251,7 @@ impl RefCell { WRITING => None, borrow => { self.borrow.set(borrow + 1); - Some(Ref { parent: self }) + Some(Ref { _parent: self }) } } } @@ -281,7 +281,7 @@ impl RefCell { match self.borrow.get() { UNUSED => { self.borrow.set(WRITING); - Some(RefMut { parent: self }) + Some(RefMut { _parent: self }) }, _ => None } @@ -317,22 +317,24 @@ impl Eq for RefCell { /// Wraps a borrowed reference to a value in a `RefCell` box. pub struct Ref<'b, T> { - parent: &'b RefCell + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell } #[unsafe_destructor] impl<'b, T> Drop for Ref<'b, T> { fn drop(&mut self) { - let borrow = self.parent.borrow.get(); + let borrow = self._parent.borrow.get(); debug_assert!(borrow != WRITING && borrow != UNUSED); - self.parent.borrow.set(borrow - 1); + self._parent.borrow.set(borrow - 1); } } impl<'b, T> Deref for Ref<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe { &*self.parent.value.get() } + unsafe { &*self._parent.value.get() } } } @@ -346,40 +348,42 @@ impl<'b, T> Deref for Ref<'b, T> { pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> { // Since this Ref exists, we know the borrow flag // is not set to WRITING. - let borrow = orig.parent.borrow.get(); + let borrow = orig._parent.borrow.get(); debug_assert!(borrow != WRITING && borrow != UNUSED); - orig.parent.borrow.set(borrow + 1); + orig._parent.borrow.set(borrow + 1); Ref { - parent: orig.parent, + _parent: orig._parent, } } /// Wraps a mutable borrowed reference to a value in a `RefCell` box. pub struct RefMut<'b, T> { - parent: &'b RefCell + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell } #[unsafe_destructor] impl<'b, T> Drop for RefMut<'b, T> { fn drop(&mut self) { - let borrow = self.parent.borrow.get(); + let borrow = self._parent.borrow.get(); debug_assert!(borrow == WRITING); - self.parent.borrow.set(UNUSED); + self._parent.borrow.set(UNUSED); } } impl<'b, T> Deref for RefMut<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe { &*self.parent.value.get() } + unsafe { &*self._parent.value.get() } } } impl<'b, T> DerefMut for RefMut<'b, T> { #[inline] fn deref_mut<'a>(&'a mut self) -> &'a mut T { - unsafe { &mut *self.parent.value.get() } + unsafe { &mut *self._parent.value.get() } } } -- cgit 1.4.1-3-g733a5