about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBasile Desloges <basile.desloges@gmail.com>2017-10-06 17:26:14 +0200
committerBasile Desloges <basile.desloges@gmail.com>2017-10-06 17:44:50 +0200
commit9ce2f3af9379246909d3e0151b956ad428bbc175 (patch)
tree6a4b7af39a21360ad90b7b9118f4786a4affa1d8
parentce3b2e779e56b99a682086d7d9575260158f1ad1 (diff)
downloadrust-9ce2f3af9379246909d3e0151b956ad428bbc175.tar.gz
rust-9ce2f3af9379246909d3e0151b956ad428bbc175.zip
mir-borrowck: Implement end-user output for field of index projection
-rw-r--r--src/librustc_mir/borrow_check.rs12
-rw-r--r--src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs23
2 files changed, 25 insertions, 10 deletions
diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs
index 384b1fb418c..0d9dbfab1e8 100644
--- a/src/librustc_mir/borrow_check.rs
+++ b/src/librustc_mir/borrow_check.rs
@@ -1125,20 +1125,12 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
                 match proj.elem {
                     ProjectionElem::Deref =>
                         self.describe_field(&proj.base, field_index),
-                    ProjectionElem::Index(..) => {
-                        debug!("End-user description not implemented for field of projection {:?}",
-                               proj);
-                        format!("<index>{}", field_index)
-                    },
-                    ProjectionElem::ConstantIndex { .. } => {
-                        debug!("End-user description not implemented for field of projection {:?}",
-                               proj);
-                        format!("<constant_index>{}", field_index)
-                    },
                     ProjectionElem::Downcast(def, variant_index) =>
                         format!("{}", def.variants[variant_index].fields[field_index].name),
                     ProjectionElem::Field(_, field_type) =>
                         self.describe_field_from_ty(&field_type, field_index),
+                    ProjectionElem::Index(..) | ProjectionElem::ConstantIndex { .. } =>
+                        format!("{}", self.describe_field(&proj.base, field_index)),
                     ProjectionElem::Subslice { .. } => {
                         debug!("End-user description not implemented for field of projection {:?}",
                                proj);
diff --git a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs
index 103aad693f7..7ead9032c13 100644
--- a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs
+++ b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs
@@ -276,4 +276,27 @@ fn main() {
             _ => panic!("other case"),
         }
     }
+    // Field of index
+    {
+        struct F {x: u32, y: u32};
+        let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
+        let _v = &mut v;
+        v[0].y;
+        //[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
+        //[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed (Ast)
+        //[mir]~| ERROR cannot use `v[..].y` because it was mutably borrowed (Mir)
+        //[mir]~| ERROR cannot use `(*v)` because it was mutably borrowed (Mir)
+    }
+    // Field of constant index
+    {
+        struct F {x: u32, y: u32};
+        let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
+        let _v = &mut v;
+        match v {
+            &[_, F {x: ref xf, ..}] => println!("{}", xf),
+            //[mir]~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable (Mir)
+            // No errors in AST
+            _ => panic!("other case")
+        }
+    }
 }