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

Exported rstest_reuse template cannot be defined at the top level #175

Closed
mauricelam opened this issue Jan 13, 2023 · 5 comments
Closed

Comments

@mauricelam
Copy link

use rstest::rstest;
use rstest_reuse::template;

#[template]
#[export]
#[rstest]
fn top_level() {
}
// Fails with: the name `top_level` is defined multiple times

fn main() {
    println!("");
}

The above fails to compile because it says top_level is defined multiple times. I think one is the function that I wrote, and the other is the macro defined by rstest_reuse. It's not a big problem since it's easy to move the definition inside a module.

https://www.rustexplorer.com/b#%2F*%0A%5Bdependencies%5D%0Arstest%20%3D%20%22*%22%0Arstest_reuse%20%3D%20%22*%22%0A*%2F%0A%0Ause%20rstest%3A%3Arstest%3B%0Ause%20rstest_reuse%3A%3Atemplate%3B%0A%0A%23%5Btemplate%5D%0A%23%5Bexport%5D%0A%23%5Brstest%5D%0Afn%20top_level()%20%7B%0A%7D%0A%2F%2F%20Fails%20with%3A%20the%20name%20%60top_level%60%20is%20defined%20multiple%20times%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22%22)%3B%0A%7D

@mauricelam mauricelam changed the title rstest_reuse template cannot be defined at the top level Exported rstest_reuse template cannot be defined at the top level Jan 13, 2023
@la10736
Copy link
Owner

la10736 commented Jan 13, 2023

Playing with macros is tricky and I cannot recognize that the template is in a module or not in the procedural macro so I cannot change the behavior of #[export] annotation to fit this possibility. The only way to implement this behavior that I can see is to introduce a #[no_local] annotation that can be used to remove pub(crate) use #macro_name as #macro_name; trick used to have locally templates.

@mauricelam
Copy link
Author

I see, that's a tricky situation. There are workarounds floating around like macro-vis and macro-pub which are based on name mangling. There are some caveats listed but overall looks like an improvement (both to avoid this particular issue and to allow the generated test case to be exported from the module namespace)

@la10736
Copy link
Owner

la10736 commented Jan 22, 2023

Ok, I decided for a breaking change and stop to support the #[macro_use]. Now I used the same approach used by macro-vis and hide the original macro by a random name.

Export at the root now works and I avoided to introduce a new wired syntax like #[no_local]. You can also export a template from a sub module but you should point it with the full path or reexport it at the root.

@la10736
Copy link
Owner

la10736 commented Jan 23, 2023

I hope to publish the new version for the end of the week....

@la10736
Copy link
Owner

la10736 commented Jan 29, 2023

Done 🎆

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

2 participants