Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The never type and diverging type variables #85021

Closed
wants to merge 2 commits into from

Commits on May 14, 2021

  1. Always replace the never type by a diverging type variable

    Previously, the never type will be replaced by a diverging
    type variable (generally) only if some coercion occurs. It
    can cause inconsistent behaviors like
    
    ```rust
    return.foo();      //~ ERROR no method named `foo` found for type `!`
    { return }.foo();  //~ ERROR type annotations needed
    ```
    
    ```rust
    let a = (return, );          // The type is `(!, )`.
    let a = ({ return }, );      // The type is `(_, )`.
    let a: (_, ) = (return, );   // The type is `(_, )`.
    ```
    
    With this commit, the never type will be replaced by a
    diverging type variable just at the end of the type check
    for every expression, even if no coercion occurs. Thus
    the problems above get solved and the consistency should
    be improved.
    lrh2000 committed May 14, 2021
    Configuration menu
    Copy the full SHA
    3231e54 View commit details
    Browse the repository at this point in the history
  2. Allow early fallback of diverging type variables in typeck

    Previously, we issue "type annotations needed" when types
    must be known at some point but the resolution failed,
    even if the type variables are just some diverging ones.
    A typical example is `{ return }.foo()`.
    
    With this commit, the information about diverging is recorded
    in the unification table, so that we can check whether
    performing the fallback affects other non-diverging type
    variables. If it doesn't, we will safely perform the fallback
    and we won't issue "type annotations needed" anymore.
    
    Note lots of auxiliary type variables should be ignored during
    the check, which is done with the help of `TypeVariableOriginKind`.
    
    As a result, "type annotations needed" will be issued for
    ```rust
    let a = return;
    { if true { a } else { return } }.foo();
    ```
    but not for
    ```rust
    let a: ! = return;
    { if true { a } else { return } }.foo();
    ```
    lrh2000 committed May 14, 2021
    Configuration menu
    Copy the full SHA
    d918387 View commit details
    Browse the repository at this point in the history