Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsoundness of Debug implementation for linked_list::IterMut #85814

Merged
merged 2 commits into from
May 31, 2021

Conversation

steffahn
Copy link
Member

Fix #85813, new marker field follows the example of linked_list::Iter.

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 29, 2021
@steffahn
Copy link
Member Author

I could also add some improvement to the implementation so that the actual items of the iterator are printed. Similar to how other collections seem to show the iterator’s elements in their Debug impls, too. The new implementation could look like

impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("IterMut")
            .field(&*mem::ManuallyDrop::new(LinkedList {
                head: self.head,
                tail: self.tail,
                len: self.len,
                marker: PhantomData,
            }))
            .field(&self.len)
            .finish()
    }
}

(and similar for Iter)
I’m only ~99% certain that this is sound, I would feel better if I could somehow run this implementation through miri, but I don’t know how to feed a custom std lib to miri.

So, if you think that this kind of impl is sound and that it’s a good idea to include it with this PR then I can add the relevant commit (b4dcdb4) to this PR.

@steffahn
Copy link
Member Author

steffahn commented May 30, 2021

I would feel better if I could somehow run this implementation through miri

Okay, today I learned how that works, thanks @RalfJung. I’m feeling comfortable regarding soundness now, so I’ll add the commit. (It’s appearing further up in the thread because the commit itself is from yesterday, but it was on a different branch until now.)


The improved Debug implementation is producing much nicer output compared to the one where only the size is printed; the usage of ManuallyDrop plus a new LinkedList is a bit hacky, but it’s way simpler than the “more proper” approach of e.g. creating a new linked_list::Iter<'_> (in both the Iter and the IterMut case) and wrapping that up into a local wrapper struct that in turn implements Debug analogously to how LinkedList does by using Formatter::debug_list.

But feel free to state if a “more proper” approach is wanted during review, or if it’s better split into a separate PR, in case either is the case.

@m-ou-se
Copy link
Member

m-ou-se commented May 31, 2021

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented May 31, 2021

📌 Commit b4dcdb4 has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2021
@bors
Copy link
Contributor

bors commented May 31, 2021

⌛ Testing commit b4dcdb4 with merge 6a3dce9...

@bors
Copy link
Contributor

bors commented May 31, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing 6a3dce9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 31, 2021
@bors bors merged commit 6a3dce9 into rust-lang:master May 31, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 31, 2021
@steffahn steffahn deleted the fix_linked_list_itermut_debug branch May 31, 2021 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unsound Debug impl for collections::linked_list::IterMut
5 participants