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

Conversation

osa1
Copy link
Contributor

@osa1 osa1 commented Feb 21, 2021

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 #82361


r? @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 21, 2021
compiler/rustc_typeck/src/check/demand.rs Outdated Show resolved Hide resolved
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 22, 2021

📌 Commit d2c0a4433c5d057ca93eb19d6e57dad4288728e5 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 22, 2021
@osa1
Copy link
Contributor Author

osa1 commented Feb 22, 2021

@rustbot label: -S-waiting-on-bors +S-waiting-on-review

@estebank addressed your comment. Could you review again?

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 22, 2021
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one minor change that is needed for the suggestion to be correct, otherwise <3.

src/test/ui/suggestions/issue-82361.stderr Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/demand.rs Outdated Show resolved Hide resolved
src/test/ui/suggestions/issue-82361.rs Show resolved Hide resolved
@osa1
Copy link
Contributor Author

osa1 commented Feb 22, 2021

@estebank all done

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 22, 2021

📌 Commit b5ec7f36ce5394882b983f5368ac8da17e1d3a6d has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 22, 2021
@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Feb 22, 2021
@bors
Copy link
Contributor

bors commented Feb 23, 2021

☔ The latest upstream changes (presumably #82430) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 23, 2021
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
Copy link
Contributor Author

osa1 commented Feb 23, 2021

@estebank I had to rebase this so this needs another approval now :-(

@osa1
Copy link
Contributor Author

osa1 commented Feb 23, 2021

@rustbot label: -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 23, 2021
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 23, 2021

📌 Commit fa74d48 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 23, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2021
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

---

r? `@estebank`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2021
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

---

r? ``@estebank``
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2021
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

---

r? ```@estebank```
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 25, 2021
Rollup of 16 pull requests

Successful merges:

 - rust-lang#75807 (Convert core/num/mod.rs to intra-doc links)
 - rust-lang#80534 (Use #[doc = include_str!()] in std)
 - rust-lang#80553 (Add an impl of Error on `Arc<impl Error>`.)
 - rust-lang#81167 (Make ptr::write const)
 - rust-lang#81575 (rustdoc: Name fields of `ResolutionFailure::WrongNamespace`)
 - rust-lang#81713 (Account for associated consts in the "unstable assoc item name colission" lint)
 - rust-lang#82078 (Make char and u8 methods const)
 - rust-lang#82087 (Fix ICE caused by suggestion with no code substitutions)
 - rust-lang#82090 (Do not consider using a semicolon inside of a different-crate macro)
 - rust-lang#82213 (Slices for vecs)
 - rust-lang#82214 (Remove redundant to_string calls)
 - rust-lang#82220 (fix the false 'defined here' messages)
 - rust-lang#82313 (Update normalize.css to 8.0.1)
 - rust-lang#82321 (AST: Remove some unnecessary boxes)
 - rust-lang#82364 (Improve error msgs when found type is deref of expected)
 - rust-lang#82514 (Update Clippy)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 12ea0f6 into rust-lang:master Feb 25, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 25, 2021
@osa1 osa1 deleted the issue82361 branch February 26, 2021 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect help text when if and else differ by reference
6 participants