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

Do not create dangling &T in Weak<T>::drop #80488

Merged
merged 1 commit into from
Dec 31, 2020

Conversation

CAD97
Copy link
Contributor

@CAD97 CAD97 commented Dec 29, 2020

Since at this point all strong pointers have been dropped, the wrapped T has also been dropped. As such, creating a &T to the dropped place is negligent at best (language UB at worst). Since we have Layout::for_value_raw now, use that instead of Layout::for_value to avoid creating the &T.

This does have implications for custom (potentially thin) DSTs, though much less severe than those discussed in #80407. Specifically, one of two things has to be true:

  • It has to be possible to use a *const T to a dropped (potentially custom, potentially thin) unsized tailed object to determine the layout (size/align) of the object. This is what is currently implemented (though with &T instead of &T). The validity of reading some location after it has been dropped is an open question IIUC (What about: use-after-move and (maybe) use-after-drop unsafe-code-guidelines#188) (except when the whole type is Copy, per drop_in_place's docs).
    In this design, custom DSTs would get a *mut T and use that to return layout, and must be able to do so while in the "zombie" (post-drop, pre-free) state.
  • RcBox/ArcInner compute and store layout eagerly, so that they don't have to ask the type for its layout after dropping it.

Importantly, this is already true today, as you can construct Rc<DST>, create a Weak<DST>, and drop the Rc before the Weak. This PR is a strict improvement over the status quo, and the above question about potentially thin DSTs will need to be resolved by any custom DST proposal.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(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 Dec 29, 2020
@jyn514 jyn514 added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 29, 2020
@m-ou-se
Copy link
Member

m-ou-se commented Dec 30, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Dec 30, 2020

📌 Commit 81685e9 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 Dec 30, 2020
@m-ou-se m-ou-se assigned m-ou-se and unassigned joshtriplett Dec 30, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 30, 2020
Rollup of 9 pull requests

Successful merges:

 - rust-lang#78934 (refactor: removing library/alloc/src/vec/mod.rs ignore-tidy-filelength)
 - rust-lang#79479 (Add `Iterator::intersperse`)
 - rust-lang#80128 (Edit rustc_ast::ast::FieldPat docs)
 - rust-lang#80424 (Don't give an error when creating a file for the first time)
 - rust-lang#80458 (Some Promotion Refactoring)
 - rust-lang#80488 (Do not create dangling &T in Weak<T>::drop)
 - rust-lang#80491 (Miri: make size/align_of_val work for dangling raw ptrs)
 - rust-lang#80495 (Rename kw::Invalid -> kw::Empty)
 - rust-lang#80513 (Add regression test for rust-lang#80062)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3d93dfd into rust-lang:master Dec 31, 2020
@rustbot rustbot added this to the 1.51.0 milestone Dec 31, 2020
@CAD97 CAD97 deleted the drop-weak-without-reference branch April 26, 2022 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants