rust-lang-nursery / lazy-static.rs

A small macro for defining lazy evaluated static variables in Rust.
Apache License 2.0
1.91k stars 111 forks source link

Does not support `crate` visibility restriction #87

Closed fuine closed 6 years ago

fuine commented 6 years ago

Consider this example:

#[macro_use]
extern crate lazy_static;

mod a {
    pub mod b {
        use std::collections::HashMap;
        lazy_static! {
            pub(in a) static ref HASHMAP: HashMap<u32, &'static str> = {
                let mut m = HashMap::new();
                m.insert(0, "baz");
                m
            };
        }
    }

    use self::b::HASHMAP;

    pub fn foo() {
        println!("{}", HASHMAP[&0]);
    }
}

fn main() {
    a::foo();
}

It works as intended, but if we switch pub(in a) to pub(crate) (both of which are valid visibility restrictions as of Rust v.1.18 and higher) we get an error:

error: no rules expected the token `crate`
 --> src/main.rs:8:17
  |
8 |             pub(crate) static ref HASHMAP: HashMap<u32, &'static str> = {
  |                 ^^^^^

I would submit a patch, but unfortunately my macro-foo is not high enough to take on this issue alone (although if someone is willing to mentor I can try to fix this).

KodrAus commented 6 years ago

Yeh it looks like we're hard-coding the in $path part here... My macro-foo is also weak, but @dtolnay posted a snippet in another crate that supports pub(crate) and pub(in path). Maybe we should have a look at that?