Skip to content

Commit

Permalink
doc/test: add UI test and reword docs for E0013 and E0015
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezrashaw committed Jan 8, 2023
1 parent 09382db commit ae61c25
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
23 changes: 8 additions & 15 deletions compiler/rustc_error_codes/src/error_codes/E0015.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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 changes: 4 additions & 0 deletions src/test/ui/error-codes/E0013.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
static X: i32 = 42;
const Y: i32 = X; //~ ERROR constants cannot refer to statics [E0013]

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/error-codes/E0013.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions src/test/ui/error-codes/E0015.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions src/test/ui/error-codes/E0015.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit ae61c25

Please sign in to comment.