]> git.lizzy.rs Git - rust.git/commitdiff
Handle size_of_val for slice types.
authorScott Olson <scott@solson.me>
Tue, 10 May 2016 03:01:12 +0000 (21:01 -0600)
committerScott Olson <scott@solson.me>
Tue, 10 May 2016 03:01:12 +0000 (21:01 -0600)
src/interpreter.rs
tests/run-pass/arrays.rs
tests/run-pass/calls.rs
tests/run-pass/intrinsics.rs [new file with mode: 0755]

index 55f94ce41dc331bc40bfd807424e7aef85e0ab05..3c91998b4a768e30841cd8796c50d87efe7d10a4 100644 (file)
@@ -580,7 +580,17 @@ fn call_intrinsic(
                     let size = self.type_size(ty) as u64;
                     self.memory.write_uint(dest, size, dest_size)?;
                 } else {
-                    panic!("unimplemented: size_of_val::<{:?}>", ty);
+                    match ty.sty {
+                        ty::TySlice(_) | ty::TyStr => {
+                            let elem_ty = ty.sequence_element_type(self.tcx);
+                            let elem_size = self.type_size(elem_ty) as u64;
+                            let ptr_size = self.memory.pointer_size as isize;
+                            let n = self.memory.read_usize(args[0].offset(ptr_size))?;
+                            self.memory.write_uint(dest, n * elem_size, dest_size)?;
+                        }
+
+                        _ => panic!("unimplemented: size_of_val::<{:?}>", ty),
+                    }
                 }
             }
 
index 1fbf24b46684e66fc6d4b94a9af207af499b88a8..d433310a6a89d8cc8ed6cda870b7fc51bca642f8 100644 (file)
@@ -46,7 +46,7 @@ fn slice_index() -> u8 {
 
 #[miri_run]
 fn main() {
-    //assert_eq!(empty_array(), []);
+    // assert_eq!(empty_array(), []);
     assert_eq!(index_unsafe(), 20);
     assert_eq!(index(), 20);
     assert_eq!(slice_index(), 106);
index 68b358145627544220011468ca28d1c82decbcc1..be975320bba93459ef12c289386bd71b93add79f 100644 (file)
@@ -33,17 +33,10 @@ fn cross_crate_fn_call() -> i64 {
     if 1i32.is_positive() { 1 } else { 0 }
 }
 
-// Test one of the simplest intrinsics.
-#[miri_run]
-fn test_size_of() -> usize {
-    ::std::mem::size_of::<Option<i32>>()
-}
-
 #[miri_run]
 fn main() {
     assert_eq!(call(), 2);
     assert_eq!(factorial_recursive(), 3628800);
-    //assert_eq!(call_generic(), (42, true));
+    assert_eq!(call_generic(), (42, true));
     assert_eq!(cross_crate_fn_call(), 1);
-    //assert_eq!(test_size_of(), 8);
 }
diff --git a/tests/run-pass/intrinsics.rs b/tests/run-pass/intrinsics.rs
new file mode 100755 (executable)
index 0000000..5666e19
--- /dev/null
@@ -0,0 +1,13 @@
+#![feature(custom_attribute)]
+#![allow(dead_code, unused_attributes)]
+
+use std::mem::{size_of, size_of_val};
+
+#[miri_run]
+fn main() {
+    assert_eq!(size_of::<Option<i32>>(), 8);
+    assert_eq!(size_of_val(&()), 0);
+    assert_eq!(size_of_val(&42), 4);
+    assert_eq!(size_of_val(&[] as &[i32]), 0);
+    assert_eq!(size_of_val(&[1, 2, 3] as &[i32]), 12);
+}