Skip to content

Commit

Permalink
Bypass const_item_mutation if const's type has Drop impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 1, 2020
1 parent b218b95 commit 17db1cb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions compiler/rustc_mir/src/transform/check_const_item_mutation.rs
Expand Up @@ -31,6 +31,15 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
None
}
}

fn is_const_item_without_destructor(&self, local: Local) -> Option<DefId> {
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,
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17db1cb

Please sign in to comment.