roc_std: fixes found by running miri

This commit is contained in:
Folkert 2023-10-15 20:09:05 +02:00
parent 23624ac7d9
commit d4e77856fe
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
5 changed files with 46 additions and 30 deletions

View file

@ -54,7 +54,7 @@ pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut
#[cfg(test)]
mod test_roc_std {
use roc_std::{RocBox, RocDec, RocList, RocResult, RocStr, SendSafeRocList, SendSafeRocStr};
use roc_std::{RocBox, RocDec, RocList, RocResult, RocStr, SendSafeRocStr};
fn roc_str_byte_representation(string: &RocStr) -> [u8; RocStr::SIZE] {
unsafe { core::mem::transmute_copy(string) }
@ -358,23 +358,6 @@ mod test_roc_std {
let roc_list = RocList::<RocStr>::empty();
assert_eq!(roc_list.is_unique(), true);
}
#[test]
fn readonly_list_is_sendsafe() {
let x = RocList::from_slice(&[1, 2, 3, 4, 5]);
unsafe { x.set_readonly() };
assert_eq!(x.is_readonly(), true);
let y = x.clone();
let z = y.clone();
let safe_x = SendSafeRocList::from(x);
let new_x = RocList::from(safe_x);
assert_eq!(new_x.is_readonly(), true);
assert_eq!(y.is_readonly(), true);
assert_eq!(z.is_readonly(), true);
assert_eq!(new_x.as_slice(), &[1, 2, 3, 4, 5]);
}
}
#[cfg(test)]
@ -413,12 +396,18 @@ mod with_terminator {
// utf16_nul_terminated
{
let answer = roc_str.utf16_nul_terminated(|ptr, len| {
let bytes: &[u16] = unsafe { slice::from_raw_parts(ptr.cast(), len + 1) };
let bytes: &[u8] = unsafe { slice::from_raw_parts(ptr as *mut u8, 2 * (len + 1)) };
let items: Vec<u16> = bytes
.chunks(2)
.map(|c| c.try_into().unwrap())
.map(|c| u16::from_ne_bytes(c))
.collect();
// Verify that it's nul-terminated
assert_eq!(bytes[len], 0);
assert_eq!(items[len], 0);
let string = String::from_utf16(&bytes[0..len]).unwrap();
let string = String::from_utf16(&items[0..len]).unwrap();
assert_eq!(string.as_str(), string);