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

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

Closed
masklinn opened this issue Sep 18, 2015 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@masklinn
Copy link
Contributor

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
@Manishearth Manishearth self-assigned this Sep 18, 2015
@Manishearth Manishearth added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 18, 2015
@alexispurslane
Copy link
Contributor

Should we change the "explain" message?

@Manishearth
Copy link
Member

We should make the error message change depending on the compiler version. Perhaps replace it with a note "switch to a nightly compiler to be able to use constant functions". The explain message should just be expanded a bit to mention nightlies.

Of course, we should leave comments there with an issue for fixing these once const fn stabilizes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

3 participants