Skip to content

Commit 473ae22

Browse files
Rollup merge of #77136 - ecstatic-morse:issue-77134, r=oli-obk
Suggest `const_mut_refs`, not `const_fn` for mutable references in `const fn` Resolves #77134. Prior to #76850, most uses of `&mut` in `const fn` ~~required~~ involved two feature gates, `const_mut_refs` and `const_fn`. The first allowed all mutable borrows of locals. The second allowed only locals, arguments and return values whose types contained `&mut`. I switched the second check to the `const_mut_refs` gate. However, I forgot update the error message with the new suggestion. Alternatively, we could revert to having two different feature gates for this. OP's code never borrows anything mutably, so it didn't need `const_mut_refs` in the past, only `const_fn`. I'd prefer to keep everything under a single gate, however. r? @oli-obk
2 parents 6f3da3d + e5e5e64 commit 473ae22

File tree

9 files changed

+53
-46
lines changed

9 files changed

+53
-46
lines changed

compiler/rustc_mir/src/transform/check_consts/ops.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ impl NonConstOp for UnsizingCast {
535535
}
536536
}
537537

538+
// Types that cannot appear in the signature or locals of a `const fn`.
538539
pub mod ty {
539540
use super::*;
540541

@@ -548,7 +549,13 @@ pub mod ty {
548549
}
549550

550551
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
551-
mcf_emit_error(ccx, span, "mutable references in const fn are unstable");
552+
feature_err(
553+
&ccx.tcx.sess.parse_sess,
554+
sym::const_mut_refs,
555+
span,
556+
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
557+
)
558+
.emit()
552559
}
553560
}
554561

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn main() {
22
foo(&mut 5);
33
}
44

