Skip to content

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

Closed
@Boscop

Description

@Boscop

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.

Activity

jonas-schievink

jonas-schievink commented on Jul 17, 2020

@jonas-schievink
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Boscop@jonas-schievink

        Issue actions

          Rustc should allow returning reference to return value of const-fn · Issue #74423 · rust-lang/rust