diff options
Diffstat (limited to '')
-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 021a0219d..ac78f66ad 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! ``` |