Skip to content

E0015 "function calls in statics" recommends using constant functions in stable even though constant functions are not stable #28490

Closed
@masklinn

Description

@masklinn

Using rustc 1.3.0 (9a92aaf 2015-09-15)

struct Foo { a: u8 }
fn bar() -> Foo {
    Foo { a: 5 }
}

static foo: Foo = bar();

fn main() {}
> rustc test.rs
test.rs:6:19: 6:24 error: function calls in statics are limited to constant functions, struct and enum constructors [E0015]
test.rs:6 static foo: Foo = bar();
                            ^~~~~
test.rs:6:19: 6:24 help: run `rustc --explain E0015` to see a detailed explanation
> rustc --explain E0015
The only functions that can be called in static or constant expressions are
`const` functions. Rust currently does not support more general compile-time
function execution.

See [RFC 911] for more details on the design of `const fn`s.

[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md

Apply suggestion and make bar a const fn:

struct Foo { a: u8 }
const fn bar() -> Foo {
    Foo { a: 5 }
}

static foo: Foo = bar();

fn main() {}

yields

> rustc test.rs
test.rs:2:1: 4:2 error: const fn is unstable
test.rs:2 const fn bar() -> Foo {
test.rs:3     Foo { a: 5 }
test.rs:4 }
error: aborting due to previous error

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions