Niko Matsakis
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      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
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    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
    # RFC 2229 Plans ## Goal The goal is to refactor closures so that they capture [HIR places] and not just variables. For the initial version, this behavior will be gated, and we will always capture the most fine-grained places that we can. This will result in better borrow check results, but less efficient code, with some changes to runtime behavior (specifically, drop code may run at different times). This version would not be expected to stabilize as is, but rather to serve as the basis for evaluation about the runtime cost and the amount of potentially affected code -- but we could conceivably stabilize it over an edition, or we might make further changes. [HIR places]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/struct.Place.html ## High-level view of the changes required * We will rewrite code that currently iterates over the "upvars" of a closure to iterate over a set of "places" produced by type-check. * The factorings below are steps in this direciton. * We will adjust the existing [upvar inference] for closures. Today, when it sees (say) a read of `a.b.c`, it tracks that down to a shared borrow of `a`. But the new mode inference will be coallescing into a minimal set of [HIR places] that are accessed (e.g., if `a.b.c` and `a.b` are both read, then we would just capture `a.b`). * We will have to rewrite the HAIR and MIR lowering so that `a.b.c` is rewritten to use the minimal place instead of always being relative to an upvar `a`. * The generated MIR that used to capture variables like `a` when constructing a [closure] will now capture the place (e.g., `a.b.c`) * The only changes required in borrow check (afaik) are related to error reporting [closure]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.AggregateKind.html#variant.Closure [upvar inference]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/check/upvar/index.html ## Future optimizations * After we complete the above work, we may wish to have some kind of "optimized form" of closures that don't capture paths like `a.b` and `a.c` individually, but instead capture just `a` at runtime (even though the borrow check and type system would view the capture as capturing `a.b` and `a.c`). I'm not sure yet how to approach this: my inclination is to write a MIR optimization that runs after borrow check and reworks the cin some grody way to capture a raw pointer (`a`) and use downcasts and things in the closure MIR. But we'll have to figure out various complications here, e.g. the closure layout, and it may not even turn out to be that important in practice, so I'm inclined to defer that. * We will definitely have to figure out how to "transition" to the new drop behavior -- my preference would be to do this over an edition. It should be easy enough to detect when we are now capturing a place instead of a root variable and "suspicious dtors" would run at different times. We can warn and suggest a change like inserting `let x = x;` in the closure to preserve the old drop behavior. The main unknown is how many such things would be produced. ## Notes ### Upvar tuple refactoring Delay the [instantiation of the "upvar tuple"](https://github.com/rust-lang/rust/issues/57482) ### Rename existing upvars query There is a [query] [`upvars`] that returns the list of "upvars" (captured variables) used in a closure. Currently, the list of variables used corresponds one to one with each thing that the closure captures, but the whole point of this change is to break that association, so that a query like `|| (x.foo, x.bar)` might capture *just* `x.foo` and `x.bar`, and not capture `x`. [query]: https://rustc-dev-guide.rust-lang.org/query.html [`upvars`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.upvars Much of the code that uses [`upvars`] therefore will want to be refactored, because it really wants to iterate over *all things captured* and not the *upvars* that the closure references. We should introduce a terminology split, actually: * "mentioned upvars" will be the list of variable names that are mentioned by a closure (i.e, `x`). This list is produced by the name resolution code. * "captures" or "captured paths" will refer to the paths captured by the closure (e.g., `x.foo`). This list is produced by the type check. We should rename the [`upvars`] query, therefore, to `upvars_mentioned`. This would make a nice, simply PR. Similarly, we should rename the [`upvar_list`] field of `TypeckTable` to `closure_captures`. [`upvar_list`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckTables.html#structfield.upvar_list The [`upvar_capture_map`] can keep its name, as I imagine it will be removed eventually, but for now it really does refer to *upvars* and not *captures*. [`upvar_capture_map`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckTables.html#structfield.upvar_capture_map ### Refactor uses of upvars query Now that the rename is done, we want to rewrite things currently using the [`upvars`] [query] (updated version of [#60205](https://github.com/rust-lang/rust/issues/60205)) to instead iterate over the *captures* from the query that is produced by type-check (the [`upvar_list`] field we renamed in the previous step). For now, the [`upvars`] query and the [`upvar_list`] field ultimately give access to a `FxIndexMap<HirId, Upvar>` map. Eventually this will change, with the [`upvar_list`] having a more general structure that expresses captured paths, not just the mentioned upvars. But we'll do that later. (You can see the return type of the query [here](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/query/queries/struct.upvars.html), in the associated query struct.) This means that we can change calls like ```rust tcx.upvars(def_id) ``` to ```rust tcx.typeck_tables(def_id).upvars_list[def_id] ``` We may not want to change every call, just those that occurs in code that comes "after" typecheck. | Change? | Directory | Why | | --- | --- | --- | | :heavy_check_mark: | `librustc_middle/mir` | MIR related code comes after typeck | | :confused: | `pretty.rs` | this pretty printing code could run at different times, it should probably be changed, but not entirely clear to what yet | | :heavy_check_mark: | librustc_mir/... | MIR related code comes after typeck | | :heavy_check_mark: | librustc_mir_build/... | MIR related code comes after typeck | | :heavy_check_mark: | librusc_passes/liveness.rs | liveness analyses come after typeck | | :confused: | librustc_typeck/expr_user_visitor | executes during typeck, so the tables are accessed differently, but still should be updated...I think | | :confused: | librustc_typeck/mem_categorization | similar to the above | | :x: | librustc_typeck/check/closure.rs | this code will be changing but it will need access to the raw mentioned upvars eventually | | :x: | librustc_typeck/check/upvar.rs | this code will likely have the job of computing the upvar-list | ### Refactor the capture list to be a path * Refactor the [UpvarListMap] so that the values are [HIR places], currently only capturing local variables * now we can express capturing `*v` or `v[0]` or whatever * have to change a bit of the code above [HIR places]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/struct.Place.html [UpvarListMap]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/type.UpvarListMap.html

    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