File tree Expand file tree Collapse file tree 1 file changed +27
-9
lines changed
src/librustc_error_codes/error_codes Expand file tree Collapse file tree 1 file changed +27
-9
lines changed Original file line number Diff line number Diff line change 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
+ }
5
9
10
+ const FOO: Option<u8> = create_some(); // error!
6
11
```
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
+
10
18
```
19
+ const fn create_some() -> Option<u8> { // declared as a const function
20
+ Some(1)
21
+ }
11
22
12
- See [ RFC 911 ] for more details on the design of ` const fn ` s.
23
+ const FOO: Option<u8> = create_some(); // ok!
13
24
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
+ ```
You can’t perform that action at this time.
0 commit comments