diff options
| author | Ezra Shaw <ezrasure@outlook.com> | 2023-01-07 18:37:40 +1300 |
|---|---|---|
| committer | Ezra Shaw <ezrasure@outlook.com> | 2023-01-08 13:33:09 +1300 |
| commit | ae61c250cd6124d0ec5095acb3be64b633268ab3 (patch) | |
| tree | ffa2f37dc44d8acc25f8b3596699aca5ff95c29c /compiler/rustc_error_codes/src | |
| parent | 09382db78b293a649115fbd3d5bc79aaaf7b8deb (diff) | |
| download | rust-ae61c250cd6124d0ec5095acb3be64b633268ab3.tar.gz rust-ae61c250cd6124d0ec5095acb3be64b633268ab3.zip | |
doc/test: add UI test and reword docs for `E0013` and `E0015`
Diffstat (limited to 'compiler/rustc_error_codes/src')
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0015.md | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0015.md b/compiler/rustc_error_codes/src/error_codes/E0015.md index 021a0219d13..ac78f66adad 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0015.md +++ b/compiler/rustc_error_codes/src/error_codes/E0015.md @@ -1,5 +1,4 @@ -A constant item was initialized with something that is not a constant -expression. +A non-`const` function was called in a `const` context. Erroneous code example: @@ -8,26 +7,20 @@ fn create_some() -> Option<u8> { Some(1) } -const FOO: Option<u8> = create_some(); // error! +// error: cannot call non-const fn `create_some` in constants +const FOO: Option<u8> = create_some(); ``` -The only functions that can be called in static or constant expressions are -`const` functions, and struct/enum constructors. +All functions used in a `const` context (constant or static expression) must +be marked `const`. To fix this error, you can declare `create_some` as a constant function: ``` -const fn create_some() -> Option<u8> { // declared as a const function +// declared as a `const` function: +const fn create_some() -> Option<u8> { Some(1) } -const FOO: Option<u8> = create_some(); // ok! - -// These are also working: -struct Bar { - x: u8, -} - -const OTHER_FOO: Option<u8> = Some(1); -const BAR: Bar = Bar {x: 1}; +const FOO: Option<u8> = create_some(); // no error! ``` |
