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

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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 1 addition & 1 deletion
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() { }
Lines changed: 3 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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
};
Lines changed: 4 additions & 7 deletions
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

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

Lines changed: 2 additions & 2 deletions
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() {}
Lines changed: 5 additions & 11 deletions
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`.

0 commit comments

Comments
 (0)