Open
Description
This function compiles due to rvalue static promotion:
fn bar() -> &'static i32 {
&1
}
This, however, does not:
struct Foo(i32);
impl Foo {
const fn new(n: i32) -> Foo {
Foo(n)
}
}
fn foo() -> &'static Foo {
&Foo::new(1)
}
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:10:5
|
10 | &Foo::new(1)
| ^-----------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
error: aborting due to previous error
Explicitly creating a static does work, however:
struct Foo(i32);
impl Foo {
const fn new(n: i32) -> Foo {
Foo(n)
}
}
fn foo() -> &'static Foo {
static FOO: Foo = Foo::new(1);
&FOO
}