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

ambiguous template name that isnt visible after import even if it was imported #155

Closed
alexzanderr opened this issue Jun 28, 2022 · 4 comments

Comments

@alexzanderr
Copy link

this is a test file in my project

use pretty_assertions::assert_eq;
// rstest
use rstest::rstest;
use rstest::fixture;
use rstest_reuse::template;
use rstest_reuse::apply;
use rstest_reuse::{self,};

#[cfg(test)]
mod mod_ansi {
    use super::rstest;
    use super::fixture;
    use super::assert_eq;
    use super::template;
    use super::apply;
    use super::rstest_reuse;

    /// this is what should look like when you convert a string into ansi codes with
    /// red, green, blue ..., functions
    pub fn generate_ansi(content: &str) -> String {
        format!("\u{1b}[31m{}\u{1b}[0m", content)
    }


    #[template]
    #[rstest]
    #[case("hello")]
    #[case("there")]
    #[case("rust")]
    #[case("is")]
    #[case("the")]
    #[case("best")]
    fn words(#[case] input_content: &str) {
    }

    #[cfg(test)]
    mod red_functions {
        use super::assert_eq;
        use super::rstest;
        use super::template;
        use super::apply;
        use super::fixture;
        use super::generate_ansi;
        use super::rstest_reuse;
        use super::words;
        use core_dev::aesthetics::ansi::red;

        #[apply(words)]
        fn test_red(#[case] input_content: &str) {
            // this is 100% correct
            let preprocessed_expected_result =
                generate_ansi(input_content);

            let result = red(input_content);
            // println!("{:?}", result);
            assert_eq!(result, preprocessed_expected_result);
        }
    }

    #[cfg(test)]
    mod impl_fixed_color_function {
        use super::rstest;
        use rstest::*;
        use super::apply;
        use super::template;
        use super::fixture;
        use super::assert_eq;
        use core_dev::impl_fixed_color_function;
        use super::generate_ansi;
        use super::rstest_reuse;
        use super::words;

        #[apply(words)]
        fn against_123(#[case] input_content: &str) {
            // color_function! { 123 };
            // let result = fixed_color_123("hello");
            // println!("{:?}", result)
        }
    }
}

when i run the test i do get this error

error[E0432]: unresolved import `super::words`
  --> tests/test_lib/aesthetics.rs:67:13
   |
67 |         use super::words;
   |             ^^^^^^^^^^^^ no `words` in `mod_ansi`

error[E0432]: unresolved import `super::words`
  --> tests/test_lib/aesthetics.rs:92:13
   |
92 |         use super::words;
   |             ^^^^^^^^^^^^ no `words` in `mod_ansi`

error[E0659]: `words` is ambiguous
  --> tests/test_lib/aesthetics.rs:69:17
   |
69 |         #[apply(words)]
   |                 ^^^^^ ambiguous name
   |
   = note: ambiguous because of a conflict between a `macro_rules` name and a non-`macro_rules` name from another module
note: `words` could refer to the macro defined here
  --> tests/test_lib/aesthetics.rs:47:5
   |
47 |     #[template]
   |     ^^^^^^^^^^^
note: `words` could also refer to the unresolved item imported here
  --> tests/test_lib/aesthetics.rs:67:13
   |
67 |         use super::words;
   |             ^^^^^^^^^^^^
   = help: use `self::words` to refer to this unresolved item unambiguously
   = note: this error originates in the attribute macro `template` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0659]: `words` is ambiguous
  --> tests/test_lib/aesthetics.rs:94:17
   |
94 |         #[apply(words)]
   |                 ^^^^^ ambiguous name
   |
   = note: ambiguous because of a conflict between a `macro_rules` name and a non-`macro_rules` name from another module
note: `words` could refer to the macro defined here
  --> tests/test_lib/aesthetics.rs:47:5
   |
47 |     #[template]
   |     ^^^^^^^^^^^
note: `words` could also refer to the unresolved item imported here
  --> tests/test_lib/aesthetics.rs:92:13
   |
