]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #41108 - arielb1:tuple-blame, r=estebank
authorCorey Farwell <coreyf@rwell.org>
Thu, 6 Apr 2017 18:55:06 +0000 (14:55 -0400)
committerGitHub <noreply@github.com>
Thu, 6 Apr 2017 18:55:06 +0000 (14:55 -0400)
don't try to blame tuple fields for immutability

Tuple fields don't have an `&T` in their declaration that can be changed
to `&mut T` - skip them..

Fixes #41104.

r? @nikomatsakis

src/librustc/middle/mem_categorization.rs
src/test/ui/did_you_mean/issue-39544.rs
src/test/ui/did_you_mean/issue-39544.stderr

index 3b52e85e08e329c798316505a993f1ca9c267ba8..7d3c17a048917adf85f0477eae8ce3e0bf29d801 100644 (file)
@@ -202,11 +202,14 @@ pub enum ImmutabilityBlame<'tcx> {
 }
 
 impl<'tcx> cmt_<'tcx> {
-    fn resolve_field(&self, field_name: FieldName) -> (&'tcx ty::AdtDef, &'tcx ty::FieldDef)
+    fn resolve_field(&self, field_name: FieldName) -> Option<(&'tcx ty::AdtDef, &'tcx ty::FieldDef)>
     {
-        let adt_def = self.ty.ty_adt_def().unwrap_or_else(|| {
-            bug!("interior cmt {:?} is not an ADT", self)
-        });
+        let adt_def = match self.ty.sty {
+            ty::TyAdt(def, _) => def,
+            ty::TyTuple(..) => return None,
+            // closures get `Categorization::Upvar` rather than `Categorization::Interior`
+            _ =>  bug!("interior cmt {:?} is not an ADT", self)
+        };
         let variant_def = match self.cat {
             Categorization::Downcast(_, variant_did) => {
                 adt_def.variant_with_id(variant_did)
@@ -220,7 +223,7 @@ fn resolve_field(&self, field_name: FieldName) -> (&'tcx ty::AdtDef, &'tcx ty::F
             NamedField(name) => variant_def.field_named(name),
             PositionalField(idx) => &variant_def.fields[idx]
         };
-        (adt_def, field_def)
+        Some((adt_def, field_def))
     }
 
     pub fn immutability_blame(&self) -> Option<ImmutabilityBlame<'tcx>> {
@@ -232,8 +235,9 @@ pub fn immutability_blame(&self) -> Option<ImmutabilityBlame<'tcx>> {
                     Categorization::Local(node_id) =>
                         Some(ImmutabilityBlame::LocalDeref(node_id)),
                     Categorization::Interior(ref base_cmt, InteriorField(field_name)) => {
-                        let (adt_def, field_def) = base_cmt.resolve_field(field_name);
-                        Some(ImmutabilityBlame::AdtFieldDeref(adt_def, field_def))
+                        base_cmt.resolve_field(field_name).map(|(adt_def, field_def)| {
+                            ImmutabilityBlame::AdtFieldDeref(adt_def, field_def)
+                        })
                     }
                     Categorization::Upvar(Upvar { id, .. }) => {
                         if let NoteClosureEnv(..) = self.note {
index 6331fc5771fcbb218694944e4034efe72e3cdad3..d7c8935560623118722a329e82234d7b59a97332 100644 (file)
@@ -51,3 +51,9 @@ pub fn with_arg(z: Z, w: &Z) {
     let _ = &mut z.x;
     let _ = &mut w.x;
 }
+
+pub fn with_tuple() {
+    let mut y = 0;
+    let x = (&y,);
+    *x.0 = 1;
+}
index e1e229a8b05725acb48415d4dc01a566cc60fab4..2e98bc65e9e9f0c268e3badf0b5e61bdc11f914a 100644 (file)
@@ -90,5 +90,11 @@ error: cannot borrow immutable field `w.x` as mutable
 52 |     let _ = &mut w.x;
    |                  ^^^ cannot mutably borrow immutable field
 
-error: aborting due to 11 previous errors
+error: cannot assign to immutable borrowed content `*x.0`
+  --> $DIR/issue-39544.rs:58:5
+   |
+58 |     *x.0 = 1;
+   |     ^^^^^^^^ cannot borrow as mutable
+
+error: aborting due to 12 previous errors