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

unresolved name `rand::thread_rng` #11

Closed
andschwa opened this Issue May 22, 2015 · 17 comments

Comments

Projects
None yet
5 participants
@andschwa

andschwa commented May 22, 2015

Hello,

I have the following code which compiles fine via cargo run:

src/main.rs

extern crate rand;

use rand::distributions::{IndependentSample, Range};

fn main() {
    let mut rng = rand::thread_rng(); // Flycheck error is here
    let range = Range::new(-512.03, 511.97);
    println!("A random number is {}", range.ind_sample(&mut rng));
}

Cargo.toml

[package]
name = "rando"
version = "0.1.0"
authors = ["Andrew Schwartzmeyer <andrew@schwartzmeyer.com>"]

[dependencies]
rand = "0.3"

However, Flycheck with flycheck-rust gives the warning "unresolved name rand::thread_rng" on line 6. It does not appear to be recognize that the function was imported from a crate.

The Flycheck project may be a better place to post this issue, but I was hoping you could help me out!

rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
cargo 0.2.0-nightly (efb482d 2015-04-30) (built 2015-04-29)
OS X 10.10.3
GNU Emacs 24.5.50.1 (x86_64-apple-darwin14.3.0, NS apple-appkit-1347.57) of 2015-04-13
Flycheck version: 0.24snapshot (package: 20150514.528)
flycheck-rust package 20150417.1048

I am using the rand crate.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

#6 is absolutely related but seems already resolved.

andschwa commented May 22, 2015

#6 is absolutely related but seems already resolved.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

I am not sure if #8 would resolve this, since right now flycheck-rust is setting flycheck-rust-library-path and flycheck-rust-crate-type correctly (once I fixed my loading of the package itself).

andschwa commented May 22, 2015

I am not sure if #8 would resolve this, since right now flycheck-rust is setting flycheck-rust-library-path and flycheck-rust-crate-type correctly (once I fixed my loading of the package itself).

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

Bah!

$ rustc main.rs
main.rs:6:19: 6:35 error: unresolved name `rand::thread_rng`
main.rs:6     let mut rng = rand::thread_rng();
                            ^~~~~~~~~~~~~~~~
error: aborting due to previous error

So how does one make rustc aware of library? I'm struggling with -L and -l so far.

andschwa commented May 22, 2015

Bah!

$ rustc main.rs
main.rs:6:19: 6:35 error: unresolved name `rand::thread_rng`
main.rs:6     let mut rng = rand::thread_rng();
                            ^~~~~~~~~~~~~~~~
error: aborting due to previous error

So how does one make rustc aware of library? I'm struggling with -L and -l so far.

@lunaryorn

This comment has been minimized.

Show comment
Hide comment
@lunaryorn

lunaryorn May 22, 2015

Contributor

@andschwa Is the crate actually installed? IOW, did you run cargo build?

Contributor

lunaryorn commented May 22, 2015

@andschwa Is the crate actually installed? IOW, did you run cargo build?

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

@lunaryorn Yes (and I'm glad you're here, you can fix everything AFAIK 😄 ):

