summary refs log tree commit diff
path: root/src/test/ui/error-codes
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-09-10 05:54:26 +0000
committerbors <bors@rust-lang.org>2020-09-10 05:54:26 +0000
commit88197214b8a9099bb3da559a3bd7bf4867c10c5f (patch)
treeac629e272c836b5dd523baec4209f32110843244 /src/test/ui/error-codes
parenta1894e4afe1a39f718cc27232a5a2f0d02b501f6 (diff)
parent4434e8cefbec96e6928ea23769eb1a83d0f198b5 (diff)
downloadrust-88197214b8a9099bb3da559a3bd7bf4867c10c5f.tar.gz
rust-88197214b8a9099bb3da559a3bd7bf4867c10c5f.zip
Auto merge of #75573 - Aaron1011:feature/const-mutation-lint, r=oli-obk
Add CONST_ITEM_MUTATION lint

Fixes #74053
Fixes #55721

This PR adds a new lint `CONST_ITEM_MUTATION`.
Given an item `const FOO: SomeType = ..`, this lint fires on:

* Attempting to write directly to a field (`FOO.field = some_val`) or
  array entry (`FOO.array_field[0] = val`)
* Taking a mutable reference to the `const` item (`&mut FOO`), including
  through an autoderef `FOO.some_mut_self_method()`

The lint message explains that since each use of a constant creates a
new temporary, the original `const` item will not be modified.
Diffstat (limited to 'src/test/ui/error-codes')
-rw-r--r--src/test/ui/error-codes/E0017.rs2
-rw-r--r--src/test/ui/error-codes/E0017.stderr41
-rw-r--r--src/test/ui/error-codes/E0388.rs2
-rw-r--r--src/test/ui/error-codes/E0388.stderr39
4 files changed, 73 insertions, 11 deletions
diff --git a/src/test/ui/error-codes/E0017.rs b/src/test/ui/error-codes/E0017.rs
index 818dec1207b..54d3cc54a84 100644
--- a/src/test/ui/error-codes/E0017.rs
+++ b/src/test/ui/error-codes/E0017.rs
@@ -3,9 +3,11 @@ const C: i32 = 2;
 static mut M: i32 = 3;
 
 const CR: &'static mut i32 = &mut C; //~ ERROR E0764
+                                     //~| WARN taking a mutable
 static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
                                               //~| ERROR E0019
                                               //~| ERROR cannot borrow
 static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
+                                              //~| WARN taking a mutable
 static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
 fn main() {}
diff --git a/src/test/ui/error-codes/E0017.stderr b/src/test/ui/error-codes/E0017.stderr
index c1d96de1dca..40ef6bd97b3 100644
--- a/src/test/ui/error-codes/E0017.stderr
+++ b/src/test/ui/error-codes/E0017.stderr
@@ -1,3 +1,18 @@
+warning: taking a mutable reference to a `const` item
+  --> $DIR/E0017.rs:5:30
+   |
+LL | const CR: &'static mut i32 = &mut C;
+   |                              ^^^^^^
+   |
+   = note: `#[warn(const_item_mutation)]` on by default
+   = 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: `const` item defined here
+  --> $DIR/E0017.rs:2:1
+   |
+LL | const C: i32 = 2;
+   | ^^^^^^^^^^^^^^^^^
+
 error[E0764]: mutable references are not allowed in constants
   --> $DIR/E0017.rs:5:30
    |
@@ -5,7 +20,7 @@ LL | const CR: &'static mut i32 = &mut C;
    |                              ^^^^^^ `&mut` is only allowed in `const fn`
 
 error[E0019]: static contains unimplemented expression type
-  --> $DIR/E0017.rs:6:39
+  --> $DIR/E0017.rs:7:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^
@@ -13,30 +28,44 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
    = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0017.rs:6:39
+  --> $DIR/E0017.rs:7:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ `&mut` is only allowed in `const fn`
 
 error[E0596]: cannot borrow immutable static item `X` as mutable
-  --> $DIR/E0017.rs:6:39
+  --> $DIR/E0017.rs:7:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ cannot borrow as mutable
 
