]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_trans/intrinsic.rs
Fixes issue 39827: ICE in volatile_store intrinsic
[rust.git] / src / librustc_trans / intrinsic.rs
index 9956c28e6412139d2f2fc2fddc106b74b020a104..5ebd9bed5c8c8a439f2054bdc3ca61e576e0e55c 100644 (file)
@@ -83,6 +83,38 @@ fn get_simple_intrinsic(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
     Some(ccx.get_intrinsic(&llvm_name))
 }
 
+fn warn_if_size_is_weird<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
+                                   tp_ty: Ty<'tcx>,
+                                   count: ValueRef,
+                                   span: Span,
+                                   name: &str) {
+    let ccx = bcx.ccx;
+    let lltp_ty = type_of::type_of(ccx, tp_ty);
+    let ty_size = machine::llsize_of(ccx, lltp_ty);
+    let total = const_to_uint( bcx.mul(ty_size, count) );
+
+    if total > 0 {
+        return;
+    }
+
+    let text = format!("suspicious monomorphization of `{}` intrinsic", name);
+    let note = match name
+    {
+        "volatile_load" | "volatile_store" =>
+             format!("'{}' was specialized with zero-sized type '{}'",
+                    name, tp_ty),
+        _ => format!("'{}' was specialized with type '{}', number of \
+                     elements is {}",
+                     name, tp_ty,
+                     const_to_uint(count))
+    };
+
+    let sess = bcx.sess();
+    sess.struct_span_warn(span, &text)
+        .note(&note)
+        .emit();
+}
+
 /// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
 /// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
 /// add them to librustc_trans/trans/context.rs
@@ -217,17 +249,24 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
         }
 
         "volatile_copy_nonoverlapping_memory" => {
-            copy_intrinsic(bcx, false, true, substs.type_at(0), llargs[0], llargs[1], llargs[2])
+            let tp_ty = substs.type_at(0);
+            warn_if_size_is_weird(bcx, tp_ty, llargs[2], span, name);
+            copy_intrinsic(bcx, false, true, tp_ty, llargs[0], llargs[1], llargs[2])
         }
         "volatile_copy_memory" => {
-            copy_intrinsic(bcx, true, true, substs.type_at(0), llargs[0], llargs[1], llargs[2])
+            let tp_ty = substs.type_at(0);
+            warn_if_size_is_weird(bcx, tp_ty, llargs[2], span, name);
+            copy_intrinsic(bcx, true, true, tp_ty, llargs[0], llargs[1], llargs[2])
         }
         "volatile_set_memory" => {
-            memset_intrinsic(bcx, true, substs.type_at(0), llargs[0], llargs[1], llargs[2])
+            let tp_ty = substs.type_at(0);
+            warn_if_size_is_weird(bcx, tp_ty, llargs[2], span, name);
+            memset_intrinsic(bcx, true, tp_ty, llargs[0], llargs[1], llargs[2])
         }
         "volatile_load" => {
             let tp_ty = substs.type_at(0);
             let mut ptr = llargs[0];
+            warn_if_size_is_weird(bcx, tp_ty, C_uint(ccx,1usize), span, name);
             if let Some(ty) = fn_ty.ret.cast {
                 ptr = bcx.pointercast(ptr, ty.ptr_to());
             }
@@ -239,6 +278,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
         },
         "volatile_store" => {
             let tp_ty = substs.type_at(0);
+            warn_if_size_is_weird(bcx, tp_ty, C_uint(ccx,1usize), span, name);
             if type_is_fat_ptr(bcx.ccx, tp_ty) {
                 bcx.volatile_store(llargs[1], get_dataptr(bcx, llargs[0]));
                 bcx.volatile_store(llargs[2], get_meta(bcx, llargs[0]));
@@ -246,7 +286,11 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
                 let val = if fn_ty.args[1].is_indirect() {
                     bcx.load(llargs[1], None)
                 } else {
-                    from_immediate(bcx, llargs[1])
+                    if !type_is_zero_size(ccx, tp_ty) {
+                        from_immediate(bcx, llargs[1])
+                    } else {
+                        C_nil(ccx)
+                    }
                 };
                 let ptr = bcx.pointercast(llargs[0], val_ty(val).ptr_to());
                 let store = bcx.volatile_store(val, ptr);