doc/test: add UI test and reword docs for E0013
and E0015
This commit is contained in:
parent
09382db78b
commit
ae61c250cd
@ -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!
|
||||
```
|
||||
|
4
src/test/ui/error-codes/E0013.rs
Normal file
4
src/test/ui/error-codes/E0013.rs
Normal file
@ -0,0 +1,4 @@
|
||||
static X: i32 = 42;
|
||||
const Y: i32 = X; //~ ERROR constants cannot refer to statics [E0013]
|
||||
|
||||
fn main() {}
|
11
src/test/ui/error-codes/E0013.stderr
Normal file
11
src/test/ui/error-codes/E0013.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
error[E0013]: constants cannot refer to statics
|
||||
--> $DIR/E0013.rs:2:16
|
||||
|
|
||||
LL | const Y: i32 = X;
|
||||
| ^
|
||||
|
|
||||
= help: consider extracting the value of the `static` to a `const`, and referring to that
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0013`.
|
8
src/test/ui/error-codes/E0015.rs
Normal file
8
src/test/ui/error-codes/E0015.rs
Normal file
@ -0,0 +1,8 @@
|
||||
fn create_some() -> Option<u8> {
|
||||
Some(1)
|
||||
}
|
||||
|
||||
const FOO: Option<u8> = create_some();
|
||||
//~^ ERROR cannot call non-const fn `create_some` in constants [E0015]
|
||||
|
||||
fn main() {}
|
11
src/test/ui/error-codes/E0015.stderr
Normal file
11
src/test/ui/error-codes/E0015.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
error[E0015]: cannot call non-const fn `create_some` in constants
|
||||
--> $DIR/E0015.rs:5:25
|
||||
|
|
||||
LL | const FOO: Option<u8> = create_some();
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0015`.
|
Loading…
Reference in New Issue
Block a user