Skip to content

'static lifetime not inferred in const context #114706

Open
@grantslatton

Description

@grantslatton

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)]

Metadata

Metadata

Labels

A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions