Skip to content

Commit 96031e2

Browse files
committed
add new error code
1 parent 1f48465 commit 96031e2

21 files changed

+95
-53
lines changed

src/librustc_error_codes/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ E0760: include_str!("./error_codes/E0760.md"),
444444
E0761: include_str!("./error_codes/E0761.md"),
445445
E0762: include_str!("./error_codes/E0762.md"),
446446
E0763: include_str!("./error_codes/E0763.md"),
447+
E0764: include_str!("./error_codes/E0764.md"),
447448
;
448449
// E0006, // merged with E0005
449450
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Mutable references (`&mut`) can only be used in constant functions, not statics
2+
or constants. This limitation exists to prevent the creation of constants that
3+
have a mutable reference in their final value. If you had a constant of `&mut
4+
i32` type, you could modify the value through that reference, making the
5+
constant essentially mutable. While there could be a more fine-grained scheme
6+
in the future that allows mutable references if they are not "leaked" to the
7+
final value, a more conservative approach was chosen for now. `const fn` do not
8+
have this problem, as the borrow checker will prevent the `const fn` from
9+
returning new mutable references.
10+
11+
Erroneous code example:
12+
13+
```compile_fail,E0764
14+
#![feature(const_fn)]
15+
#![feature(const_mut_refs)]
16+
17+
fn main() {
18+
const OH_NO: &'static mut usize = &mut 1; // error!
19+
}
20+
```
21+
22+
Remember: you cannot use a function call inside a constant or static. However,
23+
you can totally use it in constant functions:
24+
25+
```
26+
#![feature(const_fn)]
27+
#![feature(const_mut_refs)]
28+
29+
const fn foo(x: usize) -> usize {
30+
let mut y = 1;
31+
let z = &mut y;
32+
*z += x;
33+
y
34+
}
35+
36+
fn main() {
37+
const FOO: usize = foo(10); // ok!
38+
}
39+
```

