Permit mutable references in all const contexts

This commit is contained in:
oli 2021-01-03 18:46:20 +00:00
parent 1986b58c64
commit d118021f8b
35 changed files with 298 additions and 171 deletions

View File

@ -268,43 +268,41 @@ impl NonConstOp for CellBorrow {
}
#[derive(Debug)]
/// This op is for `&mut` borrows in the trailing expression of a constant
/// which uses the "enclosing scopes rule" to leak its locals into anonymous
/// static or const items.
pub struct MutBorrow(pub hir::BorrowKind);
impl NonConstOp for MutBorrow {
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
// Forbid everywhere except in const fn with a feature gate
if ccx.const_kind() == hir::ConstContext::ConstFn {
Status::Unstable(sym::const_mut_refs)
} else {
Status::Forbidden
match ccx.const_kind() {
// Mutable statics can handle mutable references in their final value
hir::ConstContext::Static(hir::Mutability::Mut) => Status::Allowed,
_ => Status::Forbidden,
}
}
fn importance(&self) -> DiagnosticImportance {
// If there were primary errors (like non-const function calls), do not emit further
// errors about mutable references.
DiagnosticImportance::Secondary
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
let raw = match self.0 {
hir::BorrowKind::Raw => "raw ",
hir::BorrowKind::Ref => "",
};
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", raw, ccx.const_kind()),
)
} else {
let mut err = struct_span_err!(
ccx.tcx.sess,
span,
E0764,
"{}mutable references are not allowed in {}s",
raw,
ccx.const_kind(),
);
err.span_label(span, format!("`&{}mut` is only allowed in `const fn`", raw));
err
};
let mut err = struct_span_err!(
ccx.tcx.sess,
span,
E0764,
"{}mutable references are not allowed in final value of {}s",
raw,
ccx.const_kind(),
);
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(
"References in statics and constants may only refer \
@ -321,6 +319,29 @@ impl NonConstOp for MutBorrow {
}
}
#[derive(Debug)]
pub struct TransientMutBorrow(pub hir::BorrowKind);
impl NonConstOp for TransientMutBorrow {
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
Status::Unstable(sym::const_mut_refs)
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
let raw = match self.0 {
hir::BorrowKind::Raw => "raw ",
hir::BorrowKind::Ref => "",
};
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_mut_refs,
span,
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
)
}
}
#[derive(Debug)]
pub struct MutDeref;
impl NonConstOp for MutDeref {
@ -329,7 +350,7 @@ impl NonConstOp for MutDeref {
}
fn importance(&self) -> DiagnosticImportance {
// Usually a side-effect of a `MutBorrow` somewhere.
// Usually a side-effect of a `TransientMutBorrow` somewhere.
DiagnosticImportance::Secondary
}

View File

@ -466,6 +466,29 @@ impl Validator<'mir, 'tcx> {
}
}
}
fn check_mut_borrow(&mut self, local: Local, kind: hir::BorrowKind) {
match self.const_kind() {
// In a const fn all borrows are transient or point to the places given via
// references in the arguments (so we already checked them with
// TransientMutBorrow/MutBorrow as appropriate).
// The borrow checker guarantees that no new non-transient borrows are created.
// NOTE: Once we have heap allocations during CTFE we need to figure out
// how to prevent `const fn` to create long-lived allocations that point
// to mutable memory.
hir::ConstContext::ConstFn => self.check_op(ops::TransientMutBorrow(kind)),
_ => {
// Locals with StorageDead do not live beyond the evaluation and can
// thus safely be borrowed without being able to be leaked to the final
// value of the constant.
if self.local_has_storage_dead(local) {
self.check_op(ops::TransientMutBorrow(kind));
} else {
self.check_op(ops::MutBorrow(kind));
}
}
}
}
}
impl Visitor<'tcx> for Validator<'mir, 'tcx> {
@ -562,15 +585,15 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
if !is_allowed {
if let BorrowKind::Mut { .. } = kind {
self.check_op(ops::MutBorrow(hir::BorrowKind::Ref));
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
} else {
self.check_op(ops::CellBorrow);
}
}
}
Rvalue::AddressOf(Mutability::Mut, _) => {
self.check_op(ops::MutBorrow(hir::BorrowKind::Raw))
Rvalue::AddressOf(Mutability::Mut, ref place) => {
self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
}
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)

View File

