Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Oct 9, 2024
1 parent 7188e5f commit d5666ed
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 33 deletions.
18 changes: 0 additions & 18 deletions tests/ui/precondition-checks/TODO
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
ptr::swap_nonoverlapping
ptr::replace
ptr::read
ptr::write
ptr::read_volatile
ptr::write_volatile

// for range::Range and for ops::Range?
str::get_unchecked
str::get_unchecked_mut
Expand All @@ -14,19 +7,8 @@ IndexRange::new_unchecked
char::from_u32_unchecked
ascii::Char::digit_unchecked

ptr::copy
ptr::write_bytes

slice::from_raw_parts
slice::from_raw_parts_mut

unchecked_add
unchecked_sub
unchecked_mul
unchecked_shr
unchecked_shl

NonZero::from_mut_unchecked

unreachable_unchecked
assert_unchecked
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/alignment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: Alignment::new_unchecked
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: Alignment::new_unchecked requires

#![feature(ptr_alignment_type)]

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/precondition-checks/assert_unchecked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: hint::assert_unchecked must never be called when the condition is false

fn main() {
unsafe {
std::hint::assert_unchecked(false);
}
}
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/copy-nonoverlapping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::copy_nonoverlapping
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: ptr::copy_nonoverlapping requires
//@ revisions: null_src null_dst misaligned_src misaligned_dst overlapping

use std::ptr;
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/precondition-checks/copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::copy requires
//@ revisions: null_src null_dst misaligned_src misaligned_dst

use std::ptr;

fn main() {
let src = [0u16; 3];
let mut dst = [0u16; 3];
let src = src.as_ptr();
let dst = dst.as_mut_ptr();
unsafe {
#[cfg(null_src)]
ptr::copy(ptr::null(), dst, 1);
#[cfg(null_dst)]
ptr::copy(src, ptr::null_mut(), 1);
#[cfg(misaligned_src)]
ptr::copy(src.byte_add(1), dst, 1);
#[cfg(misaligned_dst)]
ptr::copy(src, dst.byte_add(1), 1);
}
}
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/get-unchecked.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: slice::get_unchecked requires
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: slice::get_unchecked requires

