Skip to content

Commit 1f48465

Browse files
committed
update diagnostics for &mut in constants
1 parent 014e605 commit 1f48465

30 files changed

+111
-180
lines changed

src/librustc_mir/transform/check_consts/ops.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,23 @@ impl NonConstOp for MutBorrow {
216216
}
217217

218218
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
219-
let mut err = feature_err(
220-
&ccx.tcx.sess.parse_sess,
221-
sym::const_mut_refs,
222-
span,
223-
&format!(
224-
"references in {}s may only refer \
225-
to immutable values",
226-
ccx.const_kind()
227-
),
228-
);
229-
err.span_label(span, format!("{}s require immutable values", ccx.const_kind()));
219+
let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
220+
feature_err(
221+
&ccx.tcx.sess.parse_sess,
222+
sym::const_mut_refs,
223+
span,
224+
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
225+
)
226+
} else {
227+
struct_span_err!(
228+
ccx.tcx.sess,
229+
span,
230+
E0019,
231+
"mutable references are not allowed in {}s",
232+
ccx.const_kind(),
233+
)
234+
};
235+
err.span_label(span, "`&mut` is only allowed in `const fn`".to_string());
230236
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
231237
err.note(
232238
"References in statics and constants may only refer \

src/test/compile-fail/issue-52443.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
[(); { for _ in 0usize.. {}; 0}];
1010
//~^ ERROR `for` is not allowed in a `const`
1111
//~| ERROR calls in constants are limited to constant functions
12-
//~| ERROR references in constants may only refer to immutable values
12+
//~| ERROR mutable references are not allowed in constants
1313
//~| ERROR calls in constants are limited to constant functions
1414
//~| ERROR evaluation of constant value failed
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Checks that immutable static items can't have mutable slices
22

33
static TEST: &'static mut [isize] = &mut [];
4-
//~^ ERROR references in statics may only refer to immutable values
4+
//~^ ERROR mutable references are not allowed in statics
55

66
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0019]: 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 [];
5-
| ^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error: aborting due to previous error
118

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const _: Vec<i32> = {
77
let mut x = Vec::<i32>::new(); //~ ERROR destructors cannot be evaluated at compile-time
8-
let r = &mut x; //~ ERROR references in constants may only refer to immutable values
8+
let r = &mut x; //~ ERROR mutable references are not allowed in constants
99
let y = x;
1010
y
1111
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/issue-65394.rs:8:13
33
|
44
LL | let r = &mut x;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0493]: destructors cannot be evaluated at compile-time
118
--> $DIR/issue-65394.rs:7:9
@@ -15,5 +12,5 @@ LL | let mut x = Vec::<i32>::new();
1512

