add new error code

This commit is contained in:
Christian Poveda 2020-06-18 14:52:37 -05:00
parent 1f48465a01
commit 96031e22d2
No known key found for this signature in database
GPG key ID: 27525EF5E7420A50
21 changed files with 95 additions and 53 deletions

View file

@ -444,6 +444,7 @@ E0760: include_str!("./error_codes/E0760.md"),
E0761: include_str!("./error_codes/E0761.md"),
E0762: include_str!("./error_codes/E0762.md"),
E0763: include_str!("./error_codes/E0763.md"),
E0764: include_str!("./error_codes/E0764.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard

View file

@ -0,0 +1,39 @@
Mutable references (`&mut`) can only be used in constant functions, not statics
or constants. This limitation exists to prevent the creation of constants that
have a mutable reference in their final value. If you had a constant of `&mut
i32` type, you could modify the value through that reference, making the
constant essentially mutable. While there could be a more fine-grained scheme
in the future that allows mutable references if they are not "leaked" to the
final value, a more conservative approach was chosen for now. `const fn` do not
have this problem, as the borrow checker will prevent the `const fn` from
returning new mutable references.
Erroneous code example:
```compile_fail,E0764
#![feature(const_fn)]
#![feature(const_mut_refs)]
fn main() {
const OH_NO: &'static mut usize = &mut 1; // error!
}
```
Remember: you cannot use a function call inside a constant or static. However,
you can totally use it in constant functions:
```
#![feature(const_fn)]
#![feature(const_mut_refs)]
const fn foo(x: usize) -> usize {
let mut y = 1;
let z = &mut y;
*z += x;
y
}
fn main() {
const FOO: usize = foo(10); // ok!
}
```

View file

@ -227,7 +227,7 @@ impl NonConstOp for MutBorrow {
struct_span_err!(
ccx.tcx.sess,
span,
E0019,
E0764,
"mutable references are not allowed in {}s",
ccx.const_kind(),
)

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/check-static-immutable-mut-slices.rs:3:37
|
LL | static TEST: &'static mut [isize] = &mut [];
@ -6,4 +6,4 @@ LL | static TEST: &'static mut [isize] = &mut [];
error: aborting due to previous error
For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0764`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/issue-65394.rs:8:13
|
LL | let r = &mut x;
@ -12,5 +12,5 @@ LL | let mut x = Vec::<i32>::new();
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0019, E0493.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0493, E0764.
For more information about an error, try `rustc --explain E0493`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const-multi-ref.rs:6:13
|
LL | let p = &mut a;
@ -12,5 +12,5 @@ LL | let p = &a;
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0019, E0492.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0492, E0764.
For more information about an error, try `rustc --explain E0492`.

View file

@ -1,10 +1,10 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_mut_address_of.rs:24:5
|
LL | foo().bar();
| ^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_mut_address_of.rs:26:9
|
LL | baz(&mut foo());
@ -12,4 +12,4 @@ LL | baz(&mut foo());
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0764`.

View file

@ -1,16 +1,16 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_mut_refs.rs:31:17
|
LL | let _: [(); foo().bar()] = [(); 1];
| ^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_mut_refs.rs:33:21
|
LL | let _: [(); baz(&mut foo())] = [(); 2];
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_mut_refs.rs:35:22
|
LL | let _: [(); bazz(&mut foo())] = [(); 3];
@ -18,4 +18,4 @@ LL | let _: [(); bazz(&mut foo())] = [(); 3];
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0764`.

View file

@ -6,13 +6,13 @@ LL | self.state = x;
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_let_assign3.rs:16:5
|
LL | s.foo(3);
| ^ `&mut` is only allowed in `const fn`
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/const_let_assign3.rs:22:13
|
LL | let y = &mut x;
@ -28,4 +28,5 @@ LL | *y = 42;
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0019, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/projection_qualif.rs:10:27
|
LL | let b: *mut u32 = &mut a;
@ -15,5 +15,5 @@ LL | unsafe { *b = 5; }
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0019, E0658.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/projection_qualif.rs:10:27
|
LL | let b: *mut u32 = &mut a;
@ -23,5 +23,5 @@ LL | unsafe { *b = 5; }
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0019, E0658.
Some errors have detailed explanations: E0019, E0658, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/read_from_static_mut_ref.rs:5:26
|
LL | static OH_NO: &mut i32 = &mut 42;
@ -6,4 +6,4 @@ LL | static OH_NO: &mut i32 = &mut 42;
error: aborting due to previous error
For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0764`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
|
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
@ -6,4 +6,4 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4
error: aborting due to previous error
For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0764`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
|
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
@ -14,4 +14,5 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0019, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -2,10 +2,10 @@ static X: i32 = 1;
const C: i32 = 2;
static mut M: i32 = 3;
const CR: &'static mut i32 = &mut C; //~ ERROR E0019
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
//~| ERROR E0019
//~| ERROR cannot borrow
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0019
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0019
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
fn main() {}

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/E0017.rs:5:30
|
LL | const CR: &'static mut i32 = &mut C;
@ -12,7 +12,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:6:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
@ -24,13 +24,13 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:9:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:10:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
@ -38,5 +38,5 @@ LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0019, E0596.
Some errors have detailed explanations: E0019, E0596, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,10 +1,10 @@
static X: i32 = 1;
const C: i32 = 2;
const CR: &'static mut i32 = &mut C; //~ ERROR E0019
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
//~| ERROR cannot borrow
//~| ERROR E0019
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0019
//~| ERROR E0764
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
fn main() {}

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/E0388.rs:4:30
|
LL | const CR: &'static mut i32 = &mut C;
@ -12,7 +12,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0388.rs:5:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
@ -24,7 +24,7 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0388.rs:8:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
@ -32,5 +32,5 @@ LL | static CONST_REF: &'static mut i32 = &mut C;
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0019, E0596.
Some errors have detailed explanations: E0019, E0596, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/issue-17718-const-bad-values.rs:1:34
|
LL | const C1: &'static mut [usize] = &mut [];
@ -20,7 +20,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
|
= help: consider extracting the value of the `static` to a `const`, and referring to that
error[E0019]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in constants
--> $DIR/issue-17718-const-bad-values.rs:5:41
|
LL | const C2: &'static mut usize = unsafe { &mut S };
@ -28,5 +28,5 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0013, E0019.
Some errors have detailed explanations: E0013, E0764.
For more information about an error, try `rustc --explain E0013`.

View file

@ -1,4 +1,4 @@
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0019
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0764
fn write<T: AsRef<[u8]>>(buffer: T) { }
fn main() {

View file

@ -1,4 +1,4 @@
error[E0019]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in statics
--> $DIR/issue-46604.rs:1:25
|
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
@ -12,5 +12,5 @@ LL | buf[0]=2;
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0019, E0594.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0594, E0764.
For more information about an error, try `rustc --explain E0594`.