]> git.lizzy.rs Git - rust.git/commitdiff
Make assignments to `Copy` union fields safe
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Thu, 18 May 2017 10:40:15 +0000 (13:40 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Thu, 25 May 2017 06:36:45 +0000 (09:36 +0300)
src/librustc/middle/effect.rs
src/test/compile-fail/union/union-unsafe.rs

index 5360a86560d399235a660c877c47090fea23bf3f..94362e69c8dd948ab66fcd8f605d9aa821da2384 100644 (file)
@@ -218,6 +218,24 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
                     }
                 }
             }
+            hir::ExprAssign(ref lhs, ref rhs) => {
+                if let hir::ExprField(ref base_expr, field) = lhs.node {
+                    if let ty::TyAdt(adt, ..) = self.tables.expr_ty_adjusted(base_expr).sty {
+                        if adt.is_union() {
+                            let field_ty = self.tables.expr_ty_adjusted(lhs);
+                            let param_env = self.tcx.parameter_environment(adt.did);
+                            if field_ty.moves_by_default(self.tcx, &param_env, field.span) {
+                                self.require_unsafe(field.span,
+                                                    "assignment to non-`Copy` union field");
+                            }
+                            // Do not walk the field expr again.
+                            intravisit::walk_expr(self, base_expr);
+                            intravisit::walk_expr(self, rhs);
+                            return
+                        }
+                    }
+                }
+            }
             _ => {}
         }
 
index 97e1ec2cba865d24d9d8dcd52daf163a4f8a41e1..a67603675f1755af41142253970b0b4215680b81 100644 (file)
 
 #![feature(untagged_unions)]
 
-union U {
+union U1 {
     a: u8
 }
 
+union U2 {
+    a: String
+}
+
+union U3<T> {
+    a: T
+}
+
+union U4<T: Copy> {
+    a: T
+}
+
+fn generic_noncopy<T: Default>() {
+    let mut u3 = U3 { a: T::default() };
+    u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field requires unsafe
+}
+
+fn generic_copy<T: Copy + Default>() {
+    let mut u3 = U3 { a: T::default() };
+    // FIXME: it should be known here that `T: Copy`, need to use correct "parameter environment"
+    u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field requires unsafe
+    let mut u4 = U4 { a: T::default() };
+    u4.a = T::default(); // OK
+}
+
 fn main() {
-    let mut u = U { a: 10 }; // OK
-    let a = u.a; //~ ERROR access to union field requires unsafe function or block
-    u.a = 11; //~ ERROR access to union field requires unsafe function or block
-    let U { a } = u; //~ ERROR matching on union field requires unsafe function or block
-    if let U { a: 12 } = u {} //~ ERROR matching on union field requires unsafe function or block
-    // let U { .. } = u; // OK
+    let mut u1 = U1 { a: 10 }; // OK
+    let a = u1.a; //~ ERROR access to union field requires unsafe
+    u1.a = 11; // OK
+    let U1 { a } = u1; //~ ERROR matching on union field requires unsafe
+    if let U1 { a: 12 } = u1 {} //~ ERROR matching on union field requires unsafe
+    // let U1 { .. } = u1; // OK
+
+    let mut u2 = U2 { a: String::from("old") }; // OK
+    u2.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field requires unsafe
+    let mut u3 = U3 { a: 0 }; // OK
+    u3.a = 1; // OK
+    let mut u3 = U3 { a: String::from("old") }; // OK
+    u3.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field requires unsafe
 }