1613
error: aborting due to 2 previous errors
1714

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const _: i32 = {
55
let mut a = 5;
6-
let p = &mut a; //~ ERROR references in constants may only refer to immutable values
6+
let p = &mut a; //~ ERROR mutable references are not allowed in constants
77

88
let reborrow = {p};
99
let pp = &reborrow;
+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/const-multi-ref.rs:6:13
33
|
44
LL | let p = &mut a;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
118
--> $DIR/const-multi-ref.rs:16:13
@@ -15,5 +12,5 @@ LL | let p = &a;
1512

1613
error: aborting due to 2 previous errors
1714

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

src/test/ui/consts/const-mut-refs/const_mut_address_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const fn baz(foo: &mut Foo)-> *mut usize {
2222

2323
const _: () = {
2424
foo().bar();
25-
//~^ ERROR references in constants may only refer to immutable values
25+
//~^ ERROR mutable references are not allowed in constants
2626
baz(&mut foo());
27-
//~^ ERROR references in constants may only refer to immutable values
27+
//~^ ERROR mutable references are not allowed in constants
2828
};
2929

3030
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/const_mut_address_of.rs:24:5
33
|
44
LL | foo().bar();
5-
| ^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^ `&mut` is only allowed in `const fn`
96

10-
error[E0658]: references in constants may only refer to immutable values
7+
error[E0019]: mutable references are not allowed in constants
118
--> $DIR/const_mut_address_of.rs:26:9
129
|
1310
LL | baz(&mut foo());
14-
| ^^^^^^^^^^ constants require immutable values
15-
|
16-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
17-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
11+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
1812

1913
error: aborting due to 2 previous errors
2014

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

src/test/ui/consts/const-mut-refs/const_mut_refs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const fn bazz(foo: &mut Foo) -> usize {
2929

3030
fn main() {
3131
let _: [(); foo().bar()] = [(); 1];
32-
//~^ ERROR references in constants may only refer to immutable values
32+
//~^ ERROR mutable references are not allowed in constants
3333
let _: [(); baz(&mut foo())] = [(); 2];
34-
//~^ ERROR references in constants may only refer to immutable values
34+
//~^ ERROR mutable references are not allowed in constants
3535
let _: [(); bazz(&mut foo())] = [(); 3];
36-
//~^ ERROR references in constants may only refer to immutable values
36+
//~^ ERROR mutable references are not allowed in constants
3737
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/const_mut_refs.rs:31:17
33
|
44
LL | let _: [(); foo().bar()] = [(); 1];
5-
| ^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^ `&mut` is only allowed in `const fn`
96

10-
error[E0658]: references in constants may only refer to immutable values
7+
error[E0019]: mutable references are not allowed in constants
118
--> $DIR/const_mut_refs.rs:33:21
129
|
1310
LL | let _: [(); baz(&mut foo())] = [(); 2];
14-
| ^^^^^^^^^^ constants require immutable values
15-
|
16-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
17-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
11+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
1812

19-
error[E0658]: references in constants may only refer to immutable values
13+
error[E0019]: mutable references are not allowed in constants
2014
--> $DIR/const_mut_refs.rs:35:22
2115
|
2216
LL | let _: [(); bazz(&mut foo())] = [(); 3];
23-
| ^^^^^^^^^^ constants require immutable values
24-
|
25-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
26-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
17+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
2718

2819
error: aborting due to 3 previous errors
2920

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

src/test/ui/consts/const_let_assign3.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ impl S {
1313

1414
const FOO: S = {
1515
let mut s = S { state: 42 };
16-
s.foo(3); //~ ERROR references in constants may only refer to immutable values
16+
s.foo(3); //~ ERROR mutable references are not allowed in constants
1717
s
1818
};
1919

2020
type Array = [u32; {
2121
let mut x = 2;
2222
let y = &mut x;
23-
//~^ ERROR references in constants may only refer to immutable values
23+
//~^ ERROR mutable references are not allowed in constants
2424
*y = 42;
2525
//~^ ERROR constant contains unimplemented expression type
2626
*y

src/test/ui/consts/const_let_assign3.stderr

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

9-
error[E0658]: references in constants may only refer to immutable values
9+
error[E0019]: mutable references are not allowed in constants
1010
--> $DIR/const_let_assign3.rs:16:5
1111
|
1212
LL | s.foo(3);
13-
| ^ constants require immutable values
14-
|
15-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
16-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
13+
| ^ `&mut` is only allowed in `const fn`
1714

18-
error[E0658]: references in constants may only refer to immutable values
15+
error[E0019]: mutable references are not allowed in constants
1916
--> $DIR/const_let_assign3.rs:22:13
2017
|
2118
LL | let y = &mut x;
22-
| ^^^^^^ constants require immutable values
23-
|
24-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
25-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
19+
| ^^^^^^ `&mut` is only allowed in `const fn`
2620

2721
error[E0019]: constant contains unimplemented expression type
2822
--> $DIR/const_let_assign3.rs:24:5
@@ -34,5 +28,4 @@ LL | *y = 42;
3428

3529
error: aborting due to 4 previous errors
3630

37-
Some errors have detailed explanations: E0019, E0658.
38-
For more information about an error, try `rustc --explain E0019`.
31+
For more information about this error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/projection_qualif.rs:10:27
33
|
44
LL | let b: *mut u32 = &mut a;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0658]: dereferencing raw pointers in constants is unstable
118
--> $DIR/projection_qualif.rs:11:18
@@ -18,4 +15,5 @@ LL | unsafe { *b = 5; }
1815

1916
error: aborting due to 2 previous errors
2017

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

src/test/ui/consts/projection_qualif.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cell::Cell;
77
const FOO: &u32 = {
88
let mut a = 42;
99
{
10-
let b: *mut u32 = &mut a; //~ ERROR may only refer to immutable values
10+
let b: *mut u32 = &mut a; //~ ERROR mutable references are not allowed in constants
1111
unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
1212
//[stock]~^ contains unimplemented expression
1313
}

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0019]: mutable references are not allowed in constants
22
--> $DIR/projection_qualif.rs:10:27
33
|
44
LL | let b: *mut u32 = &mut a;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0658]: dereferencing raw pointers in constants is unstable
118
--> $DIR/projection_qualif.rs:11:18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// We are keeping this test in case we decide to allow mutable references in statics again
12
#![feature(const_mut_refs)]
23
#![allow(const_err)]
34

45
static OH_NO: &mut i32 = &mut 42;
5-
//~^ ERROR references in statics may only refer to immutable values
6+
//~^ ERROR mutable references are not allowed in statics
67
fn main() {
78
assert_eq!(*OH_NO, 42);
89
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: references in statics may only refer to immutable values
2-
--> $DIR/read_from_static_mut_ref.rs:4:26
1+
error[E0019]: mutable references are not allowed in statics
2+
--> $DIR/read_from_static_mut_ref.rs:5:26
33
|
44
LL | static OH_NO: &mut i32 = &mut 42;
5-
| ^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error: aborting due to previous error
118

12-
For more information about this error, try `rustc --explain E0658`.
9+
For more information about this error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0019]: 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; };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error: aborting due to previous error
118

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

src/test/ui/consts/static_mut_containing_mut_ref2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
static mut STDERR_BUFFER_SPACE: u8 = 0;
66

77
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
8-
//~^ ERROR references in statics may only refer to immutable values
8+
//~^ ERROR mutable references are not allowed in statics
99
//[stock]~| ERROR static contains unimplemented expression type
1010

1111
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0019]: 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; };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0019]: static contains unimplemented expression type
118
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
@@ -17,5 +14,4 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4
1714

1815
error: aborting due to 2 previous errors
1916

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

0 commit comments

Comments
 (0)