fn main() {
unsafe {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: Layout::from_size_align_unchecked
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: Layout::from_size_align_unchecked requires
//@ revisions: toolarge badalign
//@[toolarge] compile-flags: --cfg toolarge
//@[badalign] compile-flags: --cfg badalign
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/nonnull.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: NonNull::new_unchecked
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: NonNull::new_unchecked requires

fn main() {
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: NonZero::from_mut_unchecked requires

#![feature(nonzero_from_mut)]

fn main() {
unsafe {
let mut num = 0u8;
std::num::NonZeroU8::from_mut_unchecked(&mut num);
}
}
9 changes: 9 additions & 0 deletions tests/ui/precondition-checks/nonzero-new_unchecked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: NonZero::new_unchecked requires

fn main() {
unsafe {
std::num::NonZeroU8::new_unchecked(0);
}
}
18 changes: 18 additions & 0 deletions tests/ui/precondition-checks/read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::read requires
//@ revisions: null misaligned
//@ ignore-test

use std::ptr;

fn main() {
let src = [0u16; 2];
let src = src.as_ptr();
unsafe {
#[cfg(null)]
ptr::read(ptr::null::<u8>());
#[cfg(misaligned)]
ptr::read(src.byte_add(1));
}
}
17 changes: 17 additions & 0 deletions tests/ui/precondition-checks/read_volatile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::read_volatile requires
//@ revisions: null misaligned

use std::ptr;

fn main() {
let src = [0u16; 2];
let src = src.as_ptr();
unsafe {
#[cfg(null)]
ptr::read_volatile(ptr::null::<u8>());
#[cfg(misaligned)]
ptr::read_volatile(src.byte_add(1));
}
}
17 changes: 17 additions & 0 deletions tests/ui/precondition-checks/replace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::replace requires
//@ revisions: null misaligned

use std::ptr;

fn main() {
let mut dst = [0u16; 2];
let dst = dst.as_mut_ptr();
unsafe {
#[cfg(null)]
ptr::replace(ptr::null_mut::<u8>(), 1);
#[cfg(misaligned)]
ptr::replace(dst.byte_add(1), 1u16);
}
}
6 changes: 3 additions & 3 deletions tests/ui/precondition-checks/slice-from-raw-parts-mut.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts_mut
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts_mut requires
//@ revisions: null misaligned toolarge

fn main() {
Expand All @@ -11,6 +10,7 @@ fn main() {
#[cfg(misaligned)]
let _s: &mut [u16] = std::slice::from_raw_parts_mut(1usize as *mut u16, 0);
#[cfg(toolarge)]
let _s: &mut [u16] = std::slice::from_raw_parts_mut(2usize as *mut u16, isize::MAX as usize);
let _s: &mut [u16] =
std::slice::from_raw_parts_mut(2usize as *mut u16, isize::MAX as usize);
}
}
3 changes: 1 addition & 2 deletions tests/ui/precondition-checks/slice-from-raw-parts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts
//@ ignore-debug
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts requires
//@ revisions: null misaligned toolarge

fn main() {
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/precondition-checks/swap-nonoverlapping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::swap_nonoverlapping requires
//@ revisions: null_src null_dst misaligned_src misaligned_dst overlapping

use std::ptr;

fn main() {
let mut src = [0u16; 3];
let mut dst = [0u16; 3];
let src = src.as_mut_ptr();
let dst = dst.as_mut_ptr();
unsafe {
#[cfg(null_src)]
ptr::swap_nonoverlapping(ptr::null_mut(), dst, 1);
#[cfg(null_dst)]
ptr::swap_nonoverlapping(src, ptr::null_mut(), 1);
#[cfg(misaligned_src)]
ptr::swap_nonoverlapping(src.byte_add(1), dst, 1);
#[cfg(misaligned_dst)]
ptr::swap_nonoverlapping(src, dst.byte_add(1), 1);
#[cfg(overlapping)]
ptr::swap_nonoverlapping(dst, dst.add(1), 2);
}
}
9 changes: 9 additions & 0 deletions tests/ui/precondition-checks/unreachable_unchecked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached

fn main() {
unsafe {
std::hint::unreachable_unchecked();
}
}
18 changes: 18 additions & 0 deletions tests/ui/precondition-checks/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::write requires
//@ revisions: null misaligned
//@ ignore-test

use std::ptr;

fn main() {
let mut dst = [0u16; 2];
let mut dst = dst.as_mut_ptr();
unsafe {
#[cfg(null)]
ptr::write(ptr::null_mut::<u8>(), 1u8);
#[cfg(misaligned)]
ptr::write(dst.byte_add(1), 1u16);
}
}
18 changes: 18 additions & 0 deletions tests/ui/precondition-checks/write_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::write requires
//@ revisions: null misaligned
//@ ignore-test

use std::ptr;

fn main() {
let mut dst = [0u16; 2];
let mut dst = dst.as_mut_ptr();
unsafe {
#[cfg(null)]
ptr::write_bytes(ptr::null_mut::<u8>(), 1u8, 2);
#[cfg(misaligned)]
ptr::write_bytes(dst.byte_add(1), 1u8, 2);
}
}
17 changes: 17 additions & 0 deletions tests/ui/precondition-checks/write_volatile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ run-fail
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: ptr::write_volatile requires
//@ revisions: null misaligned

use std::ptr;

fn main() {
let mut dst = [0u16; 2];
let mut dst = dst.as_mut_ptr();
unsafe {
#[cfg(null)]
ptr::write_volatile(ptr::null_mut::<u8>(), 1u8);
#[cfg(misaligned)]
ptr::write_volatile(dst.byte_add(1), 1u16);
}
}

0 comments on commit d5666ed

Please sign in to comment.