@ -1,6 +1,6 @@
// Checks that immutable static items can't have mutable slices
static TEST: &'static mut [isize] = &mut [];
//~^ ERROR mutable references are not allowed in statics
//~^ ERROR mutable references are not allowed
pub fn main() { }

View File

@ -1,8 +1,8 @@
error[E0764]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/check-static-immutable-mut-slices.rs:3:37
|
LL | static TEST: &'static mut [isize] = &mut [];
| ^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^
error: aborting due to previous error

View File

@ -1,20 +1,29 @@
error[E0764]: raw mutable references are not allowed in constants
error[E0658]: raw mutable references are not allowed in constants
--> $DIR/const-address-of-mut.rs:3:32
|
LL | const A: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
| ^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: raw mutable references are not allowed in statics
error[E0658]: raw mutable references are not allowed in statics
--> $DIR/const-address-of-mut.rs:5:33
|
LL | static B: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
| ^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: raw mutable references are not allowed in statics
error[E0658]: raw mutable references are not allowed in statics
--> $DIR/const-address-of-mut.rs:7:37
|
LL | static mut C: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
| ^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: raw mutable references are not allowed in constant functions
--> $DIR/const-address-of-mut.rs:11:13
@ -27,5 +36,4 @@ LL | let y = &raw mut x;
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,8 +1,11 @@
error[E0764]: mutable references are not allowed in constants
error[E0658]: mutable references are not allowed in constants
--> $DIR/issue-65394.rs:8:13
|
LL | let r = &mut x;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0493]: destructors cannot be evaluated at compile-time
--> $DIR/issue-65394.rs:7:9
@ -15,5 +18,5 @@ LL | };
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0493, E0764.
Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.

View File

@ -1,8 +1,11 @@
error[E0764]: mutable references are not allowed in constants
error[E0658]: mutable references are not allowed in constants
--> $DIR/const-multi-ref.rs:6:13
|
LL | let p = &mut a;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
--> $DIR/const-multi-ref.rs:16:13
@ -15,5 +18,4 @@ LL | let p = &a;
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,3 +1,4 @@
// check-pass
#![feature(const_mut_refs)]
#![feature(const_fn)]
#![feature(raw_ref_op)]
@ -22,9 +23,7 @@ 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() {}

View File

@ -1,15 +0,0 @@
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`.

View File

@ -1,3 +1,4 @@
// check-pass
#![feature(const_mut_refs)]
struct Foo {
@ -29,9 +30,6 @@ 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
}

View File

@ -1,21 +0,0 @@
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`.

View File

@ -0,0 +1,24 @@
#![feature(const_mut_refs)]
#![feature(const_fn)]
#![feature(raw_ref_op)]
const NULL: *mut i32 = std::ptr::null_mut();
const A: *const i32 = &4;
// It could be made sound to allow it to compile,
// but we do not want to allow this to compile,
// as that would be an enormous footgun in oli-obk's opinion.
const B: *mut i32 = &mut 4; //~ ERROR mutable references are not allowed
// Could be ok, but the same analysis that prevents the mutable one above will also bail out here
// Using a block with some complex content, because just `&45` would get promoted,
// which is not what we want to test here.
const C: *const i32 = &{
let mut x = 42;
x += 3;
x
};
fn main() {
println!("{}", unsafe { *A });
unsafe { *B = 4 } // Bad news
}

View File

@ -0,0 +1,9 @@
error[E0764]: mutable references are not allowed in final value of constants
--> $DIR/mut_ref_in_final.rs:10:21
|
LL | const B: *mut i32 = &mut 4;
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0764`.

View File

@ -0,0 +1,22 @@
#![feature(const_mut_refs)]
#![feature(const_fn)]
#![feature(raw_ref_op)]
use std::cell::UnsafeCell;
struct NotAMutex<T>(UnsafeCell<T>);
unsafe impl<T> Sync for NotAMutex<T> {}
const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
//~^ ERROR temporary value dropped while borrowed
// `BAR` works, because `&42` promotes immediately instead of relying on
// "final value lifetime extension".
const BAR: NotAMutex<&i32> = NotAMutex(UnsafeCell::new(&42));
fn main() {
unsafe {
**FOO.0.get() = 99;
assert_eq!(**FOO.0.get(), 99);
}
}

View File

@ -0,0 +1,13 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/mut_ref_in_final_ok.rs:10:65
|
LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
| -------------------------------^^--
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary which is freed while still in use
| using this value as a constant requires that borrow lasts for `'static`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0716`.

