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

Rustc should allow returning reference to return value of const-fn #74423

Closed
Boscop opened this issue Jul 17, 2020 · 1 comment
Closed

Rustc should allow returning reference to return value of const-fn #74423

Boscop opened this issue Jul 17, 2020 · 1 comment

Comments

@Boscop
Copy link

Boscop commented Jul 17, 2020

It's unintuitive that this doesn't compile:

struct Foo;

const fn bar() -> Foo { Foo }

// doesn't compile
fn foo() -> &'static Foo {
    &bar()
}

// this compiles
fn foo2() -> &'static Foo {
    const FOO: &'static Foo = &bar();
    FOO
}

// this compiles
fn foo3() -> &'static Foo {
    const FOO: Foo = bar();
    &FOO
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3b50f81085d7e51908ce0378ae865d0f

rustc should just generate a const (or static) behind the scenes and typecheck the code as if the user had written that.

@jonas-schievink
Copy link
Contributor

This is intended behavior according to rust-lang/const-eval#19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants