@@ -205,22 +205,34 @@ impl NonConstOp for CellBorrow {
205
205
#[ derive( Debug ) ]
206
206
pub struct MutBorrow ;
207
207
impl NonConstOp for MutBorrow {
208
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
209
+ // Forbid everywhere except in const fn
210
+ ccx. const_kind ( ) == hir:: ConstContext :: ConstFn
211
+ && ccx. tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
212
+ }
213
+
208
214
fn feature_gate ( ) -> Option < Symbol > {
209
215
Some ( sym:: const_mut_refs)
210
216
}
211
217
212
218
fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
213
- let mut err = feature_err (
214
- & ccx. tcx . sess . parse_sess ,
215
- sym:: const_mut_refs,
216
- span,
217
- & format ! (
218
- "references in {}s may only refer \
219
- to immutable values",
220
- ccx. const_kind( )
221
- ) ,
222
- ) ;
223
- 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
+ E0764 ,
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 ( ) ) ;
224
236
if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
225
237
err. note (
226
238
"References in statics and constants may only refer \
0 commit comments