src/librustc_mir/transform/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl NonConstOp for MutBorrow {
227227
struct_span_err!(
228228
ccx.tcx.sess,
229229
span,
230-
E0019,
230+
E0764,
231231
"mutable references are not allowed in {}s",
232232
ccx.const_kind(),
233233
)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0019]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/check-static-immutable-mut-slices.rs:3:37
33
|
44
LL | static TEST: &'static mut [isize] = &mut [];
55
| ^^^^^^^ `&mut` is only allowed in `const fn`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0019`.
9+
For more information about this error, try `rustc --explain E0764`.

src/test/ui/consts/const-eval/issue-65394.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/issue-65394.rs:8:13
33
|
44
LL | let r = &mut x;
@@ -12,5 +12,5 @@ LL | let mut x = Vec::<i32>::new();
1212

1313
error: aborting due to 2 previous errors
1414

15-
Some errors have detailed explanations: E0019, E0493.
16-
For more information about an error, try `rustc --explain E0019`.
15+
Some errors have detailed explanations: E0493, E0764.
16+
For more information about an error, try `rustc --explain E0493`.

src/test/ui/consts/const-multi-ref.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/const-multi-ref.rs:6:13
33
|
44
LL | let p = &mut a;
@@ -12,5 +12,5 @@ LL | let p = &a;
1212

1313
error: aborting due to 2 previous errors
1414

15-
Some errors have detailed explanations: E0019, E0492.
16-
For more information about an error, try `rustc --explain E0019`.
15+
Some errors have detailed explanations: E0492, E0764.
16+
For more information about an error, try `rustc --explain E0492`.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/const_mut_address_of.rs:24:5
33
|
44
LL | foo().bar();
55
| ^^^^^ `&mut` is only allowed in `const fn`
66

7-
error[E0019]: mutable references are not allowed in constants
7+
error[E0764]: mutable references are not allowed in constants
88
--> $DIR/const_mut_address_of.rs:26:9
99
|
1010
LL | baz(&mut foo());
1111
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
1212

1313
error: aborting due to 2 previous errors
1414

15-
For more information about this error, try `rustc --explain E0019`.
15+
For more information about this error, try `rustc --explain E0764`.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/const_mut_refs.rs:31:17
33
|
44
LL | let _: [(); foo().bar()] = [(); 1];
55
| ^^^^^ `&mut` is only allowed in `const fn`
66

7-
error[E0019]: mutable references are not allowed in constants
7+
error[E0764]: mutable references are not allowed in constants
88
--> $DIR/const_mut_refs.rs:33:21
99
|
1010
LL | let _: [(); baz(&mut foo())] = [(); 2];
1111
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
1212

13-
error[E0019]: mutable references are not allowed in constants
13+
error[E0764]: mutable references are not allowed in constants
1414
--> $DIR/const_mut_refs.rs:35:22
1515
|
1616
LL | let _: [(); bazz(&mut foo())] = [(); 3];
1717
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
1818

1919
error: aborting due to 3 previous errors
2020

21-
For more information about this error, try `rustc --explain E0019`.
21+
For more information about this error, try `rustc --explain E0764`.

src/test/ui/consts/const_let_assign3.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ LL | self.state = x;
66
|
77
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
88

9-
error[E0019]: mutable references are not allowed in constants
9+
error[E0764]: mutable references are not allowed in constants
1010
--> $DIR/const_let_assign3.rs:16:5
1111
|
1212
LL | s.foo(3);
1313
| ^ `&mut` is only allowed in `const fn`
1414

15-
error[E0019]: mutable references are not allowed in constants
15+
error[E0764]: mutable references are not allowed in constants
1616
--> $DIR/const_let_assign3.rs:22:13
1717
|
1818
LL | let y = &mut x;
@@ -28,4 +28,5 @@ LL | *y = 42;
2828

2929
error: aborting due to 4 previous errors
3030

31-
For more information about this error, try `rustc --explain E0019`.
31+
Some errors have detailed explanations: E0019, E0764.
32+
For more information about an error, try `rustc --explain E0019`.

src/test/ui/consts/projection_qualif.mut_refs.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/projection_qualif.rs:10:27
33
|
44
LL | let b: *mut u32 = &mut a;
@@ -15,5 +15,5 @@ LL | unsafe { *b = 5; }
1515

1616
error: aborting due to 2 previous errors
1717

18-
Some errors have detailed explanations: E0019, E0658.
19-
For more information about an error, try `rustc --explain E0019`.
18+
Some errors have detailed explanations: E0658, E0764.
19+
For more information about an error, try `rustc --explain E0658`.

src/test/ui/consts/projection_qualif.stock.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/projection_qualif.rs:10:27
33
|
44
LL | let b: *mut u32 = &mut a;
@@ -23,5 +23,5 @@ LL | unsafe { *b = 5; }
2323

2424
error: aborting due to 3 previous errors
2525

26-
Some errors have detailed explanations: E0019, E0658.
26+
Some errors have detailed explanations: E0019, E0658, E0764.
2727
For more information about an error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0019]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/read_from_static_mut_ref.rs:5:26
33
|
44
LL | static OH_NO: &mut i32 = &mut 42;
55
| ^^^^^^^ `&mut` is only allowed in `const fn`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0019`.
9+
For more information about this error, try `rustc --explain E0764`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0019]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
33
|
44
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0019`.
9+
For more information about this error, try `rustc --explain E0764`.

src/test/ui/consts/static_mut_containing_mut_ref2.stock.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
33
|
44
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
@@ -14,4 +14,5 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4
1414

1515
error: aborting due to 2 previous errors
1616

17-
For more information about this error, try `rustc --explain E0019`.
17+
Some errors have detailed explanations: E0019, E0764.
18+
For more information about an error, try `rustc --explain E0019`.

src/test/ui/error-codes/E0017.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ static X: i32 = 1;
22
const C: i32 = 2;
33
static mut M: i32 = 3;
44

5-
const CR: &'static mut i32 = &mut C; //~ ERROR E0019
6-
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
5+
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
6+
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
77
//~| ERROR E0019
88
//~| ERROR cannot borrow
9-
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0019
10-
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0019
9+
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
10+
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
1111
fn main() {}

src/test/ui/error-codes/E0017.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/E0017.rs:5:30
33
|
44
LL | const CR: &'static mut i32 = &mut C;
@@ -12,7 +12,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
1212
|
1313
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1414

15-
error[E0019]: mutable references are not allowed in statics
15+
error[E0764]: mutable references are not allowed in statics
1616
--> $DIR/E0017.rs:6:39
1717
|
1818
LL | static STATIC_REF: &'static mut i32 = &mut X;
@@ -24,19 +24,19 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
2424
LL | static STATIC_REF: &'static mut i32 = &mut X;
2525
| ^^^^^^ cannot borrow as mutable
2626

27-
error[E0019]: mutable references are not allowed in statics
27+
error[E0764]: mutable references are not allowed in statics
2828
--> $DIR/E0017.rs:9:38
2929
|
3030
LL | static CONST_REF: &'static mut i32 = &mut C;
3131
| ^^^^^^ `&mut` is only allowed in `const fn`
3232

33-
error[E0019]: mutable references are not allowed in statics
33+
error[E0764]: mutable references are not allowed in statics
3434
--> $DIR/E0017.rs:10:52
3535
|
3636
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
3737
| ^^^^^^ `&mut` is only allowed in `const fn`
3838

3939
error: aborting due to 6 previous errors
4040

41-
Some errors have detailed explanations: E0019, E0596.
41+
Some errors have detailed explanations: E0019, E0596, E0764.
4242
For more information about an error, try `rustc --explain E0019`.

src/test/ui/error-codes/E0388.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
static X: i32 = 1;
22
const C: i32 = 2;
33

4-
const CR: &'static mut i32 = &mut C; //~ ERROR E0019
4+
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
55
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
66
//~| ERROR cannot borrow
7-
//~| ERROR E0019
8-
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0019
7+
//~| ERROR E0764
8+
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
99

1010
fn main() {}

src/test/ui/error-codes/E0388.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/E0388.rs:4:30
33
|
44
LL | const CR: &'static mut i32 = &mut C;
@@ -12,7 +12,7 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
1212
|
1313
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1414

15-
error[E0019]: mutable references are not allowed in statics
15+
error[E0764]: mutable references are not allowed in statics
1616
--> $DIR/E0388.rs:5:39
1717
|
1818
LL | static STATIC_REF: &'static mut i32 = &mut X;
@@ -24,13 +24,13 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
2424
LL | static STATIC_REF: &'static mut i32 = &mut X;
2525
| ^^^^^^ cannot borrow as mutable
2626

27-
error[E0019]: mutable references are not allowed in statics
27+
error[E0764]: mutable references are not allowed in statics
2828
--> $DIR/E0388.rs:8:38
2929
|
3030
LL | static CONST_REF: &'static mut i32 = &mut C;
3131
| ^^^^^^ `&mut` is only allowed in `const fn`
3232

3333
error: aborting due to 5 previous errors
3434

35-
Some errors have detailed explanations: E0019, E0596.
35+
Some errors have detailed explanations: E0019, E0596, E0764.
3636
For more information about an error, try `rustc --explain E0019`.

src/test/ui/issues/issue-17718-const-bad-values.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in constants
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/issue-17718-const-bad-values.rs:1:34
33
|
44
LL | const C1: &'static mut [usize] = &mut [];
@@ -20,13 +20,13 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
2020
|
2121
= help: consider extracting the value of the `static` to a `const`, and referring to that
2222

23-
error[E0019]: mutable references are not allowed in constants
23+
error[E0764]: mutable references are not allowed in constants
2424
--> $DIR/issue-17718-const-bad-values.rs:5:41
2525
|
2626
LL | const C2: &'static mut usize = unsafe { &mut S };
2727
| ^^^^^^ `&mut` is only allowed in `const fn`
2828

2929
error: aborting due to 4 previous errors
3030

31-
Some errors have detailed explanations: E0013, E0019.
31+
Some errors have detailed explanations: E0013, E0764.
3232
For more information about an error, try `rustc --explain E0013`.

src/test/ui/issues/issue-46604.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0019
1+
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0764
22
fn write<T: AsRef<[u8]>>(buffer: T) { }
33

44
fn main() {

src/test/ui/issues/issue-46604.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0019]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/issue-46604.rs:1:25
33
|
44
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
@@ -12,5 +12,5 @@ LL | buf[0]=2;
1212

1313
error: aborting due to 2 previous errors
1414

15-
Some errors have detailed explanations: E0019, E0594.
16-
For more information about an error, try `rustc --explain E0019`.
15+
Some errors have detailed explanations: E0594, E0764.
16+
For more information about an error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)