diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index 997762efcb3e..738b3bc7539a 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 000000000000..e9061f988ac5 --- /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 d5059c98c951..733ae9084511 100644 --- a/src/librustc_mir/transform/check_consts/ops.rs +++ b/src/librustc_mir/transform/check_consts/ops.rs @@ -205,22 +205,34 @@ impl NonConstOp for CellBorrow { #[derive(Debug)] pub struct MutBorrow; impl NonConstOp for MutBorrow { + fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool { + // Forbid everywhere except in const fn + ccx.const_kind() == hir::ConstContext::ConstFn + && ccx.tcx.features().enabled(Self::feature_gate().unwrap()) + } + fn feature_gate() -> Option { Some(sym::const_mut_refs) } fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { - let mut err = feature_err( - &ccx.tcx.sess.parse_sess, - sym::const_mut_refs, - span, - &format!( - "references in {}s may only refer \ - to immutable values", - ccx.const_kind() - ), - ); - err.span_label(span, format!("{}s require immutable values", ccx.const_kind())); + let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn { + feature_err( + &ccx.tcx.sess.parse_sess, + sym::const_mut_refs, + span, + &format!("mutable references are not allowed in {}s", ccx.const_kind()), + ) + } else { + struct_span_err!( + ccx.tcx.sess, + span, + E0764, + "mutable references are not allowed in {}s", + ccx.const_kind(), + ) + }; + err.span_label(span, "`&mut` is only allowed in `const fn`".to_string()); if ccx.tcx.sess.teach(&err.get_code().unwrap()) { err.note( "References in statics and constants may only refer \ diff --git a/src/test/compile-fail/issue-52443.rs b/src/test/compile-fail/issue-52443.rs index 976195933527..00aca1d14baa 100644 --- a/src/test/compile-fail/issue-52443.rs +++ b/src/test/compile-fail/issue-52443.rs @@ -9,7 +9,7 @@ fn main() { [(); { for _ in 0usize.. {}; 0}]; //~^ ERROR `for` is not allowed in a `const` //~| ERROR calls in constants are limited to constant functions - //~| ERROR references in constants may only refer to immutable values + //~| ERROR mutable references are not allowed in constants //~| ERROR calls in constants are limited to constant functions //~| ERROR evaluation of constant value failed } diff --git a/src/test/ui/check-static-immutable-mut-slices.rs b/src/test/ui/check-static-immutable-mut-slices.rs index d5e9fb2dede9..3be02f6a0f67 100644 --- a/src/test/ui/check-static-immutable-mut-slices.rs +++ b/src/test/ui/check-static-immutable-mut-slices.rs @@ -1,6 +1,6 @@ // Checks that immutable static items can't have mutable slices static TEST: &'static mut [isize] = &mut []; -//~^ ERROR references in statics may only refer to immutable values +//~^ ERROR mutable references are not allowed in statics pub fn main() { } diff --git a/src/test/ui/check-static-immutable-mut-slices.stderr b/src/test/ui/check-static-immutable-mut-slices.stderr index 66fe8646e101..9ffbb483d139 100644 --- a/src/test/ui/check-static-immutable-mut-slices.stderr +++ b/src/test/ui/check-static-immutable-mut-slices.stderr @@ -1,12 +1,9 @@ -error[E0658]: references in statics may only refer to immutable values +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 []; - | ^^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^^ `&mut` is only allowed in `const fn` error: aborting due to previous error -For more information about this error, try `rustc --explain E0658`. +For more information about this error, try `rustc --explain E0764`. diff --git a/src/test/ui/consts/const-eval/issue-65394.rs b/src/test/ui/consts/const-eval/issue-65394.rs index b1c058eac9e4..2518e4ed40b3 100644 --- a/src/test/ui/consts/const-eval/issue-65394.rs +++ b/src/test/ui/consts/const-eval/issue-65394.rs @@ -5,7 +5,7 @@ const _: Vec = { let mut x = Vec::::new(); //~ ERROR destructors cannot be evaluated at compile-time - let r = &mut x; //~ ERROR references in constants may only refer to immutable values + let r = &mut x; //~ ERROR mutable references are not allowed in constants let y = x; y }; diff --git a/src/test/ui/consts/const-eval/issue-65394.stderr b/src/test/ui/consts/const-eval/issue-65394.stderr index d85a1a1a3c32..f843a94fabd8 100644 --- a/src/test/ui/consts/const-eval/issue-65394.stderr +++ b/src/test/ui/consts/const-eval/issue-65394.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/issue-65394.rs:8:13 | LL | let r = &mut x; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0493]: destructors cannot be evaluated at compile-time --> $DIR/issue-65394.rs:7:9 @@ -15,5 +12,5 @@ LL | let mut x = Vec::::new(); error: aborting due to 2 previous errors -Some errors have detailed explanations: E0493, E0658. +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.rs b/src/test/ui/consts/const-multi-ref.rs index 5e2be0d4f3f0..18645efc8871 100644 --- a/src/test/ui/consts/const-multi-ref.rs +++ b/src/test/ui/consts/const-multi-ref.rs @@ -3,7 +3,7 @@ const _: i32 = { let mut a = 5; - let p = &mut a; //~ ERROR references in constants may only refer to immutable values + let p = &mut a; //~ ERROR mutable references are not allowed in constants let reborrow = {p}; let pp = &reborrow; diff --git a/src/test/ui/consts/const-multi-ref.stderr b/src/test/ui/consts/const-multi-ref.stderr index e01dd4e57473..9a7914b45887 100644 --- a/src/test/ui/consts/const-multi-ref.stderr +++ b/src/test/ui/consts/const-multi-ref.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/const-multi-ref.rs:6:13 | LL | let p = &mut a; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead --> $DIR/const-multi-ref.rs:16:13 @@ -15,5 +12,5 @@ LL | let p = &a; error: aborting due to 2 previous errors -Some errors have detailed explanations: E0492, E0658. +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.rs b/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs index 130ba9283b1d..5819daa817af 100644 --- a/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs +++ b/src/test/ui/consts/const-mut-refs/const_mut_address_of.rs @@ -1,5 +1,3 @@ -// check-pass - #![feature(const_mut_refs)] #![feature(const_fn)] #![feature(raw_ref_op)] @@ -24,7 +22,9 @@ const fn baz(foo: &mut Foo)-> *mut usize { const _: () = { foo().bar(); + //~^ ERROR mutable references are not allowed in constants baz(&mut foo()); + //~^ ERROR mutable references are not allowed in constants }; fn main() {} 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 new file mode 100644 index 000000000000..2214ce6ee1c8 --- /dev/null +++ b/src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr @@ -0,0 +1,15 @@ +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[E0764]: mutable references are not allowed in constants + --> $DIR/const_mut_address_of.rs:26:9 + | +LL | baz(&mut foo()); + | ^^^^^^^^^^ `&mut` is only allowed in `const fn` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0764`. diff --git a/src/test/ui/consts/const-mut-refs/const_mut_refs.rs b/src/test/ui/consts/const-mut-refs/const_mut_refs.rs index 99006a20b1bc..9099d5a1b8ea 100644 --- a/src/test/ui/consts/const-mut-refs/const_mut_refs.rs +++ b/src/test/ui/consts/const-mut-refs/const_mut_refs.rs @@ -1,5 +1,3 @@ -// run-pass - #![feature(const_mut_refs)] struct Foo { @@ -31,6 +29,9 @@ const fn bazz(foo: &mut Foo) -> usize { fn main() { let _: [(); foo().bar()] = [(); 1]; + //~^ ERROR mutable references are not allowed in constants let _: [(); baz(&mut foo())] = [(); 2]; + //~^ ERROR mutable references are not allowed in constants let _: [(); bazz(&mut foo())] = [(); 3]; + //~^ ERROR mutable references are not allowed in constants } 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 new file mode 100644 index 000000000000..4ca7b128b7c4 --- /dev/null +++ b/src/test/ui/consts/const-mut-refs/const_mut_refs.stderr @@ -0,0 +1,21 @@ +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[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[E0764]: mutable references are not allowed in constants + --> $DIR/const_mut_refs.rs:35:22 + | +LL | let _: [(); bazz(&mut foo())] = [(); 3]; + | ^^^^^^^^^^ `&mut` is only allowed in `const fn` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0764`. diff --git a/src/test/ui/consts/const_let_assign3.rs b/src/test/ui/consts/const_let_assign3.rs index cbe73923e9c4..f993a427b489 100644 --- a/src/test/ui/consts/const_let_assign3.rs +++ b/src/test/ui/consts/const_let_assign3.rs @@ -13,14 +13,14 @@ impl S { const FOO: S = { let mut s = S { state: 42 }; - s.foo(3); //~ ERROR references in constants may only refer to immutable values + s.foo(3); //~ ERROR mutable references are not allowed in constants s }; type Array = [u32; { let mut x = 2; let y = &mut x; -//~^ ERROR references in constants may only refer to immutable values +//~^ ERROR mutable references are not allowed in constants *y = 42; //~^ ERROR constant contains unimplemented expression type *y diff --git a/src/test/ui/consts/const_let_assign3.stderr b/src/test/ui/consts/const_let_assign3.stderr index 62fd04ea522c..dd05a4c0bb06 100644 --- a/src/test/ui/consts/const_let_assign3.stderr +++ b/src/test/ui/consts/const_let_assign3.stderr @@ -6,23 +6,17 @@ LL | self.state = x; | = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/const_let_assign3.rs:16:5 | LL | s.foo(3); - | ^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^ `&mut` is only allowed in `const fn` -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/const_let_assign3.rs:22:13 | LL | let y = &mut x; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0019]: constant contains unimplemented expression type --> $DIR/const_let_assign3.rs:24:5 @@ -34,5 +28,5 @@ LL | *y = 42; error: aborting due to 4 previous errors -Some errors have detailed explanations: E0019, E0658. +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 0945a23f3b12..fad8f011f75f 100644 --- a/src/test/ui/consts/projection_qualif.mut_refs.stderr +++ b/src/test/ui/consts/projection_qualif.mut_refs.stderr @@ -1,3 +1,9 @@ +error[E0764]: mutable references are not allowed in constants + --> $DIR/projection_qualif.rs:10:27 + | +LL | let b: *mut u32 = &mut a; + | ^^^^^^ `&mut` is only allowed in `const fn` + error[E0658]: dereferencing raw pointers in constants is unstable --> $DIR/projection_qualif.rs:11:18 | @@ -7,6 +13,7 @@ LL | unsafe { *b = 5; } = note: see issue #51911 for more information = help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable -error: aborting due to previous error +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0658`. +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.rs b/src/test/ui/consts/projection_qualif.rs index cfe8e7f03d5e..7db970cf1379 100644 --- a/src/test/ui/consts/projection_qualif.rs +++ b/src/test/ui/consts/projection_qualif.rs @@ -7,7 +7,7 @@ use std::cell::Cell; const FOO: &u32 = { let mut a = 42; { - let b: *mut u32 = &mut a; //[stock]~ ERROR may only refer to immutable values + let b: *mut u32 = &mut a; //~ ERROR mutable references are not allowed in constants unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants //[stock]~^ contains unimplemented expression } diff --git a/src/test/ui/consts/projection_qualif.stock.stderr b/src/test/ui/consts/projection_qualif.stock.stderr index cfa48d947c99..212f12286455 100644 --- a/src/test/ui/consts/projection_qualif.stock.stderr +++ b/src/test/ui/consts/projection_qualif.stock.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/projection_qualif.rs:10:27 | LL | let b: *mut u32 = &mut a; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0658]: dereferencing raw pointers in constants is unstable --> $DIR/projection_qualif.rs:11:18 @@ -26,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.rs b/src/test/ui/consts/read_from_static_mut_ref.rs index c18227e0f551..5faa983ab09f 100644 --- a/src/test/ui/consts/read_from_static_mut_ref.rs +++ b/src/test/ui/consts/read_from_static_mut_ref.rs @@ -1,10 +1,9 @@ -// run-pass +// We are keeping this test in case we decide to allow mutable references in statics again #![feature(const_mut_refs)] #![allow(const_err)] -static OH_YES: &mut i32 = &mut 42; - +static OH_NO: &mut i32 = &mut 42; +//~^ ERROR mutable references are not allowed in statics fn main() { - // Make sure `OH_YES` can be read. - assert_eq!(*OH_YES, 42); + assert_eq!(*OH_NO, 42); } diff --git a/src/test/ui/consts/read_from_static_mut_ref.stderr b/src/test/ui/consts/read_from_static_mut_ref.stderr new file mode 100644 index 000000000000..c936ac0b7d58 --- /dev/null +++ b/src/test/ui/consts/read_from_static_mut_ref.stderr @@ -0,0 +1,9 @@ +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; + | ^^^^^^^ `&mut` is only allowed in `const fn` + +error: aborting due to previous error + +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 8db75dd63cf2..36c280ca5c60 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,9 +1,9 @@ -error[E0080]: could not evaluate static initializer - --> $DIR/static_mut_containing_mut_ref2.rs:7:45 +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; }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn` error: aborting due to previous error -For more information about this error, try `rustc --explain E0080`. +For more information about this error, try `rustc --explain E0764`. diff --git a/src/test/ui/consts/static_mut_containing_mut_ref2.rs b/src/test/ui/consts/static_mut_containing_mut_ref2.rs index 74162fbd54b0..a6bbe8d6ec24 100644 --- a/src/test/ui/consts/static_mut_containing_mut_ref2.rs +++ b/src/test/ui/consts/static_mut_containing_mut_ref2.rs @@ -5,8 +5,7 @@ static mut STDERR_BUFFER_SPACE: u8 = 0; pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; }; -//[mut_refs]~^ ERROR could not evaluate static initializer -//[stock]~^^ ERROR references in statics may only refer to immutable values +//~^ ERROR mutable references are not allowed in statics //[stock]~| ERROR static contains unimplemented expression type fn main() {} 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 cc169351bf26..57fb27e642e6 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,11 +1,8 @@ -error[E0658]: references in statics may only refer to immutable values +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; }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn` error[E0019]: static contains unimplemented expression type --> $DIR/static_mut_containing_mut_ref2.rs:7:45 @@ -17,5 +14,5 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4 error: aborting due to 2 previous errors -Some errors have detailed explanations: E0019, E0658. +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 64be41170d0c..818dec1207b9 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 E0658 -static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658 +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 E0658 -static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0658 +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 f959ad0d0088..c1d96de1dca7 100644 --- a/src/test/ui/error-codes/E0017.stderr +++ b/src/test/ui/error-codes/E0017.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/E0017.rs:5:30 | LL | const CR: &'static mut i32 = &mut C; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0019]: static contains unimplemented expression type --> $DIR/E0017.rs:6:39 @@ -15,14 +12,11 @@ LL | static STATIC_REF: &'static mut i32 = &mut X; | = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable -error[E0658]: references in statics may only refer to immutable values +error[E0764]: mutable references are not allowed in statics --> $DIR/E0017.rs:6:39 | LL | static STATIC_REF: &'static mut i32 = &mut X; - | ^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0596]: cannot borrow immutable static item `X` as mutable --> $DIR/E0017.rs:6:39 @@ -30,25 +24,19 @@ error[E0596]: cannot borrow immutable static item `X` as mutable LL | static STATIC_REF: &'static mut i32 = &mut X; | ^^^^^^ cannot borrow as mutable -error[E0658]: references in statics may only refer to immutable values +error[E0764]: mutable references are not allowed in statics --> $DIR/E0017.rs:9:38 | LL | static CONST_REF: &'static mut i32 = &mut C; - | ^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` -error[E0658]: references in statics may only refer to immutable values +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 }; - | ^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error: aborting due to 6 previous errors -Some errors have detailed explanations: E0019, E0596, E0658. +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 5954e3490b06..13131017c2e0 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 E0658 -static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658 +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 E0658 + //~| 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 8bdfbac36816..f09100bac43c 100644 --- a/src/test/ui/error-codes/E0388.stderr +++ b/src/test/ui/error-codes/E0388.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +error[E0764]: mutable references are not allowed in constants --> $DIR/E0388.rs:4:30 | LL | const CR: &'static mut i32 = &mut C; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0019]: static contains unimplemented expression type --> $DIR/E0388.rs:5:39 @@ -15,14 +12,11 @@ LL | static STATIC_REF: &'static mut i32 = &mut X; | = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable -error[E0658]: references in statics may only refer to immutable values +error[E0764]: mutable references are not allowed in statics --> $DIR/E0388.rs:5:39 | LL | static STATIC_REF: &'static mut i32 = &mut X; - | ^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error[E0596]: cannot borrow immutable static item `X` as mutable --> $DIR/E0388.rs:5:39 @@ -30,16 +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[E0658]: references in statics may only refer to immutable values +error[E0764]: mutable references are not allowed in statics --> $DIR/E0388.rs:8:38 | LL | static CONST_REF: &'static mut i32 = &mut C; - | ^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error: aborting due to 5 previous errors -Some errors have detailed explanations: E0019, E0596, E0658. +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.rs b/src/test/ui/issues/issue-17718-const-bad-values.rs index 9355c8ab1525..49023f18ddbf 100644 --- a/src/test/ui/issues/issue-17718-const-bad-values.rs +++ b/src/test/ui/issues/issue-17718-const-bad-values.rs @@ -1,10 +1,10 @@ const C1: &'static mut [usize] = &mut []; -//~^ ERROR: references in constants may only refer to immutable values +//~^ ERROR: mutable references are not allowed in constants static mut S: usize = 3; const C2: &'static mut usize = unsafe { &mut S }; //~^ ERROR: constants cannot refer to statics //~| ERROR: constants cannot refer to statics -//~| ERROR: references in constants may only refer to immutable values +//~| ERROR: mutable references are not allowed in constants fn main() {} 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 688efcdd022e..7c50978d4ebb 100644 --- a/src/test/ui/issues/issue-17718-const-bad-values.stderr +++ b/src/test/ui/issues/issue-17718-const-bad-values.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in constants may only refer to immutable values +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 []; - | ^^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^^ `&mut` is only allowed in `const fn` error[E0013]: constants cannot refer to statics --> $DIR/issue-17718-const-bad-values.rs:5:46 @@ -23,16 +20,13 @@ 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[E0658]: references in constants may only refer to immutable values +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 }; - | ^^^^^^ constants require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^ `&mut` is only allowed in `const fn` error: aborting due to 4 previous errors -Some errors have detailed explanations: E0013, E0658. +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 e1967eb76554..273187a5a13b 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 E0658 +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 771e368a35d9..5421721dec2e 100644 --- a/src/test/ui/issues/issue-46604.stderr +++ b/src/test/ui/issues/issue-46604.stderr @@ -1,11 +1,8 @@ -error[E0658]: references in statics may only refer to immutable values +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]; - | ^^^^^^^^^^^^^^^^^^^^ statics require immutable values - | - = note: see issue #57349 for more information - = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable + | ^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn` error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item --> $DIR/issue-46604.rs:6:5 @@ -15,5 +12,5 @@ LL | buf[0]=2; error: aborting due to 2 previous errors -Some errors have detailed explanations: E0594, E0658. +Some errors have detailed explanations: E0594, E0764. For more information about an error, try `rustc --explain E0594`.