92 |         use super::words;
   |             ^^^^^^^^^^^^
   = help: use `self::words` to refer to this unresolved item unambiguously
   = note: this error originates in the attribute macro `template` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0425, E0432, E0659.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `core-dev` due to 5 previous errors

the solution to this problem is to remove the modules and put everything in one module

like this

#[cfg(test)]
mod mod_ansi {
    use super::rstest;
    use super::fixture;
    use super::assert_eq;
    use super::template;
    use super::apply;
    use super::rstest_reuse;

    /// this is what should look like when you convert a string into ansi codes with
    /// red, green, blue ..., functions
    pub fn generate_ansi(content: &str) -> String {
        format!("\u{1b}[31m{}\u{1b}[0m", content)
    }

    use core_dev::aesthetics::ansi::red;

    #[template]
    #[rstest]
    #[case("hello")]
    #[case("there")]
    #[case("rust")]
    #[case("is")]
    #[case("the")]
    #[case("best")]
    fn words(#[case] input_content: &str) {
    }

    #[apply(words)]
    fn test_red(#[case] input_content: &str) {
        // this is 100% correct
        let preprocessed_expected_result =
            generate_ansi(input_content);

        let result = red(input_content);
        // println!("{:?}", result);
        assert_eq!(result, preprocessed_expected_result);
    }

    #[apply(words)]
    fn against_123(#[case] input_content: &str) {
        // color_function! { 123 };
        // let result = fixed_color_123("hello");
        // println!("{:?}", result)
    }
}

now everything is fine

successes:
    mod_ansi::against_123::case_1
    mod_ansi::against_123::case_2
    mod_ansi::against_123::case_3
    mod_ansi::against_123::case_4
    mod_ansi::against_123::case_5
    mod_ansi::against_123::case_6
    mod_ansi::test_red::case_1
    mod_ansi::test_red::case_2
    mod_ansi::test_red::case_3
    mod_ansi::test_red::case_4
    mod_ansi::test_red::case_5
    mod_ansi::test_red::case_6

test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

my problem is this error ambiguous because of a conflict between a macro_rules name and a non-macro_rulesname from another module note:words could refer to the macro defined here and the other errors related to the template and the ambiguous part

why is this happening ?
why cant i put the template in root module and then import the template words in the rest of the modules ?

what im trying to achieve is to make a usable template, words template, that i could import from inner modules along the program as test data for multiple tests.

any ideas? thanks in advance.

@alexzanderr alexzanderr changed the title ambiguous ambiguous template name that isnt visible after import even if it was imported Jun 28, 2022
@la10736
Copy link
Owner

la10736 commented Jun 28, 2022

Hi,
apologize me... I didn't read your question deeply and cannot try it now... but try to just remove use super::words from your example.

#[template] create a macro that should be already visible in following modules... I've tried to explain this kind of tricks in README .... but it's clear that I failed my job :)

@danielhenrymantilla
Copy link

@la10736 if you change

macro_rules! #macro_name {
( $test:item ) => {
$crate::rstest_reuse::merge_attrs! {
#template,
$test
}
}
}
to become:

macro_rules! some_unique_name { ... }
use some_unique_name as #macro_name;

then stuff will follow the usual scoping rules of other items, as explained here 🙂

@la10736
Copy link
Owner

la10736 commented Jun 29, 2022 via email

@la10736 la10736 closed this as completed in b888bb9 Jul 4, 2022
@alexzanderr
Copy link
Author

alexzanderr commented Jul 5, 2022

it works.

now i can create a template and import it inside submodules like this

// ...
    #[template]
    #[rstest]
    #[case("hello")]
    #[case("there")]
    #[case("rust")]
    #[case("is")]
    #[case("the")]
    #[case("best")]
    fn random_words_template(#[case] input_content: &str) {}

    mod fixed_color_fn {
        use super::random_words_template;
        //...
    }

// ...
}

thanks a lot man!

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

3 participants