mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
roc_std: fixes found by running miri
This commit is contained in:
parent
23624ac7d9
commit
d4e77856fe
5 changed files with 46 additions and 30 deletions
|
@ -829,4 +829,30 @@ mod tests {
|
|||
drop(a);
|
||||
drop(b);
|
||||
}
|
||||
|
||||
#[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]);
|
||||
|
||||
let ptr = new_x.ptr_to_allocation();
|
||||
|
||||
drop(y);
|
||||
drop(z);
|
||||
drop(new_x);
|
||||
|
||||
// free the underlying memory
|
||||
unsafe { crate::roc_dealloc(ptr, std::mem::align_of::<usize>() as u32) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -325,7 +325,9 @@ impl RocStr {
|
|||
}
|
||||
}
|
||||
RocStrInnerRef::SmallString(small_str) => {
|
||||
let mut bytes = small_str.bytes;
|
||||
let mut bytes = [0; size_of::<RocList<u8>>()];
|
||||
let mut it = small_str.bytes.iter();
|
||||
bytes = bytes.map(|_| it.next().copied().unwrap_or_default());
|
||||
|
||||
// Even if the small string is at capacity, there will be room to write
|
||||
// a terminator in the byte that's used to store the length.
|
||||
|
@ -380,9 +382,7 @@ impl RocStr {
|
|||
self.with_terminator(terminator, |dest_ptr: *mut u16, str_slice: &str| {
|
||||
// Translate UTF-8 source bytes into UTF-16 and write them into the destination.
|
||||
for (index, wchar) in str_slice.encode_utf16().enumerate() {
|
||||
unsafe {
|
||||
*(dest_ptr.add(index)) = wchar;
|
||||
}
|
||||
unsafe { std::ptr::write_unaligned(dest_ptr.add(index), wchar) };
|
||||
}
|
||||
|
||||
func(dest_ptr, str_slice.len())
|
||||
|
@ -467,7 +467,7 @@ impl RocStr {
|
|||
use core::mem::align_of;
|
||||
|
||||
let terminate = |alloc_ptr: *mut E, str_slice: &str| unsafe {
|
||||
*(alloc_ptr.add(str_slice.len())) = terminator;
|
||||
std::ptr::write_unaligned(alloc_ptr.add(str_slice.len()), terminator);
|
||||
|
||||
func(alloc_ptr, str_slice)
|
||||
};
|
||||
|
@ -548,7 +548,8 @@ impl RocStr {
|
|||
let available_bytes = size_of::<SmallString>();
|
||||
|
||||
if needed_bytes < available_bytes {
|
||||
terminate(small_str.bytes.as_ptr() as *mut E, self.as_str())
|
||||
let mut bytes = small_str.bytes;
|
||||
terminate(&mut bytes as *mut u8 as *mut E, self.as_str())
|
||||
} else {
|
||||
fallback(self.as_str())
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue