1
Fork 0

Rollup merge of #66606 - christianpoveda:mut-refs-in-const-fn, r=oli-obk

Add feature gate for mut refs in const fn

r? @oli-obk
This commit is contained in:
Mazdak Farrokhzad 2019-12-06 23:26:54 +01:00 committed by GitHub
commit 2bd35c065c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 311 additions and 142 deletions

View file

@ -18,7 +18,6 @@ E0010: include_str!("./error_codes/E0010.md"),
E0013: include_str!("./error_codes/E0013.md"),
E0014: include_str!("./error_codes/E0014.md"),
E0015: include_str!("./error_codes/E0015.md"),
E0017: include_str!("./error_codes/E0017.md"),
E0019: include_str!("./error_codes/E0019.md"),
E0023: include_str!("./error_codes/E0023.md"),
E0025: include_str!("./error_codes/E0025.md"),

View file

@ -1,20 +0,0 @@
References in statics and constants may only refer to immutable values.
Erroneous code example:
```compile_fail,E0017
static X: i32 = 1;
const C: i32 = 2;
// these three are not allowed:
const CR: &mut i32 = &mut C;
static STATIC_REF: &'static mut i32 = &mut X;
static CONST_REF: &'static mut i32 = &mut C;
```
Statics are shared everywhere, and if they refer to mutable data one might
violate memory safety since holding multiple mutable references to shared data
is not allowed.
If you really want global mutable state, try using `static mut` or a global
`UnsafeCell`.

View file

@ -523,6 +523,9 @@ declare_features! (
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
(active, cfg_sanitize, "1.41.0", Some(39699), None),
/// Allows using `&mut` in constant functions.
(active, const_mut_refs, "1.41.0", Some(57349), None),
// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------

View file

@ -1,7 +1,6 @@
//! Concrete error types for all operations which may be invalid in a certain const context.
use rustc::hir::def_id::DefId;
use rustc::mir::BorrowKind;
use rustc::session::config::nightly_options;
use rustc::ty::TyCtxt;
use syntax::feature_gate::feature_err;
@ -181,38 +180,53 @@ impl NonConstOp for Loop {
}
#[derive(Debug)]
pub struct MutBorrow(pub BorrowKind);
impl NonConstOp for MutBorrow {
pub struct CellBorrow;
impl NonConstOp for CellBorrow {
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
let kind = self.0;
if let BorrowKind::Mut { .. } = kind {
let mut err = struct_span_err!(item.tcx.sess, span, E0017,
"references in {}s may only refer \
to immutable values", item.const_kind());
err.span_label(span, format!("{}s require immutable values",
item.const_kind()));
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note("References in statics and constants may only refer \
to immutable values.\n\n\
Statics are shared everywhere, and if they refer to \
mutable data one might violate memory safety since \
holding multiple mutable references to shared data \
is not allowed.\n\n\
If you really want global mutable state, try using \
static mut or a global UnsafeCell.");
}
err.emit();
} else {
span_err!(item.tcx.sess, span, E0492,
"cannot borrow a constant which may contain \
interior mutability, create a static instead");
span_err!(item.tcx.sess, span, E0492,
"cannot borrow a constant which may contain \
interior mutability, create a static instead");
}
}
#[derive(Debug)]
pub struct MutBorrow;
impl NonConstOp for MutBorrow {
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
Some(tcx.features().const_mut_refs)
}
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
let mut err = feature_err(
&item.tcx.sess.parse_sess,
sym::const_mut_refs,
span,
&format!("references in {}s may only refer \
to immutable values", item.const_kind())
);
err.span_label(span, format!("{}s require immutable values",
item.const_kind()));
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note("References in statics and constants may only refer \
to immutable values.\n\n\
Statics are shared everywhere, and if they refer to \
mutable data one might violate memory safety since \
holding multiple mutable references to shared data \
is not allowed.\n\n\
If you really want global mutable state, try using \
static mut or a global UnsafeCell.");
}
err.emit();
}
}
#[derive(Debug)]
pub struct MutDeref;
impl NonConstOp for MutDeref {}
impl NonConstOp for MutDeref {
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
Some(tcx.features().const_mut_refs)
}
}
#[derive(Debug)]
pub struct Panic;

View file

