]> git.lizzy.rs Git - rust.git/commitdiff
mir-borrowck: Implement end-user output for field of index projection
authorBasile Desloges <basile.desloges@gmail.com>
Fri, 6 Oct 2017 15:26:14 +0000 (17:26 +0200)
committerBasile Desloges <basile.desloges@gmail.com>
Fri, 6 Oct 2017 15:44:50 +0000 (17:44 +0200)
src/librustc_mir/borrow_check.rs
src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs

index 384b1fb418cfd1bccd2d1bbb3cbae043897c7c74..0d9dbfab1e888affca4d07cb35c0d127bbd3478a 100644 (file)
@@ -1125,20 +1125,12 @@ fn describe_field(&self, base: &Lvalue, field_index: usize) -> String {
                 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);
index 103aad693f78279a01f37635dbd7bdacfa99ddb9..7ead9032c135b3884030f282626b3774eb78b785 100644 (file)
@@ -276,4 +276,27 @@ enum E<X> { A(X), B { x: X } }
             _ => 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")
+        }
+    }
 }