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

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

Closed
Karrq opened this issue Oct 5, 2020 · 9 comments
Closed

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

Karrq opened this issue Oct 5, 2020 · 9 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-zst Area: Zero-sized types (ZST). C-bug Category: This is a bug.

Comments

@Karrq
Copy link

Karrq commented Oct 5, 2020

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

@Karrq Karrq added the C-bug Category: This is a bug. label Oct 5, 2020
@SNCPlay42

This comment has been minimized.

@Karrq
Copy link
Author

Karrq commented Oct 5, 2020

Found out why, I attached the wrong playground link, where I removed the const keywords.

This one is the correct one. It compiles both in stable and beta, but not on nightly.

@Karrq Karrq changed the title Previosuly working code doesn't work on nightly (zero-sized types) Previously working code doesn't work on nightly (zero-sized types) Oct 5, 2020
@SNCPlay42
Copy link
Contributor

I believe this was an expected result of #75502 (and a cargo bisect-rustc points to its rollup, #76912).

As a workaround, this code works:

struct ZST;
impl ZST {
  pub const fn new() -> Self { Self }

  pub const fn static_ref() -> &'static Self {
    const VAL: ZST = ZST::new();
    &VAL
  }
}

@RalfJung
Copy link
Member

RalfJung commented Oct 5, 2020

Cc @rust-lang/wg-const-eval

This code used to work accidentally, and stopped working when #75586 got fixed. Indeed the intended way to write this is what @SNCPlay42 suggested.

We're sorry that this broke existing code, but there just is no good way to support that kind of code longer-term as const fn become more powerful. A more ergonomic alternative to an explicit const VAL is under development at #76001.

@Karrq
Copy link
Author

Karrq commented Oct 5, 2020

Alright, it's fine. This code was written way back in Sept 2019.

I'd like to point out that there exists a different workaround:

struct ZST;
impl ZST {
  pub const fn new() -> Self { Self }

  pub const fn static_ref() -> &'static Self {
    &Self
  }
}

as you can see there's no more function call.

The only thing is that moving forward this will break code that is on stable if I'm not mistaken, right?

@oli-obk
Copy link
Contributor

oli-obk commented Oct 5, 2020

The only thing is that moving forward this will break code that is on stable if I'm not mistaken, right?

yes

@jonas-schievink jonas-schievink added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Oct 5, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 5, 2020
@camelid camelid removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Oct 7, 2020
@camelid
Copy link
Member

camelid commented Oct 7, 2020

Removing regression labels since this is an intended regression.

@camelid
Copy link
Member

camelid commented Oct 7, 2020

(I edited the issue to show the correct code and the full error.)

@camelid camelid added the A-zst Area: Zero-sized types (ZST). label Oct 7, 2020
@oli-obk
Copy link
Contributor

oli-obk commented Dec 24, 2020

Closing as working-as-intended. Sorry about the accidental stabilization. We're working on more convenient ways to do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-zst Area: Zero-sized types (ZST). C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

7 participants