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

Improve error msgs when found type is deref of expected #82364

Merged
merged 1 commit into from
Feb 25, 2021

Commits on Feb 23, 2021

  1. Improve error msgs when found type is deref of expected

    This improves help messages in two cases:
    
    - When expected type is `T` and found type is `&T`, we now look through blocks
      and suggest dereferencing the expression of the block, rather than the whole
      block.
    
    - In the above case, if the expression is an `&`, we not suggest removing the
      `&` instead of adding `*`.
    
    Both of these are demonstrated in the regression test. Before this patch the
    first error in the test would be:
    
        error[E0308]: `if` and `else` have incompatible types
         --> test.rs:8:9
          |
        5 | /     if true {
        6 | |         a
          | |         - expected because of this
        7 | |     } else {
        8 | |         b
          | |         ^ expected `usize`, found `&usize`
        9 | |     };
          | |_____- `if` and `else` have incompatible types
          |
        help: consider dereferencing the borrow
          |
        7 |     } else *{
        8 |         b
        9 |     };
          |
    
    Now:
    
        error[E0308]: `if` and `else` have incompatible types
         --> test.rs:8:9
          |
        5 | /     if true {
        6 | |         a
          | |         - expected because of this
        7 | |     } else {
        8 | |         b
          | |         ^
          | |         |
          | |         expected `usize`, found `&usize`
          | |         help: consider dereferencing the borrow: `*b`
        9 | |     };
          | |_____- `if` and `else` have incompatible types
    
    The second error:
    
        error[E0308]: `if` and `else` have incompatible types
          --> test.rs:14:9
           |
        11 | /     if true {
        12 | |         1
           | |         - expected because of this
        13 | |     } else {
        14 | |         &1
           | |         ^^ expected integer, found `&{integer}`
        15 | |     };
           | |_____- `if` and `else` have incompatible types
           |
        help: consider dereferencing the borrow
           |
        13 |     } else *{
        14 |         &1
        15 |     };
           |
    
    now:
    
        error[E0308]: `if` and `else` have incompatible types
          --> test.rs:14:9
           |
        11 | /     if true {
        12 | |         1
           | |         - expected because of this
        13 | |     } else {
        14 | |         &1
           | |         ^-
           | |         ||
           | |         |help: consider removing the `&`: `1`
           | |         expected integer, found `&{integer}`
        15 | |     };
           | |_____- `if` and `else` have incompatible types
    
    Fixes rust-lang#82361
    osa1 committed Feb 23, 2021
    Configuration menu
    Copy the full SHA
    fa74d48 View commit details
    Browse the repository at this point in the history