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

Error code's long explanation cleanup #66633

Merged
merged 5 commits into from
Nov 23, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/librustc_error_codes/error_codes/E0015.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
The only functions that can be called in static or constant expressions are
`const` functions, and struct/enum constructors. `const` functions are only
available on a nightly compiler. Rust currently does not support more general
compile-time function execution.
A constant item was initialized with something that is not a constant expression.

Erroneous code example:

```compile_fail,E0015
fn create_some() -> Option<u8> {
Some(1)
}
const FOO: Option<u8> = create_some(); // error!
```
const FOO: Option<u8> = Some(1); // enum constructor
struct Bar {x: u8}
const BAR: Bar = Bar {x: 1}; // struct constructor

The only functions that can be called in static or constant expressions are
`const` functions, and struct/enum constructors.

To fix this error, you can declare `create_some` as a constant function:

```
const fn create_some() -> Option<u8> { // declared as a const function
Some(1)
}
See [RFC 911] for more details on the design of `const fn`s.
const FOO: Option<u8> = create_some(); // ok!
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
// These are also working:
struct Bar {
x: u8,
}
const OTHER_FOO: Option<u8> = Some(1);
const BAR: Bar = Bar {x: 1};
```