Skip to content

Commit 17db1cb

Browse files
committed
Bypass const_item_mutation if const's type has Drop impl
1 parent b218b95 commit 17db1cb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

compiler/rustc_mir/src/transform/check_const_item_mutation.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
3131
None
3232
}
3333
}
34+
35+
fn is_const_item_without_destructor(&self, local: Local) -> Option<DefId> {
36+
let def_id = self.is_const_item(local)?;
37+
match self.tcx.adt_def(def_id).destructor(self.tcx) {
38+
Some(_) => None,
39+
None => Some(def_id),
40+
}
41+
}
42+
3443
fn lint_const_item_usage(
3544
&self,
3645
const_item: DefId,
@@ -59,7 +68,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
5968
// Assigning directly to a constant (e.g. `FOO = true;`) is a hard error,
6069
// so emitting a lint would be redundant.
6170
if !lhs.projection.is_empty() {
62-
if let Some(def_id) = self.is_const_item(lhs.local) {
71+
if let Some(def_id) = self.is_const_item_without_destructor(lhs.local) {
6372
// Don't lint on writes through a pointer
6473
// (e.g. `unsafe { *FOO = 0; *BAR.field = 1; }`)
6574
if !matches!(lhs.projection.last(), Some(PlaceElem::Deref)) {
@@ -89,7 +98,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
8998
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) {
9099
if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue {
91100
let local = place.local;
92-
if let Some(def_id) = self.is_const_item(local) {
101+
if let Some(def_id) = self.is_const_item_without_destructor(local) {
93102
// If this Rvalue is being used as the right-hand side of a
94103
// `StatementKind::Assign`, see if it ends up getting used as
95104
// the `self` parameter of a method call (as the terminator of our current

0 commit comments

Comments
 (0)