$ cargo build -v
       Fresh libc v0.1.8
       Fresh rand v0.3.8
       Fresh rando v0.1.0 (file:///Users/andrew/src/rando)
$ cargo run -v
       Fresh libc v0.1.8
       Fresh rand v0.3.8
       Fresh rando v0.1.0 (file:///Users/andrew/src/rando)
     Running `/Users/andrew/src/rando/target/debug/rando`
A random number is 153.906207

andschwa commented May 22, 2015

@lunaryorn Yes (and I'm glad you're here, you can fix everything AFAIK 😄 ):

$ cargo build -v
       Fresh libc v0.1.8
       Fresh rand v0.3.8
       Fresh rando v0.1.0 (file:///Users/andrew/src/rando)
$ cargo run -v
       Fresh libc v0.1.8
       Fresh rand v0.3.8
       Fresh rando v0.1.0 (file:///Users/andrew/src/rando)
     Running `/Users/andrew/src/rando/target/debug/rando`
A random number is 153.906207
@lunaryorn

This comment has been minimized.

Show comment
Hide comment
@lunaryorn

lunaryorn May 22, 2015

Contributor

@andschwa Then what does C-c ! C-c rustc show you?

Contributor

lunaryorn commented May 22, 2015

@andschwa Then what does C-c ! C-c rustc show you?

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

read-flycheck-checker: rustc is no Flycheck syntax checker

But C-c ! C-c rust (thank you! Now I have something verbose to help debug) gives:

-*- mode: compilation; default-directory: "~/src/rando/src/" -*-
Compilation started at Fri May 22 10:48:49

rustc -Z no-trans --crate-type bin -L/Users/andrew/src/rando/target/debug -L/Users/andrew/src/rando/target/debug/deps /Users/andrew/src/rando/src/flycheck_main.rs
/Users/andrew/src/rando/src/flycheck_main.rs:6:19: 6:35 error: unresolved name `rand::thread_rng`
/Users/andrew/src/rando/src/flycheck_main.rs:6     let mut rng = rand::thread_rng();
                                                                 ^~~~~~~~~~~~~~~~
error: aborting due to previous error

Compilation exited abnormally with code 101 at Fri May 22 10:48:49

andschwa commented May 22, 2015

read-flycheck-checker: rustc is no Flycheck syntax checker

But C-c ! C-c rust (thank you! Now I have something verbose to help debug) gives:

-*- mode: compilation; default-directory: "~/src/rando/src/" -*-
Compilation started at Fri May 22 10:48:49

rustc -Z no-trans --crate-type bin -L/Users/andrew/src/rando/target/debug -L/Users/andrew/src/rando/target/debug/deps /Users/andrew/src/rando/src/flycheck_main.rs
/Users/andrew/src/rando/src/flycheck_main.rs:6:19: 6:35 error: unresolved name `rand::thread_rng`
/Users/andrew/src/rando/src/flycheck_main.rs:6     let mut rng = rand::thread_rng();
                                                                 ^~~~~~~~~~~~~~~~
error: aborting due to previous error

Compilation exited abnormally with code 101 at Fri May 22 10:48:49
@lunaryorn

This comment has been minimized.

Show comment
Hide comment
@lunaryorn

lunaryorn May 22, 2015

Contributor

@andschwa I can reproduce the issue, but I'm not sure yet what causes it. I think there are two different rand crates available (may there's one in the Rust compiler or so). If you take a look at cargo build -v you'll see that it explicitly tells rustc which library to use for the rand crate.

Flycheck would need to do the same to get exactly the same results as Cargo, but unfortunately there's no easy way to do that yet, since Cargo doesn't export it's internal information in a structured format (see #8), and parsing toml files isn't that easy either.

Contributor

lunaryorn commented May 22, 2015

@andschwa I can reproduce the issue, but I'm not sure yet what causes it. I think there are two different rand crates available (may there's one in the Rust compiler or so). If you take a look at cargo build -v you'll see that it explicitly tells rustc which library to use for the rand crate.

Flycheck would need to do the same to get exactly the same results as Cargo, but unfortunately there's no easy way to do that yet, since Cargo doesn't export it's internal information in a structured format (see #8), and parsing toml files isn't that easy either.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

@lunaryorn Ah! So I'm not all that dumb, which is a relief. I think you're right about the cause, since why else would it point specifically to the crate file. We're just waiting on that cargo metadata then.

Cargo's use of TOML totally surprised me, it seems pretty young.

Thanks for your help! If I'm ever in your neck of the woods, I owe you a beer or ten.

andschwa commented May 22, 2015

@lunaryorn Ah! So I'm not all that dumb, which is a relief. I think you're right about the cause, since why else would it point specifically to the crate file. We're just waiting on that cargo metadata then.

Cargo's use of TOML totally surprised me, it seems pretty young.

Thanks for your help! If I'm ever in your neck of the woods, I owe you a beer or ten.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 22, 2015

@lunaryorn Is there a way I can append a flag locally to the syntax checker? Specifically --extern rand=/Users/andrew/src/rando/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib.

andschwa commented May 22, 2015

@lunaryorn Is there a way I can append a flag locally to the syntax checker? Specifically --extern rand=/Users/andrew/src/rando/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 23, 2015

Given the support added in my PR, and the following .dir-locals.el:

((rust-mode
  (flycheck-rust-extern "rand=/Users/andrew/src/rando/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib")))

The syntax checking works just fine!

andschwa commented May 23, 2015

Given the support added in my PR, and the following .dir-locals.el:

((rust-mode
  (flycheck-rust-extern "rand=/Users/andrew/src/rando/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib")))

The syntax checking works just fine!

@tomjakubowski

This comment has been minimized.

Show comment
Hide comment
@tomjakubowski

tomjakubowski May 24, 2015

thanks for the tip @andschwa

tomjakubowski commented May 24, 2015

thanks for the tip @andschwa

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 24, 2015

No problemo @tomjakubowski! Glad it helped more than just me.

andschwa commented May 24, 2015

No problemo @tomjakubowski! Glad it helped more than just me.

@andschwa

This comment has been minimized.

Show comment
Hide comment
@andschwa

andschwa May 24, 2015

With the latest updates to flycheck adding flycheck-rust-args, just use this:

((rust-mode
  (flycheck-rust-args . ("--extern" "rand=/Users/andrew/src/ga-rust/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib"))))

andschwa commented May 24, 2015

With the latest updates to flycheck adding flycheck-rust-args, just use this:

((rust-mode
  (flycheck-rust-args . ("--extern" "rand=/Users/andrew/src/ga-rust/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib"))))

@andschwa andschwa closed this May 24, 2015

@lunaryorn

This comment has been minimized.

Show comment
Hide comment
@lunaryorn

lunaryorn May 24, 2015

Contributor

@andschwa 👍

Contributor

lunaryorn commented May 24, 2015

@andschwa 👍

@jeremyjh

This comment has been minimized.

Show comment
Hide comment
@jeremyjh

jeremyjh May 24, 2015

@andschwa great timing for me, thanks!

jeremyjh commented May 24, 2015

@andschwa great timing for me, thanks!

@thatdoorguy

This comment has been minimized.

Show comment
Hide comment
@thatdoorguy

thatdoorguy Sep 6, 2015

Is there a way that i could set this relative to the current file I am editing?
for instance is there some syntax for the project root directory, that way I could just point it at
/< current project dir where the toml file is >/target/debug/deps

that way no matter what file I open it will try to find the crate i am actually using?

thatdoorguy commented Sep 6, 2015

Is there a way that i could set this relative to the current file I am editing?
for instance is there some syntax for the project root directory, that way I could just point it at
/< current project dir where the toml file is >/target/debug/deps

that way no matter what file I open it will try to find the crate i am actually using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment