Skip to content

Commit

Permalink
add new error code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Jun 19, 2020
1 parent 1f48465 commit 96031e2
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/librustc_error_codes/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/librustc_error_codes/error_codes/E0764.md
Original file line number Diff line number Diff line change
@@ -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!
}
```
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/check-static-immutable-mut-slices.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 [];
| ^^^^^^^ `&mut` is only allowed in `const fn`

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`.
6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/issue-65394.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,5 +12,5 @@ LL | let mut x = Vec::<i32>::new();

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0019, E0493.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0493, E0764.
For more information about an error, try `rustc --explain E0493`.
6 changes: 3 additions & 3 deletions src/test/ui/consts/const-multi-ref.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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`.
6 changes: 3 additions & 3 deletions src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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());
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`

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`.
8 changes: 4 additions & 4 deletions src/test/ui/consts/const-mut-refs/const_mut_refs.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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];
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`

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`.
7 changes: 4 additions & 3 deletions src/test/ui/consts/const_let_assign3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`.
6 changes: 3 additions & 3 deletions src/test/ui/consts/projection_qualif.mut_refs.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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`.
4 changes: 2 additions & 2 deletions src/test/ui/consts/projection_qualif.stock.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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`.
4 changes: 2 additions & 2 deletions src/test/ui/consts/read_from_static_mut_ref.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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;
| ^^^^^^^ `&mut` is only allowed in `const fn`

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`.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`

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`.
Original file line number Diff line number Diff line change
@@ -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; };
Expand All @@ -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`.
8 changes: 4 additions & 4 deletions src/test/ui/error-codes/E0017.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
10 changes: 5 additions & 5 deletions src/test/ui/error-codes/E0017.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -24,19 +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[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 };
| ^^^^^^ `&mut` is only allowed in `const fn`

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`.
6 changes: 3 additions & 3 deletions src/test/ui/error-codes/E0388.rs
Original file line number Diff line number Diff line change
@@ -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() {}
8 changes: 4 additions & 4 deletions src/test/ui/error-codes/E0388.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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/E0388.rs:8:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ `&mut` is only allowed in `const fn`

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`.
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-17718-const-bad-values.stderr
Original file line number Diff line number Diff line change
@@ -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 [];
Expand All @@ -20,13 +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[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 };
| ^^^^^^ `&mut` is only allowed in `const fn`

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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-46604.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0019
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0764
fn write<T: AsRef<[u8]>>(buffer: T) { }

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-46604.stderr
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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`.

0 comments on commit 96031e2

Please sign in to comment.