-
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
Make from{,_mut}_ptr_range
const
#97419
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
19caa8c
Make `from{,_mut}_ptr_range` const
WaffleLapkin ff9efd8
Add reexport of slice::from{,_mut}_ptr_range to alloc & std
WaffleLapkin a63a83a
Add ui tests for `slice::from_{ptr_range,raw_parts}`
WaffleLapkin 10ee6f8
Rename slice_from_ptr_range_const -> const_slice_from_ptr_range
WaffleLapkin 0cca47e
Use `// error-pattern:` header in `forbidden_slices.rs` test
WaffleLapkin 6d10217
--bless
WaffleLapkin 1f8a641
test forbidden slices on all two usizesizes
WaffleLapkin 1e0747f
normalize forbidden slices
WaffleLapkin 3a2bb78
normalize harder
WaffleLapkin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// run-pass | ||
#![feature( | ||
const_slice_from_raw_parts, | ||
slice_from_ptr_range, | ||
const_slice_from_ptr_range, | ||
pointer_byte_offsets, | ||
const_pointer_byte_offsets | ||
)] | ||
use std::{ | ||
mem::MaybeUninit, | ||
ptr, | ||
slice::{from_ptr_range, from_raw_parts}, | ||
}; | ||
|
||
// Dangling is ok, as long as it's either for ZST reads or for no reads | ||
pub static S0: &[u32] = unsafe { from_raw_parts(dangling(), 0) }; | ||
pub static S1: &[()] = unsafe { from_raw_parts(dangling(), 3) }; | ||
|
||
// References are always valid of reads of a single element (basically `slice::from_ref`) | ||
pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 1) }; | ||
pub static S3: &[MaybeUninit<&u32>] = unsafe { from_raw_parts(&D1, 1) }; | ||
|
||
// Reinterpreting data is fine, as long as layouts match | ||
pub static S4: &[u8] = unsafe { from_raw_parts((&D0) as *const _ as _, 3) }; | ||
// This is only valid because D1 has uninitialized bytes, if it was an initialized pointer, | ||
// that would reinterpret pointers as integers which is UB in CTFE. | ||
pub static S5: &[MaybeUninit<u8>] = unsafe { from_raw_parts((&D1) as *const _ as _, 2) }; | ||
// Even though u32 and [bool; 4] have different layouts, D0 has a value that | ||
// is valid as [bool; 4], so this is not UB (it's basically a transmute) | ||
pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; | ||
|
||
// Structs are considered single allocated objects, | ||
// as long as you don't reinterpret padding as initialized | ||
// data everything is ok. | ||
pub static S7: &[u16] = unsafe { | ||
let ptr = (&D2 as *const Struct as *const u16).byte_add(4); | ||
|
||
from_raw_parts(ptr, 3) | ||
}; | ||
pub static S8: &[MaybeUninit<u16>] = unsafe { | ||
let ptr = &D2 as *const Struct as *const MaybeUninit<u16>; | ||
|
||
from_raw_parts(ptr, 6) | ||
}; | ||
|
||
pub static R0: &[u32] = unsafe { from_ptr_range(dangling()..dangling()) }; | ||
// from_ptr_range panics on zst | ||
//pub static R1: &[()] = unsafe { from_ptr_range(dangling(), dangling().byte_add(3)) }; | ||
pub static R2: &[u32] = unsafe { | ||
let ptr = &D0 as *const u32; | ||
from_ptr_range(ptr..ptr.add(1)) | ||
}; | ||
pub static R3: &[MaybeUninit<&u32>] = unsafe { | ||
let ptr = &D1 as *const MaybeUninit<&u32>; | ||
from_ptr_range(ptr..ptr.add(1)) | ||
}; | ||
pub static R4: &[u8] = unsafe { | ||
let ptr = &D0 as *const u32 as *const u8; | ||
from_ptr_range(ptr..ptr.add(3)) | ||
}; | ||
pub static R5: &[MaybeUninit<u8>] = unsafe { | ||
let ptr = &D1 as *const MaybeUninit<&u32> as *const MaybeUninit<u8>; | ||
from_ptr_range(ptr..ptr.add(2)) | ||
}; | ||
pub static R6: &[bool] = unsafe { | ||
let ptr = &D0 as *const u32 as *const bool; | ||
from_ptr_range(ptr..ptr.add(4)) | ||
}; | ||
pub static R7: &[u16] = unsafe { | ||
let d2 = &D2; | ||
let l = &d2.b as *const u32 as *const u16; | ||
let r = &d2.d as *const u8 as *const u16; | ||
|
||
from_ptr_range(l..r) | ||
}; | ||
pub static R8: &[MaybeUninit<u16>] = unsafe { | ||
let d2 = &D2; | ||
let l = d2 as *const Struct as *const MaybeUninit<u16>; | ||
let r = &d2.d as *const u8 as *const MaybeUninit<u16>; | ||
|
||
from_ptr_range(l..r) | ||
}; | ||
|
||
// Using valid slice is always valid | ||
pub static R9: &[u32] = unsafe { from_ptr_range(R0.as_ptr_range()) }; | ||
pub static R10: &[u32] = unsafe { from_ptr_range(R2.as_ptr_range()) }; | ||
|
||
const D0: u32 = (1 << 16) | 1; | ||
const D1: MaybeUninit<&u32> = MaybeUninit::uninit(); | ||
const D2: Struct = Struct { a: 1, b: 2, c: 3, d: 4 }; | ||
|
||
const fn dangling<T>() -> *const T { | ||
ptr::NonNull::dangling().as_ptr() as _ | ||
} | ||
|
||
#[repr(C)] | ||
struct Struct { | ||
a: u8, | ||
// _pad: [MaybeUninit<u8>; 3] | ||
b: u32, | ||
c: u16, | ||
d: u8, | ||
// _pad: [MaybeUninit<u8>; 1] | ||
} | ||
|
||
fn main() {} |
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.
Oh no, I messed up the feature gate