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

Include explicit (de)referencing in E0283 #84156

Open
eggyal opened this issue Apr 13, 2021 · 1 comment
Open

Include explicit (de)referencing in E0283 #84156

eggyal opened this issue Apr 13, 2021 · 1 comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eggyal
Copy link
Contributor

eggyal commented Apr 13, 2021

Given the following code (playground):

use std::borrow::Borrow;

fn main() {
    let v = vec![0];
    v.borrow();
}

The current output is:

error[E0283]: type annotations needed
 --> src/main.rs:5:7
  |
5 |     v.borrow();
  |     --^^^^^^--
  |     | |
  |     | cannot infer type for type parameter `Borrowed` declared on the trait `Borrow`
  |     this method call resolves to `&Borrowed`
  |     help: use the fully qualified path for the potential candidate: `<Vec<T> as Borrow<[T]>>::borrow(v)`
  |
  = note: cannot satisfy `Vec<i32>: Borrow<_>`

Ideally the output should look like:

error[E0283]: type annotations needed
 --> src/main.rs:5:7
  |
5 |     v.borrow();
  |     --^^^^^^--
  |     | |
  |     | cannot infer type for type parameter `Borrowed` declared on the trait `Borrow`
  |     this method call resolves to `&Borrowed`
  |     help: use the fully qualified path for the potential candidate: `<Vec<T> as Borrow<[T]>>::borrow(&v)`
  |
  = note: cannot satisfy `Vec<i32>: Borrow<_>`

The compiler already determined that the v needed to be deref coerced to &Borrowed but then suggested invoking ::borrow(v) which obviously won't type check; it should suggest invoking ::borrow(&v) instead (and likewise for the mutable case). If dereferencing was required in the method resolution, then that should also be included in the suggestion: ::borrow(&**v) etc.

@rustbot label: A-coercions A-diagnostics A-inference -C-enhancement C-bug D-invalid-suggestion

@eggyal eggyal added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 13, 2021
@rustbot rustbot added A-coercions Area: implicit and explicit `expr as Type` coercions A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Apr 13, 2021
@estebank
Copy link
Contributor

Current output:

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed
 --> src/main.rs:5:7
  |
5 |     v.borrow();
  |       ^^^^^^
  |
  = note: multiple `impl`s satisfying `Vec<i32>: Borrow<_>` found in the following crates: `alloc`, `core`:
          - impl<T, A> Borrow<[T]> for Vec<T, A>
            where A: Allocator;
          - impl<T> Borrow<T> for T
            where T: ?Sized;
help: try using a fully qualified path to specify the expected types
  |
5 |     <Vec<i32> as Borrow<Borrowed>>::borrow(&v);
  |     ++++++++++++++++++++++++++++++++++++++++ ~

It should be

help: try using a fully qualified path to specify the expected types
  |
5 |     <Vec<i32> as Borrow<[i32]>>::borrow(&v);
  |     ++++++++++++++++++++++++++++++++++++++++ ~

@estebank estebank removed C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants