about summary refs log tree commit diff
path: root/library/core/src/slice/raw.rs
AgeCommit message (Collapse)AuthorLines
2025-06-20Add diagnostic items for ClippySamuel Tardieu-0/+1
2025-05-31terminology: allocated object → allocationRalf Jung-10/+10
2025-05-21Add some track_caller info to precondition panicsBen Kimock-0/+3
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-4/+4
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-02-23rename sub_ptr 😅bendn-2/+2
2024-11-16clean up const stability around UB checksRalf Jung-2/+2
2024-10-15update bootstrap configsJosh Stone-2/+0
2024-10-15replace placeholder versionJosh Stone-2/+2
(cherry picked from commit 567fd9610cbfd220844443487059335d7e1ff021)
2024-10-14Rollup merge of #131384 - saethlin:precondition-tests, r=ibraheemdevMatthias Krüger-2/+2
Update precondition tests (especially for zero-size access to null) I don't much like the current way I've updated the precondition check helpers, but I couldn't come up with anything better. Ideas welcome. I've organized `tests/ui/precondition-checks` mostly with one file per function that has `assert_unsafe_precondition` in it, with revisions that check each precondition. The important new test is `tests/ui/precondition-checks/zero-size-null.rs`.
2024-10-09Allow zero-size reads/writes on null pointersBen Kimock-2/+2
2024-10-09stabilize `{slice,array}::from_mut`ultrabear-1/+2
2024-10-01Stabilize `const_slice_from_raw_parts_mut`Eduardo Sánchez Muñoz-1/+2
2024-09-02clarify language around non-null ptrs in slice::rawoskgo-8/+8
2024-07-29Reformat `use` declarations.Nicholas Nethercote-3/+1
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-12Rollup merge of #123374 - mgeier:doc-slice-from-raw-parts, r=scottmcmJubilee-0/+33
DOC: Add FFI example for slice::from_raw_parts() For some discussion, see https://users.rust-lang.org/t/missing-guidance-on-converting-ffi-ptr-length-to-slice/106048 See also #120608.
2024-05-13Add `size_of`, `size_of_val`, `align_of`, and `align_of_val` to the preludeJosh Triplett-1/+0
Many, many projects use `size_of` to get the size of a type. However, it's also often equally easy to hardcode a size (e.g. `8` instead of `size_of::<u64>()`). Minimizing friction in the use of `size_of` helps ensure that people use it and make code more self-documenting. The name `size_of` is unambiguous: the name alone, without any prefix or path, is self-explanatory and unmistakeable for any other functionality. Adding it to the prelude cannot produce any name conflicts, as any local definition will silently shadow the one from the prelude. Thus, we don't need to wait for a new edition prelude to add it. Add `size_of_val`, `align_of`, and `align_of_val` as well, with similar justification: widely useful, self-explanatory, unmistakeable for anything else, won't produce conflicts.
2024-04-26Add missing .into_iter()Matthias Geier-1/+1
2024-04-26Extend the example code and assert the resultMatthias Geier-3/+10
2024-04-26Add "safety" commentMatthias Geier-0/+5
2024-04-02DOC: Add FFI example for slice::from_raw_parts()Matthias Geier-0/+21
2024-03-23move assert_unsafe_preconditions to its own fileRalf Jung-9/+7
These macros and functions are not intrinsics, after all.
2024-03-08Distinguish between library and lang UB in assert_unsafe_preconditionBen Kimock-0/+2
2024-02-08Rewrite assert_unsafe_precondition around the new intrinsicBen Kimock-4/+17
2023-10-05Add more diagnostic items for clippyJason Newcomb-0/+2
2023-06-16slice::from_raw_parts: mention no-wrap-around conditionRalf Jung-10/+14
2022-10-26Print the precondition we violated, and visible through output captureBen Kimock-4/+8
Co-authored-by: Ralf Jung <post@ralfj.de>
2022-10-20Use a faster allocation size check in slice::from_raw_partsBen Kimock-5/+5
2022-10-03Sync docs of `slice::{from_ptr_range, from_ptr_range_mut}`Maybe Waffle-1/+15
2022-10-03Document when `slice::from_ptr_range[_mut]` panicMaybe Waffle-0/+8
2022-09-04Make `const_eval_select` a real intrinsicDeadbeef-2/+2
2022-07-09Partially stabilize const_slice_from_raw_partsKonrad Borowski-2/+2
This doesn't stabilize methods working on mutable pointers.
2022-06-03Rollup merge of #97366 - WaffleLapkin:stabilize_array_slice_from_ref, r=dtolnayDylan DPC-1/+1
Stabilize `{slice,array}::from_ref` This PR stabilizes the following APIs as `const` functions in Rust `1.63`: ```rust // core::array pub const fn from_ref<T>(s: &T) -> &[T; 1]; // core::slice pub const fn from_ref<T>(s: &T) -> &[T]; ``` Note that the `mut` versions are not stabilized as unique references (`&mut _`) are [unstable in const context]. FCP: https://github.com/rust-lang/rust/issues/90206#issuecomment-1134586665 r? rust-lang/libs-api `@rustbot` label +T-libs-api -T-libs [unstable in const context]: https://github.com/rust-lang/rust/issues/57349
2022-05-31Fixup feature name to be more consistent with othersMaybe Waffle-1/+1
`slice_from_mut_ptr_range_const` -> `const_slice_from_mut_ptr_range`, we usually have `const` in the front.
2022-05-30Rename slice_from_ptr_range_const -> const_slice_from_ptr_rangeMaybe Waffle-1/+1
This is in line with other `const fn` features.
2022-05-30Make `from{,_mut}_ptr_range` constMaybe Waffle-2/+4
- `from_ptr_range` uses `#![feature(slice_from_ptr_range_const)]` - `from_mut_ptr_range` uses `#![feature(slice_from_mut_ptr_range_const)]`
2022-05-24Stabilize `{slice,array}::from_ref`Maybe Waffle-1/+1
2022-05-11Rename `unsigned_offset_from` to `sub_ptr`Scott McMurray-2/+2
2022-05-11Add `unsigned_offset_from` on pointersScott McMurray-2/+2
Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
2022-04-03Auto merge of #92686 - saethlin:unsafe-debug-asserts, r=Amanieubors-38/+13
Add debug assertions to some unsafe functions As suggested by https://github.com/rust-lang/rust/issues/51713 ~~Some similar code calls `abort()` instead of `panic!()` but aborting doesn't work in a `const fn`, and the intrinsic for doing dispatch based on whether execution is in a const is unstable.~~ This picked up some invalid uses of `get_unchecked` in the compiler, and fixes them. I can confirm that they do in fact pick up invalid uses of `get_unchecked` in the wild, though the user experience is less-than-awesome: ``` Running unittests (target/x86_64-unknown-linux-gnu/debug/deps/rle_decode_fast-04b7918da2001b50) running 6 tests error: test failed, to rerun pass '--lib' Caused by: process didn't exit successfully: `/home/ben/rle-decode-helper/target/x86_64-unknown-linux-gnu/debug/deps/rle_decode_fast-04b7918da2001b50` (signal: 4, SIGILL: illegal instruction) ``` ~~As best I can tell these changes produce a 6% regression in the runtime of `./x.py test` when `[rust] debug = true` is set.~~ Latest commit (https://github.com/rust-lang/rust/pull/92686/commits/6894d559bdb4365243b3f4bf73f18e4b1bed04d1) brings the additional overhead from this PR down to 0.5%, while also adding a few more assertions. I think this actually covers all the places in `core` that it is reasonable to check for safety requirements at runtime. Thoughts?
2022-03-29Add debug assertions to some unsafe functionsBen Kimock-38/+13
These debug assertions are all implemented only at runtime using `const_eval_select`, and in the error path they execute `intrinsics::abort` instead of being a normal debug assertion to minimize the impact of these assertions on code size, when enabled. Of all these changes, the bounds checks for unchecked indexing are expected to be most impactful (case in point, they found a problem in rustc).
2022-03-24add #[must_use] to functions of slice and its iterators.Jendrik-0/+4
2022-02-27add `slice::{from_ptr_range, from_mut_ptr_range}`Ibraheem Ahmed-0/+111
2021-12-10Rollup merge of #91325 - RalfJung:const_eval_select, r=dtolnayMatthias Krüger-2/+2
adjust const_eval_select documentation "The Rust compiler assumes" indicates that this is language UB, but [I don't think that is a good idea](https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/const_eval_select.20assumptions). This UB would be very hard to test for and looks like a way-too-big footgun. ``@oli-obk`` suggested this is meant to be more like "library UB", so I tried to adjust the docs accordingly. I also removed all references to "referential transparency". That is a rather vague concept used to mean many different things, and I honestly have no idea what exactly is meant by it in this specific instance. But I assume ``@fee1-dead`` had in their mind a property that all `const fn` code upholds, so by demanding that the runtime code and the const-time code are *observably equivalent*, whatever that property is would also be enforced here. Cc ``@rust-lang/wg-const-eval``
2021-11-30Apply cfg-bootstrap switchMark Rousskov-2/+2
2021-11-28adjust some const_eval_select safety commentsRalf Jung-2/+2
2021-10-29Apply changes proposed in the reviewMaybe Waffle-8/+9
2021-10-29Use proper issue number for `feature(const_slice_from_raw_parts)`Maybe Waffle-4/+4
2021-10-28Make `core::slice::from_raw_parts[_mut]` constMaybe Waffle-14/+41
2021-10-23Fill tracking issue for `const_slice_from_ref` and `const_array_from_ref`Maybe Waffle-2/+2
2021-10-22Mark {array,slice}::{from_ref,from_mut} as const fnMaybe Waffle-2/+4