View File

@ -7,19 +7,24 @@ LL | const fn foo(&mut self, x: u32) {
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: mutable references are not allowed in constants
error[E0658]: 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`
| ^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: mutable references are not allowed in constants
error[E0658]: mutable references are not allowed in constants
--> $DIR/const_let_assign3.rs:22:13
|
LL | let y = &mut x;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,10 +1,9 @@
const C1: &'static mut [usize] = &mut [];
//~^ ERROR: mutable references are not allowed in constants
//~^ ERROR: mutable references are not allowed
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: mutable references are not allowed in constants
fn main() {}

View File

@ -1,8 +1,8 @@
error[E0764]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in final value of constants
--> $DIR/issue-17718-const-bad-values.rs:1:34
|
LL | const C1: &'static mut [usize] = &mut [];
| ^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-const-bad-values.rs:5:46
@ -20,13 +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[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 };
| ^^^^^^ `&mut` is only allowed in `const fn`
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0013, E0764.
For more information about an error, try `rustc --explain E0013`.

View File

@ -1,9 +1,3 @@
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
|
@ -13,7 +7,6 @@ LL | unsafe { *b = 5; }
= note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information
= help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
error: aborting due to 2 previous errors
error: aborting due to previous error
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -7,7 +7,7 @@ use std::cell::Cell;
const FOO: &u32 = {
let mut a = 42;
{
let b: *mut u32 = &mut a; //~ ERROR mutable references are not allowed in constants
let b: *mut u32 = &mut a; //[stock]~ ERROR mutable references are not allowed in constants
unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
}
&{a}

View File

@ -1,8 +1,11 @@
error[E0764]: mutable references are not allowed in constants
error[E0658]: 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`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: dereferencing raw pointers in constants is unstable
--> $DIR/projection_qualif.rs:11:18
@ -15,5 +18,4 @@ LL | unsafe { *b = 5; }
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,9 +1,8 @@
// 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_NO: &mut i32 = &mut 42;
//~^ ERROR mutable references are not allowed in statics
static OH_NO: &mut i32 = &mut 42; //~ ERROR mutable references are not allowed
fn main() {
assert_eq!(*OH_NO, 42);
*OH_NO = 43; //~ ERROR cannot assign to `*OH_NO`, as `OH_NO` is an immutable static
}

View File

@ -1,9 +1,16 @@
error[E0764]: mutable references are not allowed in statics
--> $DIR/read_from_static_mut_ref.rs:5:26
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/read_from_static_mut_ref.rs:4:26
|
LL | static OH_NO: &mut i32 = &mut 42;
| ^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^
error: aborting due to previous error
error[E0594]: cannot assign to `*OH_NO`, as `OH_NO` is an immutable static item
--> $DIR/read_from_static_mut_ref.rs:7:5
|
LL | *OH_NO = 43;
| ^^^^^^^^^^^ cannot assign
For more information about this error, try `rustc --explain E0764`.
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0594, E0764.
For more information about an error, try `rustc --explain E0594`.

View File

@ -1,9 +1,9 @@
error[E0764]: mutable references are not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
error[E0080]: could not evaluate static initializer
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
|
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
error: aborting due to previous error
For more information about this error, try `rustc --explain E0764`.
For more information about this error, try `rustc --explain E0080`.

View File

@ -5,6 +5,7 @@
static mut STDERR_BUFFER_SPACE: u8 = 0;
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
//~^ ERROR mutable references are not allowed in statics
//[mut_refs]~^ ERROR could not evaluate static initializer
//[stock]~^^ ERROR mutable references are not allowed in statics
fn main() {}

View File

@ -1,9 +1,12 @@
error[E0764]: mutable references are not allowed in statics
error[E0658]: 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; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0764`.
For more information about this error, try `rustc --explain E0658`.

View File

@ -2,12 +2,13 @@ static X: i32 = 1;
const C: i32 = 2;
static mut M: i32 = 3;
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
//~| WARN taking a mutable
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
//~| ERROR cannot borrow
//~| ERROR mutable references are not allowed
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
//~| WARN taking a mutable
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR mutable references are not
fn main() {}

View File

@ -13,17 +13,26 @@ note: `const` item defined here
LL | const C: i32 = 2;
| ^^^^^^^^^^^^^^^^^
error[E0764]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in final value of constants
--> $DIR/E0017.rs:5:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
error[E0764]: mutable references are not allowed in statics
error[E0658]: mutation through a reference is not allowed in statics
--> $DIR/E0017.rs:7:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/E0017.rs:7:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0017.rs:7:39
@ -32,7 +41,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
warning: taking a mutable reference to a `const` item
--> $DIR/E0017.rs:10:38
--> $DIR/E0017.rs:11:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^
@ -45,19 +54,19 @@ note: `const` item defined here
LL | const C: i32 = 2;
| ^^^^^^^^^^^^^^^^^
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:10:38
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/E0017.rs:11:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:12:52
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/E0017.rs:13:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
error: aborting due to 5 previous errors; 2 warnings emitted
error: aborting due to 6 previous errors; 2 warnings emitted
Some errors have detailed explanations: E0596, E0764.
Some errors have detailed explanations: E0596, E0658, E0764.
For more information about an error, try `rustc --explain E0596`.

View File

@ -1,12 +1,13 @@
static X: i32 = 1;
const C: i32 = 2;
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
//~| WARN taking a mutable
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR cannot borrow
//~| ERROR E0764
//~| ERROR E0658
//~| ERROR mutable references are not allowed
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed
//~| WARN taking a mutable
fn main() {}

View File

@ -13,17 +13,26 @@ note: `const` item defined here
LL | const C: i32 = 2;
| ^^^^^^^^^^^^^^^^^
error[E0764]: mutable references are not allowed in constants
error[E0764]: mutable references are not allowed in final value of constants
--> $DIR/E0388.rs:4:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
error[E0764]: mutable references are not allowed in statics
error[E0658]: mutation through a reference is not allowed in statics
--> $DIR/E0388.rs:6:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/E0388.rs:6:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0388.rs:6:39
@ -32,7 +41,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
warning: taking a mutable reference to a `const` item
--> $DIR/E0388.rs:9:38
--> $DIR/E0388.rs:10:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^
@ -45,13 +54,13 @@ note: `const` item defined here
LL | const C: i32 = 2;
| ^^^^^^^^^^^^^^^^^
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0388.rs:9:38
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/E0388.rs:10:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^
error: aborting due to 4 previous errors; 2 warnings emitted
error: aborting due to 5 previous errors; 2 warnings emitted
Some errors have detailed explanations: E0596, E0764.
Some errors have detailed explanations: E0596, E0658, E0764.
For more information about an error, try `rustc --explain E0596`.

View File

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

View File

@ -1,8 +1,8 @@
error[E0764]: mutable references are not allowed in statics
error[E0764]: mutable references are not allowed in final value of statics
--> $DIR/issue-46604.rs:1:25
|
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
| ^^^^^^^^^^^^^^^^^^^^ `&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

View File

@ -39,11 +39,14 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | [(); { for _ in 0usize.. {}; 0}];
| ^^^^^^^^
error[E0764]: mutable references are not allowed in constants
error[E0658]: mutable references are not allowed in constants
--> $DIR/issue-52443.rs:9:21
|
LL | [(); { for _ in 0usize.. {}; 0}];
| ^^^^^^^^ `&mut` is only allowed in `const fn`
| ^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-52443.rs:9:21
@ -53,5 +56,5 @@ LL | [(); { for _ in 0usize.. {}; 0}];
error: aborting due to 6 previous errors; 1 warning emitted
Some errors have detailed explanations: E0015, E0308, E0744, E0764.
Some errors have detailed explanations: E0015, E0308, E0658, E0744.
For more information about an error, try `rustc --explain E0015`.

View File

@ -18,3 +18,9 @@ const fn bar() -> NonZero<u32> {
let y = unsafe { &mut x.0 }; //~ ERROR mutable references
unsafe { NonZero(1) }
}
const fn boo() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
unsafe { let y = &mut x.0; } //~ ERROR mutable references
unsafe { NonZero(1) }
}

View File

@ -16,6 +16,15 @@ LL | let y = unsafe { &mut x.0 };
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/ranged_ints2_const.rs:24:22
|
LL | unsafe { let y = &mut x.0; }
| ^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints2_const.rs:11:13
|
@ -24,7 +33,7 @@ LL | let y = &mut x.0;
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to 3 previous errors
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0133, E0658.
For more information about an error, try `rustc --explain E0133`.