blitzerr
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Fallback and never types ###### tags: `rustc`, `never-type`, `bug` ## Summary - My work-item: Regression from fallback to `()` [67225](https://github.com/rust-lang/rust/issues/67225) - [Mentoring notes](https://github.com/rust-lang/rust/issues/66173#issuecomment-606836855) - The Umbrella tracking Issue: [35121](https://github.com/rust-lang/rust/issues/35121) ## Solution - Step 1 - Add a lint. This is tracked by [66173](https://github.com/rust-lang/rust/issues/66173#issuecomment-606836855) ## Background ### [RFC 1216](https://github.com/rust-lang/rfcs/blob/master/text/1216-bang-type.md) - Introduces the _bang type_ or _empty type_. An empty type is something that never exists in the runtime as there is no way to create one and therefore, such type in code gives us a static gurantee that a piece of code will never be executed. ```rust= use std::process::exit; fn main() { let x: ! = exit(0); // `x` is in scope, everything below // here is dead code. let y: String = match x { // no match cases as `Never` has // no variants }; // we can still use `y` though println!("Our string is: {}", y); // we can use `x` to diverge x } ``` #### Why a new type. - Gives us a way to use diverging functions in generic types. ### What is a divergent type An HIR node is divergent if it does not exit normally, instead it _`return`s / `break`s / `panic`s_ leaving behind dead code. In rustc, this is tracked using the enum [Diverges](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/check/enum.Diverges.html). There are different variants of it: 1. `Maybe` - Some of the cases diverge, while others require further analysis to determine. 2. `Always` - Certainly known to diverge, so next sibling and parent are unreachable. 3. `WarnedAlways` - Same as `Always` with a reachability warning always emitted. _Further explanations given by Niko:_ So, what would a never returning node result in ? Ans: `!` type. Similarly, `!` is also a type that can be assigned. Therefore, sometimes the compiler creates an inference variable `?X` to represent _the type to which `!` is being coersed_. e.g. ```rust let x: u32 = return ... ``` will type-check, because the type of return will be `?X` and `?X = u32` is a valid result. ## The problem The problem is that we generally require all inference variables to have a defined value. Which is why `let x = None` won't type-check on its own and would require `Option<?T>`. What is `?T` ? Ans: Its a constraint. But it would be annoying if we required to constrain the divergent variables as it should not matter what `!` is coersed to. Therefore, ```rust let x = return 22; ``` shouldn't fail compilation if we can't figure out the type of `x`. Therefore, **today**, we assign `?X` as the type of `x` and at the end of type-checking, when we still couldn't resolve `?X`, we fallback to `()` as the type of `x`. ##### The solution The fallback type in this case should be `!` instead of `()`. We start out by detecting cases where that's a problem. ----------------------- ```rust fn unconstrained_return<T>() -> Result<T, String> { let ffi: fn() -> T = transmute(some_pointer); Ok(ffi()) } fn foo() { match unconstrained_return::<_>() { Ok(x) => x, // `x` has type `_`, which is unconstrained Err(s) => panic!(s), // … except for unifying with the type of `panic!()` // so that both `match` arms have the same type. // Therefore `_` resolves to `!` and we "return" an `Ok(!)` value. }; } ``` Walk through what goes wrong: - the type parameter `T` gets specified as `_` -- `_` means "make fresh inference variable", let's call it `?X` - type of `unconstrained_return::<?X>()` is `Result<?X, String>` - type of `x` in `Ok(x)` is `?X` - type of the match arm `Ok(x) => x` is `?X` - type of `s` in `Err(s)` is `String` - type of the match arm `Err(s) => panic!(s)` is `!` - because the type of the expression is `!` we create a fresh variable `?V` - and we mark `?V` as diverging - the two match arms have to have the same type - more specifically, there must be some type `M` such that both match arms can be coerced to that type `M` - create a inference variable `?M` where `?X` can be coerced to `?M` - and `?V` can be coerced to `?M` - effectively we unify `?X = ?V = ?M` - at the end, `?X` and `?V` are both unconstrained - no fallback for `?X` - `?V` falls back to `!`, so effectively `?V` becomes `!` - because `?X` is unified, it becomes `!` Where is this implemented? * [link](https://github.com/nikomatsakis/rust/blob/cacda2d7a0d43c6038329e980b8ab339dd0ab9af/src/librustc_typeck/check/mod.rs#L1060) * [`fallback_if_possible`](https://github.com/nikomatsakis/rust/blob/cacda2d7a0d43c6038329e980b8ab339dd0ab9af/src/librustc_typeck/check/mod.rs#L3316) * my [commit](https://github.com/nikomatsakis/rust/commit/e3a96450d501bc72f876945aadbd05307a9570c9) tweaks this to compute "tainted" types by * save the variables before we apply fallback * apply fallback, solve trait obligations * then check those variables again to see which became `!` Slightly bigger than the set of unified variables ```rust fn foo<T, U>() where T: Eq<U> { } trait Eq<A> { } impl Eq<!> for ! { } ``` ### Path to solution - Niko's initial [PR](https://github.com/nikomatsakis/rust/commit/e3a96450d501bc72f876945aadbd05307a9570c9) to add a lint - Key Idea in the PR: warn whenever the type of ***any*** expression is changed as a result of `diverging fallback`. - identify type variables that that got their value as a result of diverging fallback - warn when we find those type of variables in "certain contexts" - What we actually want ? - warn whenever the type of a ***live*** expression is changed as the result of `diverging fallback`. #### Niko's suggestion for solution 1. _mark dead nodes_: `HIR_ID` for expressions where flag was set to `warnedAlways` on entry. 2. [`warn_if_unreachable`](https://github.com/nikomatsakis/rust/blob/e3a96450d501bc72f876945aadbd05307a9570c9/src/librustc_typeck/check/mod.rs#L2779) 3. probably add the `hir_id` to some set [roughly on this line](https://github.com/nikomatsakis/rust/blob/e3a96450d501bc72f876945aadbd05307a9570c9/src/librustc_typeck/check/mod.rs#L2805) if `self.diverges()` is set to `WarnedAlways` 3. Issues a warning if: 1. the type of a HIR_ID contains a type variable whose value is set by divergent fallback **AND** 2. The expression is not in the set of dead nodes (i.e. it maybe live). Example of case where we would NOT issue a warning but is still UB: ```rust // the bet is that an unconstrained return value like `T` here signals data // that is never used, e.g. this fn could return `Ok(())`, but in this case // that is not true: fn unconstrained_return<T>() -> Result<(), T> { let ffi: fn() -> T = transmute(some_pointer); ffi(); // produces a value of type T } fn foo() { match unconstrained_return::<_>() { // _ is ?X Ok(_) => Ok(()), // type of `Ok` is `Result<(), ?A>` Err(s) => Err(panic!(s)), // type of `Err(_)` is `Result<?B, ?V>` where `?V` is diverging }; // type of expression is `Result<(), ?V>` } ``` ## Progress - [Initial WIP PR](https://github.com/rust-lang/rust/pull/74535) - [This UT](src/test/ui/never_type/never-value-fallback-issue-66757.rs) can be used as reference. -

    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