@ -359,7 +359,11 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
};
if !is_allowed {
self.check_op(ops::MutBorrow(kind));
if let BorrowKind::Mut{ .. } = kind {
self.check_op(ops::MutBorrow);
} else {
self.check_op(ops::CellBorrow);
}
}
}
@ -384,7 +388,11 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
);
if borrowed_place_has_mut_interior {
self.check_op(ops::MutBorrow(kind));
if let BorrowKind::Mut{ .. } = kind {
self.check_op(ops::MutBorrow);
} else {
self.check_op(ops::CellBorrow);
}
}
}
@ -451,7 +459,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
}
}
}
fn visit_projection_elem(
&mut self,
place_base: &PlaceBase<'tcx>,

View file

@ -79,10 +79,14 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -
fn check_ty(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, fn_def_id: DefId) -> McfResult {
for ty in ty.walk() {
match ty.kind {
ty::Ref(_, _, hir::Mutability::Mutable) => return Err((
span,
"mutable references in const fn are unstable".into(),
)),
ty::Ref(_, _, hir::Mutability::Mutable) => {
if !tcx.features().const_mut_refs {
return Err((
span,
"mutable references in const fn are unstable".into(),
))
}
}
ty::Opaque(..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
ty::FnPtr(..) => {
if !tcx.const_fn_is_allowed_fn_ptr(fn_def_id) {

View file

@ -213,6 +213,7 @@ symbols! {
const_indexing,
const_in_array_repeat_expressions,
const_let,
const_mut_refs,
const_panic,
const_raw_ptr_deref,
const_raw_ptr_to_usize_cast,

View file

@ -6,7 +6,7 @@ const fn f(x: usize) -> usize {
let mut sum = 0;
for i in 0..x {
//~^ ERROR E0015
//~| ERROR E0017
//~| ERROR E0658
//~| ERROR E0080
//~| ERROR E0744
//~| ERROR E0019

View file

@ -1,9 +1,12 @@
error[E0017]: references in statics may only refer to immutable values
error[E0658]: references in statics may only refer to immutable values
--> $DIR/check-static-immutable-mut-slices.rs:3:37
|
LL | static TEST: &'static mut [isize] = &mut [];
| ^^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= 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 E0017`.
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,8 +1,11 @@
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/issue-65394.rs:8:13
|
LL | let r = &mut x;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= 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
@ -12,5 +15,5 @@ LL | let mut x = Vec::<i32>::new();
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0017, E0493.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.

View file

@ -1,8 +1,11 @@
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/const-multi-ref.rs:6:13
|
LL | let p = &mut a;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/const-multi-ref.rs:16:13
@ -12,5 +15,5 @@ LL | let p = &a;
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0017, E0492.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0492, E0658.
For more information about an error, try `rustc --explain E0492`.

View file

@ -0,0 +1,36 @@
// run-pass
#![feature(const_mut_refs)]
struct Foo {
x: usize
}
const fn foo() -> Foo {
Foo { x: 0 }
}
impl Foo {
const fn bar(&mut self) -> usize {
self.x = 1;
self.x
}
}
const fn baz(foo: &mut Foo) -> usize {
let x = &mut foo.x;
*x = 2;
*x
}
const fn bazz(foo: &mut Foo) -> usize {
foo.x = 3;
foo.x
}
fn main() {
let _: [(); foo().bar()] = [(); 1];
let _: [(); baz(&mut foo())] = [(); 2];
let _: [(); bazz(&mut foo())] = [(); 3];
}

View file

@ -0,0 +1,7 @@
fn main() {
foo(&mut 5);
}
const fn foo(x: &mut i32) -> i32 { //~ ERROR mutable references in const fn are unstable
*x + 1
}

View file

@ -0,0 +1,12 @@
error[E0723]: mutable references in const fn are unstable
--> $DIR/feature-gate-const_mut_refs.rs:5:14
|
LL | const fn foo(x: &mut i32) -> i32 {
| ^
|
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
= help: add `#![feature(const_fn)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0723`.

View file

@ -4,17 +4,23 @@ error[E0019]: constant function contains unimplemented expression type
LL | self.state = x;
| ^^^^^^^^^^^^^^
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/const_let_assign3.rs:16:5
|
LL | s.foo(3);
| ^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/const_let_assign3.rs:22:13
|
LL | let y = &mut x;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: constant contains unimplemented expression type
--> $DIR/const_let_assign3.rs:24:5
@ -24,5 +30,5 @@ LL | *y = 42;
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0017, E0019.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0019, E0658.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,6 +1,7 @@
// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_raw_ptr_deref)]
#![feature(const_mut_refs)]
#![deny(const_err)]
use std::cell::UnsafeCell;
@ -12,9 +13,7 @@ const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
const MUTATING_BEHIND_RAW: () = {
// Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
unsafe {
*MUTABLE_BEHIND_RAW = 99 //~ WARN skipping const checks
//~^ ERROR any use of this value will cause an error
//~^^ tried to modify constant memory
*MUTABLE_BEHIND_RAW = 99 //~ ERROR any use of this value will cause an error
}
};

View file

@ -1,30 +1,23 @@
warning: skipping const checks
--> $DIR/mutable_const.rs:9:38
--> $DIR/mutable_const.rs:10:38
|
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
| ^^^^^^^^^^^^^^^^^^^^
warning: skipping const checks
--> $DIR/mutable_const.rs:15:9
|
LL | *MUTABLE_BEHIND_RAW = 99
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: any use of this value will cause an error
--> $DIR/mutable_const.rs:15:9
--> $DIR/mutable_const.rs:16:9
|
LL | / const MUTATING_BEHIND_RAW: () = {
LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
LL | | unsafe {
LL | | *MUTABLE_BEHIND_RAW = 99
| | ^^^^^^^^^^^^^^^^^^^^^^^^ tried to modify constant memory
... |
LL | | }
LL | | };
| |__-
|
note: lint level defined here
--> $DIR/mutable_const.rs:4:9
--> $DIR/mutable_const.rs:5:9
|
LL | #![deny(const_err)]
| ^^^^^^^^^

View file

@ -1,20 +1,22 @@
// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_mut_refs)]
#![allow(const_err)]
use std::cell::UnsafeCell;
// a test demonstrating what things we could allow with a smarter const qualification
// this is fine because is not possible to mutate through an immutable reference.
static FOO: &&mut u32 = &&mut 42;
//~^ WARN: skipping const checks
// this is fine because accessing an immutable static `BAR` is equivalent to accessing `*&BAR`
// which puts the mutable reference behind an immutable one.
static BAR: &mut () = &mut ();
//~^ WARN: skipping const checks
struct Foo<T>(T);
// this is fine for the same reason as `BAR`.
static BOO: &mut Foo<()> = &mut Foo(());
//~^ WARN: skipping const checks
struct Meh {
x: &'static UnsafeCell<i32>,
@ -27,8 +29,8 @@ static MEH: Meh = Meh {
//~^ WARN: skipping const checks
};
// this is fine for the same reason as `BAR`.
static OH_YES: &mut i32 = &mut 42;
//~^ WARN: skipping const checks
fn main() {
unsafe {

View file

@ -1,35 +1,11 @@
warning: skipping const checks
--> $DIR/mutable_references.rs:8:26
|
LL | static FOO: &&mut u32 = &&mut 42;
| ^^^^^^^
warning: skipping const checks
--> $DIR/mutable_references.rs:11:23
|
LL | static BAR: &mut () = &mut ();
| ^^^^^^^
warning: skipping const checks
--> $DIR/mutable_references.rs:16:28
|
LL | static BOO: &mut Foo<()> = &mut Foo(());
| ^^^^^^^^^^^^
warning: skipping const checks
--> $DIR/mutable_references.rs:26:8
--> $DIR/mutable_references.rs:28:8
|
LL | x: &UnsafeCell::new(42),
| ^^^^^^^^^^^^^^^^^^^^
warning: skipping const checks
--> $DIR/mutable_references.rs:30:27
|
LL | static OH_YES: &mut i32 = &mut 42;
| ^^^^^^^
error[E0594]: cannot assign to `*OH_YES`, as `OH_YES` is an immutable static item
--> $DIR/mutable_references.rs:37:5
--> $DIR/mutable_references.rs:39:5
|
LL | *OH_YES = 99;
| ^^^^^^^^^^^^ cannot assign

View file

@ -0,0 +1,11 @@
// run-pass
// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_mut_refs)]
#![allow(const_err)]
static OH_YES: &mut i32 = &mut 42;
fn main() {
// Make sure `OH_YES` can be read.
assert_eq!(*OH_YES, 42);
}

View file

@ -0,0 +1,12 @@
error[E0658]: dereferencing raw pointers in constants is unstable
--> $DIR/projection_qualif.rs:11:18
|
LL | unsafe { *b = 5; }
| ^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51911
= help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,11 +1,15 @@
// revisions: stock mut_refs
#![cfg_attr(mut_refs, feature(const_mut_refs))]
use std::cell::Cell;
const FOO: &u32 = {
let mut a = 42;
{
let b: *mut u32 = &mut a; //~ ERROR may only refer to immutable values
let b: *mut u32 = &mut a; //[stock]~ ERROR may only refer to immutable values
unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
//~^ contains unimplemented expression
//[stock]~^ contains unimplemented expression
}
&{a}
};

View file

@ -1,11 +1,14 @@
error[E0017]: references in constants may only refer to immutable values
--> $DIR/projection_qualif.rs:6:27
error[E0658]: references in constants may only refer to immutable values
--> $DIR/projection_qualif.rs:10:27
|
LL | let b: *mut u32 = &mut a;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= 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:7:18
--> $DIR/projection_qualif.rs:11:18
|
LL | unsafe { *b = 5; }
| ^^^^^^
@ -14,12 +17,12 @@ LL | unsafe { *b = 5; }
= help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
error[E0019]: constant contains unimplemented expression type
--> $DIR/projection_qualif.rs:7:18
--> $DIR/projection_qualif.rs:11:18
|
LL | unsafe { *b = 5; }
| ^^^^^^
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0017, E0019, E0658.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0019, E0658.
For more information about an error, try `rustc --explain E0019`.

View file

@ -0,0 +1,9 @@
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; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tried to modify 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 E0080`.

View file

@ -1,7 +1,12 @@
// revisions: stock mut_refs
#![cfg_attr(mut_refs, feature(const_mut_refs))]
static mut STDERR_BUFFER_SPACE: u8 = 0;
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
//~^ ERROR references in statics may only refer to immutable values
//~| ERROR static contains unimplemented expression type
//[mut_refs]~^ ERROR could not evaluate static initializer
//[stock]~^^ ERROR references in statics may only refer to immutable values
//[stock]~| ERROR static contains unimplemented expression type
fn main() {}

View file

@ -1,16 +1,19 @@
error[E0017]: references in statics may only refer to immutable values
--> $DIR/static_mut_containing_mut_ref2.rs:3:46
error[E0658]: references in statics may only refer to immutable values
--> $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: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: static contains unimplemented expression type
--> $DIR/static_mut_containing_mut_ref2.rs:3:45
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
|
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0017, E0019.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0019, E0658.
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 E0017
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
const CR: &'static mut i32 = &mut C; //~ ERROR E0658
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
//~| ERROR E0019
//~| ERROR cannot borrow
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0017
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0658
fn main() {}

View file

@ -1,8 +1,11 @@
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/E0017.rs:5:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: static contains unimplemented expression type
--> $DIR/E0017.rs:6:39
@ -10,11 +13,14 @@ error[E0019]: static contains unimplemented expression type
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^
error[E0017]: references in statics may only refer to immutable values
error[E0658]: references in statics may only refer to immutable values
--> $DIR/E0017.rs:6:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0017.rs:6:39
@ -22,19 +28,25 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0017]: references in statics may only refer to immutable values
error[E0658]: references in statics may only refer to immutable values
--> $DIR/E0017.rs:9:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0017]: references in statics may only refer to immutable values
error[E0658]: references in statics may only refer to immutable values
--> $DIR/E0017.rs:10:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
| ^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0017, E0019, E0596.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0019, E0596, E0658.
For more information about an error, try `rustc --explain E0019`.

View file

@ -0,0 +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
//~| ERROR cannot borrow
//~| ERROR E0019
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
fn main() {}

View file

@ -0,0 +1,43 @@
error[E0658]: references in constants may only refer to immutable values
--> $DIR/E0388.rs:4:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0019]: static contains unimplemented expression type
--> $DIR/E0388.rs:5:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^
error[E0658]: references in statics may only refer to immutable values
--> $DIR/E0388.rs:5:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0388.rs:5:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0658]: references in statics may only refer to immutable values
--> $DIR/E0388.rs:8:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0019, E0596, E0658.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,8 +1,11 @@
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/issue-17718-const-bad-values.rs:1:34
|
LL | const C1: &'static mut [usize] = &mut [];
| ^^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0013]: constants cannot refer to statics, use a constant instead
--> $DIR/issue-17718-const-bad-values.rs:5:46
@ -10,13 +13,16 @@ error[E0013]: constants cannot refer to statics, use a constant instead
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
error[E0017]: references in constants may only refer to immutable values
error[E0658]: references in constants may only refer to immutable values
--> $DIR/issue-17718-const-bad-values.rs:5:41
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^^^^^^ constants require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0013, E0017.
Some errors have detailed explanations: E0013, E0658.
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 E0017
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0658
fn write<T: AsRef<[u8]>>(buffer: T) { }
fn main() {

View file

@ -1,8 +1,11 @@
error[E0017]: references in statics may only refer to immutable values
error[E0658]: references in statics may only refer to immutable values
--> $DIR/issue-46604.rs:1:25
|
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
| ^^^^^^^^^^^^^^^^^^^^ statics require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
--> $DIR/issue-46604.rs:6:5
@ -12,5 +15,5 @@ LL | buf[0]=2;
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0017, E0594.
For more information about an error, try `rustc --explain E0017`.
Some errors have detailed explanations: E0594, E0658.
For more information about an error, try `rustc --explain E0594`.