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

Compiler suggests dereferencing an else block #79736

Closed
jyn514 opened this issue Dec 5, 2020 · 1 comment · Fixed by #83489
Closed

Compiler suggests dereferencing an else block #79736

jyn514 opened this issue Dec 5, 2020 · 1 comment · Fixed by #83489
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@jyn514
Copy link
Member

jyn514 commented Dec 5, 2020

I tried this code (playground):

    let a = &1;
    let b = &2;
    let val = if true {
        a + 1
    } else {
        b
    };

I expected to see this happen: The compiler suggests turning b into *b, since a + 1 is an i32 but b is a &i32.

Instead, this happened:

error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:9:9
   |
6  |       let val = if true {
   |  _______________-
7  | |         a + 1
   | |         ----- expected because of this
8  | |     } else {
9  | |         b
   | |         ^ expected integer, found `&{integer}`
10 | |     };
   | |_____- `if` and `else` have incompatible types
   |
help: consider dereferencing the borrow
   |
8  |     } else *{
9  |         b
10 |     };
   |

If you apply the suggestion, you get another compile error:

error: expected `{`, found `*`
 --> src/main.rs:6:12
  |
6 |     } else *{
  |            ^ expected `{`
  |
help: try placing this code inside a block
  |
6 |     } else { *{
7 |         b
8 |     } };
  |

Applying that suggestion makes it compile, but is much harder to read than just *b.

Meta

rustc --version: 1.50.0-nightly (2020-12-03 5be3f9f)

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 5, 2020
@mibac138
Copy link
Contributor

mibac138 commented Dec 5, 2020

@rustbot claim

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 C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
2 participants