5-
const fn foo(x: &mut i32) -> i32 { //~ ERROR mutable references in const fn
5+
const fn foo(x: &mut i32) -> i32 { //~ ERROR mutable references
66
*x + 1
77

88
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0723]: mutable references in const fn are unstable
1+
error[E0658]: mutable references are not allowed in constant functions
22
--> $DIR/feature-gate-const_mut_refs.rs:5:14
33
|
44
LL | const fn foo(x: &mut i32) -> i32 {
55
| ^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
99

1010
error: aborting due to previous error
1111

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

src/test/ui/consts/const_let_assign3.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0723]: mutable references in const fn are unstable
1+
error[E0658]: mutable references are not allowed in constant functions
22
--> $DIR/const_let_assign3.rs:8:18
33
|
44
LL | const fn foo(&mut self, x: u32) {
55
| ^^^^^^^^^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
99

1010
error[E0764]: mutable references are not allowed in constants
1111
--> $DIR/const_let_assign3.rs:16:5
@@ -29,5 +29,5 @@ LL | *y = 42;
2929

3030
error: aborting due to 4 previous errors
3131

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

src/test/ui/consts/min_const_fn/min_const_fn.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ impl<T> Foo<T> {
3737
const fn into_inner(self) -> T { self.0 } //~ destructors cannot be evaluated
3838
const fn get(&self) -> &T { &self.0 }
3939
const fn get_mut(&mut self) -> &mut T { &mut self.0 }
40-
//~^ mutable references in const fn are unstable
40+
//~^ mutable references
4141
}
4242
impl<'a, T> Foo<T> {
4343
const fn new_lt(t: T) -> Self { Foo(t) }
4444
const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
4545
const fn get_lt(&'a self) -> &T { &self.0 }
4646
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
47-
//~^ mutable references in const fn are unstable
47+
//~^ mutable references
4848
}
4949
impl<T: Sized> Foo<T> {
5050
const fn new_s(t: T) -> Self { Foo(t) }
5151
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
5252
const fn get_s(&self) -> &T { &self.0 }
5353
const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
54-
//~^ mutable references in const fn are unstable
54+
//~^ mutable references
5555
}
5656
impl<T: ?Sized> Foo<T> {
5757
const fn get_sq(&self) -> &T { &self.0 }
5858
const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
59-
//~^ mutable references in const fn are unstable
59+
//~^ mutable references
6060
}
6161

6262

@@ -99,7 +99,7 @@ const fn foo30_2_with_unsafe(x: *mut u32) -> usize { unsafe { x as usize } }
9999
//~^ ERROR casting pointers to integers
100100
const fn foo30_6() -> bool { let x = true; x }
101101
const fn inc(x: &mut i32) { *x += 1 }
102-
//~^ ERROR mutable references in const fn are unstable
102+
//~^ ERROR mutable references
103103

104104
// ok
105105
const fn foo36(a: bool, b: bool) -> bool { a && b }

src/test/ui/consts/min_const_fn/min_const_fn.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ LL | const fn into_inner(self) -> T { self.0 }
66
| |
77
| constant functions cannot evaluate destructors
88

9-
error[E0723]: mutable references in const fn are unstable
9+
error[E0658]: mutable references are not allowed in constant functions
1010
--> $DIR/min_const_fn.rs:39:36
1111
|
1212
LL | const fn get_mut(&mut self) -> &mut T { &mut self.0 }
1313
| ^^^^^^
1414
|
15-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
16-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
1717

1818
error[E0493]: destructors cannot be evaluated at compile-time
1919
--> $DIR/min_const_fn.rs:44:28
@@ -23,14 +23,14 @@ LL | const fn into_inner_lt(self) -> T { self.0 }
2323
| |
2424
| constant functions cannot evaluate destructors
2525

26-
error[E0723]: mutable references in const fn are unstable
26+
error[E0658]: mutable references are not allowed in constant functions
2727
--> $DIR/min_const_fn.rs:46:42
2828
|
2929
LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
3030
| ^^^^^^
3131
|
32-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
33-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
32+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
33+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
3434

3535
error[E0493]: destructors cannot be evaluated at compile-time
3636
--> $DIR/min_const_fn.rs:51:27
@@ -40,23 +40,23 @@ LL | const fn into_inner_s(self) -> T { self.0 }
4040
| |
4141
| constant functions cannot evaluate destructors
4242

43-
error[E0723]: mutable references in const fn are unstable
43+
error[E0658]: mutable references are not allowed in constant functions
4444
--> $DIR/min_const_fn.rs:53:38
4545
|
4646
LL | const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
4747
| ^^^^^^
4848
|
49-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
50-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
49+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
50+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5151

52-
error[E0723]: mutable references in const fn are unstable
52+
error[E0658]: mutable references are not allowed in constant functions
5353
--> $DIR/min_const_fn.rs:58:39
5454
|
5555
LL | const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
5656
| ^^^^^^
5757
|
58-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
59-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
58+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
59+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
6060

6161
error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
6262
--> $DIR/min_const_fn.rs:76:16
@@ -164,14 +164,14 @@ LL | const fn foo30_2_with_unsafe(x: *mut u32) -> usize { unsafe { x as usize }
164164
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
165165
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
166166

167-
error[E0723]: mutable references in const fn are unstable
167+
error[E0658]: mutable references are not allowed in constant functions
168168
--> $DIR/min_const_fn.rs:101:14
169169
|
170170
LL | const fn inc(x: &mut i32) { *x += 1 }
171171
| ^
172172
|
173-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
174-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
173+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
174+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
175175

176176
error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
177177
--> $DIR/min_const_fn.rs:110:6

src/test/ui/consts/min_const_fn/mutable_borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fn mutable_ref_in_const() -> u8 {
22
let mut a = 0;
3-
let b = &mut a; //~ ERROR mutable references in const fn
3+
let b = &mut a; //~ ERROR mutable references
44
*b
55
}
66

@@ -9,7 +9,7 @@ struct X;
99
impl X {
1010
const fn inherent_mutable_ref_in_const() -> u8 {
1111
let mut a = 0;
12-
let b = &mut a; //~ ERROR mutable references in const fn
12+
let b = &mut a; //~ ERROR mutable references
1313
*b
1414
}
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0723]: mutable references in const fn are unstable
1+
error[E0658]: mutable references are not allowed in constant functions
22
--> $DIR/mutable_borrow.rs:3:9
33
|
44
LL | let b = &mut a;
55
| ^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
99

10-
error[E0723]: mutable references in const fn are unstable
10+
error[E0658]: mutable references are not allowed in constant functions
1111
--> $DIR/mutable_borrow.rs:12:13
1212
|
1313
LL | let b = &mut a;
1414
| ^
1515
|
16-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
1818

1919
error: aborting due to 2 previous errors
2020

21-
For more information about this error, try `rustc --explain E0723`.
21+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0723]: mutable references in const fn are unstable
1+
error[E0658]: mutable references are not allowed in constant functions
22
--> $DIR/ranged_ints2_const.rs:11:9
33
|
44
LL | let y = &mut x.0;
55
| ^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
99

10-
error[E0723]: mutable references in const fn are unstable
10+
error[E0658]: mutable references are not allowed in constant functions
1111
--> $DIR/ranged_ints2_const.rs:18:9
1212
|
1313
LL | let y = unsafe { &mut x.0 };
1414
| ^
1515
|
16-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
1818

1919
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
2020
--> $DIR/ranged_ints2_const.rs:11:13
@@ -26,5 +26,5 @@ LL | let y = &mut x.0;
2626

2727
error: aborting due to 3 previous errors
2828

29-
Some errors have detailed explanations: E0133, E0723.
29+
Some errors have detailed explanations: E0133, E0658.
3030
For more information about an error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)