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

Dereferencing Deref types other than references is missing suggestion #59819

Closed
oli-obk opened this issue Apr 9, 2019 · 0 comments · Fixed by #60155 or #72292
Closed

Dereferencing Deref types other than references is missing suggestion #59819

oli-obk opened this issue Apr 9, 2019 · 0 comments · Fixed by #60155 or #72292
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Apr 9, 2019

The b = a assignment below suggests dereferencing a in order to make the code compile. The same suggestion applies to y = x, but no suggestion is produced by the compiler. I think we should report this suggestion for all type mismatches that satisfy GotType: Deref<Target = ExpectedType>

struct Foo(i32);

impl std::ops::Deref for Foo {
    type Target = i32;
    fn deref(&self) -> &i32 {
        &self.0
    }
}

fn main() {
    let x = Foo(42);
    let y: i32 = x;
    let a = &42;
    let b: i32 = a;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:12:18
   |
12 |     let y: i32 = x;
   |                  ^ expected i32, found struct `Foo`
   |
   = note: expected type `i32`
              found type `Foo`

error[E0308]: mismatched types
  --> src/main.rs:14:18
   |
14 |     let b: i32 = a;
   |                  ^
   |                  |
   |                  expected i32, found &{integer}
   |                  help: consider dereferencing the borrow: `*a`
   |
   = note: expected type `i32`
              found type `&{integer}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

@oli-obk oli-obk added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. labels Apr 9, 2019
@davidtwco davidtwco self-assigned this Apr 21, 2019
bors added a commit that referenced this issue Apr 23, 2019
Suggest dereferencing when `Deref` is implemented.

Fixes #59819.

r? @oli-obk
cc @estebank
RalfJung added a commit to RalfJung/rust that referenced this issue May 23, 2020
Replace obligation construction with deref_steps()

1. Use `probe()` to avoid unwanted binding committing during `deref_steps()`.
2. Fixes rust-lang#59819 again by using `deref_steps()`, make the code cleaner. And if we want to suggest multiple dereferences (like: `consider dereferencing the borrow: "****a"`) in the future, this change will make it easier to achieve.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 23, 2020
Replace obligation construction with deref_steps()

1. Use `probe()` to avoid unwanted binding committing during `deref_steps()`.
2. Fixes rust-lang#59819 again by using `deref_steps()`, make the code cleaner. And if we want to suggest multiple dereferences (like: `consider dereferencing the borrow: "****a"`) in the future, this change will make it easier to achieve.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 23, 2020
Replace obligation construction with deref_steps()

1. Use `probe()` to avoid unwanted binding committing during `deref_steps()`.
2. Fixes rust-lang#59819 again by using `deref_steps()`, make the code cleaner. And if we want to suggest multiple dereferences (like: `consider dereferencing the borrow: "****a"`) in the future, this change will make it easier to achieve.
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`.
Projects
None yet
2 participants