File tree Expand file tree Collapse file tree 6 files changed +54
-17
lines changed
src/librustc_error_codes/error_codes Expand file tree Collapse file tree 6 files changed +54
-17
lines changed Original file line number Diff line number Diff line change 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
+ }
5
9
10
+ const FOO: Option<u8> = create_some(); // error!
6
11
```
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
+
10
18
```
19
+ const fn create_some() -> Option<u8> { // declared as a const function
20
+ Some(1)
21
+ }
11
22
12
- See [ RFC 911 ] for more details on the design of ` const fn ` s.
23
+ const FOO: Option<u8> = create_some(); // ok!
13
24
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
+ ```
Original file line number Diff line number Diff line change @@ -2,11 +2,18 @@ A pattern attempted to extract an incorrect number of fields from a variant.
2
2
3
3
Erroneous code example:
4
4
5
- ```
5
+ ``` compile_fail,E0023
6
6
enum Fruit {
7
7
Apple(String, String),
8
8
Pear(u32),
9
9
}
10
+
11
+ let x = Fruit::Apple(String::new(), String::new());
12
+
13
+ match x {
14
+ Fruit::Apple(a) => {}, // error!
15
+ _ => {}
16
+ }
10
17
```
11
18
12
19
A pattern used to match against an enum variant must provide a sub-pattern for
Original file line number Diff line number Diff line change @@ -24,4 +24,4 @@ dereferencing the pointer.
24
24
You can read more about trait objects in the [ Trait Objects] section of the
25
25
Reference.
26
26
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
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ cause this problem.)
62
62
In such a case, the compiler cannot predict the return type of ` foo() ` in a
63
63
situation like the following:
64
64
65
- ``` compile_fail
65
+ ``` compile_fail,E0038
66
66
trait Trait {
67
67
fn foo(&self) -> Self;
68
68
}
@@ -183,7 +183,7 @@ fn call_foo(thing: Box<Trait>) {
183
183
184
184
We don't just need to create a table of all implementations of all methods of
185
185
` 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
187
187
types being fed to ` foo() ` ) = 30 implementations!
188
188
189
189
With real world traits these numbers can grow drastically.
Original file line number Diff line number Diff line change 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.
4
2
5
- An example using a closure :
3
+ Erroneous code example :
6
4
7
5
``` compile_fail,E0057
8
6
let f = |x| x * 3;
@@ -11,6 +9,10 @@ let b = f(4); // this works!
11
9
let c = f(2, 3); // invalid, too many parameters
12
10
```
13
11
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
+
14
16
A generic function must be treated similarly:
15
17
16
18
```
Original file line number Diff line number Diff line change
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
+
1
11
The number of arguments passed to a function must match the number of arguments
2
12
specified in the function signature.
3
13
You can’t perform that action at this time.
0 commit comments