Tyler Mandry
    • 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

      This note has no invitees

    • 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
    • Note Insights
    • 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 Note Insights 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

    This note has no invitees

  • 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
    --- title: RPITIT Capture Rules tags: design-meeting url: https://hackmd.io/sFaSIMJOQcuwCdnUvCxtuQ --- ## Background ### Opaque types: Terminology and status An `impl Trait` appearing in the following positions creates an opaque type that references a hidden type: * Return position (RPIT) in: * Free functions * Inherent impls * Traits and trait impls (RPITIT) * Type aliases (TAIT) * Associated types (ATPIT, aka ITIAT) The **opaque type** is what is seen outside the defining scope. This code can only depend on what is mentioned in the **bounds** of the opaque type (for `impl Trait + 'a`, this is the `Trait + 'a` part), modulo leaked auto traits (which are out of scope for this doc). The **hidden type** is the actual concrete type, only known inside the defining scope. Of the above list, only RPIT on free functions and inherent impls are currently stable. The focus of today's discussion will be on the behavior of RPITIT. ### What does it mean to "capture" a lifetime? An opaque type "captures" a lifetime if its hidden type is allowed to name that lifetime. For example: ```rust struct HiddenMessage(String); impl HiddenMessage { fn as_debug<'a>(&'a self) -> impl Debug { self.0.clone() } } ``` In this case the return type of `as_debug` is not allowed to name the lifetime `'a`. This could be changed by mentioning the lifetime anywhere in the `impl Trait`: ```rust impl HiddenMessage { fn as_debug<'a>(&'a self) -> impl Debug + 'a { &self.0 } } ``` _Note that we are using explicit lifetimes for absolute clarity, but this would commonly be written with an elided lifetime as `impl Debug + '_`._ For the borrow checker to function with an opaque type it _must_ know what lifetimes it may capture, so it's important that this information can be deduced from the signature. ## Working with capture rules today When we discuss whether we want capture rules that are more or less inclusive, it's important to consider the options users may have for overriding them in either direction. ### Working with the capture rules for lifetime parameters As we've seen, a lifetime parameter is not captured by an opaque type unless it is mentioned in the opaque type's bounds. In the previous example we solved that problem by adding `+ 'a` to the bounds, and this is the canonical example that is usually given for what `async fn` desugars to. But there is a bit of a "lie" in that solution, and this lie gives rise to a cliff of complexity when multiple lifetimes are composed together. To see this, first think about what `impl Debug + 'a` means: We have a hidden type that implements `Debug` and outlives `'a`. But _outliving_ `'a` was never actually a promise we wanted to make to our API user! Instead, we wanted to say that our hidden type might reference `'a`; in other words, *that `'a` must outlive our hidden type*. :::info _It is the opinion of the author that this **inversion of meaning** gives rise to a whole host of confusion surrounding lifetimes, outlives rules, and opaque types._ ::: Nevertheless, adding `+ 'a` solved our problem. Not because of its meaning on the surface, but because of its effect on the capture rules. By mentioning `'a` in the bounds of our opaque type we gained the ability to reference it in our hidden type. There is another, more "honest" way to accomplish this; it's called the `Captures` trick. We could instead have written our impl like this: ```rust trait Captures<T> {} impl<T, U> Captures<T> for U {} impl HiddenMessage { fn as_debug<'a>(&'a self) -> impl Debug + Captures<&'a ()> { &self.0 } } ``` Because any type `U` trivially implements `Captures<T>` for any type `T`, a bound on `Captures` is effectively no bound at all. But it has the same effect on the capture rules, allowing us to name the lifetime in question. :::info The `Captures` trick is useful in other positions. For example, if you ever wanted to say that a lifetime outlived a generic type parameter, you probably could have used the `Captures` trick to allow the type parameter to name the lifetime. This is because we allow generics to name lifetimes that appear in their bounds. [Link to example](https://hackmd.io/zgairrYRSACgTeZHP1x0Zg?view#Refresher-on-the-Captures-trick) ::: ### Working with the capture rules for type parameters So far we have talked about the idea of capturing lifetime parameters, but the idea of capturing applies to type parameters too. It is not talked about much because _opaque types always capture all type parameters in scope_. We discuss the history and reasoning of this difference in the section below. A majority of the time, the fact that opaque types capture type parameters in scope is not really visible outside the function. But type parameters can themselves contain lifetimes. For example, if we had a `chain` helper defined like this: ```rust fn chain<T, U, I>(first: T, second: U) -> impl Iterator<Item = I> where T: Iterator<Item = I>, U: Iterator<Item = I>, { Chain::new(self, other) } ``` We could call it and use it like so: ```rust let a = [1, 2, 3]; let b = [4, 5, 6]; let chained = chain(a.iter(), b.iter()); // Borrows a ^^^^^^^^ // Borrows b ^^^^^^^^ for x in chained { dbg!(x); } ``` Moreover, it would be an error to drop `b` before the for loop, because `chained` captures a borrow of `b` via its type parameter `U`. If this behavior is not desired, the solution (using nightly-only features, today) is to define the opaque type in a scope that _cannot_ name the type parameter. For example: ```rust type Opaque = impl Debug; fn captures_no_types<T, U>(x: T, y: U) -> Opaque { ... } ``` This can come up because... TODO ### Theme: Revisiting the lifetime capture rules The decision to capture all type parameters, but not lifetime parameters, was originally made in [RFC 1951](https://github.com/rust-lang/rfcs/blob/master/text/1951-expand-impl-trait.md#scoping-for-type-and-lifetime-parameters). It was based on some assumptions, namely: 1. Capturing all type parameters sufficed for the vast majority of use cases. 2. We would always have a more explicit syntax for controlling captures later. 3. It was considered bad ergonomics to allow lifetime capture without explicit syntax. The authors of this doc are not aware of any evidence to contradict the first point. The second point remains as an expectation, because TAIT and to a lesser extent ATPIT give us finer grained control over what lifetimes an opaque type is allowed to capture. On the third point however, we have since had some significant developments in the Rust language, namely the addition of async. The `async fn` syntax was created in part out of necessity because of the rather awkward desugaring it produced. We can see this in a simple example: ```rust struct MyServer { db: Database, } impl MyServer { async fn query(&self) -> Response { let results = self.db.query().await; Response::new(results) } } ``` The above method desugars to a signature like this: ```rust impl MyServer { fn query(&self) -> impl Future<Output = Response> + '_ { async { ... } } } ``` The reason for this is evident in the method body above. Almost every async method needs access to the `self` reference to do anything useful. Since all code in an async function runs as part of the coroutine embedded in the opaque future type, that opaque type therefore needs to capture that lifetime. There is an argument to be made that given this, along with the ergonomics issues of doing anything different we'll go into below, we should flip the default over an edition to capturing all lifetimes in scope. If we do not, we will probably have to invent some new syntax or other clever solution out of the problems we are about to cover. Eventually the lang team will need to decide on a resolution to this question, and it is useful context for the discussion we are having today. However, it is **not** an explicit outcome to be decided in today's meeting. ### What to do when the capture rules don't match your use case * `+ 'a` * `Captures` * Desugar to ITPIT or TAIT * Currently unstable * Alternative: Opt out syntax. But if `+ 'a` was already confusing, `+ ?'a` is going to be much worse. ## Framing There are two questions to be answered about RPITIT capture rules: * When should RPITITs be allowed to name lifetimes from their *method signature*? * When should RPITITs be allowed to name lifetimes from their *enclosing item* (impl, trait)? Let's take these one at a time. ### When should RPITITs be allowed to name lifetimes from their *method signature*? This question relates directly to the one we posed above. #### Answer: Only if mentioned ... The hazard of answering "only if mentioned" is that traits involving lifetimes and RPITITs are harder to write correctly. #### Answer: Always If we decide the answer should be "yes" to the "all generic lifetimes" question, we could decide to implement that behavior *now* for RPITITs. The hazard of answering "always" is that it is inconsistent with the existing behavior of RPIT in free functions and inherent impls. This inconsistency is likely to be noticed: You would need `+ 'a` (or `+ Captures<&'a ()>`) in some places and not in others. #### Recommendation TODO ### When should RPITITs be allowed to name lifetimes from their *enclosing item* (impl, trait)? #### Today: RPIT capture rules for inherent impls (example) The problem with this precedent is it's unworkable for RPITITs: (examples) #### Answer: Always * Inconsistent with inherent impls: If you copy the signature from an inherent impl to a trait or trait impl, you no longer need to mention `+ 'a` * Inconsistent with desugaring to ATPIT today, but this can be changed * Presumes that we have a way to "opt out" by desugaring to ATPITs or TAITs #### Answer: Only if mentioned in the impl * Inconsistent with inherent on the trait * Non copyable signatures from trait -> impl Additionally, if we answer "always" to the above question, it would be very surprising to answer "only if mentioned" to this one. #### Answer: Only if mentioned in the trait * Fully consistent with inherent * Unworkable #### Recommendation TODO ## Summary TODO: Matrix showing each possible decision, plus a list of use cases (below), plus an icon that says whether it works, works with style lint, works but has versioning hazard, or errors. Use cases: * Copying a signature from trait to trait impl * Copying a signature from inherent impl to trait * Copying a signature from inherent impl to trait impl * The above, but in reverse?? (Less important) * Authoring a trait from scratch Also consider: * Restricting captures of an RPIT in various positions * (related) Stages of desugaring ## Appendix We can include more examples here. Examples should have names.

    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