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

Bug in import #16217

Closed
GuillaumeGomez opened this issue Aug 3, 2014 · 11 comments
Closed

Bug in import #16217

GuillaumeGomez opened this issue Aug 3, 2014 · 11 comments

Comments

@GuillaumeGomez
Copy link
Member

Hey, here's the problem :

pub mod Gsl {
    use ffi;

    pub struct VectorFloat {
        vec: *mut ffi::gsl_vector_float
    }

    impl VectorFloat {
      ...
    }
}

When I compile it, the compiler says :

> make
src/vector.rs:5:6: 5:9 warning: unused import, #[warn(unused_imports)] on by default
src/vector.rs:5  use ffi;

So I remove the import and then I get :

mkdir -p lib
rustc --out-dir=lib src/rgsl.rs
src/vector.rs:9:19: 9:40 error: failed to resolve. Use of undeclared module `ffi`
src/vector.rs:9         vec: *mut ffi::gsl_vector_float
                                  ^~~~~~~~~~~~~~~~~~~~~
src/vector.rs:9:19: 9:40 error: use of undeclared type name `ffi::gsl_vector_float`
src/vector.rs:9         vec: *mut ffi::gsl_vector_float
...

Is that normal ? Since the ffi import is used, I'm not supposed to have a unused warning, am I ?

@alexcrichton
Copy link
Member

Do you have a standalone example of your code that could be tested?

@GuillaumeGomez
Copy link
Member Author

On my repo if you want : https://github.com/GuillaumeGomez/rust-GSL . The file is vector.rs (like said in the errors).

@GuillaumeGomez
Copy link
Member Author

I actually think that the problem is the following : inside a module, this is a different scope (right ?) so what you import inside is only for inside functions, not inside struct and their methods. That's my opinion. However, I found out that it's possible to not import ffi and use it like this :

pub mod Gsl {
    pub struct SomeStruct {
        ...
    }

    impl SomeStruct {
        pub fn however() {
            ::ffi::some_function();
        }
    }
}

But once again, is that normal ?

PS: my compiler version is the following :

> rustc --version
rustc 0.12.0-pre-nightly (4d4eb1023 2014-08-02 23:36:09 +0000)

@alexcrichton
Copy link
Member

I tried checking our your repo, but I see no vector.rs and when I build it I get no warnings. What revision of your repo produces the warnings?

@GuillaumeGomez
Copy link
Member Author

Yes, sorry, I updated and I forgot to create a branch to keep this bug in sight. The "good" version is the following one :

> git checkout 0166fde6730730145b226305750566106b536781

@alexcrichton
Copy link
Member

At that commit there are two imports of use ffi;, one outside and one inside the Gsl module. The one on the outside is being warned about, not the one on the inside. Are you sure that's the right commit?

@GuillaumeGomez
Copy link
Member Author

I can't find the good commit... I think it's an error of mine... Sorry for making you losing your time. I updated the rust compiler yesterday and today so maybe it's why we can't see the bug but I think I just didn't see the bad import. It seems kinda strange but it doesn't matter, even if it was a bug, now it doesn't appear anymore.

@alexcrichton
Copy link
Member

No problem! I'm going to close for now and feel free to reopen if you run across it in the future!

@ajprax
Copy link

ajprax commented Jan 12, 2016

I've just encountered this same bug. Reproduced here: https://github.com/ajprax/rust_imports_bug (run cargo test on either commit)/
The first commit gives a warning for unused import and the second an error for unable to resolve. I encountered this while (mostly) following https://doc.rust-lang.org/stable/book/concurrency.html

@ranma42
Copy link
Contributor

ranma42 commented Jan 13, 2016

@ajprax what you see is related to the fact that use std::thread is only needed when compiling for testing in your repo. In particular, the latest commit (the one which errors out if you cargo test it), builds just fine with cargo build. The examples in the concurrency book do not have the same problem because they unconditionally use thread (as it is used in the main function rather than in tests).

I think the normal approach here is to move the use:

#[test]
fn it_works() {
    use std::thread;
    thread::spawn(|| {
        println!("Hello from a thread!");
    });
}

or to make it dependent on #[cfg(test)]:

#[cfg(test)]
use std::thread;

#[test]
fn it_works() {
    thread::spawn(|| {
        println!("Hello from a thread!");
    });
}

@ajprax
Copy link

ajprax commented Jan 13, 2016

Presumably this is the result of cargo test running compilation without the test config first so that the secondary test compilation can link against the main library?

A suggestion from the book, which seems appropriate here is to encapsulate the tests in a separate module:

#[cfg(tests)]
pub mod tests {
    use std::thread;

    #[test]
    fn it_works() {
        thread::spawn(|| {
            println!("Hello from a thread!");
        });
    }
}

lnicola pushed a commit to lnicola/rust that referenced this issue Jan 3, 2024
…icola

internal: Simplify implementation of apply_document_changes

While reading through the code base, I stumbled across a piece of code that I found hard to read despite its simple purpose. This is my attempt at making the code easier to understand for future readers.

I won't be offended if this is too minor and not worth your time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants