Rust Types Team
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
https://github.com/rust-lang/types-team/issues/92 ## What do I mean with dropck We generally require the type of locals to be well-formed whenever the local is used. This includes proving the where-bounds of the local and also requires all regions used by it to be live. The only exception to this is the implicitly dropping values when they go out of scope. This does not necessarily require the value to be live: ```rust= fn main() { let x = vec![]; { let y = String::from("I am temporary"); x.push(&y); } // `x` goes out of scope here, after the reference to `y` // is invalidated. This means that while dropping `x` its type // is not well-formed as it contain regions which are not live. } ``` This is only sound if dropping the value does not try to access any dead region. The code responsible for this is `dropck_outlives`. The rest of this document uses the following type definition for a type which requires its region parameter to be live: ```rust struct PrintOnDrop<'a>(&'a str); impl<'a> Drop for PrintOnDrop<'_> { fn drop(&mut self) { println!("{}", self.0); } } ``` ## How values are dropped I mostly got the terminology and implementation details here from looking at the source. Please tell me if something feels off. At its core, a value of type `T` is dropped by executing its "drop glue". Drop glue is compiler generated and first calls `<T as Drop>::drop` and then recursively calls the drop glue of any recursively owned values. - If `T` has an explicit `Drop` impl, call `<T as Drop>::drop`. - Regardless of whether `T` implements `Drop`, recurse into all values *owned* by `T`: - references, raw pointers, function pointers, function items, trait objects[^traitobj], and scalars do not own anything. - tuples, slices, and arrays consider their elements to be owned. For arrays of length zero we do not own any value of the element type. - all fields (of all variants) of ADTs are considered owned. We consider all variants for enums. The exception here is `ManuallyDrop<U>` which is not considered to own `U`. `PhantomData<U>` also does not own anything. - closures and generators own their captured upvars. Whether a type has drop glue is returned by [`fn Ty::needs_drop`](https://github.com/rust-lang/rust/blob/320b412f9c55bf480d26276ff0ab480e4ecb29c0/compiler/rustc_middle/src/ty/util.rs#L1086-L1108). ### Partially dropping a local For types which do not implement `Drop` themselves, we can also partially move parts of the value before dropping the rest. In this case only the drop glue for the not-yet moved values is called, e.g. ```rust fn main() { let mut x = (PrintOnDrop("third"), PrintOnDrop("first")); drop(x.1); println!("second") } ``` During MIR building we assume that a local may get dropped whenever it goes out of scope *as long as its type needs drop*. Computing the exact drop glue for a variable happens **after** borrowck in the `ElaborateDrops` pass. This means that even if some part of the local have been dropped previously, dropck still requires this value to be live. This is the case even if we completely moved a local. ```rust= fn main() { let mut x; { let temp = String::from("I am temporary"); x = PrintOnDrop(&temp); drop(x); } } //~ ERROR `temp` does not live long enough. ``` I think that it should be possible to add some amount of drop elaboration before borrowck, allowing this example to compile. There is an unstable feature to move drop elaboration before const checking: [#73255](https://github.com/rust-lang/rust/issues/73255). As far as I know such a feature gate does not exist for doing some drop elaboration before borrowck. [^traitobj]: you can consider trait objects to have a builtin `Drop` implementation which directly uses the `drop_in_place` provided by the vtable. This `Drop` implementation requires all its generic parameters to be live. :shrug: ### `dropck_outlives` During borrowck, we require a local to be valid for drop [at all locations before it is dropped](https://github.com/rust-lang/rust/blob/320b412f9c55bf480d26276ff0ab480e4ecb29c0/compiler/rustc_borrowck/src/type_check/liveness/trace.rs#L140-L162)[^livefacts] by adding the region constraints returned by [`dropck_outlives`](https://github.com/rust-lang/rust/blob/320b412f9c55bf480d26276ff0ab480e4ecb29c0/compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs#L80). For locals whose type does not need drop, i.e. `Ty::needs_drop` returned false, we do not emit drop statements during mir building, so these locals are never required to be valid wrt to drop. The constraints computed by `dropck_outlives` for a type closely match the generated drop glue for that type. Unlike drop glue, `dropck_outlives` cares about the types of owned values, not the values itself. For a value of type `T` - if `T` has an explicit `Drop`, require all generic arguments to be live, unless they are marked with `#[may_dangle]` in which case they are fully ignored - regardless of whether `T` has an explicit `Drop`, recurse into all types *owned* by `T` - references, raw pointers, function pointers, function items, trait objects[^traitobj], and scalars do not own anything. - tuples, slices and arrays consider their element type to be owned. **For arrays we currently do not check whether their length is zero**. - all fields (of all variants) of ADTs are considered owned. The exception here is `ManuallyDrop<U>` which is not considered to own `U`. **We consider `PhantomData<U>` to own `U`**. - closures and generators own their captured upvars. The sections marked in bold are cases where `dropck_outlives` considers types to be owned which are ignored by `Ty::needs_drop`. We only rely on `dropck_outlives` if `Ty::needs_drop` for the containing local returned `true`.This means liveness requirements can change depending on whether a type is contained in a larger local. **This is inconsistent, and should be fixed: an example [for arrays](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8b5f5f005a03971b22edb1c20c5e6cbe) and [for `PhantomData`](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=44c6e2b1fae826329fd54c347603b6c8).**[^core] I believe the only ways these inconsistencies can be fixed is by MIR building to be more pessimistic, probably by making `Ty::needs_drop` weaker, or alternatively, changing `dropck_outlives` to be more precise, requiring fewer regions to be live. ## Fixing the `[T; 0]` inconsistency ([#110288]) I propose to change `dropck_outlives` to not add outlives requirements for the element type of zero length arrays. Summarizing the discussion in that issue ([full summary](https://github.com/rust-lang/rust/issues/110288#issuecomment-1517872777)): - We already specialcase arrays of length zero in quite a few other places, e.g. for `[T; 0]: Default` and also inside of the compiler itself. - It feels sensible to not require the elements of zero length arrays to be live, people do not rely on the current `dropck_outlives` behavior. Making rustc more precise is generally preferable over making it "weaker". - Changing `needs_drop` to be more pessimistic causes actual breakage found via crater. ## Fixing the `PhantomData<U>` inconsistency ([RFC 3417]) I propose to change `dropck_outlives` to not add outlives requirements for `U`. I still have to update the RFC, the main motivation is that `PhantomData` is `Copy`, returning `true` in `needs_drop` would also be inconsistent. We have the issue that `PhantomData` is currently used together with `#[may_dangle]` to specify the exact `dropck_outlives` requirements. `#[may_dangle]` *unsafely states* "this parameter *is not accessed* directly in the `Drop` implementation". Parameters with `#[may_dangle]` can still be required to be live by recursively owned types. This is incredibly subtle if the `Drop` implementation manually drops values of the parameter type. For this to be sound, the parameter has to be considered live because it is recursively owned. This is often not the case when manually dropping values. We therefore currently add a `PhantomData` field owning the parameter to the type. This is subtle and easy to miss and we have already gotten this wrong multiple times: [#76367](https://github.com/rust-lang/rust/issues/76367) and [#99408](https://github.com/rust-lang/rust/issues/99408). [RFC 3417] proposes to change may dangle to stop relying on recursive ownership by explicitly stating whether a parameter is completely unused or only dropped. I propose the syntax `#[may_dangle(must_not_use)]` and `#[may_dangle(can_drop)]` for this. We can then use a recursive ownership check to warn against unnecessarily *strong* `#[may_dangle]` attributes on `Drop` impls. ## The bright future We can add assertions that `!dropck_outlives.is_empty()` implies `Ty::needs_drop`. `dropck_outlives` and `Ty::needs_drop` agree on the definitation of recursive ownership. For a given type `T`, we have the following ownership rules: - references, raw pointers, function pointers, function items, trait objects[^traitobj], and scalars do not own anything. - tuples, slices, and arrays consider their elements to be owned. For arrays of length zero we do not own any value of the element type. - all fields (of all variants) of ADTs are considered owned. We consider all variants for enums. The exception here is `ManuallyDrop<U>` which is not considered to own `U`. `PhantomData<U>` does not own anything. - closures and generators own their captured upvars. Drop glue is generated as follows: - if `T` has an explicit `Drop` impl, call `<T as Drop>::drop`[^traitobj]. - regardless of whether `T` has a `Drop` impl, recusively drop all values owned by `T` The outlives requirements are computed as follows: - if `T` has an explicit `Drop` impl, require all parameters to be live unless they are marked with `#[may_dangle]`: - `#[may_dangle]` on lifetime parameters and `#[may_dangle(must_not_use)]` on type parameters cause us to fully ignore the parameter - `#[may_dangle(may_drop)]` recurses into the type parameter as if it were recursively owned - regardless of whether `T` has a `Drop` impl, recursively add the outlives requirements for types owned by `T` [^livefacts]: I think should be equivalent to require the value to be valid for drop only when actually dropping it? With polonius we add additional constraints for exactly the locations where drop is used I think? Unsure, should be fine to think about this as: borrowck requires a value to be valid for drop whenever it could get dropped. [^core]: This is the core assumption of [#110288] and [RFC 3417]. [#110288]: https://github.com/rust-lang/rust/issues/110288 [RFC 3417]: https://github.com/rust-lang/rfcs/pull/3417 ## question section ### @lcnr: how do i ask a question i want to ask a question but don't know how, how do I do it? @lcnr: like this :3 ### "trait objects do not own anything" pnkfelixs: I assume "trait object" there means `&dyn Trait`/`&mut Trait`/`*const Trait` ... @lcnr: I mean `dyn Trait<'a>`, I consider it easier to think about trait objects as having an explicit drop, but not owning anything. Because that's what is actually going on, even though its currently not really represented by the type system. pnkfelix: but can't `Box<dyn Trait>` own things .... ah I see, you treat the embedded ownership as being tied to an explicit `<T as Drop>::drop` method, even if the underlying implementing type does not actually implement Drop? lcnr: :+1: yeah, as we explicitly call `vtable.drop_in_place`, while not recursing into anything. nikomatsakis: I always say it as more like "vtables have drop, which is defined by calling a method from the vtable". I'm not sure if this is an important distinction though. ### dropck_outlives vs needs_drop inconsistency pnkfelix: What are the concrete negative outcomes, from a Rust programmer's perspective (vs a rustc dev's perspective) of the inconsistency here? Is it unsoundnes? Or surprising cases where code is rejected by the compiler? Or difficulty in reasoning about unsafe code soundness? Or something else? pnkfelix: Specifically, with respect to the PhantomData inconsistency: Are all the negative outcomes there surfacing as instances of bad interactions with `#[may_dangle]` ? (Note the text here *does* provide pointers to examples of the `#[may_dangle]` problems.) Or are there other issues (that I don't see documented here)? [Discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/326132-t-types.2Fmeetings/topic/2023-07-17.20dropck/near/376045297) ### elaboration before dropck > As far as I know such a feature gate does not exist for doing some drop elaboration before borrowck. nikomatsakis: There was work on this. It was meant to unblock polonius. I am not sure the current state. Relevant MCP: https://github.com/rust-lang/compiler-team/issues/558 ### "drop-live" > During borrowck, we require a local to be valid for drop at all locations before it is dropped[2] by adding the region constraints returned by dropck_outlives. For locals whose type does not need drop, i.e. Ty::needs_drop returned false, we do not emit drop statements during mir building, so these locals are never required to be valid wrt to drop. nikomatsakis: This is accurate, I just want to add a bit of "color" to the description. The way I think about it is that there are two distinct "liveness" computations that we perform: * a value `v` is *use-live* at location `L` if it maybe "used" later; a *use* here is basically anything that is not a *drop* * a value `v` is *drop-live* at location `L` if it maybe dropped later When things are *use-live*, their entire type must be valid at `L`. When they are *drop-live*, all regions that are required by dropck must be valid at `L`. These "values" would be better thought of as places. ### What are the "cycle" implications of fixing the `[T;0]` inconsistency? > Fixing the `[T; 0]` inconsistency ([#110288]) nikomatsakis: This implies that we perform const eval for the array length, right? Is that an issue? We would have to be able to do that without access to borrow check, I think that's the primary implication? lcnr: good catch :sweat_smile: it's not an issue on stable. Given that we generally have to try to evaluate constants during typeck so any constant leaking into the type system already has to be evaluatable to avoid cycle, I don't expect this to cause more issues, even with `feature(generic_const_exprs)`. ### What does "may dangle" mean? > `#[may_dangle]` unsafely states “this parameter is not accessed directly in the Drop implementation”. nikomatsakis: I don't think this is the right way to describe it. Well, maybe it is! I was going to say that "may dangle" meant: "may be dropped", but actually I think that's untrue, and I guess this is the point of the text that follows? i.e., we basically want to characterize in one of 3 ways: * values of type `T` should be *use-live* when the drop executes (chosen if there is a `Drop` impl unless there is a `#[may_dangle]` attribute) * values of type `T` should be *drop-live* when the drop executes (chosen if the type owns a value of type `T` or (today) includes a `PhantomData<T>`, even if there is a `#[may_dangle]` attribute) * values of type `T` don't need to be live at all when the drop executes (chosen if the type is only borrowed) Is this an accurate summary? lcnr: yes, though "chosen if the type owns a value of type `T` or (today) includes a `PhantomData<T>`, even if there is a `#[may_dangle]` attribute" feels less clear to me then saying "`Drop` impls requires all parameters to be live, unless they are marked with `#[may_dangle]`. In this case we only consider the parameter if it is recursively owned" which is what I wanted to state in the doc ^^ ### examples of finer-grained may_dangle? pnkfelix: Regarding the extensions `#[may_dangle(must_not_use)]` and `#[may_dangle(can_drop)]`: do we know how the existing uses of `#[may_dangle]` are going to map to each of those two new forms? pnkfelix: E.g. what will `unsafe impl<#[may_dangle] T> Drop for Vec<T>` map to?

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully