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

Values created by const fns aren't rvalue static promoted #60502

Open
sfackler opened this issue May 3, 2019 · 3 comments
Open

Values created by const fns aren't rvalue static promoted #60502

sfackler opened this issue May 3, 2019 · 3 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sfackler
Copy link
Member

sfackler commented May 3, 2019

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
}
@jonas-schievink jonas-schievink added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 3, 2019
@hanna-kruppe
Copy link
Contributor

This is intentional. A blanket "any const fn call is promotable" is problematic and more restricted versions are, of course, more controversial to design. See #53851 for the PR that implemented the current restriction and rust-lang/const-eval#19 for further discussion.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 3, 2019
@Centril
Copy link
Contributor

Centril commented May 3, 2019

I think however that the diagnostic message could be clearer about this being intentional; cc @oli-obk @estebank

@sfackler
Copy link
Member Author

sfackler commented May 3, 2019

Oh, interesting. Yeah, it does seem like it'd be nice if the error message would be a bit more explicit.

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-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants