Skip to content

Commit ae61c25

Browse files
committed
doc/test: add UI test and reword docs for E0013 and E0015
1 parent 09382db commit ae61c25

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
A constant item was initialized with something that is not a constant
2-
expression.
1+
A non-`const` function was called in a `const` context.
32

43
Erroneous code example:
54

@@ -8,26 +7,20 @@ fn create_some() -> Option<u8> {
87
Some(1)
98
}
109
11-
const FOO: Option<u8> = create_some(); // error!
10+
// error: cannot call non-const fn `create_some` in constants
11+
const FOO: Option<u8> = create_some();
1212
```
1313

14-
The only functions that can be called in static or constant expressions are
15-
`const` functions, and struct/enum constructors.
14+
All functions used in a `const` context (constant or static expression) must
15+
be marked `const`.
1616

1717
To fix this error, you can declare `create_some` as a constant function:
1818

1919
```
20-
const fn create_some() -> Option<u8> { // declared as a const function
20+
// declared as a `const` function:
21+
const fn create_some() -> Option<u8> {
2122
Some(1)
2223
}
2324
24-
const FOO: Option<u8> = create_some(); // ok!
25-
26-
// These are also working:
27-
struct Bar {
28-
x: u8,
29-
}
30-
31-
const OTHER_FOO: Option<u8> = Some(1);
32-
const BAR: Bar = Bar {x: 1};
25+
const FOO: Option<u8> = create_some(); // no error!
3326
```

src/test/ui/error-codes/E0013.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
static X: i32 = 42;
2+
const Y: i32 = X; //~ ERROR constants cannot refer to statics [E0013]
3+
4+
fn main() {}

src/test/ui/error-codes/E0013.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0013]: constants cannot refer to statics
2+
--> $DIR/E0013.rs:2:16
3+
|
4+
LL | const Y: i32 = X;
5+
| ^
6+
|
7+
= help: consider extracting the value of the `static` to a `const`, and referring to that
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0013`.

src/test/ui/error-codes/E0015.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn create_some() -> Option<u8> {
2+
Some(1)
3+
}
4+
5+
const FOO: Option<u8> = create_some();
6+
//~^ ERROR cannot call non-const fn `create_some` in constants [E0015]
7+
8+
fn main() {}

src/test/ui/error-codes/E0015.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0015]: cannot call non-const fn `create_some` in constants
2+
--> $DIR/E0015.rs:5:25
3+
|
4+
LL | const FOO: Option<u8> = create_some();
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)