forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#66633 - GuillaumeGomez:err-codes-cleanup, r…
…=Dylan-DPC Error code's long explanation cleanup Continuing to clean up the error code's long explanation. r? @Dylan-DPC
- Loading branch information
Showing
6 changed files
with
54 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters