about summary refs log tree commit diff
path: root/library/core/tests/ptr.rs
AgeCommit message (Collapse)AuthorLines
2025-01-26Put all coretests in a separate cratebjorn3-994/+0
2025-01-08update cfg(bootstrap)Pietro Albini-1/+1
2024-12-23core: fix const ptr::swap_nonoverlapping when there are pointers at odd ↵Ralf Jung-10/+53
offsets in the type
2024-12-02Allow fn pointers comparisons lint in libraryUrgau-0/+1
2024-11-30add test for bytewise ptr::swap of a pointerRalf Jung-0/+19
2024-11-03remove const-support for align_offsetRalf Jung-257/+0
Operations like is_aligned would return actively wrong results at compile-time, i.e. calling it on the same pointer at compiletime and runtime could yield different results. That's no good. Instead of having hacks to make align_offset kind-of work in const-eval, just use const_eval_select in the few places where it makes sense, which also ensures those places are all aware they need to make sure the fallback behavior is consistent.
2024-08-12ignore some vtable/fn ptr equality tests in Miri, their result is not fully ↵Ralf Jung-3/+6
predictable
2024-07-26Fix doc nitsJohn Arundel-1/+1
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-05-29[ACP 362] genericize `ptr::from_raw_parts`Scott McMurray-2/+2
2024-05-28Add an intrinsic for `ptr::metadata`Scott McMurray-0/+12
2024-04-21Use it in the library, and `InstSimplify` it away in the easy placesScott McMurray-0/+8
2024-03-31Require Pointee::Metadata to be FreezeStepan Koltsov-1/+2
So pointee metadata can be used in anonymous statics. This is prerequisite for implementing ThinBox without allocation for ZST. See https://github.com/rust-lang/rust/pull/123184#discussion_r1544627488
2024-03-29Require Debug for Pointee::MetadataStepan Koltsov-1/+9
Useful for debugging
2024-03-16Add as_(mut_)ptr and as_(mut_)slice to raw array pointersYotam Ofek-0/+13
2024-02-21rename ptr::invalid -> ptr::without_provenanceRalf Jung-30/+30
also introduce ptr::dangling matching NonNull::dangling
2024-02-15Replace `NonZero::<_>::new` with `NonZero::new`.Markus Reiter-4/+3
2024-02-15Use generic `NonZero` internally.Markus Reiter-3/+3
2024-01-11apply fmtklensy-4/+20
2024-01-02Adjust library tests for unused_tuple_struct_fields -> dead_codeJake Goulding-19/+19
2023-06-15Extend `unused_must_use` to cover block exprs许杰友 Jieyou Xu (Joe)-1/+1
2023-02-12avoid mixing accesses of ptrs derived from a mutable ref and parent ptrsRalf Jung-3/+3
2022-12-30Replace libstd, libcore, liballoc in line comments.jonathanCogan-1/+1
2022-12-28Update bootstrap cfgPietro Albini-18/+0
2022-11-20avoid non-strict-provenance casts in libcore testsRalf Jung-1/+1
2022-11-19update provenance testLukas Markeffsky-2/+46
* fix allocation alignment for 16bit platforms * add edge case where `stride % align != 0` on pointers with provenance
2022-11-19fix const `align_offset` implementationLukas Markeffsky-41/+63
2022-11-19add coretests for `is_aligned`Lukas Markeffsky-0/+48
2022-11-19add coretests for const `align_offset`Lukas Markeffsky-0/+165
2022-10-22Fix mod_inv termination for the last iterationSimonas Kazlauskas-0/+12
On usize=u64 platforms, the 4th iteration would overflow the `mod_gate` back to 0. Similarly for usize=u32 platforms, the 3rd iteration would overflow much the same way. I tested various approaches to resolving this, including approaches with `saturating_mul` and `widening_mul` to a double usize. Turns out LLVM likes `mul_with_overflow` the best. In fact now, that LLVM can see the iteration count is limited, it will happily unroll the loop into a nice linear sequence. You will also notice that the code around the loop got simplified somewhat. Now that LLVM is handling the loop nicely, there isn’t any more reasons to manually unroll the first iteration out of the loop (though looking at the code today I’m not sure all that complexity was necessary in the first place). Fixes #103361
2022-08-23Make use of `[wrapping_]byte_{add,sub}`Maybe Waffle-1/+1
...replacing `.cast().wrapping_offset().cast()` & similar code.
2022-07-16Auto merge of #98866 - nagisa:nagisa/align-offset-wroom, r=Mark-Simulacrumbors-27/+36
Add a special case for align_offset /w stride != 1 This generalizes the previous `stride == 1` special case to apply to any situation where the requested alignment is divisible by the stride. This in turn allows the test case from #98809 produce ideal assembly, along the lines of: leaq 15(%rdi), %rax andq $-16, %rax This also produces pretty high quality code for situations where the alignment of the input pointer isn’t known: pub unsafe fn ptr_u32(slice: *const u32) -> *const u32 { slice.offset(slice.align_offset(16) as isize) } // => movl %edi, %eax andl $3, %eax leaq 15(%rdi), %rcx andq $-16, %rcx subq %rdi, %rcx shrq $2, %rcx negq %rax sbbq %rax, %rax orq %rcx, %rax leaq (%rdi,%rax,4), %rax Here LLVM is smart enough to replace the `usize::MAX` special case with a branch-less bitwise-OR approach, where the mask is constructed using the neg and sbb instructions. This appears to work across various architectures I’ve tried. This change ends up introducing more branches and code in situations where there is less knowledge of the arguments. For example when the requested alignment is entirely unknown. This use-case was never really a focus of this function, so I’m not particularly worried, especially since llvm-mca is saying that the new code is still appreciably faster, despite all the new branching. Fixes #98809. Sadly, this does not help with #72356.
2022-07-17Add a special case for align_offset /w stride != 1Simonas Kazlauskas-27/+36
This generalizes the previous `stride == 1` special case to apply to any situation where the requested alignment is divisible by the stride. This in turn allows the test case from #98809 produce ideal assembly, along the lines of: leaq 15(%rdi), %rax andq $-16, %rax This also produces pretty high quality code for situations where the alignment of the input pointer isn’t known: pub unsafe fn ptr_u32(slice: *const u32) -> *const u32 { slice.offset(slice.align_offset(16) as isize) } // => movl %edi, %eax andl $3, %eax leaq 15(%rdi), %rcx andq $-16, %rcx subq %rdi, %rcx shrq $2, %rcx negq %rax sbbq %rax, %rax orq %rcx, %rax leaq (%rdi,%rax,4), %rax Here LLVM is smart enough to replace the `usize::MAX` special case with a branch-less bitwise-OR approach, where the mask is constructed using the neg and sbb instructions. This appears to work across various architectures I’ve tried. This change ends up introducing more branches and code in situations where there is less knowledge of the arguments. For example when the requested alignment is entirely unknown. This use-case was never really a focus of this function, so I’m not particularly worried, especially since llvm-mca is saying that the new code is still appreciably faster, despite all the new branching. Fixes #98809. Sadly, this does not help with #72356.
2022-07-05Rollup merge of #97712 - RalfJung:untyped, r=scottmcmDylan DPC-0/+25
ptr::copy and ptr::swap are doing untyped copies The consensus in https://github.com/rust-lang/rust/issues/63159 seemed to be that these operations should be "untyped", i.e., they should treat the data as raw bytes, should work when these bytes violate the validity invariant of `T`, and should exactly preserve the initialization state of the bytes that are being copied. This is already somewhat implied by the description of "copying/swapping size*N bytes" (rather than "N instances of `T`"). The implementations mostly already work that way (well, for LLVM's intrinsics the documentation is not precise enough to say what exactly happens to poison, but if this ever gets clarified to something that would *not* perfectly preserve poison, then I strongly assume there will be some way to make a copy that *does* perfectly preserve poison). However, I had to adjust `swap_nonoverlapping`; after ``@scottmcm's`` [recent changes](https://github.com/rust-lang/rust/pull/94212), that one (sometimes) made a typed copy. (Note that `mem::swap`, which works on mutable references, is unchanged. It is documented as "swapping the values at two mutable locations", which to me strongly indicates that it is indeed typed. It is also safe and can rely on `&mut T` pointing to a valid `T` as part of its safety invariant.) On top of adding a test (that will be run by Miri), this PR then also adjusts the documentation to indeed stably promise the untyped semantics. I assume this means the PR has to go through t-libs (and maybe t-lang?) FCP. Fixes https://github.com/rust-lang/rust/issues/63159
2022-07-01update cfg(bootstrap)sPietro Albini-10/+7
2022-06-27libcore tests: avoid int2ptr castsRalf Jung-13/+13
2022-06-07Use repr(C) when depending on struct layout in ptr testsBen Kimock-0/+1
2022-06-05change ptr::swap methods to do untyped copiesRalf Jung-0/+25
2022-06-03test const_copy to make sure bytewise pointer copies are workingRalf Jung-0/+40
2022-05-13Extend ptr::null and null_mut to all thin (including extern) typesSimon Sapin-0/+12
Fixes https://github.com/rust-lang/rust/issues/93959 This change was accepted in https://rust-lang.github.io/rfcs/2580-ptr-meta.html Note that this changes the signature of **stable** functions. The change should be backward-compatible, but it is **insta-stable** since it cannot (easily, at all?) be made available only through a `#![feature(…)]` opt-in. The RFC also proposed the same change for `NonNull::dangling`, which makes sense it terms of its signature but not in terms of its implementation. `dangling` uses `align_of()` as an address. But what `align_of()` should be for extern types or whether it should be allowed at all remains an open question. This commit depends on https://github.com/rust-lang/rust/pull/93977, which is not yet part of the bootstrap compiler. So `#[cfg]` is used to only apply the change in stage 1+. As far a I know bounds cannot be made conditional with `#[cfg]`, so the entire functions are duplicated. This is unfortunate but temporary. Since this duplication makes it less obvious in the diff, the new definitions differ in: * More permissive bounds (`Thin` instead of implied `Sized`) * Different implementation * Having `rustc_allow_const_fn_unstable(ptr_metadata)`
2022-04-02Rollup merge of #95556 - declanvk:nonnull-provenance, r=dtolnayDylan DPC-0/+78
Implement provenance preserving methods on NonNull ### Description Add the `addr`, `with_addr`, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`. ### Motivation The `NonNull` type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The adding these methods makes it more ergonomic. ### Testing Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance. ### Related Extension of APIs proposed in #95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).
2022-04-01Implement provenance preserving method on NonNullDeclan Kelly-0/+78
**Description** Add the `addr`, `with_addr, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`. **Motiviation** The `NonNull` type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The addition of these methods simply make it more ergonomic to use. **Testing** Added a unit test of a nonnull tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer.
2022-03-31ptr_metadata test: avoid ptr-to-int transmutesRalf Jung-5/+5
2022-03-22Limit test_variadic_fnptr to unixbjorn3-1/+1
2022-03-20Don't declare test_variadic_fnptr with two conflicting signaturesbjorn3-6/+8
It is UB for LLVM and results in a compile error for Cranelift
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-01-30Rollup merge of #92887 - pietroalbini:pa-bootstrap-update, r=Mark-SimulacrumEric Huss-1/+0
Bootstrap compiler update r? ``@Mark-Simulacrum``
2022-01-28update cfg(bootstrap)sPietro Albini-1/+0
2022-01-28Add a test case for using NonNull::new in const contextwoppopo-0/+15
2021-12-12Make `(*mut T)::write_bytes` `const`woppopo-0/+15
2021-04-04Bump cfgsMark Rousskov-7/+0