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+ }
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+ ```
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.
22
33Erroneous code example:
44
5- ```
5+ ``` compile_fail,E0023
66enum 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
1219A 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.
2424You can read more about trait objects in the [ Trait Objects] section of the
2525Reference.
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
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ cause this problem.)
6262In such a case, the compiler cannot predict the return type of ` foo() ` in a
6363situation like the following:
6464
65- ``` compile_fail
65+ ``` compile_fail,E0038
6666trait Trait {
6767 fn foo(&self) -> Self;
6868}
@@ -183,7 +183,7 @@ fn call_foo(thing: Box<Trait>) {
183183
184184We 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
187187types being fed to ` foo() ` ) = 30 implementations!
188188
189189With 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.
42
5- An example using a closure :
3+ Erroneous code example :
64
75``` compile_fail,E0057
86let f = |x| x * 3;
@@ -11,6 +9,10 @@ let b = f(4); // this works!
119let 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+
1416A generic function must be treated similarly:
1517
1618```
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+
111The number of arguments passed to a function must match the number of arguments
212specified in the function signature.
313
You can’t perform that action at this time.
0 commit comments