From 96031e22d22fd3b98e6caa3851b99272e2b4618d Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 18 Jun 2020 14:52:37 -0500 Subject: [PATCH] add new error code --- src/librustc_error_codes/error_codes.rs | 1 + src/librustc_error_codes/error_codes/E0764.md | 39 +++++++++++++++++++ .../transform/check_consts/ops.rs | 2 +- .../check-static-immutable-mut-slices.stderr | 4 +- .../ui/consts/const-eval/issue-65394.stderr | 6 +-- src/test/ui/consts/const-multi-ref.stderr | 6 +-- .../const_mut_address_of.stderr | 6 +-- .../const-mut-refs/const_mut_refs.stderr | 8 ++-- src/test/ui/consts/const_let_assign3.stderr | 7 ++-- .../consts/projection_qualif.mut_refs.stderr | 6 +-- .../ui/consts/projection_qualif.stock.stderr | 4 +- .../ui/consts/read_from_static_mut_ref.stderr | 4 +- ...ic_mut_containing_mut_ref2.mut_refs.stderr | 4 +- ...tatic_mut_containing_mut_ref2.stock.stderr | 5 ++- src/test/ui/error-codes/E0017.rs | 8 ++-- src/test/ui/error-codes/E0017.stderr | 10 ++--- src/test/ui/error-codes/E0388.rs | 6 +-- src/test/ui/error-codes/E0388.stderr | 8 ++-- .../issue-17718-const-bad-values.stderr | 6 +-- src/test/ui/issues/issue-46604.rs | 2 +- src/test/ui/issues/issue-46604.stderr | 6 +-- 21 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 src/librustc_error_codes/error_codes/E0764.md diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index 997762efcb3e5..738b3bc7539a6 100644 --- a/src/librustc_error_codes/error_codes.rs +++ b/src/librustc_error_codes/error_codes.rs @@ -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 diff --git a/src/librustc_error_codes/error_codes/E0764.md b/src/librustc_error_codes/error_codes/E0764.md new file mode 100644 index 0000000000000..e9061f988ac59 --- /dev/null +++ b/src/librustc_error_codes/error_codes/E0764.md @@ -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! +} +``` diff --git a/src/librustc_mir/transform/check_consts/ops.rs b/src/librustc_mir/transform/check_consts/ops.rs index 5e3bd2c1d3de8..733ae9084511c 100644 --- a/src/librustc_mir/transform/check_consts/ops.rs +++ b/src/librustc_mir/transform/check_consts/ops.rs @@ -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(), ) diff --git a/src/test/ui/check-static-immutable-mut-slices.stderr b/src/test/ui/check-static-immutable-mut-slices.stderr index 855f6dda18293..9ffbb483d139d 100644 --- a/src/test/ui/check-static-immutable-mut-slices.stderr +++ b/src/test/ui/check-static-immutable-mut-slices.stderr @@ -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`. diff --git a/src/test/ui/consts/const-eval/issue-65394.stderr b/src/test/ui/consts/const-eval/issue-65394.stderr index 827af6d832df8..f843a94fabd8d 100644 --- a/src/test/ui/consts/const-eval/issue-65394.stderr +++ b/src/test/ui/consts/const-eval/issue-65394.stderr @@ -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::::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`. diff --git a/src/test/ui/consts/const-multi-ref.stderr b/src/test/ui/consts/const-multi-ref.stderr index 84ff5457eef19..9a7914b458874 100644 --- a/src/test/ui/consts/const-multi-ref.stderr +++ b/src/test/ui/consts/const-multi-ref.stderr @@ -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`. diff --git a/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr b/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr index 3940ce236a3ed..2214ce6ee1c87 100644 --- a/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr +++ b/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr @@ -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`. diff --git a/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr b/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr index 39d0a391bab18..4ca7b128b7c4b 100644 --- a/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr +++ b/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr @@ -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`. diff --git a/src/test/ui/consts/const_let_assign3.stderr b/src/test/ui/consts/const_let_assign3.stderr index a5810e60a09d2..dd05a4c0bb069 100644 --- a/src/test/ui/consts/const_let_assign3.stderr +++ b/src/test/ui/consts/const_let_assign3.stderr @@ -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`. diff --git a/src/test/ui/consts/projection_qualif.mut_refs.stderr b/src/test/ui/consts/projection_qualif.mut_refs.stderr index e84c86f5a78be..fad8f011f75f5 100644 --- a/src/test/ui/consts/projection_qualif.mut_refs.stderr +++ b/src/test/ui/consts/projection_qualif.mut_refs.stderr @@ -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`. diff --git a/src/test/ui/consts/projection_qualif.stock.stderr b/src/test/ui/consts/projection_qualif.stock.stderr index 01e12e186fe59..212f12286455f 100644 --- a/src/test/ui/consts/projection_qualif.stock.stderr +++ b/src/test/ui/consts/projection_qualif.stock.stderr @@ -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`. diff --git a/src/test/ui/consts/read_from_static_mut_ref.stderr b/src/test/ui/consts/read_from_static_mut_ref.stderr index c51ddae23d8d1..c936ac0b7d585 100644 --- a/src/test/ui/consts/read_from_static_mut_ref.stderr +++ b/src/test/ui/consts/read_from_static_mut_ref.stderr @@ -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`. diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr b/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr index 2c6abb3a4ed62..36c280ca5c607 100644 --- a/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr +++ b/src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr @@ -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`. diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr b/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr index 47c9401704ce5..57fb27e642e6f 100644 --- a/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr +++ b/src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr @@ -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`. diff --git a/src/test/ui/error-codes/E0017.rs b/src/test/ui/error-codes/E0017.rs index 8e6de93f44000..818dec1207b96 100644 --- a/src/test/ui/error-codes/E0017.rs +++ b/src/test/ui/error-codes/E0017.rs @@ -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() {} diff --git a/src/test/ui/error-codes/E0017.stderr b/src/test/ui/error-codes/E0017.stderr index d952afa2521a5..c1d96de1dca7c 100644 --- a/src/test/ui/error-codes/E0017.stderr +++ b/src/test/ui/error-codes/E0017.stderr @@ -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`. diff --git a/src/test/ui/error-codes/E0388.rs b/src/test/ui/error-codes/E0388.rs index 90ea8649b8b63..13131017c2e07 100644 --- a/src/test/ui/error-codes/E0388.rs +++ b/src/test/ui/error-codes/E0388.rs @@ -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() {} diff --git a/src/test/ui/error-codes/E0388.stderr b/src/test/ui/error-codes/E0388.stderr index 04b3d9542509c..f09100bac43ce 100644 --- a/src/test/ui/error-codes/E0388.stderr +++ b/src/test/ui/error-codes/E0388.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-17718-const-bad-values.stderr b/src/test/ui/issues/issue-17718-const-bad-values.stderr index b62adbd849396..7c50978d4ebb8 100644 --- a/src/test/ui/issues/issue-17718-const-bad-values.stderr +++ b/src/test/ui/issues/issue-17718-const-bad-values.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-46604.rs b/src/test/ui/issues/issue-46604.rs index 3eba1bb20bd9a..273187a5a13be 100644 --- a/src/test/ui/issues/issue-46604.rs +++ b/src/test/ui/issues/issue-46604.rs @@ -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>(buffer: T) { } fn main() { diff --git a/src/test/ui/issues/issue-46604.stderr b/src/test/ui/issues/issue-46604.stderr index b0acf9936c5c8..5421721dec2e3 100644 --- a/src/test/ui/issues/issue-46604.stderr +++ b/src/test/ui/issues/issue-46604.stderr @@ -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`.