From 2f9d33830c7bf1189dffec72cd4a9319c9e3250f Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Sat, 6 Jun 2020 19:25:02 -0500 Subject: [PATCH] forbid mutable references in all constants except const fns --- src/librustc_mir/transform/check_consts/ops.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/librustc_mir/transform/check_consts/ops.rs b/src/librustc_mir/transform/check_consts/ops.rs index d5059c98c951..39095d13dedc 100644 --- a/src/librustc_mir/transform/check_consts/ops.rs +++ b/src/librustc_mir/transform/check_consts/ops.rs @@ -205,6 +205,12 @@ impl NonConstOp for CellBorrow { #[derive(Debug)] pub struct MutBorrow; impl NonConstOp for MutBorrow { + fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool { + // Forbid everywhere except in const fn + ccx.const_kind() == hir::ConstContext::ConstFn + && ccx.tcx.features().enabled(Self::feature_gate().unwrap()) + } + fn feature_gate() -> Option { Some(sym::const_mut_refs) }