Skip to content

Rollup of 13 pull requests #73511

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

Merged
merged 46 commits into from
Jun 20, 2020
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7349f2c
Added unsafety documentation to shift_head
hbina Apr 25, 2020
9e8b42c
Added unsafety documentation to shift_tail
hbina Apr 25, 2020
9e1e989
Document unsafety in partial_insertion_sort
hbina Apr 25, 2020
c471519
Added unsafety documentation with partition and partition equal
hbina Apr 26, 2020
c710461
Added some unsafety documentation to partition_equal
hbina Apr 26, 2020
afbbd38
Note numeric literals that can never fit in an expected type
ayazhafiz Jun 13, 2020
2b936bb
fixup! Note numeric literals that can never fit in an expected type
ayazhafiz Jun 14, 2020
840176a
asm: Unify pseudo-keyword parsing using `eat`, rather than a final `e…
joshtriplett Jun 14, 2020
50d6d4d
asm: When pretty-printing, don't escape characters twice
joshtriplett Jun 15, 2020
a4337cc
Use `LocalDefId` for import IDs in trait map
petrochenkov Jun 14, 2020
fc13fd0
typeck: Use `LocalDefId`s for the unused trait import set
petrochenkov Jun 15, 2020
8956a7f
Only display other method receiver candidates if they actually apply
Aaron1011 Jun 15, 2020
1078b6f
asm: Allow multiple template strings; interpret them as newline-separ…
joshtriplett Jun 15, 2020
fd9ed30
asm: Update chapter in unstable book for multiple template string arg…
joshtriplett Jun 15, 2020
95e5605
Suggest `?Sized` when applicable for ADTs
estebank Jun 12, 2020
d2b8e60
Account for derived obligations to suggest `?Sized` bound
estebank Jun 12, 2020
f0d3689
fixup! Note numeric literals that can never fit in an expected type
ayazhafiz Jun 16, 2020
7a9f29d
Add initial asm!() support for hexagon
androm3da Jun 9, 2020
5a9df84
Added some more documentations to unsafety blocks in slice/sort.rs
hbina Jun 13, 2020
aa84b0f
Provide `help` when `T: ?Sized` can't be suggested
estebank Jun 17, 2020
d7277df
fixup! Note numeric literals that can never fit in an expected type
ayazhafiz Jun 17, 2020
7a89a33
fixup! Note numeric literals that can never fit in an expected type
ayazhafiz Jun 17, 2020
f633dd3
Implement crate level only lints checking.
crlf0710 Jun 13, 2020
e5c5df8
Add specialization of `ToString for char`
tesuji Jun 18, 2020
40b27ff
review comments: add comment
estebank Jun 18, 2020
3659406
Refactor hir::Place
arora-aman Jun 17, 2020
d5b0737
ty: projections in `transparent_newtype_field`
davidtwco Jun 11, 2020
0e31380
save_analysis: improve handling of enum struct variant
marmeladema Jun 11, 2020
76ad38d
lint: prohibit fields with opaque types
davidtwco Jun 12, 2020
0cccaa0
lint: unify enum variant, union and struct logic
davidtwco Jun 12, 2020
a730d88
ty: simplify `transparent_newtype_field`
davidtwco Jun 12, 2020
39e29ce
`#[deny(unsafe_op_in_unsafe_fn)]` in liballoc
LeSeulArtichaut May 28, 2020
7b63986
Apply suggestions from code review
LeSeulArtichaut May 31, 2020
85e1c3b
Rollup merge of #71568 - hbina:document_unsafety_slice_sort, r=joshtr…
Manishearth Jun 19, 2020
55479de
Rollup merge of #72709 - LeSeulArtichaut:unsafe-liballoc, r=nikomatsakis
Manishearth Jun 19, 2020
bc773fe
Rollup merge of #73214 - androm3da:hex_inline_asm_00, r=Amanieu
Manishearth Jun 19, 2020
186640a
Rollup merge of #73248 - marmeladema:save-analysis-various-fixes, r=X…
Manishearth Jun 19, 2020
17064da
Rollup merge of #73257 - davidtwco:issue-73249-improper-ctypes-projec…
Manishearth Jun 19, 2020
4910206
Rollup merge of #73261 - estebank:generics-sized, r=nikomatsakis
Manishearth Jun 19, 2020
058971c
Rollup merge of #73300 - crlf0710:crate_level_only_check, r=petrochenkov
Manishearth Jun 19, 2020
b285d68
Rollup merge of #73334 - ayazhafiz:err/num-type-cannot-fit, r=estebank
Manishearth Jun 19, 2020
65c33ed
Rollup merge of #73357 - petrochenkov:tmap, r=davidtwco
Manishearth Jun 19, 2020
687f929
Rollup merge of #73364 - joshtriplett:inline-asm, r=Amanieu
Manishearth Jun 19, 2020
b443a10
Rollup merge of #73382 - Aaron1011:fix/self-receiver-candidates, r=pe…
Manishearth Jun 19, 2020
d2272d4
Rollup merge of #73465 - lzutao:spec-char-tostring, r=sfackler
Manishearth Jun 19, 2020
a88182f
Rollup merge of #73489 - sexxi-goose:init_place_refactor, r=nikomatsakis
Manishearth Jun 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added some more documentations to unsafety blocks in slice/sort.rs
  • Loading branch information
