Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compiler doesn't have found my variable identifier ? #36532

Closed
Songbird0 opened this issue Sep 16, 2016 · 3 comments
Closed

Compiler doesn't have found my variable identifier ? #36532

Songbird0 opened this issue Sep 16, 2016 · 3 comments

Comments

@Songbird0
Copy link
Contributor

Songbird0 commented Sep 16, 2016

Hi,

When I run my cargo test command, the compiler doesn't have found my variable - she's initialized in a macro.

I tried this code:

No worries for #[macro_export] attribute, the macro is in a module.

//We're in lib.rs file
#[macro_export]
macro_rules! log {
        ($log_type:expr, $message_content:expr) => {
            let mut content : String = String::new();
            content.push_str($log_type);
            content.push_str($message_content);
            println!("{}", &content);
    };

    ($log_type:expr, $message_content:expr, $variable:expr) => {
            let mut content : String = String::new();
            content.push_str($log_type);
            content.push_str($message_content);
            content.push_str($variable);
            println!("{}", &content);
    };
}

// This test is in the same file which the macro's declaration..
#[test]
fn pack_logger_proce_custom()
{
    log!("[CUSTOM]:", "CUSTOM log is OK. (run with two args)"); //Will fail... Why ?
}

I expected to see this happen:

[CUSTOM]: CUSTOM log is OK. (run with two args)

Instead, this happened: Nothing, an error.

Meta

rustc --version --verbose:

rustc 1.9.0 (e4e8b6668 2016-05-18)
binary: rustc
commit-hash: e4e8b666850a763fdf1c3c2c142856ab51e32779
commit-date: 2016-05-18
host: x86_64-unknown-linux-gnu
release: 1.9.0

Backtrace (in verbose mode):

   Compiling verbose_bird v0.0.1 (file:///home/songbird/Documents/RustDev/Verbose_Bird)
     Running `rustc src/lib.rs --crate-name verbose_bird --crate-type lib -g --test -C metadata=08d4ada3bc6a277f -C extra-filename=-08d4ada3bc6a277f --out-dir /home/songbird/Documents/RustDev/Verbose_Bird/target/debug --emit=dep-info,link -L dependency=/home/songbird/Documents/RustDev/Verbose_Bird/target/debug -L dependency=/home/songbird/Documents/RustDev/Verbose_Bird/target/debug/deps`
src/lib.rs:21:5: 21:12 error: unresolved name `content` [E0425]
src/lib.rs:21               content.push_str($log_type);
                            ^~~~~~~
src/lib.rs:70:2: 70:61 note: in this expansion of log! (defined in src/lib.rs)
src/lib.rs:21:5: 21:12 help: run `rustc --explain E0425` to see a detailed explanation
src/lib.rs:22:5: 22:12 error: unresolved name `content` [E0425]
src/lib.rs:22               content.push_str($message_content);
                            ^~~~~~~
src/lib.rs:70:2: 70:61 note: in this expansion of log! (defined in src/lib.rs)
src/lib.rs:22:5: 22:12 help: run `rustc --explain E0425` to see a detailed explanation
src/lib.rs:23:21: 23:28 error: unresolved name `content` [E0425]
src/lib.rs:23               println!("{}", &content);
                                            ^~~~~~~
<std macros>:2:25: 2:56 note: in this expansion of format_args!
<std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
src/lib.rs:23:5: 23:29 note: in this expansion of println! (defined in <std macros>)
src/lib.rs:70:2: 70:61 note: in this expansion of log! (defined in src/lib.rs)
src/lib.rs:23:21: 23:28 help: run `rustc --explain E0425` to see a detailed explanation
error: aborting due to 3 previous errors
error: Could not compile `verbose_bird`.

Caused by:
  Process didn't exit successfully: `rustc src/lib.rs --crate-name verbose_bird --crate-type lib -g --test -C metadata=08d4ada3bc6a277f -C extra-filename=-08d4ada3bc6a277f --out-dir /home/songbird/Documents/RustDev/Verbose_Bird/target/debug --emit=dep-info,link -L dependency=/home/songbird/Documents/RustDev/Verbose_Bird/target/debug -L dependency=/home/songbird/Documents/RustDev/Verbose_Bird/target/debug/deps` (exit code: 101)

I hope this can help you, thanks for your help.

@Songbird0
Copy link
Contributor Author

Ow, my bad...

I see, I haven't use #[cfg(test)] attribute on a module, sorry. (I'm a newbie Rust developer)

So, it's not possible to declare a test function like this:

#[test] //without module
fn foo(){}

?

Thanks.

@TimNN
Copy link
Contributor

TimNN commented Sep 16, 2016

Hi @Songbird0,

as far as I can tell this was a problem in previous versions of rust: Starting with rust 1.10 your code works as expected. (Looking at the release notes, the fix was probably #32923).

Also,

#[test] //without module
fn foo(){}

should work without any problems.

@Songbird0
Copy link
Contributor Author

Hello @TimNN

Oh, ok, I'll upgrade my Rust SDK so.

Thanks for your help !

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants