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
      • 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 Sharing URL Create Help
Create Create new note Create a note from template
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
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
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
# Stabilization Report: Minimal Type Alias Impl Trait ## Links * Relevant RFCs: * Relevant tracking issue(s): * Implementation history: too hard to track ## To be stabilized *Type alias impl Trait* (TAIT) refers to impl Trait appearing in a type alias. `impl Trait` in this position can appear as the entire value of the type alias: ```rust= type Alias1 = impl Trait; ``` ### Desugarings and extended forms Most of this report is concerned only with the 'simple' TAIT described in the previous form, and thus written as if each TAIT has a name. However, we do support some extended forms, which are effectively desugarings. These extended forms are described here. #### Non-trivial type aliases `impl Trait` does not have to appear at the "top level" of a type alias. It may appear embedded within other types: ```rust= type Alias2 = (impl Trait, impl Trait); ``` One can think of the latter case (`Alias2`) as a kind of shorthand that expands to multiple named aliases: ```rust= type Temp0 = impl Trait; type Temp1 = impl Trait; type Alias2 = (Temp0, Temp1); ``` FIXME: should maybe be a bit more specific here; for example, this doesn't work (should it?): ```rust= type Z = fn() -> impl Sized; fn bar() -> Z { || 0u32 } ``` #### In an impl Impl Trait can also appear in an impl: ```rust= impl Service for MyType { type Future = impl Future<Output = Response>; fn process_request(&self) -> Self::Future { async move { ... } } } ``` This can effectively be desugared into a TAIT whose scope encompasses just the impl. Roughly like the following: FIXME: double check this desugaring is correct ```rust= mod _hidden_module { use super::*; type Future = impl Future<Output = Response>; impl Service for MyType { fn process_request(&self) -> Future { async move { ... } } } } ``` ### Semantics of a type alias impl Trait as discussed in the RFC A TAIT conceptually creates an "opaque type" whose underlying concrete type is inferred by the compiler. The inference is done based on the other items within the same module as the impl Trait (transitively). Example: ```rust= mod m { pub type MyFuture = impl Future<Output = u32>; pub fn foo() -> MyFuture { async move { 22 } } } ``` References to a type alias impl trait from outside the module treat it like an "opaque alias" (i.e., some type that implements future). Note that it has identity, even if we don't know precisely what team it is: ```rust= mod m { .. /* as above */ .. } pub fn bar() -> m::MyFuture { foo() // OK } ``` Type alias impl Trait requires uses within the module where it is defined. You cannot have a type alias impl trait that is defined by uses outside of the module: ```rust= mod m { pub type MyFuture = impl Future; // Error, no uses from outside } pub fn bar() -> m::MyFuture { foo() // OK } ``` ### Semantics that are up for stabilization ("minimal type alias impl Trait") The "minimal type alias impl Trait" (MTAIT) up for stabilization today limits the set of places where a TAIT `T` is used. To be accepted, a TAIT `T` defined in the module `m` must be used only in the following places: * outside of the module `m` (in which case, `T` is used as an opaque alias) * "defining uses" within the module `m`: * in the return type of a function * as the value for an associated type #### Higher-order pattern matching, not unification XXX ### Places where the compiler's behavior today differs from the RFC (not eligible for standardization) The RFC permits TAIT to be used in many locations, most of which are still considered unstable. This is partly because the compiler's inference algorithm is not yet able to handle the full complexity described by the RFC. Here are some examples that remain unstable: ```rust= type MyFuture = impl Future; pub fn foo(x: MyFuture) { // Illegal: cannot use MyFuture in an argument } pub fn foo(x: MyFuture) -> MyFuture { // Illegal: cannot use MyFuture in an argument x } struct Foo { x: MyFuture // Illegal: cannot use MyFuture in a field type } ``` FIXME(Jack): maybe add a comment about submodule workaround for using type in struct field ## Applications of this subset This can be used to define traits that return futures, although to truly handle the use case in full one also needs generic associated types. But MTAIT can be used for the tower [`Service`](https://docs.rs/tower/0.4.8/tower/trait.Service.html) trait, for example. ## Test cases * Defining use is in a submodule * Defining use that doesn't meet the required bounds * Use of `impl Trait` in an impl as value of an associated type * `impl Service { type Future = impl Future; fn foo() -> Self::Future { ... } }` * Desugaring: * Principle: any place that the value of the impl Trait is uniquely determined by the value of Foo * use of `impl Trait` in a tuple * `type Foo = (impl Trait, u32)` * use of `impl Trait` in a struct argument * `type Foo = Vec<impl Trait>` * use of `impl Trait` in an impl as the value for an associated type in a dyn * `type Foo = Box<dyn Iterator<Item = impl Debug>>` * use of `impl Trait` in an impl as the value for an associated type in an impl trait * `type Foo = impl Iterator<Item = impl Debug>` * `type Bar = impl Debug; type Foo = impl Iterator<Item = Bar>` * use in a fn type `type Foo = fn(impl Trait)` * `type Foo = fn(impl Debug)` * Incomplete inference for the type (some parts unspecified) * [Example that should error](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=86a2e58c143a916822a50ef052e142a5) * Disagreement between fns in types * Disagreement between fns in the lifetimes * Use outside of a "defining use" * type of a `let` * argument types * field types * static/const type ? * TODO input type in impl Trait (knowing the self type doesn't tell us the other type parameters) * `type Bar = impl Debug; type Foo = impl PartialEq<Bar>;` * `type Bar = impl Debug; fn foo() -> impl PartialEq<Bar> { }` * [compiles today if inference can succeed](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f74583da2541166f778169bcc744eeac) * [but not otherwise](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=ae806589bbb58dbaf88897c438925f05) * Defining use sites: * value of an associated type * `type Foo = impl Trait; impl Foo { type Item = Foo }` * function return type * `type Foo = impl Trait; fn foo() -> Foo { }` * Inference cycle * Auto trait leakage * Weird return types: * `-> impl Future<Output = N>` * `-> impl Trait<N>` * `fn(N)` ## Logical definition/Chalk FIXME - Current tracking issue for Chalk [#335](https://github.com/rust-lang/chalk/issues/335) - Outstanding issue there is WF/implied bounds - Followup with hidden type "isn't known"?

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