Commit Graph

149 Commits

Author SHA1 Message Date
Deadbeef cf61cbcb07 Move into const impl blocks 2026-01-01 19:17:11 -05:00
Deadbeef 3982d3e706 Vec::push in consts MVP 2026-01-01 19:17:11 -05:00
bors 1d6c526bb0 Auto merge of #149690 - RustyYato:inline-layout-helper, r=saethlin
Add `#[inline]` to `Layout::is_size_align_valid`

Fixes rust-lang/rust#149687

r? `@saethlin`
2025-12-07 13:43:57 +00:00
RustyYato 5f9aca7e74 Add #[inline] to Layout::is_size_align_valid
add Alignment::new_unchecked::precondition_check to allowlist
2025-12-06 19:47:13 -06:00
Matthias Krüger 865650a52f Rollup merge of #146826 - bluurryy:impl-allocator-for-mut-a, r=scottmcm
Implement `Allocator` for `&mut A` where `A: Allocator + ?Sized`

This is a breaking change to the unstable `allocator_api` feature. Tracking issue rust-lang/rust#32838.
It implements the accepted api change proposal from rust-lang/libs-team#508.

The code for the blanket implementation is the same as the one for `&A`, just with the `mut` added.
2025-12-06 09:57:57 +01:00
Orson Peters f9b0811d01 Add documentation guaranteeing global allocator use of TLS
Remove outdated part of comment claiming thread_local re-enters global allocator

Fix typo in doc comment

Add comments for guarantees given and footnote that System may still be called

Revise mention of using the global allocator

Allow for the possibility that the global allocator is the system allocator.

Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2025-11-28 00:09:12 +01:00
Scott McMurray 30f4a2ae16 See if this is the time we can remove layout::size_align
This was a bad idea before, but now that `size_of` and `align_of` work completely differently than when removing it was first tried in 2020, maybe it makes sense now.

