misleading error message for trait usage from external scope

1076f69
Opened by Jesse at 2022-12-16 00:58:12

Trying to implement a trait for a type and then running a unit test produces the following misleading error message.

I tried this code:

use std::vec::Vec;
mod rlinq {
    trait RLINQ {
        fn is_where(&self);
    }

    impl RLINQ for Vec<String> {
        fn is_where(&self) {
            println!("{:?}", "Vec now implements where");
        }
    }
}
#[cfg(test)]
mod tests {
    use rlinq::RLINQ;
    #[test]
    fn is_where_test() {
        let x: Vec<String> = Vec::new();
        x.is_where();
    }
}

I expected to see this happen: Error letting me know trait was private and unable to be used in current scope

Instead, this happened:

help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
   = help: candidate #1: `use rlinq::RLINQ`

Meta

rustc --version --verbose:

rustc 1.14.0-nightly (7c69b0d5a 2016-11-01)
binary: rustc
commit-hash: 7c69b0d5ae8c404c4847fa214126ebd7c0246277
commit-date: 2016-11-01
host: x86_64-unknown-linux-gnu
release: 1.14.0-nightly
LLVM version: 3.9

Backtrace: N/A

  1. Note that the real error is reported as well, the full output is:

    error: trait `RLINQ` is private
      --> <anon>:15:9
       |
    15 |     use rlinq::RLINQ;
       |         ^^^^^^^^^^^^
    
    error: no method named `is_where` found for type `std::vec::Vec<std::string::String>` in the current scope
      --> <anon>:19:11
       |
    19 |         x.is_where();
       |           ^^^^^^^^
       |
       = help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
       = help: candidate #1: `use rlinq::RLINQ`
    
    error: aborting due to previous error
    

    Tim Neumann at 2016-11-07 15:23:44

  2. Triage: no change.

    Esteban Kuber at 2018-02-10 02:46:50

  3. @estebank Should we close this issue as it's probably changed (Ill confirm soon) with the recent overhaul of the error messages in Rust.

    Jesse at 2018-05-23 17:00:39

  4. @TheOpenDevProject the current output is:

    error[E0603]: trait `RLINQ` is private
      --> src/main.rs:14:9
       |
    14 |     use rlinq::RLINQ;
       |         ^^^^^^^^^^^^
    
    error[E0599]: no method named `is_where` found for type `std::vec::Vec<std::string::String>` in the current scope
      --> src/main.rs:18:11
       |
    18 |         x.is_where();
       |           ^^^^^^^^
       |
       = help: items from traits can only be used if the trait is implemented and in scope
       = note: the following trait defines an item `is_where`, perhaps you need to implement it:
               candidate #1: `rlinq::RLINQ`
    

    There's still room for improvement, I believe.

    Esteban Kuber at 2018-05-23 22:12:34

  5. If you want to assign this one to me ill take a look next week

    Jesse at 2018-05-24 07:00:17

  6. Triage: no change.

    Esteban Kuber at 2019-10-11 22:56:53

  7. Current output:

    error[E0603]: trait `RLINQ` is private
      --> src/lib.rs:14:16
       |
    14 |     [use rlinq::RLINQ;](https://play.rust-lang.org/?gist=a296dc331e14c85c860158172a765497&version=nightly&mode=debug#)
       |                ^^^^^ private trait
       |
    note: the trait `RLINQ` is defined here
      --> src/lib.rs:2:5
       |
    2  |     trait RLINQ {
       |     ^^^^^^^^^^^
    
    error[E0599]: no method named `is_where` found for struct `Vec<String>` in the current scope
      --> src/lib.rs:18:11
       |
    18 |         x.is_where();
       |           ^^^^^^^^ method not found in `Vec<String>`
       |
       = help: items from traits can only be used if the trait is implemented and in scope
    note: `RLINQ` defines an item `is_where`, perhaps you need to implement it
      --> src/lib.rs:2:5
       |
    2  |     trait RLINQ {
       |     ^^^^^^^^^^^
    

    Esteban Kuber at 2022-12-16 00:58:12