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

Lines changed: 1 addition & 0 deletions
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
Lines changed: 39 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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
)
Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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`.
Lines changed: 3 additions & 3 deletions
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`.
Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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`.

0 commit comments

Comments
 (0)