Skip to content

Commit 8be9e90

Browse files
authored
Rollup merge of #66633 - GuillaumeGomez:err-codes-cleanup, r=Dylan-DPC
Error code's long explanation cleanup Continuing to clean up the error code's long explanation. r? @Dylan-DPC
2 parents a699945 + a8de11c commit 8be9e90

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
The only functions that can be called in static or constant expressions are
2-
`const` functions, and struct/enum constructors. `const` functions are only
3-
available on a nightly compiler. Rust currently does not support more general
4-
compile-time function execution.
1+
A constant item was initialized with something that is not a constant expression.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0015
6+
fn create_some() -> Option<u8> {
7+
Some(1)
8+
}
59
10+
const FOO: Option<u8> = create_some(); // error!
611
```
7-
const FOO: Option<u8> = Some(1); // enum constructor
8-
struct Bar {x: u8}
9-
const BAR: Bar = Bar {x: 1}; // struct constructor
12+
13+
The only functions that can be called in static or constant expressions are
14+
`const` functions, and struct/enum constructors.
15+
16+
To fix this error, you can declare `create_some` as a constant function:
17+
1018
```
19+
const fn create_some() -> Option<u8> { // declared as a const function
20+
Some(1)
21+
}
1122
12-
See [RFC 911] for more details on the design of `const fn`s.
23+
const FOO: Option<u8> = create_some(); // ok!
1324
14-
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
25+
// These are also working:
26+
struct Bar {
27+
x: u8,
28+
}
29+
30+
const OTHER_FOO: Option<u8> = Some(1);
31+
const BAR: Bar = Bar {x: 1};
32+
```

src/librustc_error_codes/error_codes/E0023.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ A pattern attempted to extract an incorrect number of fields from a variant.
22

33
Erroneous code example:
44

5-
```
5+
```compile_fail,E0023
66
enum Fruit {
77
Apple(String, String),
88
Pear(u32),
99
}
10+
11+
let x = Fruit::Apple(String::new(), String::new());
12+
13+
match x {
14+
Fruit::Apple(a) => {}, // error!
15+
_ => {}
16+
}
1017
```
1118

1219
A pattern used to match against an enum variant must provide a sub-pattern for

src/librustc_error_codes/error_codes/E0033.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ dereferencing the pointer.
2424
You can read more about trait objects in the [Trait Objects] section of the
2525
Reference.
2626

27-
[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects
27+
[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects

src/librustc_error_codes/error_codes/E0038.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cause this problem.)
6262
In such a case, the compiler cannot predict the return type of `foo()` in a
6363
situation like the following:
6464

65-
```compile_fail
65+
```compile_fail,E0038
6666
trait Trait {
6767
fn foo(&self) -> Self;
6868
}
@@ -183,7 +183,7 @@ fn call_foo(thing: Box<Trait>) {
183183

184184
We don't just need to create a table of all implementations of all methods of
185185
`Trait`, we need to create such a table, for each different type fed to
186-
`foo()`. In this case this turns out to be (10 types implementing `Trait`)*(3
186+
`foo()`. In this case this turns out to be (10 types implementing `Trait`)\*(3
187187
types being fed to `foo()`) = 30 implementations!
188188

189189
With real world traits these numbers can grow drastically.

src/librustc_error_codes/error_codes/E0057.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
When invoking closures or other implementations of the function traits `Fn`,
2-
`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
3-
function must match its definition.
1+
An invalid number of arguments was given when calling a closure.
42

5-
An example using a closure:
3+
Erroneous code example:
64

75
```compile_fail,E0057
86
let f = |x| x * 3;
@@ -11,6 +9,10 @@ let b = f(4); // this works!
119
let c = f(2, 3); // invalid, too many parameters
1210
```
1311

12+
When invoking closures or other implementations of the function traits `Fn`,
13+
`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
14+
function must match its definition.
15+
1416
A generic function must be treated similarly:
1517

1618
```

src/librustc_error_codes/error_codes/E0061.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
An invalid number of arguments was passed when calling a function.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0061
6+
fn f(u: i32) {}
7+
8+
f(); // error!
9+
```
10+
111
The number of arguments passed to a function must match the number of arguments
212
specified in the function signature.
313

0 commit comments

Comments
 (0)