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

array_into_iter does not fire for lazy_static arrays #88099

Closed
ehuss opened this issue Aug 17, 2021 · 1 comment · Fixed by #88512
Closed

array_into_iter does not fire for lazy_static arrays #88099

ehuss opened this issue Aug 17, 2021 · 1 comment · Fixed by #88512
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 17, 2021

I tried this code:

struct Bar {}

lazy_static::lazy_static! {
    static ref FOO: [Bar; 1] = [
        Bar{}
    ];
}

fn main() {
    let _ = FOO.into_iter();
}

I expected to see this happen: array_into_iter should fire for this. Or, maybe this shouldn't fail in 2021?

Instead, this happened: Lint does not fire. When migrating to 2021, this fails to compile:

error[E0508]: cannot move out of type `[Bar; 1]`, a non-copy array
  --> src/main.rs:10:13
   |
10 |     let _ = FOO.into_iter();
   |             ^^^
   |             |
   |             cannot move out of here
   |             move occurs because value has type `[Bar; 1]`, which does not implement the `Copy` trait

lazy_static uses a Deref to access the underlying value. The expanded code looks like this:

struct FOO {
    __private_field: (),
}
#[doc(hidden)]
static FOO: FOO = FOO {
    __private_field: (),
};
impl ::lazy_static::__Deref for FOO {
    type Target = [Bar; 1];
    fn deref(&self) -> &[Bar; 1] {
        #[inline(always)]
        fn __static_ref_initialize() -> [Bar; 1] {
            [Bar {}]
        }
        #[inline(always)]
        fn __stability() -> &'static [Bar; 1] {
            static LAZY: ::lazy_static::lazy::Lazy<[Bar; 1]> = ::lazy_static::lazy::Lazy::INIT;
            LAZY.get(__static_ref_initialize)
        }
        __stability()
    }
}
impl ::lazy_static::LazyStatic for FOO {
    fn initialize(lazy: &Self) {
        let _ = &**lazy;
    }
}

I don't know if it is feasible to have a fix suggestion for this, or why exactly the existing lint doesn't fire.

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (8007b506a 2021-08-14)
binary: rustc
commit-hash: 8007b506ac5da629f223b755f5a5391edd5f6d01
commit-date: 2021-08-14
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 12.0.1
@ehuss ehuss added C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-edition-2021 Area: The 2021 edition labels Aug 17, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Aug 17, 2021

I'm guessing this is the same issue as mentioned here: #84147 (comment)

@m-ou-se m-ou-se added this to Migration crater run blockers in 2021 Edition Blockers Aug 17, 2021
@m-ou-se m-ou-se moved this from Migration crater run blockers to Not-really blockers (?) in 2021 Edition Blockers Aug 17, 2021
@bors bors closed this as completed in ea82d06 Sep 2, 2021
2021 Edition Blockers automation moved this from Not-really blockers (?) to Completed items Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug.
Projects
2021 Edition Blockers
  
Completed items
Development

Successfully merging a pull request may close this issue.

2 participants