(Or maybe I'll just add another attempt to the list in the comments...)
2025-11-19 12:55:21 -08:00
Azzybana Raccoon 79c0897aef Update layout.rs
perf: removed unnecessary let for return only
2025-10-25 02:54:28 -04:00
Ben Kimock 72396d9f18 Clarify that UB will occur, not can/may in GlobalAlloc docs 2025-10-14 21:12:11 -04:00
bluurryy 2b228ce310 Implement Allocator for &mut A where A: Allocator + ?Sized 2025-09-21 01:42:06 +02:00
Sacha Ayoun 427be23b22 Address dangling doc
Signed-off-by: Sacha Ayoun <sachaayoun@gmail.com>
2025-08-12 13:18:48 +01:00
Marijn Schouten 1a1b52acbf clippy fix: indentation 2025-07-04 11:52:17 +00:00
Ben Kimock e36dc78edd Add some track_caller info to precondition panics 2025-05-21 09:10:06 -04:00
Janggun Lee 0369ccb532 Fix some grammar errors and hyperlinks in doc for trait Allocator
* "while until either" could also be changed to "for a while until either", but I just deleted "while".
* fixed sentence with incorrect "at" and "has/have".
* linked [*currently allocated*] similar to other methods.
2025-04-14 22:26:43 +09:00
James Wainwright 78e9621390 Pass Alignment for RawVecInner::new_in
Encodes the safety constraint that `Unique`'s pointer must be non-zero
into the API.
2025-03-26 21:41:11 +00:00
Thalia Archibald 988eb19970 library: Use size_of from the prelude instead of imported
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-03-06 20:20:38 -08:00
Eric Huss 4e36f46464 core: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
Askar Safin ac31e9572b library: doc: core::alloc::Allocator: trivial typo fix 2025-02-07 09:14:17 +03:00
Marijn Schouten 52519e145e Cleanup docs for Allocator 2025-01-29 20:15:49 +01:00
Samuel Tardieu 9ab77f1ccb Use NonNull::without_provenance within the standard library
This API removes the need for several `unsafe` blocks, and leads to
clearer code.
2025-01-10 23:23:10 +01:00
Pietro Albini 4ae92b7adb update version placeholders 2025-01-08 20:02:18 +01:00
Ralf Jung 88b88f336b stabilize const_alloc_layout 2024-12-25 19:28:52 +01:00
Ralf Jung 2b88e4c716 stabilize const_{size,align}_of_val 2024-12-02 20:18:25 +01:00
Boxy 22998f0785 update cfgs 2024-11-27 15:14:54 +00:00
Ralf Jung c9e77e8776 make const_alloc_layout feature gate only about functions that are already stable
the rest has their constness guarded by their usual feature gate
2024-11-01 14:32:59 +01:00
bors 54761cb3e8 Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors
Const stability checks v2

The const stability system has served us well ever since `const fn` were first stabilized. It's main feature is that it enforces *recursive* validity -- a stable const fn cannot internally make use of unstable const features without an explicit marker in the form of `#[rustc_allow_const_fn_unstable]`. This is done to make sure that we don't accidentally expose unstable const features on stable in a way that would be hard to take back. As part of this, it is enforced that a `#[rustc_const_stable]` can only call `#[rustc_const_stable]` functions. However, some problems have been coming up with increased usage:
- It is baffling that we have to mark private or even unstable functions as `#[rustc_const_stable]` when they are used as helpers in regular stable `const fn`, and often people will rather add `#[rustc_allow_const_fn_unstable]` instead which was not our intention.
- The system has several gaping holes: a private `const fn` without stability attributes whose inherited stability (walking up parent modules) is `#[stable]` is allowed to call *arbitrary* unstable const operations, but can itself be called from stable `const fn`. Similarly, `#[allow_internal_unstable]` on a macro completely bypasses the recursive nature of the check.

Fundamentally, the problem is that we have *three* disjoint categories of functions, and not enough attributes to distinguish them:
1. const-stable functions
2. private/unstable functions that are meant to be callable from const-stable functions
3. functions that can make use of unstable const features

Functions in the first two categories cannot use unstable const features and they can only call functions from the first two categories.

This PR implements the following system:
- `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions.
- `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category.
- `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls.

Also, all the holes mentioned above have been closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to be manually marked `#[rustc_const_stable_indirect]` to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine.

The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability.

Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.

Also see the updated dev-guide at https://github.com/rust-lang/rustc-dev-guide/pull/2098.

I think in the future we may want to tweak this further, so that in the hopefully common case where a public function's const-stability just exactly mirrors its regular stability, we never have to add any attribute. But right now, once the function is stable this requires `#[rustc_const_stable]`.

### Open question

There is one point I could see we might want to do differently, and that is putting `#[rustc_const_unstable]`  functions (but not intrinsics) in category 2 by default, and requiring an extra attribute for `#[rustc_const_not_exposed_on_stable]` or so. This would require a bunch of extra annotations, but would have the advantage that turning a `#[rustc_const_unstable]` into `#[rustc_const_stable]`  will never change the way the function is const-checked. Currently, we often discover in the const stabilization PR that a function needs some other unstable const things, and then we rush to quickly deal with that. In this alternative universe, we'd work towards getting rid of the `rustc_const_not_exposed_on_stable` before stabilization, and once that is done stabilization becomes a trivial matter. `#[rustc_const_stable_indirect]` would then only be used for intrinsics.

I think I like this idea, but might want to do it in a follow-up PR, as it will need a whole bunch of annotations in the standard library. Also, we probably want to convert all const intrinsics to the "new" form (`#[rustc_intrinsic]` instead of an `extern` block) before doing this to avoid having to deal with two different ways of declaring intrinsics.

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
Part of https://github.com/rust-lang/rust/issues/129815 (but not finished since this is not yet sufficient to safely let us expose `const fn` from hashbrown)
Fixes https://github.com/rust-lang/rust/issues/131073 by making it so that const-stable functions are always stable

try-job: test-various
2024-10-25 23:29:40 +00:00
Ralf Jung a0215d8e46 Re-do recursive const stability checks
Fundamentally, we have *three* disjoint categories of functions:
1. const-stable functions
2. private/unstable functions that are meant to be callable from const-stable functions
3. functions that can make use of unstable const features

This PR implements the following system:
- `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions.
- `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category.
- `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls.

Also, several holes in recursive const stability checking are being closed.
There's still one potential hole that is hard to avoid, which is when MIR
building automatically inserts calls to a particular function in stable
functions -- which happens in the panic machinery. Those need to *not* be
`rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be
sure they follow recursive const stability. But that's a fairly rare and special
case so IMO it's fine.

The net effect of this is that a `#[unstable]` or unmarked function can be
constified simply by marking it as `const fn`, and it will then be
const-callable from stable `const fn` and subject to recursive const stability
requirements. If it is publicly reachable (which implies it cannot be unmarked),
it will be const-unstable under the same feature gate. Only if the function ever
becomes `#[stable]` does it need a `#[rustc_const_unstable]` or
`#[rustc_const_stable]` marker to decide if this should also imply
const-stability.

Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to
use unstable const lang features (including intrinsics), or (b) `#[stable]`
functions that are not yet intended to be const-stable. Adding
`#[rustc_const_stable]` is only needed for functions that are actually meant to
be directly callable from stable const code. `#[rustc_const_stable_indirect]` is
used to mark intrinsics as const-callable and for `#[rustc_const_unstable]`
functions that are actually called from other, exposed-on-stable `const fn`. No
other attributes are required.
2024-10-25 20:31:40 +02:00
Ralf Jung 854e3c43e0 library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
Johnathan W 8b754fbb4f Fixing rustDoc for LayoutError. 2024-10-10 16:18:56 -04:00
Matthias Krüger 1d45203779 Rollup merge of #123932 - adamse:global-alloc-safety-preconds-positive, r=tgross35
restate GlobalAlloc method safety preconditions in terms of what the caller has to do for greater clarity
2024-09-29 20:17:35 +02:00
Scott McMurray 18ca8bf8ee Take more advantage of the isize::MAX limit in Layout
Things like `padding_needed_for` are current implemented being super careful to handle things like `Layout::size` potentially being `usize::MAX`.

But now that 95295 has happened, that's no longer a concern.  It's possible to add two `Layout::size`s together without risking overflow now.

So take advantage of that to remove a bunch of checked math that's not actually needed.  For example, the round-up-and-add-next-size in `extend` doesn't need any overflow checks at all, just the final check for compatibility with the alignment.

(And while I was doing that I made it all unstably const, because there's nothing in `Layout` that's fundamentally runtime-only.)
2024-09-17 20:05:57 -07:00
Adam Sandberg Ericsson 896ccaa8ac restate GlobalAlloc method safety preconditions in terms of what the caller has to do for greater clarity 2024-09-07 15:21:52 +01:00
Ben Kimock e6b0f27e00 Try to golf down the amount of code in Layout 2024-08-20 18:41:07 -04:00
Ben Kimock 7f5d282185 Add a precondition check for Layout::from_size_align_unchecked 2024-08-19 17:18:17 -04:00
Nicholas Nethercote 84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
John Arundel a19472a93e Fix doc nits
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-07-26 13:26:33 +01:00
Ralf Jung 287b66b0b5 size_of_val_raw: for length 0 this is safe to call 2024-07-10 18:01:06 +02:00
Ralf Jung 7731135af2 alloc::Layout: explicitly document size invariant on the type level 2024-03-25 20:18:46 +01:00
Mara Bos a2c74b8445 SeqCst->Relaxed in doc examples.
SeqCst is unnecessary here.
2024-03-19 15:27:11 +01:00
Ralf Jung b58f647d54 rename ptr::invalid -> ptr::without_provenance
also introduce ptr::dangling matching NonNull::dangling
2024-02-21 20:15:52 +01:00
Amanieu d'Antras 8e9c8dd10a Add information about allocation lifetime to Allocator::allocate 2024-02-13 14:12:51 +00:00
Amanieu d'Antras fb5ed2986e Clarify the lifetimes of allocations returned by the Allocator trait
The previous definition (accidentally) disallowed the implementation of
stack-based allocators whose memory would become invalid once the
lifetime of the allocator type ended.

This also ensures the validity of the following blanket implementation:
```rust
impl<A: Allocator> Allocator for &'_ A {}
```
2024-02-12 14:02:30 +00:00
joboet fa9a911a57 libs: use assert_unchecked instead of intrinsic 2024-01-13 20:10:00 +01:00
Mark Rousskov b81e788d16 Indicate that multiplication in Layout::array cannot overflow
This allows LLVM to optimize comparisons to zero before & after the
multiplication into one, saving on code size and eliminating an (always
true) branch from most Vec allocations.
2023-11-23 22:05:45 -05:00
Joshua Liebow-Feeser a9b0966aa5 Update library/core/src/alloc/layout.rs
Co-authored-by: David Tolnay <dtolnay@gmail.com>
2023-10-12 16:03:45 -07:00
Joshua Liebow-Feeser 25b81024a5 Guarantee that Layout::align returns a non-zero power of two 2023-09-07 12:54:34 -07:00
Nilstrieb 18c9baf4fe Rollup merge of #107916 - reez12g:issue-107040, r=Amanieu
fix comment on Allocator trait

fixes https://github.com/rust-lang/rust/issues/107040
2023-05-30 12:57:38 +02:00
reez12g 000cd9b5fb fix comment on Allocator trait 2023-05-30 13:56:57 +09:00
Matthias Krüger 1d06bb9612 Rollup merge of #108630 - overlookmotel:realloc-docs-fix, r=Amanieu
Fix docs for `alloc::realloc`

Fixes #108546.

Corrects the docs for `alloc::realloc` to bring the safety constraints into line with `Layout::from_size_align_unchecked`'s constraints.
2023-05-27 20:40:27 +02:00
Deadbeef 63e0ddbf1d core is now compilable 2023-04-16 07:20:26 +00:00