Skip to content

Commit ea62c2e

Browse files
Improve E0015 long error explanation
1 parent f1b882b commit ea62c2e

File tree

1 file changed

+27
-9
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+27
-9
lines changed
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
The only functions that can be called in static or constant expressions are
2-
`const` functions, and struct/enum constructors. `const` functions are only
3-
available on a nightly compiler. Rust currently does not support more general
4-
compile-time function execution.
1+
A constant item was initialized with something that is not a constant expression.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0015
6+
fn create_some() -> Option<u8> {
7+
Some(1)
8+
}
59
10+
const FOO: Option<u8> = create_some(); // error!
611
```
7-
const FOO: Option<u8> = Some(1); // enum constructor
8-
struct Bar {x: u8}
9-
const BAR: Bar = Bar {x: 1}; // struct constructor
12+
13+
The only functions that can be called in static or constant expressions are
14+
`const` functions, and struct/enum constructors.
15+
16+
To fix this error, you can declare `create_some` as a constant function:
17+
1018
```
19+
const fn create_some() -> Option<u8> { // declared as a const function
20+
Some(1)
21+
}
1122
12-
See [RFC 911] for more details on the design of `const fn`s.
23+
const FOO: Option<u8> = create_some(); // ok!
1324
14-
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
25+
// These are also working:
26+
struct Bar {
27+
x: u8,
28+
}
29+
30+
const OTHER_FOO: Option<u8> = Some(1);
31+
const BAR: Bar = Bar {x: 1};
32+
```

0 commit comments

Comments
 (0)