Skip to content

Previously working code doesn't work on nightly (zero-sized types) #77563

Closed
@Karrq

Description

@Karrq

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()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ZSTsArea: Zero-sized types (ZSTs).A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-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