about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-31 15:22:51 +0000
committerbors <bors@rust-lang.org>2021-05-31 15:22:51 +0000
commit6a3dce99f6ee4fd1ae317fe486b214dad7f4c1ee (patch)
tree1365403cd16b03918183915fb9007468c9deba93 /library/alloc/src
parentd9feaaa548ce380159a1de68f4f6e605db9a9fc5 (diff)
parentb4dcdb4b473304b1c52b1ba79bc174d208107cdc (diff)
downloadrust-6a3dce99f6ee4fd1ae317fe486b214dad7f4c1ee.tar.gz
rust-6a3dce99f6ee4fd1ae317fe486b214dad7f4c1ee.zip
Auto merge of #85814 - steffahn:fix_linked_list_itermut_debug, r=m-ou-se
Fix unsoundness of Debug implementation for linked_list::IterMut

Fix #85813, new `marker` field follows the example of `linked_list::Iter`.
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/collections/linked_list.rs27
1 files changed, 20 insertions, 7 deletions
diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs
index 5dda8c47688..1a58ad51f78 100644
--- a/library/alloc/src/collections/linked_list.rs
+++ b/library/alloc/src/collections/linked_list.rs
@@ -64,7 +64,15 @@ pub struct Iter<'a, T: 'a> {
 #[stable(feature = "collection_debug", since = "1.17.0")]
 impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_tuple("Iter").field(&self.len).finish()
+        f.debug_tuple("Iter")
+            .field(&*mem::ManuallyDrop::new(LinkedList {
+                head: self.head,
+                tail: self.tail,
+                len: self.len,
+                marker: PhantomData,
+            }))
+            .field(&self.len)
+            .finish()
     }
 }
 
@@ -82,19 +90,24 @@ impl<T> Clone for Iter<'_, T> {
 /// documentation for more.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct IterMut<'a, T: 'a> {
-    // We do *not* exclusively own the entire list here, references to node's `element`
-    // have been handed out by the iterator! So be careful when using this; the methods
-    // called must be aware that there can be aliasing pointers to `element`.
-    list: &'a mut LinkedList<T>,
     head: Option<NonNull<Node<T>>>,
     tail: Option<NonNull<Node<T>>>,
     len: usize,
+    marker: PhantomData<&'a mut Node<T>>,
 }
 
 #[stable(feature = "collection_debug", since = "1.17.0")]
 impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_tuple("IterMut").field(&self.list).field(&self.len).finish()
+        f.debug_tuple("IterMut")
+            .field(&*mem::ManuallyDrop::new(LinkedList {
+                head: self.head,
+                tail: self.tail,
+                len: self.len,
+                marker: PhantomData,
+            }))
+            .field(&self.len)
+            .finish()
     }
 }
 
@@ -493,7 +506,7 @@ impl<T> LinkedList<T> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn iter_mut(&mut self) -> IterMut<'_, T> {
-        IterMut { head: self.head, tail: self.tail, len: self.len, list: self }
+        IterMut { head: self.head, tail: self.tail, len: self.len, marker: PhantomData }
     }
 
     /// Provides a cursor at the front element.