Skip to content

&[1, 2, 3] should always be &'static [<generic integer>] #12853

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
thestinger opened this issue Mar 12, 2014 · 5 comments
Closed

&[1, 2, 3] should always be &'static [<generic integer>] #12853

thestinger opened this issue Mar 12, 2014 · 5 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@thestinger
Copy link
Contributor

The following works:

fn foo<'a>(xs: &'a [int]) -> &'a [int] {
    if xs.len() & 1 == 0 {
        static ys: &'static [int] = &[1, 2, 3];
        ys
    } else {
        xs
    }
}

This should also work, but does not:

fn foo<'a>(xs: &'a [int]) -> &'a [int] {
    if xs.len() & 1 == 0 {
        &[1, 2, 3]
    } else {
        xs
    }
}
@nikomatsakis
Copy link
Contributor

cc me

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#827

@thestinger
Copy link
Contributor Author

This isn't a proposal for a language change, it's a bug report.

@steveklabnik
Copy link
Member

We had an IRC chat about it, and it seems to me like an enhancement. Changing &[1, 2, 3] to go from "a reference to a stack allocated array" to "a statically allocated array" seems like a semantic change, no?

@thestinger
Copy link
Contributor Author

The fact that the compiler is treating immutable constant expressions as stack allocations is a bug. There is no language change to discuss here, only an implementation improvement.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 13, 2024
fix [`redundant_closure`] suggesting incorrect code with `F: Fn()`

fixes: rust-lang#12853

---

changelog: fix [`redundant_closure`] suggesting incorrect code with `F: Fn()`
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

3 participants