Closed
Description
I tried this code (playground):
struct ZST;
impl ZST {
pub const fn new() -> Self { Self }
pub const fn static_ref() -> &'static Self {
&Self::new()
}
}
I expected to see this happen: no errors
Instead, this happened: compiler errored with:
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:6:5
|
6 | &Self::new()
| ^-----------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
Meta
rustc --version --verbose
:
rustc 1.49.0-nightly (beb5ae474 2020-10-04)
binary: rustc
commit-hash: beb5ae474d2835962ebdf7416bd1c9ad864fe101
commit-date: 2020-10-04
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0
This doesn't error in stable or beta, just nightly.
I worked this around by just returning the ZST (&Self
) instead of calling the constructor (&Self::new()
).