Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28b6bb8

Browse files
authoredOct 14, 2019
Rollup merge of rust-lang#65265 - GuillaumeGomez:cleanup-librustc_mir-err-codes, r=Mark-Simulacrum
Cleanup librustc mir err codes Three things are done in this PR: * Sort error codes * Uncomment an error code long error explanation (they should **never** be commented) * Unify explanations
2 parents da1cddd + 06a02b5 commit 28b6bb8

File tree

1 file changed

+273
-230
lines changed

1 file changed

+273
-230
lines changed
 

‎src/librustc_mir/error_codes.rs

+273-230
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ E0004: r##"
6464
This error indicates that the compiler cannot guarantee a matching pattern for
6565
one or more possible inputs to a match expression. Guaranteed matches are
6666
required in order to assign values to match expressions, or alternatively,
67-
determine the flow of execution. Erroneous code example:
67+
determine the flow of execution.
68+
69+
Erroneous code example:
6870
6971
```compile_fail,E0004
7072
enum Terminator {
@@ -109,7 +111,9 @@ match x {
109111

110112
E0005: r##"
111113
Patterns used to bind names must be irrefutable, that is, they must guarantee
112-
that a name will be extracted in all cases. Erroneous code example:
114+
that a name will be extracted in all cases.
115+
116+
Erroneous code example:
113117
114118
```compile_fail,E0005
115119
let x = Some(1);
@@ -145,6 +149,8 @@ like the following is invalid as it requires the entire `Option<String>` to be
145149
moved into a variable called `op_string` while simultaneously requiring the
146150
inner `String` to be moved into a variable called `s`.
147151
152+
Erroneous code example:
153+
148154
```compile_fail,E0007
149155
let x = Some("s".to_string());
150156
@@ -208,15 +214,128 @@ match x {
208214
```
209215
"##,
210216

