about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2020-09-30 22:16:32 -0700
committerDavid Tolnay <dtolnay@gmail.com>2020-09-30 22:19:45 -0700
commit75c2fdf34a5384627db9ba240c5dcc0723aea763 (patch)
treed625768656d76dc440df4dfaf4bba5b8a17c7497
parenteef5104c53114bf9a074fe8d83cfd782bd78652d (diff)
downloadrust-75c2fdf34a5384627db9ba240c5dcc0723aea763.tar.gz
rust-75c2fdf34a5384627db9ba240c5dcc0723aea763.zip
Warn on method call mutating const, even if it has destructor
-rw-r--r--compiler/rustc_mir/src/transform/check_const_item_mutation.rs2
-rw-r--r--src/test/ui/lint/lint-const-item-mutation.rs2
-rw-r--r--src/test/ui/lint/lint-const-item-mutation.stderr27
3 files changed, 28 insertions, 3 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 98477b03237..a0edb377243 100644
--- a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs
+++ b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs
@@ -111,7 +111,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_without_destructor(local) {
+            if let Some(def_id) = self.is_const_item(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
diff --git a/src/test/ui/lint/lint-const-item-mutation.rs b/src/test/ui/lint/lint-const-item-mutation.rs
index 9f6a4e59088..c49a13f1065 100644
--- a/src/test/ui/lint/lint-const-item-mutation.rs
+++ b/src/test/ui/lint/lint-const-item-mutation.rs
@@ -49,5 +49,5 @@ fn main() {
 
     MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
     MUTABLE2.msg = "wow"; //~ WARN attempting to modify
-    VEC.push(0); // no warning
+    VEC.push(0); //~ WARN taking a mutable reference to a `const` item
 }
diff --git a/src/test/ui/lint/lint-const-item-mutation.stderr b/src/test/ui/lint/lint-const-item-mutation.stderr
index 0e02ba928bb..11b5124b2d2 100644
--- a/src/test/ui/lint/lint-const-item-mutation.stderr
+++ b/src/test/ui/lint/lint-const-item-mutation.stderr
@@ -98,5 +98,30 @@ note: `const` item defined here
 LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-warning: 7 warnings emitted
+warning: taking a mutable reference to a `const` item
+  --> $DIR/lint-const-item-mutation.rs:52:5
+   |
+LL |     VEC.push(0);
+   |     ^^^^^^^^^^^
+   |
+   = note: each usage of a `const` item creates a new temporary
+   = note: the mutable reference will refer to this temporary, not the original `const` item
+note: mutable reference created due to call to this method
+  --> $SRC_DIR/alloc/src/vec.rs:LL:COL
+   |
+LL | /     pub fn push(&mut self, value: T) {
+LL | |         // This will panic or abort if we would allocate > isize::MAX bytes
+LL | |         // or if the length increment would overflow for zero-sized types.
+LL | |         if self.len == self.buf.capacity() {
+...  |
+LL | |         }
+LL | |     }
+   | |_____^
+note: `const` item defined here
+  --> $DIR/lint-const-item-mutation.rs:31:1
+   |
+LL | const VEC: Vec<i32> = Vec::new();
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+warning: 8 warnings emitted