Skip to content

Cannot infer type in test from section 12.4 of rust book v2 #45164

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

Open
ghost opened this issue Oct 10, 2017 · 3 comments
Open

Cannot infer type in test from section 12.4 of rust book v2 #45164

ghost opened this issue Oct 10, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Oct 10, 2017

Hello everyone!
I'm learning Rust now using this book and faced strange(as for me) error on section 12.4 when running tests.

You can reproduce a bug with next snippet of code

pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> {
    text.lines().filter(|line| line.contains(q)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_should_compile_but_it_doesnt () {
        let q = "queryABCD";
        let t = "text with query";
        assert_eq!( vec![], search(q, t) );
    }
}

// error[E0282]: type annotations needed
//   --> src/lib.rs:13:21
//    |
// 13 |         assert_eq!( vec![], search(q, t) );
//    |                     ^^^^^^ cannot infer type for `T`
//    |
//    = note: this error originates in a macro outside of the current crate

However, it's fairly obvious that compiler knows the type annotation that must be infered from vec![] macro. Next snippet makes it clear

pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> {
    text.lines().filter(|line| line.contains(q)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_should_compile_but_it_doesnt () {
        let q = "queryABCD";
        let t = "text with query";
        assert_eq!( 333, search(q, t) );
    }
}

// error[E0277]: the trait bound `{integer}: std::cmp::PartialEq<std::vec::Vec<&str>>` is not satisfied
//   --> src/lib.rs:13:9
//    |
// 13 |         assert_eq!( 333, search(q, t) );
//    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `{integer}` with `std::vec::Vec<&str>`
//    |
//    = help: the trait `std::cmp::PartialEq<std::vec::Vec<&str>>` is not implemented for `{integer}`
//    = note: this error originates in a macro outside of the current crate

I think it's a bug but not sure since I'm novice in rust. I tried different toolchains, specifically 1.16, 1.18, 1.20(stable) and nightly and got the same result.

@TimNN
Copy link
Contributor

TimNN commented Oct 10, 2017

I think this is expected behaviour, but maybe the error could be better.

The problem is that while the compiler knows that you want to compare something to Vec<&str>, it doesn't know what exactly: You can, for example, compare both a Vec<String> and Vec<&str>.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 10, 2017
@ghost
Copy link
Author

ghost commented Oct 10, 2017

Thanks for your reply, I got the idea. Should I file a bug elsewhere for documentation team to fix their code in problematic section?

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference labels Oct 29, 2019
@estebank
Copy link
Contributor

Current output

error[E0282]: type annotations needed
  --> src/lib.rs:13:21
   |
13 |         assert_eq!( vec![], search(q, t) );
   |                     ^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Vec`
   |
   = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
  --> src/lib.rs:13:21
   |
13 |         assert_eq!( vec![], search(q, t) );
   |         ------------^^^^^^----------------
   |         |           |
   |         |           cannot infer type of the type parameter `T` declared on the struct `Vec`
   |         type must be known at this point
   |
   = note: multiple `impl`s satisfying `_: PartialEq<&str>` found in the following crates: `alloc`, `core`, `std`:
           - impl<'a, 'b> PartialEq<&'a str> for String;
           - impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>;
           - impl<> PartialEq<&str> for OsString;
           - impl<A, B> PartialEq<&B> for &A
             where A: PartialEq<B>, A: ?Sized, B: ?Sized;
           - impl<A, B> PartialEq<&B> for &mut A
             where A: PartialEq<B>, A: ?Sized, B: ?Sized;
   = note: required for `Vec<_>` to implement `PartialEq<Vec<&str>>`
   = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

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-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

2 participants