From 17db1cb5d5a864d611e17d6e2466731c3b50a794 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 26 Sep 2020 21:14:31 -0400 Subject: [PATCH] Bypass const_item_mutation if const's type has Drop impl --- .../src/transform/check_const_item_mutation.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs index 0281c478a6c..b556e2d217d 100644 --- a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs +++ b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs @@ -31,6 +31,15 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> { None } } + + fn is_const_item_without_destructor(&self, local: Local) -> Option { + let def_id = self.is_const_item(local)?; + match self.tcx.adt_def(def_id).destructor(self.tcx) { + Some(_) => None, + None => Some(def_id), + } + } + fn lint_const_item_usage( &self, const_item: DefId, @@ -59,7 +68,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> { // Assigning directly to a constant (e.g. `FOO = true;`) is a hard error, // so emitting a lint would be redundant. if !lhs.projection.is_empty() { - if let Some(def_id) = self.is_const_item(lhs.local) { + if let Some(def_id) = self.is_const_item_without_destructor(lhs.local) { // Don't lint on writes through a pointer // (e.g. `unsafe { *FOO = 0; *BAR.field = 1; }`) if !matches!(lhs.projection.last(), Some(PlaceElem::Deref)) { @@ -89,7 +98,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> { fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) { if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue { let local = place.local; - if let Some(def_id) = self.is_const_item(local) { + if let Some(def_id) = self.is_const_item_without_destructor(local) { // If this Rvalue is being used as the right-hand side of a // `StatementKind::Assign`, see if it ends up getting used as // the `self` parameter of a method call (as the terminator of our current