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

lazy_static! fails to compile when a static ref is cfg'd out #165

Open
LunarLambda opened this issue Jan 1, 2020 · 7 comments
Open

lazy_static! fails to compile when a static ref is cfg'd out #165

LunarLambda opened this issue Jan 1, 2020 · 7 comments

Comments

@LunarLambda
Copy link

LunarLambda commented Jan 1, 2020

rustc: 1.40.0
lazy_static: 1.4.0

I am calling a thread-unsafe unix libc function. So, I have a Mutex in my code to mitigate this issue. Linux provides a thread-safe version, so it's not needed on there.

So, I have the following code:

lazy_static! {
    #[cfg(all(unix, not(target_os = "linux")))]
    static ref MY_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
}

This configuration predicate works fine on functions, types, and even normal statics, but when used inside lazy_static!, I instead get a rather unhelpful "expected type, found static MY_MUTEX" error.

@sfackler
Copy link
Member

sfackler commented Jan 2, 2020

That error does not have anything to do with the attribute. The type of MY_MUTEX is not Mutex<()>, but rather a new type (named MY_MUTEX) that derefs to Mutex<()>. You may need to do e.g. &*MY_MUTEX to get a &Mutex<()>.

@LunarLambda
Copy link
Author

LunarLambda commented Jan 2, 2020 via email

@sfackler
Copy link
Member

sfackler commented Jan 2, 2020

What code specifically compiles or does not compile?

@LunarLambda
Copy link
Author

LunarLambda commented Jan 2, 2020 via email

@sfackler
Copy link
Member

sfackler commented Jan 2, 2020

Oh, sorry, I thought that error was coming from the usage site.

It doesn't appear to be due to the complexity of the cfg, just if the cfg evaluates to true or false. You can move the cfg to the macro call itself as a workaround:

#[cfg(all(unix, not(target_os = "linux")))]
lazy_static! {
    static ref MY_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
}

@LunarLambda
Copy link
Author

LunarLambda commented Jan 2, 2020 via email

@sfackler sfackler changed the title lazy_static! chokes on #[cfg(all(..., not(...)))] attribute lazy_static! fails to compile when a static ref is cfg'd out Jan 2, 2020
@KodrAus
Copy link
Contributor

KodrAus commented Jan 13, 2020

Since lazy_static evaluates to multiple items, and not all of those items accept all attributes, we'd potentially break callers if we fixed this by duplicating the attributes on the impl blocks. If we could replace the macro internals with something like rust-lang/rfcs#2788 so that the macro expanded to a single item then it might be possible, but that would probably also break uses.

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