-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update precondition tests (especially for zero-size access to null) #131384
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//@ run-fail | ||
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes | ||
//@ error-pattern: unsafe precondition(s) violated: Alignment::new_unchecked requires | ||
|
||
#![feature(ptr_alignment_type)] | ||
|
||
fn main() { | ||
unsafe { | ||
std::ptr::Alignment::new_unchecked(0); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/ui/precondition-checks/ascii-char-digit_unchecked.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//@ run-fail | ||
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes | ||
//@ error-pattern: unsafe precondition(s) violated: `ascii::Char::digit_unchecked` input cannot exceed 9 | ||
|
||
#![feature(ascii_char)] | ||
|
||
fn main() { | ||
unsafe { | ||
std::ascii::Char::digit_unchecked(b'a'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: invalid value for `char` | ||
|
||
fn main() { | ||
unsafe { | ||
char::from_u32_unchecked(0xD801); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::copy_nonoverlapping requires | ||
//@ revisions: null_src null_dst misaligned_src misaligned_dst overlapping | ||
|
||
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_nonoverlapping(ptr::null(), dst, 1); | ||
#[cfg(null_dst)] | ||
ptr::copy_nonoverlapping(src, ptr::null_mut(), 1); | ||
#[cfg(misaligned_src)] | ||
ptr::copy_nonoverlapping(src.byte_add(1), dst, 1); | ||
#[cfg(misaligned_dst)] | ||
ptr::copy_nonoverlapping(src, dst.byte_add(1), 1); | ||
#[cfg(overlapping)] | ||
ptr::copy_nonoverlapping(dst, dst.add(1), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//@ run-fail | ||
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes | ||
//@ 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 | ||
|
||
fn main() { | ||
unsafe { | ||
#[cfg(toolarge)] | ||
std::alloc::Layout::from_size_align_unchecked(isize::MAX as usize, 2); | ||
#[cfg(badalign)] | ||
std::alloc::Layout::from_size_align_unchecked(1, 3); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: NonNull::new_unchecked requires | ||
|
||
fn main() { | ||
unsafe { | ||
std::ptr::NonNull::new_unchecked(std::ptr::null_mut::<u8>()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
tests/ui/precondition-checks/out-of-bounds-get-unchecked.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment above should also be updated