Skip to content

Odd inference error reporting around generics  #75398

Closed
@izik1

Description

@izik1

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions