diff options
| author | Yuki Okushi <huyuumi.dev+love@gmail.com> | 2023-01-08 17:01:48 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-08 17:01:48 +0900 |
| commit | 3b5afa590bec0652eabc4d97c967cd9f26a6ef5e (patch) | |
| tree | 990ce8265a5e8ee4e96e93b311afc7e157d56456 /compiler/rustc_error_codes/src | |
| parent | 7997ff6612c1beadb74edd54f772c97147e14b96 (diff) | |
| parent | ae61c250cd6124d0ec5095acb3be64b633268ab3 (diff) | |
| download | rust-3b5afa590bec0652eabc4d97c967cd9f26a6ef5e.tar.gz rust-3b5afa590bec0652eabc4d97c967cd9f26a6ef5e.zip | |
Rollup merge of #106557 - Ezrashaw:ui-test-fixups-1, r=GuillaumeGomez
Add some UI tests and reword error-code docs Added UI tests for `E0013` and `E0015`. Error code docs for `E0015` were a bit unclear (they referred to all non-const errors in const context, when only non-const functions applied), so I touched them up a bit. I also fixed up some issues in the new `error_codes.rs` tidy check (linked #106341), that I overlooked previously. r? ``@GuillaumeGomez``
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! ``` |