hbina committed Jun 16, 2020
commit 5a9df8406ffadb399bf85d4c4d844a4254f89f81
50 changes: 39 additions & 11 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct CopyOnDrop<T> {

impl<T> Drop for CopyOnDrop<T> {
fn drop(&mut self) {
// SAFETY: This is a helper class.
// Please refer to its usage for correctness.
// Namely, one must be sure that `src` and `dst` does not overlap as required by `ptr::copy_nonoverlapping`.
unsafe {
ptr::copy_nonoverlapping(self.src, self.dest, 1);
}
Expand All @@ -43,7 +46,8 @@ where
// 1. We are obtaining pointers to references which are guaranteed to be valid.
// 2. They cannot overlap because we obtain pointers to difference indices of the slice.
// Namely, `i` and `i-1`.
// 3. FIXME: Guarantees that the elements are properly aligned?
// 3. If the slice is properly aligned, the elements are properly aligned.
// It is the caller's responsibility to make sure the slice is properly aligned.
//
// See comments below for further detail.
unsafe {
Expand Down Expand Up @@ -76,18 +80,19 @@ where
F: FnMut(&T, &T) -> bool,
{
let len = v.len();
// SAFETY: As with shift_head, the unsafe operations below involves indexing without a bound check (`get_unchecked` and `get_unchecked_mut`)
// SAFETY: The unsafe operations below involves indexing without a bound check (`get_unchecked` and `get_unchecked_mut`)
// and copying memory (`ptr::copy_nonoverlapping`).
//
// a. Indexing:
// 1. We checked the size of the array to >=2.
// 2. All the indexing that we will do is always between {0 <= index < len-1} at most.
// 1. We checked the size of the array to >= 2.
// 2. All the indexing that we will do is always between `0 <= index < len-1` at most.
//
// b. Memory copying
// 1. We are obtaining pointers to references which are guaranteed to be valid.
// 2. They cannot overlap because we obtain pointers to difference indices of the slice.
// Namely, `i` and `i+1`.
// 3. FIXME: Guarantees that the elements are properly aligned?
// 3. If the slice is properly aligned, the elements are properly aligned.
// It is the caller's responsibility to make sure the slice is properly aligned.
//
// See comments below for further detail.
unsafe {
Expand Down Expand Up @@ -131,8 +136,8 @@ where
let mut i = 1;

for _ in 0..MAX_STEPS {
// SAFETY: We already explicitly done the bound checking with `i<len`
// All our indexing following that is only in the range {0 <= index < len}
// SAFETY: We already explicitly did the bound checking with `i < len`.
// All our subsequent indexing is only in the range `0 <= index < len`
unsafe {
// Find the next pair of adjacent out-of-order elements.
while i < len && !is_less(v.get_unchecked(i), v.get_unchecked(i - 1)) {
Expand Down Expand Up @@ -299,6 +304,16 @@ where
let mut elem = l;

for i in 0..block_l {
// SAFETY: The unsafety operations below involve the usage of the `offset`.
// According to the conditions required by the function, we satisfy them because:
// 1. `offsets_l` is stack-allocated, and thus considered separate allocated object.
// 2. The function `is_less` returns a `bool`.
// Casting a `bool` will never overflow `isize`.
// 3. We have guaranteed that `block_l` will be `<= BLOCK`.
// Plus, `end_l` was initially set to the begin pointer of `offsets_` which was declared on the stack.
// Thus, we know that even in the worst case (all invocations of `is_less` returns false) we will only be at most 1 byte pass the end.
// Another unsafety operation here is dereferencing `elem`.
// However, `elem` was initially the begin pointer to the slice which is always valid.
unsafe {
// Branchless comparison.
*end_l = i as u8;
Expand All @@ -315,6 +330,17 @@ where
let mut elem = r;

for i in 0..block_r {
// SAFETY: The unsafety operations below involve the usage of the `offset`.
// According to the conditions required by the function, we satisfy them because:
// 1. `offsets_r` is stack-allocated, and thus considered separate allocated object.
// 2. The function `is_less` returns a `bool`.
// Casting a `bool` will never overflow `isize`.
// 3. We have guaranteed that `block_r` will be `<= BLOCK`.
// Plus, `end_r` was initially set to the begin pointer of `offsets_` which was declared on the stack.
// Thus, we know that even in the worst case (all invocations of `is_less` returns true) we will only be at most 1 byte pass the end.
// Another unsafety operation here is dereferencing `elem`.
// However, `elem` was initially `1 * sizeof(T)` past the end and we decrement it by `1 * sizeof(T)` before accessing it.
// Plus, `block_r` was asserted to be less than `BLOCK` and `elem` will therefore at most be pointing to the beginning of the slice.
unsafe {
// Branchless comparison.
elem = elem.offset(-1);
Expand Down Expand Up @@ -437,8 +463,9 @@ where
let mut r = v.len();

// SAFETY: The unsafety below involves indexing an array.
// For the first one: we already do the bound checking here with `l<r`.
// For the secondn one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
// For the first one: We already do the bounds checking here with `l < r`.
// For the second one: We initially have `l == 0` and `r == v.len()` and we checked that `l < r` at every indexing operation.
// From here we know that `r` must be at least `r == l` which was shown to be valid from the first one.
unsafe {
// Find the first element greater than or equal to the pivot.
while l < r && is_less(v.get_unchecked(l), pivot) {
Expand Down Expand Up @@ -489,8 +516,9 @@ where
let mut r = v.len();
loop {
// SAFETY: The unsafety below involves indexing an array.
// For the first one: we already do the bound checking here with `l<r`.
// For the second one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
// For the first one: We already do the bounds checking here with `l < r`.
// For the second one: We initially have `l == 0` and `r == v.len()` and we checked that `l < r` at every indexing operation.
// From here we know that `r` must be at least `r == l` which was shown to be valid from the first one.
unsafe {
// Find the first element greater than the pivot.
while l < r && !is_less(pivot, v.get_unchecked(l)) {
Expand Down