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

Unable to analyze lazy_static? #3712

Closed
NichtsHsu opened this issue Mar 25, 2020 · 10 comments
Closed

Unable to analyze lazy_static? #3712

NichtsHsu opened this issue Mar 25, 2020 · 10 comments
Labels
A-macro macro expansion

Comments

@NichtsHsu
Copy link

use lazy_static::lazy_static;
lazy_static! {
   static ref VEC: Vec<u8> = vec![0x18u8, 0x11u8];
}

fn main() {
   let b = VEC.last();
}

b will be analyzed as Option<{unknown}>.

Using the lastest version from compiling lastest master branch. Is RA unable to analyze lazy_static yet?

@edwin0cheng edwin0cheng added the A-macro macro expansion label Mar 25, 2020
@edwin0cheng
Copy link
Member

edwin0cheng commented Mar 25, 2020

I.e :

lazy_static! {
   static ref VEC: Vec<u8> = vec![0x18u8, 0x11u8];
}

The code above expanded to: (By command Expand macro recursively)

__lazy_static_internal!(()static ref VEC:Vec<u8>  = vec![0x18u8,0x11u8];
);

And __lazy_static_internal! macro is defined as #[macro_export(local_inner_macros)]. Seem like it is because we do not support #[macro_export(local_inner_macros)] yet.

@edwin0cheng
Copy link
Member

Fixed by #4305

OT: About 1 year ago, I started using rust-analyzer, but found that it cannot analyze lazy_static, but now it is finally done! 🎉

@teohhanhui
Copy link

teohhanhui commented May 18, 2020

If I understand correctly, this has been released in 2020-05-11, right?

Rust Analyzer: Show RA version:

rust-analyzer version: 2020-05-18 (31611da)

I'm still getting all the {unknown}s.

Code sample: https://github.com/teohhanhui/rust-exercises/blob/767414fe4034c67b8661bf0023c02c459d618c7a/temperature_converter/src/main.rs

@edwin0cheng edwin0cheng reopened this May 18, 2020
@teohhanhui
Copy link

Looks like I should use https://docs.rs/once_cell/1.4.0/once_cell/#lazy-initialized-global-data instead 😄

@matklad
Copy link
Member

matklad commented May 19, 2020

Lol yes, at least partially, the motivation for once_cell was that IDEs at that time had troubles with lazy_static! :D

@edwin0cheng
Copy link
Member

edwin0cheng commented May 19, 2020

Yeah, @matklad is the best one to answer this question. Lol

@matklad
Copy link
Member

matklad commented Jul 15, 2020

works on master

Uploading image.png…

@matklad matklad closed this as completed Jul 15, 2020
@bjorn3
Copy link
Member

bjorn3 commented Jul 15, 2020

Image is broken

@FallenWarrior2k
Copy link

Image is broken

Yes, the "Uploading image.png..." indicates the post was submitted before the image finished uploading.

In any case, lazy_static! in global scope seems to work, but when you have a lazy_static! block in a function, e.g. to keep implementation details inside, it still shows {unknown}.
I've tested this on nightly RA ("rust-analyzer.updates.channel": "nightly"), since the last comment was "works on master".

Compare:
image
image

Code for easy copy-pasting for those trying to repro:
Global:

use lazy_static::lazy_static;

lazy_static! {
    static ref VEC: Vec<u8> = vec![0u8, 1u8, 2u8];
}

fn main() {
    let x = VEC.last();
}

In function:

use lazy_static::lazy_static;

fn main() {
    lazy_static! {
        static ref VEC: Vec<u8> = vec![0u8, 1u8, 2u8];
    }

    let x = VEC.last();
}

@kiljacken
Copy link
Contributor

That problem is presumably #1559

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion
Projects
None yet
Development

No branches or pull requests

7 participants