+warning: taking a mutable reference to a `const` item
+  --> $DIR/E0017.rs:10:38
+   |
+LL | static CONST_REF: &'static mut i32 = &mut C;
+   |                                      ^^^^^^
+   |
+   = 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: `const` item defined here
+  --> $DIR/E0017.rs:2:1
+   |
+LL | const C: i32 = 2;
+   | ^^^^^^^^^^^^^^^^^
+
 error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0017.rs:9:38
+  --> $DIR/E0017.rs:10:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
    |                                      ^^^^^^ `&mut` is only allowed in `const fn`
 
 error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0017.rs:10:52
+  --> $DIR/E0017.rs:12:52
    |
 LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
    |                                                    ^^^^^^ `&mut` is only allowed in `const fn`
 
-error: aborting due to 6 previous errors
+error: aborting due to 6 previous errors; 2 warnings emitted
 
 Some errors have detailed explanations: E0019, E0596, E0764.
 For more information about an error, try `rustc --explain E0019`.
diff --git a/src/test/ui/error-codes/E0388.rs b/src/test/ui/error-codes/E0388.rs
index 13131017c2e..8ad586bb30f 100644
--- a/src/test/ui/error-codes/E0388.rs
+++ b/src/test/ui/error-codes/E0388.rs
@@ -2,9 +2,11 @@ static X: i32 = 1;
 const C: i32 = 2;
 
 const CR: &'static mut i32 = &mut C; //~ ERROR E0764
+                                     //~| WARN taking a mutable
 static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
                                               //~| ERROR cannot borrow
                                               //~| ERROR E0764
 static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
+                                             //~| WARN taking a mutable
 
 fn main() {}
diff --git a/src/test/ui/error-codes/E0388.stderr b/src/test/ui/error-codes/E0388.stderr
index f09100bac43..39bc717ceec 100644
--- a/src/test/ui/error-codes/E0388.stderr
+++ b/src/test/ui/error-codes/E0388.stderr
@@ -1,3 +1,18 @@
+warning: taking a mutable reference to a `const` item
+  --> $DIR/E0388.rs:4:30
+   |
+LL | const CR: &'static mut i32 = &mut C;
+   |                              ^^^^^^
+   |
+   = note: `#[warn(const_item_mutation)]` on by default
+   = 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: `const` item defined here
+  --> $DIR/E0388.rs:2:1
+   |
+LL | const C: i32 = 2;
+   | ^^^^^^^^^^^^^^^^^
+
 error[E0764]: mutable references are not allowed in constants
   --> $DIR/E0388.rs:4:30
    |
@@ -5,7 +20,7 @@ LL | const CR: &'static mut i32 = &mut C;
    |                              ^^^^^^ `&mut` is only allowed in `const fn`
 
 error[E0019]: static contains unimplemented expression type
-  --> $DIR/E0388.rs:5:39
+  --> $DIR/E0388.rs:6:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^
@@ -13,24 +28,38 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
    = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
 
 error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0388.rs:5:39
+  --> $DIR/E0388.rs:6:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ `&mut` is only allowed in `const fn`
 
 error[E0596]: cannot borrow immutable static item `X` as mutable
-  --> $DIR/E0388.rs:5:39
+  --> $DIR/E0388.rs:6:39
    |
 LL | static STATIC_REF: &'static mut i32 = &mut X;
    |                                       ^^^^^^ cannot borrow as mutable
 
+warning: taking a mutable reference to a `const` item
+  --> $DIR/E0388.rs:9:38
+   |
+LL | static CONST_REF: &'static mut i32 = &mut C;
+   |                                      ^^^^^^
+   |
+   = 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: `const` item defined here
+  --> $DIR/E0388.rs:2:1
+   |
+LL | const C: i32 = 2;
+   | ^^^^^^^^^^^^^^^^^
+
 error[E0764]: mutable references are not allowed in statics
-  --> $DIR/E0388.rs:8:38
+  --> $DIR/E0388.rs:9:38
    |
 LL | static CONST_REF: &'static mut i32 = &mut C;
    |                                      ^^^^^^ `&mut` is only allowed in `const fn`
 
-error: aborting due to 5 previous errors
+error: aborting due to 5 previous errors; 2 warnings emitted
 
 Some errors have detailed explanations: E0019, E0596, E0764.
 For more information about an error, try `rustc --explain E0019`.