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

'static lifetime not inferred in const context #114706

Open
grantslatton opened this issue Aug 10, 2023 · 4 comments
Open

'static lifetime not inferred in const context #114706

grantslatton opened this issue Aug 10, 2023 · 4 comments
Assignees
Labels
A-lifetimes Area: lifetime related C-bug Category: This is a bug.

Comments

@grantslatton
Copy link

I tried this code:

struct Foo;

impl Foo {
    const ALL_METHODS: &[(&str, fn(&Self) -> usize)] = &[("foo", Self::foo)];
    
    fn foo(&self) -> usize {
        123
    }
}

I expected to see this happen: successful compile

Instead, this happened:

error[[E0491]](https://doc.rust-lang.org/stable/error_codes/E0491.html): in type `&[(&str, for<'a> fn(&'a Foo) -> usize)]`, reference has a longer lifetime than the data it references
 --> src/lib.rs:5:5
  |
5 |     const ALL_METHODS: &[(&str, fn(&Self) -> usize)] = &[("foo", Self::foo)];
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the pointer is valid for the anonymous lifetime as defined here
 --> src/lib.rs:5:24
  |
5 |     const ALL_METHODS: &[(&str, fn(&Self) -> usize)] = &[("foo", Self::foo)];
  |                        ^
note: but the referenced data is only valid for the anonymous lifetime as defined here
 --> src/lib.rs:5:24
  |
5 |     const ALL_METHODS: &[(&str, fn(&Self) -> usize)] = &[("foo", Self::foo)];
  |

Adding some 'statics fixes it, but I think these are supposed to be unnecessary now:

const ALL_METHODS: &'static [(&'static str, fn(&Self) -> usize)] = &[("foo", Self::foo)]
@grantslatton grantslatton added the C-bug Category: This is a bug. label Aug 10, 2023
@grantslatton grantslatton changed the title &'static lifetime not inferred in const context 'static lifetime not inferred in const context Aug 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 10, 2023
@compiler-errors
Copy link
Member

Minimized:

struct Foo;

impl Foo {
    const CONST: &[&str] = &["foo"];
}

fn main() {}

@compiler-errors compiler-errors self-assigned this Aug 10, 2023
@compiler-errors compiler-errors added A-lifetimes Area: lifetime related and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 10, 2023
@fmease
Copy link
Member

fmease commented Aug 10, 2023

Note that this works as intended. A PR for fail→pass would need to be t-lang fcp'ed.

@compiler-errors
Copy link
Member

This is inadvertently introducing early-bound lifetimes for elided lifetimes in associated const params -- that is:

impl Foo {
  const CONST: &str = "";
}

Is desugaring to:

impl<'a> Foo<'a> {
  const CONST: &'a str = "";
}

I don't consider this correct behavior.

@compiler-errors
Copy link
Member

Though I do agree with your assessment @fmease, a PR to make this code pass would require at least a T-lang FCP. I will probably not do that given the opinion stated in the comment you linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants