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``
This commit is contained in:
commit
3b5afa590b
6 changed files with 57 additions and 27 deletions
|
@ -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!
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue