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

Odd inference error reporting around generics #75398

Closed
izik1 opened this issue Aug 11, 2020 · 1 comment
Closed

Odd inference error reporting around generics #75398

izik1 opened this issue Aug 11, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@izik1
Copy link

izik1 commented Aug 11, 2020

I made a MRE of a bug I encountered (in someone else's code actually):

struct S<T> {
    _t: std::marker::PhantomData<T>
}

impl<T> S<T> {
    fn f<U: ?Sized>(k: &U) where
    T: std::borrow::Borrow<U> {}
}

fn main() {
    let _ = |_: String| {};
    S::<String>::f("".as_ref());
}

I expected to see this happen: A compile error mentioning something about not being able to infer the type that as_ref should return, I suppose.
Instead, this happened:

error[E0283]: type annotations needed
  --> src/main.rs:12:5
   |
6  | /     fn f<U: ?Sized>(k: &U) where
7  | |     T: std::borrow::Borrow<U> {}
   | |________________________________- required by `S::<T>::f`
...
12 |       S::<String>::f("".as_ref());
   |       ^^^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
   |
   = note: cannot satisfy `std::string::String: std::borrow::Borrow<_>`

error: aborting due to previous error

Meta

Tested on playground, using stable, beta, and nightly (same error for all):

1.45.2
1.46.0-beta.4
1.47.0-nightly (2020-08-10 770bd3d1d03f0de2e27b)

Interesting variation:

struct S<T> {
    _t: std::marker::PhantomData<T>
}

impl<T> S<T> {
    fn f<U: ?Sized>(k: &U) where
    T: std::borrow::Borrow<U> {}
}

fn main() {
    S::<String>::f("".as_ref());
}

Which instead yields:

error[E0283]: type annotations needed
  --> src/main.rs:11:5
   |
6  | /     fn f<U: ?Sized>(k: &U) where
7  | |     T: std::borrow::Borrow<U> {}
   | |________________________________- required by `S::<T>::f`
...
11 |       S::<String>::f("".as_ref());
   |       ^^^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
   |
   = note: cannot satisfy `std::string::String: std::borrow::Borrow<_>`

error: aborting due to previous error

(which is just a poor error, I think)

@izik1 izik1 added the C-bug Category: This is a bug. label Aug 11, 2020
@jonas-schievink jonas-schievink 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 Aug 11, 2020
@lcnr
Copy link
Contributor

lcnr commented Oct 20, 2020

The error message has been fixed by #78111 and now is

error[E0283]: type annotations needed
  --> src/main.rs:12:5
   |
6  | /     fn f<U: ?Sized>(k: &U) where
7  | |     T: std::borrow::Borrow<U> {}
   | |_____________________________- required by `S::<T>::f`
...
12 |       S::<String>::f("".as_ref());
   |       ^^^^^^^^^^^^^^ ----------- this method call resolves to `&T`
   |       |
   |       cannot infer type for type parameter `U` declared on the associated function `f`
   |
   = note: cannot satisfy `String: Borrow<_>`

As we already added a very similar test in that PR I am closing this as is without adding an additional test for it.

Thanks for opening this issue.

@lcnr lcnr closed this as completed Oct 20, 2020
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. 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