217+
E0010: r##"
218+
The value of statics and constants must be known at compile time, and they live
219+
for the entire lifetime of a program. Creating a boxed value allocates memory on
220+
the heap at runtime, and therefore cannot be done at compile time.
221+
222+
Erroneous code example:
223+
224+
```compile_fail,E0010
225+
const CON: Vec<i32> = vec![0]; // error!
226+
```
227+
"##,
228+
229+
E0013: r##"
230+
Static and const variables can refer to other const variables. But a const
231+
variable cannot refer to a static variable.
232+
233+
Erroneous code example:
234+
235+
```compile_fail,E0013
236+
static X: i32 = 42;
237+
const Y: i32 = X;
238+
```
239+
240+
In this example, `Y` cannot refer to `X` here. To fix this, the value can be
241+
extracted as a const and then used:
242+
243+
```
244+
const A: i32 = 42;
245+
static X: i32 = A;
246+
const Y: i32 = A;
247+
```
248+
"##,
249+
250+
// FIXME(#57563) Change the language here when const fn stabilizes
251+
E0015: r##"
252+
The only functions that can be called in static or constant expressions are
253+
`const` functions, and struct/enum constructors. `const` functions are only
254+
available on a nightly compiler. Rust currently does not support more general
255+
compile-time function execution.
256+
257+
```
258+
const FOO: Option<u8> = Some(1); // enum constructor
259+
struct Bar {x: u8}
260+
const BAR: Bar = Bar {x: 1}; // struct constructor
261+
```
262+
263+
See [RFC 911] for more details on the design of `const fn`s.
264+
265+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
266+
"##,
267+
268+
E0017: r##"
269+
References in statics and constants may only refer to immutable values.
270+
271+
Erroneous code example:
272+
273+
```compile_fail,E0017
274+
static X: i32 = 1;
275+
const C: i32 = 2;
276+
277+
// these three are not allowed:
278+
const CR: &mut i32 = &mut C;
279+
static STATIC_REF: &'static mut i32 = &mut X;
280+
static CONST_REF: &'static mut i32 = &mut C;
281+
```
282+
283+
Statics are shared everywhere, and if they refer to mutable data one might
284+
violate memory safety since holding multiple mutable references to shared data
285+
is not allowed.
286+
287+
If you really want global mutable state, try using `static mut` or a global
288+
`UnsafeCell`.
289+
"##,
290+
291+
E0019: r##"
292+
A function call isn't allowed in the const's initialization expression
293+
because the expression's value must be known at compile-time.
294+
295+
Erroneous code example:
296+
297+
```compile_fail,E0019
298+
#![feature(box_syntax)]
299+
300+
fn main() {
301+
struct MyOwned;
302+
303+
static STATIC11: Box<MyOwned> = box MyOwned; // error!
304+
}
305+
```
306+
307+
Remember: you can't use a function call inside a const's initialization
308+
expression! However, you can totally use it anywhere else:
309+
310+
```
311+
enum Test {
312+
V1
313+
}
314+
315+
impl Test {
316+
fn func(&self) -> i32 {
317+
12
318+
}
319+
}
320+
321+
fn main() {
322+
const FOO: Test = Test::V1;
323+
324+
FOO.func(); // here is good
325+
let x = FOO.func(); // or even here!
326+
}
327+
```
328+
"##,
329+
211330
E0030: r##"
212331
When matching against a range, the compiler verifies that the range is
213-
non-empty. Range patterns include both end-points, so this is equivalent to
332+
non-empty. Range patterns include both end-points, so this is equivalent to
214333
requiring the start of the range to be less than or equal to the end of the
215334
range.
216335
217-
For example:
336+
Erroneous code example:
218337
219-
```compile_fail
338+
```compile_fail,E0030
220339
match 5u32 {
221340
// This range is ok, albeit pointless.
222341
1 ..= 1 => {}
@@ -226,7 +345,61 @@ match 5u32 {
226345
```
227346
"##,
228347

348+
E0133: r##"
349+
Unsafe code was used outside of an unsafe function or block.
350+
351+
Erroneous code example:
352+
353+
```compile_fail,E0133
354+
unsafe fn f() { return; } // This is the unsafe code
355+
356+
fn main() {
357+
f(); // error: call to unsafe function requires unsafe function or block
358+
}
359+
```
360+
361+
Using unsafe functionality is potentially dangerous and disallowed by safety
362+
checks. Examples:
363+
364+
* Dereferencing raw pointers
365+
* Calling functions via FFI
366+
* Calling functions marked unsafe
367+
368+
These safety checks can be relaxed for a section of the code by wrapping the
369+
unsafe instructions with an `unsafe` block. For instance:
370+
371+
```
372+
unsafe fn f() { return; }
373+
374+
fn main() {
375+
unsafe { f(); } // ok!
376+
}
377+
```
378+
379+
See also https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
380+
"##,
381+
229382
E0158: r##"
383+
An associated const has been referenced in a pattern.
384+
385+
Erroneous code example:
386+
387+
```compile_fail,E0158
388+
enum EFoo { A, B, C, D }
389+
390+
trait Foo {
391+
const X: EFoo;
392+
}
393+
394+
fn test<A: Foo>(arg: EFoo) {
395+
match arg {
396+
A::X => { // error!
397+
println!("A::X");
398+
}
399+
}
400+
}
401+
```
402+
230403
`const` and `static` mean different things. A `const` is a compile-time
231404
constant, an alias for a literal value. This property means you can match it
232405
directly within a pattern.
@@ -247,6 +420,39 @@ match Some(42) {
247420
```
248421
"##,
249422

423+
E0161: r##"
424+
A value was moved. However, its size was not known at compile time, and only
425+
values of a known size can be moved.
426+
427+
Erroneous code example:
428+
429+
```compile_fail,E0161
430+
#![feature(box_syntax)]
431+
432+
fn main() {
433+
let array: &[isize] = &[1, 2, 3];
434+
let _x: Box<[isize]> = box *array;
435+
// error: cannot move a value of type [isize]: the size of [isize] cannot
436+
// be statically determined
437+
}
438+
```
439+
440+
In Rust, you can only move a value when its size is known at compile time.
441+
442+
To work around this restriction, consider "hiding" the value behind a reference:
443+
either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
444+
it around as usual. Example:
445+
446+
```
447+
#![feature(box_syntax)]
448+
449+
fn main() {
450+
let array: &[isize] = &[1, 2, 3];
451+
let _x: Box<&[isize]> = box array; // ok!
452+
}
453+
```
454+
"##,
455+
250456
E0162: r##"
251457
#### Note: this error code is no longer emitted by the compiler.
252458
@@ -468,158 +674,6 @@ The `op_string_ref` binding has type `&Option<&String>` in both cases.
468674
See also https://github.com/rust-lang/rust/issues/14587
469675
"##,
470676

471-
E0010: r##"
472-
The value of statics and constants must be known at compile time, and they live
473-
for the entire lifetime of a program. Creating a boxed value allocates memory on
474-
the heap at runtime, and therefore cannot be done at compile time. Erroneous
475-
code example:
476-
477-
```compile_fail,E0010
478-
#![feature(box_syntax)]
479-
480-
const CON : Box<i32> = box 0;
481-
```
482-
"##,
483-
484-
E0013: r##"
485-
Static and const variables can refer to other const variables. But a const
486-
variable cannot refer to a static variable. For example, `Y` cannot refer to
487-
`X` here:
488-
489-
```compile_fail,E0013
490-
static X: i32 = 42;
491-
const Y: i32 = X;
492-
```
493-
494-
To fix this, the value can be extracted as a const and then used:
495-
496-
```
497-
const A: i32 = 42;
498-
static X: i32 = A;
499-
const Y: i32 = A;
500-
```
501-
"##,
502-
503-
// FIXME(#57563) Change the language here when const fn stabilizes
504-
E0015: r##"
505-
The only functions that can be called in static or constant expressions are
506-
`const` functions, and struct/enum constructors. `const` functions are only
507-
available on a nightly compiler. Rust currently does not support more general
508-
compile-time function execution.
509-
510-
```
511-
const FOO: Option<u8> = Some(1); // enum constructor
512-
struct Bar {x: u8}
513-
const BAR: Bar = Bar {x: 1}; // struct constructor
514-
```
515-
516-
See [RFC 911] for more details on the design of `const fn`s.
517-
518-
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
519-
"##,
520-
521-
E0017: r##"
522-
References in statics and constants may only refer to immutable values.
523-
Erroneous code example:
524-
525-
```compile_fail,E0017
526-
static X: i32 = 1;
527-
const C: i32 = 2;
528-
529-
// these three are not allowed:
530-
const CR: &mut i32 = &mut C;
531-
static STATIC_REF: &'static mut i32 = &mut X;
532-
static CONST_REF: &'static mut i32 = &mut C;
533-
```
534-
535-
Statics are shared everywhere, and if they refer to mutable data one might
536-
violate memory safety since holding multiple mutable references to shared data
537-
is not allowed.
538-
539-
If you really want global mutable state, try using `static mut` or a global
540-
`UnsafeCell`.
541-
"##,
542-
543-
E0019: r##"
544-
A function call isn't allowed in the const's initialization expression
545-
because the expression's value must be known at compile-time. Erroneous code
546-
example:
547-
548-
```compile_fail
549-
enum Test {
550-
V1
551-
}
552-
553-
impl Test {
554-
fn test(&self) -> i32 {
555-
12
556-
}
557-
}
558-
559-
fn main() {
560-
const FOO: Test = Test::V1;
561-
562-
const A: i32 = FOO.test(); // You can't call Test::func() here!
563-
}
564-
```
565-
566-
Remember: you can't use a function call inside a const's initialization
567-
expression! However, you can totally use it anywhere else:
568-
569-
```
570-
enum Test {
571-
V1
572-
}
573-
574-
impl Test {
575-
fn func(&self) -> i32 {
576-
12
577-
}
578-
}
579-
580-
fn main() {
581-
const FOO: Test = Test::V1;
582-
583-
FOO.func(); // here is good
584-
let x = FOO.func(); // or even here!
585-
}
586-
```
587-
"##,
588-
589-
E0133: r##"
590-
Unsafe code was used outside of an unsafe function or block.
591-
592-
Erroneous code example:
593-
594-
```compile_fail,E0133
595-
unsafe fn f() { return; } // This is the unsafe code
596-
597-
fn main() {
598-
f(); // error: call to unsafe function requires unsafe function or block
599-
}
600-
```
601-
602-
Using unsafe functionality is potentially dangerous and disallowed by safety
603-
checks. Examples:
604-
605-
* Dereferencing raw pointers
606-
* Calling functions via FFI
607-
* Calling functions marked unsafe
608-
609-
These safety checks can be relaxed for a section of the code by wrapping the
610-
unsafe instructions with an `unsafe` block. For instance:
611-
612-
```
613-
unsafe fn f() { return; }
614-
615-
fn main() {
616-
unsafe { f(); } // ok!
617-
}
618-
```
619-
620-
See also https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
621-
"##,
622-
623677
E0373: r##"
624678
This error occurs when an attempt is made to use data captured by a closure,
625679
when that data may no longer exist. It's most commonly seen when attempting to
@@ -672,7 +726,9 @@ about safety.
672726
"##,
673727

674728
E0381: r##"
675-
It is not allowed to use or capture an uninitialized variable. For example:
729+
It is not allowed to use or capture an uninitialized variable.
730+
731+
Erroneous code example:
676732
677733
```compile_fail,E0381
678734
fn main() {
@@ -694,7 +750,9 @@ fn main() {
694750

695751
E0382: r##"
696752
This error occurs when an attempt is made to use a variable after its contents
697-
have been moved elsewhere. For example:
753+
have been moved elsewhere.
754+
755+
Erroneous code example:
698756
699757
```compile_fail,E0382
700758
struct MyStruct { s: u32 }
@@ -842,7 +900,8 @@ x = Foo { a: 2 };
842900

843901
E0384: r##"
844902
This error occurs when an attempt is made to reassign an immutable variable.
845-
For example:
903+
904+
Erroneous code example:
846905
847906
```compile_fail,E0384
848907
fn main() {
@@ -862,13 +921,15 @@ fn main() {
862921
```
863922
"##,
864923

865-
/*E0386: r##"
924+
E0386: r##"
925+
#### Note: this error code is no longer emitted by the compiler.
926+
866927
This error occurs when an attempt is made to mutate the target of a mutable
867928
reference stored inside an immutable container.
868929
869930
For example, this can happen when storing a `&mut` inside an immutable `Box`:
870931
871-
```compile_fail,E0386
932+
```
872933
let mut x: i64 = 1;
873934
let y: Box<_> = Box::new(&mut x);
874935
**y = 2; // error, cannot assign to data in an immutable container
@@ -892,13 +953,15 @@ let x: i64 = 1;
892953
let y: Box<Cell<_>> = Box::new(Cell::new(x));
893954
y.set(2);
894955
```
895-
"##,*/
956+
"##,
896957

897958
E0387: r##"
898959
#### Note: this error code is no longer emitted by the compiler.
899960
900961
This error occurs when an attempt is made to mutate or mutably reference data
901-
that a closure has captured immutably. Examples of this error are shown below:
962+
that a closure has captured immutably.
963+
964+
Erroneous code example:
902965
903966
```compile_fail
904967
// Accepts a function or a closure that captures its environment immutably.
@@ -963,7 +1026,7 @@ An attempt was made to mutate data using a non-mutable reference. This
9631026
commonly occurs when attempting to assign to a non-mutable reference of a
9641027
mutable reference (`&(&mut T)`).
9651028
966-
Example of erroneous code:
1029+
Erroneous code example:
9671030
9681031
```compile_fail
9691032
struct FancyNum {
@@ -1022,43 +1085,11 @@ fn main() {
10221085
```
10231086
"##,
10241087

1025-
E0161: r##"
1026-
A value was moved. However, its size was not known at compile time, and only
1027-
values of a known size can be moved.
1088+
E0492: r##"
1089+
A borrow of a constant containing interior mutability was attempted.
10281090
10291091
Erroneous code example:
10301092
1031-
```compile_fail
1032-
#![feature(box_syntax)]
1033-
1034-
fn main() {
1035-
let array: &[isize] = &[1, 2, 3];
1036-
let _x: Box<[isize]> = box *array;
1037-
// error: cannot move a value of type [isize]: the size of [isize] cannot
1038-
// be statically determined
1039-
}
1040-
```
1041-
1042-
In Rust, you can only move a value when its size is known at compile time.
1043-
1044-
To work around this restriction, consider "hiding" the value behind a reference:
1045-
either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
1046-
it around as usual. Example:
1047-
1048-
```
1049-
#![feature(box_syntax)]
1050-
1051-
fn main() {
1052-
let array: &[isize] = &[1, 2, 3];
1053-
let _x: Box<&[isize]> = box array; // ok!
1054-
}
1055-
```
1056-
"##,
1057-
1058-
E0492: r##"
1059-
A borrow of a constant containing interior mutability was attempted. Erroneous
1060-
code example:
1061-
10621093
```compile_fail,E0492
10631094
use std::sync::atomic::AtomicUsize;
10641095
@@ -1174,7 +1205,9 @@ static FOO: Foo = Foo { field1: DropType::A }; // We initialize all fields
11741205
"##,
11751206

11761207
E0499: r##"
1177-
A variable was borrowed as mutable more than once. Erroneous code example:
1208+
A variable was borrowed as mutable more than once.
1209+
1210+
Erroneous code example:
11781211
11791212
```compile_fail,E0499
11801213
let mut i = 0;
@@ -1205,7 +1238,9 @@ a;
12051238
"##,
12061239

12071240
E0500: r##"
1208-
A borrowed variable was used by a closure. Example of erroneous code:
1241+
A borrowed variable was used by a closure.
1242+
1243+
Erroneous code example:
12091244
12101245
```compile_fail,E0500
12111246
fn you_know_nothing(jon_snow: &mut i32) {
@@ -1256,7 +1291,7 @@ situation, the closure is borrowing the variable. Take a look at
12561291
http://rustbyexample.com/fn/closures/capture.html for more information about
12571292
capturing.
12581293
1259-
Example of erroneous code:
1294+
Erroneous code example:
12601295
12611296
```compile_fail,E0501
12621297
fn inside_closure(x: &mut i32) {
@@ -1329,7 +1364,7 @@ E0502: r##"
13291364
This error indicates that you are trying to borrow a variable as mutable when it
13301365
has already been borrowed as immutable.
13311366
1332-
Example of erroneous code:
1367+
Erroneous code example:
13331368
13341369
```compile_fail,E0502
13351370
fn bar(x: &mut i32) {}
@@ -1360,7 +1395,7 @@ https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html.
13601395
E0503: r##"
13611396
A value was used after it was mutably borrowed.
13621397
1363-
Example of erroneous code:
1398+
Erroneous code example:
13641399
13651400
```compile_fail,E0503
13661401
fn main() {
@@ -1418,7 +1453,7 @@ E0504: r##"
14181453
This error occurs when an attempt is made to move a borrowed variable into a
14191454
closure.
14201455
1421-
Example of erroneous code:
1456+
Erroneous code example:
14221457
14231458
```compile_fail
14241459
struct FancyNum {
@@ -1609,7 +1644,7 @@ http://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
16091644
E0506: r##"
16101645
This error occurs when an attempt is made to assign to a borrowed value.
16111646
1612-
Example of erroneous code:
1647+
Erroneous code example:
16131648
16141649
```compile_fail,E0506
16151650
struct FancyNum {
@@ -1827,7 +1862,7 @@ http://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
18271862
E0508: r##"
18281863
A value was moved out of a non-copy fixed-size array.
18291864
1830-
Example of erroneous code:
1865+
Erroneous code example:
18311866
18321867
```compile_fail,E0508
18331868
struct NonCopy;
@@ -1872,7 +1907,7 @@ E0509: r##"
18721907
This error occurs when an attempt is made to move out of a value whose type
18731908
implements the `Drop` trait.
18741909
1875-
Example of erroneous code:
1910+
Erroneous code example:
18761911
18771912
```compile_fail,E0509
18781913
struct FancyNum {
@@ -1982,30 +2017,14 @@ Here executing `x = None` would modify the value being matched and require us
19822017
to go "back in time" to the `None` arm.
19832018
"##,
19842019

1985-
E0579: r##"
1986-
When matching against an exclusive range, the compiler verifies that the range
1987-
is non-empty. Exclusive range patterns include the start point but not the end
1988-
point, so this is equivalent to requiring the start of the range to be less
1989-
than the end of the range.
1990-
1991-
For example:
1992-
1993-
```compile_fail
1994-
match 5u32 {
1995-
// This range is ok, albeit pointless.
1996-
1 .. 2 => {}
1997-
// This range is empty, and the compiler can tell.
1998-
5 .. 5 => {}
1999-
}
2000-
```
2001-
"##,
2002-
20032020
E0515: r##"
20042021
Cannot return value that references local variable
20052022
20062023
Local variables, function parameters and temporaries are all dropped before the
20072024
end of the function body. So a reference to them cannot be returned.
20082025
2026+
Erroneous code example:
2027+
20092028
```compile_fail,E0515
20102029
fn get_dangling_reference() -> &'static i32 {
20112030
let x = 0;
@@ -2101,6 +2120,28 @@ fn dragoooon(x: &mut isize) {
21012120
```
21022121
"##,
21032122

2123+
E0579: r##"
2124+
When matching against an exclusive range, the compiler verifies that the range
2125+
is non-empty. Exclusive range patterns include the start point but not the end
2126+
point, so this is equivalent to requiring the start of the range to be less
2127+
than the end of the range.
2128+
2129+
Erroneous code example:
2130+
2131+
```compile_fail,E0579
2132+
#![feature(exclusive_range_pattern)]
2133+
2134+
fn main() {
2135+
match 5u32 {
2136+
// This range is ok, albeit pointless.
2137+
1 .. 2 => {}
2138+
// This range is empty, and the compiler can tell.
2139+
5 .. 5 => {} // error!
2140+
}
2141+
}
2142+
```
2143+
"##,
2144+
21042145
E0595: r##"
21052146
#### Note: this error code is no longer emitted by the compiler.
21062147
@@ -2124,7 +2165,7 @@ let mut c = || { x += 1 };
21242165
E0596: r##"
21252166
This error occurs because you tried to mutably borrow a non-mutable variable.
21262167
2127-
Example of erroneous code:
2168+
Erroneous code example:
21282169
21292170
```compile_fail,E0596
21302171
let x = 1;
@@ -2143,7 +2184,7 @@ let y = &mut x; // ok!
21432184
E0597: r##"
21442185
This error occurs because a value was dropped while it was still borrowed
21452186
2146-
Example of erroneous code:
2187+
Erroneous code example:
21472188
21482189
```compile_fail,E0597
21492190
struct Foo<'a> {
@@ -2180,6 +2221,8 @@ E0626: r##"
21802221
This error occurs because a borrow in a generator persists across a
21812222
yield point.
21822223
2224+
Erroneous code example:
2225+
21832226
```compile_fail,E0626
21842227
# #![feature(generators, generator_trait, pin)]
21852228
# use std::ops::Generator;
@@ -2271,7 +2314,7 @@ E0712: r##"
22712314
This error occurs because a borrow of a thread-local variable was made inside a
22722315
function which outlived the lifetime of the function.
22732316
2274-
Example of erroneous code:
2317+
Erroneous code example:
22752318
22762319
```compile_fail,E0712
22772320
#![feature(thread_local)]
@@ -2293,7 +2336,7 @@ E0713: r##"
22932336
This error occurs when an attempt is made to borrow state past the end of the
22942337
lifetime of a type that implements the `Drop` trait.
22952338
2296-
Example of erroneous code:
2339+
Erroneous code example:
22972340
22982341
```compile_fail,E0713
22992342
#![feature(nll)]

0 commit comments

Comments
 (0)
Please sign in to comment.