Skip to content

Misleading "error: source trait is private" when a trait is not imported #22050

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

Closed
gkoz opened this issue Feb 7, 2015 · 13 comments
Closed

Misleading "error: source trait is private" when a trait is not imported #22050

gkoz opened this issue Feb 7, 2015 · 13 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@gkoz
Copy link
Contributor

gkoz commented Feb 7, 2015

This example uses rust-crypto 0.2.14. Failing to import the Encryptor trait that implements encrypt causes an unexpected error message. The trait isn't private and importing it compiles as expected.

extern crate crypto;

use crypto::{ buffer, aes, blockmodes };
use crypto::buffer::{ ReadBuffer, WriteBuffer };

fn enc(key: &[u8], iv: &[u8], data: &[u8]) {
    // Don't import the trait that implements encrypt()
    //use crypto::symmetriccipher::Encryptor;

    let mut encryptor = aes::cbc_encryptor(aes::KeySize::KeySize256,
                key, iv, blockmodes::PkcsPadding);

    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    // Expected error: type `_` does not implement any method in scope named `encrypt`
    // Get error: source trait is private
    encryptor.encrypt(&mut read_buffer, &mut write_buffer, true);
}

fn main() {
}
src/main.rs:19:5: 19:65 error: source trait is private
src/main.rs:19     encryptor.encrypt(&mut read_buffer, &mut write_buffer, true);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

rustc 1.0.0-nightly (706be5ba1 2015-02-05 23:14:28 +0000)

@kmcallister kmcallister added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 7, 2015
@GuillaumeGomez
Copy link
Member

The new error message would need to be something like "error: source trait not found" I guess ?

@shepmaster
Copy link
Member

Another person got bit

@Wilfred
Copy link
Contributor

Wilfred commented Jul 17, 2015

For what it's worth, I was confused by this issue today. I was trying to use methods on quickcheck's Gen trait, which inherits from rand::Rng.

I tried adding use rand::Rng but the same error occurred. I had to add to my Cargo.toml:

[dependencies]
rand = "*"

This wasn't obvious to me at all.

@steveklabnik
Copy link
Member

me too

@GuillaumeGomez
Copy link
Member

So should I replace it by "error: source trait not found" ?

@steveklabnik
Copy link
Member

I would argue it should be

error: use of undeclared trait name `Foo`

Because that's what happens when you do something like

fn foo<T: Foo>() {}

fn main() {

}

@Wilfred
Copy link
Contributor

Wilfred commented Jul 17, 2015

@GuillaumeGomez That would be clearer, yep. It could also suggest adding an extern crate... or an entry in Cargo.toml, although perhaps this occurs in other cases that haven't occurred to me.

@steveklabnik
Copy link
Member

well, i guess that's different code than the OP's. Basically, the error should mirror what happens when it doesn't exist

@gkoz
Copy link
Contributor Author

gkoz commented Jul 18, 2015

Am I wrong to expect

error: no method named `encrypt` found for type `_` in the current scope

Possibly with a hint about importing the correct trait. Y'know like it usually happens.

@jethrogb
Copy link
Contributor

Also expecting the error message @gkoz mentions. What is different about this than, for example:

let mut buf=[0u8;32];
(&mut buf[..]).write_all(&b"test");

which yields

<anon>:3:20: 3:39 error: no method named `write_all` found for type `&mut [u8]` in the current scope
<anon>:3     (&mut buf[..]).write_all(&b"test");
                            ^~~~~~~~~~~~~~~~~~~
<anon>:3:20: 3:39 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:
<anon>:3:20: 3:39 help: candidate #1: use `std::io::Write`

@naufraghi
Copy link

naufraghi commented Apr 28, 2016

Got the same problem (1.8.0):

//extern crate num;
extern crate nalgebra;

pub fn fps_look_at<T: nalgebra::BaseFloat>(eye: Vector3<T>, pitch: T, yaw: T) -> Matrix4<T> {
    let (sin_pitch, cos_pitch) = pitch.sin_cos();
}

Gives the user no hints where to search for the sin_cos impl.

If the compiler knows which is the private trait that is missing, an useful message could show that name as @steveklabnik suggested. Even better with the suggested extern crate.

Update:
I see from the master that the first half of the request has been already implemented, but without the extern crate suggestion.

Even better, on the latest nightly there is no need to include the num crate at all.

This issue is perhaps obsolete and can be closed.

@petrochenkov
Copy link
Contributor

petrochenkov commented May 1, 2016

This is fixed in #32073 & #31920

@alexcrichton alexcrichton added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label May 1, 2016
@jseyfried
Copy link
Contributor

This is a duplicate of #21670 -- the example here is covered by the test from #32073.

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 E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests