@@ -205,22 +205,34 @@ impl NonConstOp for CellBorrow {
205205#[ derive( Debug ) ]
206206pub struct MutBorrow ;
207207impl 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+
208214 fn feature_gate ( ) -> Option < Symbol > {
209215 Some ( sym:: const_mut_refs)
210216 }
211217
212218 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 ( ) ) ;
224236 if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
225237 err. note (
226238 "References in statics and constants may only refer \
0 commit comments