Commit Graph

74 Commits

Author SHA1 Message Date
Dylan DPC 880d3ea3c2 Rollup merge of #97034 - fee1-dead-contrib:layout-hash, r=dtolnay
Implement `Hash` for `core::alloc::Layout`

This was brought up on [reddit](https://www.reddit.com/r/rust/comments/uoypui/the_standard_library_types_are_good_except_when/), and I don't see why Layout shouldn't implement `Hash`. Feel free to comment if I am wrong though :)
2022-05-28 08:45:51 +02:00
CAD97 09dc24bc04 Remove potentially misleading realloc parenthetical 2022-05-14 22:30:14 -05:00
Deadbeef af9168c467 Implement Hash for core::alloc::Layout 2022-05-14 14:44:42 +10:00
Jacob Pratt 4fbe73e0b7 Remove use of #[rustc_deprecated] 2022-04-14 01:33:13 -04:00
liangyongrui 03b2588837 fix Layout struct member naming style 2022-04-11 13:35:18 +08:00
Dylan DPC e4b4bf1535 Rollup merge of #95361 - scottmcm:valid-align, r=Mark-Simulacrum
Make non-power-of-two alignments a validity error in `Layout`

Inspired by the zulip conversation about how `Layout` should better enforce `size <= isize::MAX as usize`, this uses an N-variant enum on N-bit platforms to require at the validity level that the existing invariant of "must be a power of two" is upheld.

This was MIRI can catch it, and means there's a more-specific type for `Layout` to store than just `NonZeroUsize`.

It's left as `pub(crate)` here; a future PR could consider giving it a tracking issue for non-internal usage.
2022-04-09 18:26:25 +02:00
Scott McMurray fe0c08a4f2 Make non-power-of-two alignments a validity error in Layout
Inspired by the zulip conversation about how `Layout` should better enforce `size < isize::MAX as usize`, this uses an N-variant enum on N-bit platforms to require at the validity level that the existing invariant of "must be a power of two" is upheld.

This was MIRI can catch it, and means there's a more-specific type for `Layout` to store than just `NonZeroUsize`.
2022-04-08 20:17:38 -07:00
Dylan DPC d7a24003d8 Rollup merge of #95354 - dtolnay:rustc_const_stable, r=lcnr
Handle rustc_const_stable attribute in library feature collector

The library feature collector in [compiler/rustc_passes/src/lib_features.rs](https://github.com/rust-lang/rust/blob/551b4fa395fa588d91cbecfb0cdfe1baa02670cf/compiler/rustc_passes/src/lib_features.rs) has only been looking at `#[stable(…)]`, `#[unstable(…)]`, and `#[rustc_const_unstable(…)]` attributes, while ignoring `#[rustc_const_stable(…)]`. The consequences of this were:

- When any const feature got stabilized (changing one or more `rustc_const_unstable` to `rustc_const_stable`), users who had previously enabled that unstable feature using `#![feature(…)]` would get told "unknown feature", rather than rustc's nicer "the feature … has been stable since … and no longer requires an attribute to enable".

    This can be seen in the way that https://github.com/rust-lang/rust/pull/93957#issuecomment-1079794660 failed after rebase:

    ```console
    error[E0635]: unknown feature `const_ptr_offset`
      --> $DIR/offset_from_ub.rs:1:35
       |
    LL | #![feature(const_ptr_offset_from, const_ptr_offset)]
       |                                   ^^^^^^^^^^^^^^^^
    ```

- We weren't enforcing that a particular feature is either stable everywhere or unstable everywhere, and that a feature that has been stabilized has the same stabilization version everywhere, both of which we enforce for the other stability attributes.

This PR updates the library feature collector to handle `rustc_const_stable`, and fixes places in the standard library and test suite where `rustc_const_stable` was being used in a way that does not meet the rules for a stability attribute.
2022-04-02 03:34:21 +02:00
Autumn e2466821ad add notes about alignment-altering reallocs to Allocator docs 2022-03-31 16:13:19 -07:00
David Tolnay 4246916619 Adjust feature names that disagree on const stabilization version 2022-03-31 12:34:48 -07:00
Aria Beingessner c7de289e1c Make the stdlib largely conform to strict provenance.
Some things like the unwinders and system APIs are not fully conformant,
this only covers a lot of low-hanging fruit.
2022-03-29 20:18:21 -04:00
Frank Steffahn a957cefda6 Fix a bunch of typos 2021-12-14 16:40:43 +01:00
Nicholas Nethercote f3bda74d36 Optimize Layout::array.
The current implementation is much more conservative than it needs to
be, because it's dealing with the size and alignment of a given `T`,
which are more restricted than an arbitrary `Layout`.

For example, imagine a struct with a `u32` and a `u4`. You can safely
create a `Layout { size_: 5, align_: 4 }` by hand, but
`Layout::new::<T>` will give `Layout { size_: 8, align_: 4}`, where the
size already has padding that accounts for the alignment. (And the
existing `debug_assert_eq!` in `Layout::array` already demonstrates that
no additional padding is required.)
2021-11-26 19:30:35 +11:00
Nicholas Nethercote 026edbb4ef Use unchecked construction in Layout::pad_to_align.
Other, similar methods for `Layout` do likewise, and there's already an
`unwrap()` around the result demonstrating the safety.
2021-11-26 19:30:35 +11:00
John Kugelman 68b0d86294 Add #[must_use] to remaining core functions 2021-10-30 18:21:29 -04:00
John Kugelman c3f0577002 Add #[must_use] to non-mutating verb methods 2021-10-11 21:21:32 -04:00
Guillaume Gomez 96ffc74fe3 Rollup merge of #89753 - jkugelman:must-use-from_value-conversions, r=joshtriplett
Add #[must_use] to from_value conversions

I added two methods to the list myself. Clippy did not flag them because they take `mut` args, but neither modifies their argument.

```rust
core::str           const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str;
std::ffi::CString   unsafe fn from_raw(ptr: *mut c_char) -> CString;
```

I put a custom note on `from_raw`:

```rust
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
```

Parent issue: #89692

r? ``@joshtriplett``
2021-10-11 14:11:45 +02:00
John Kugelman cf2bcd10ed Add #[must_use] to from_value conversions 2021-10-10 19:00:33 -04:00
John Kugelman 5b5c12be1c Add #[must_use] to core and std constructors 2021-10-10 02:44:26 -04:00
Frank Steffahn 8d2bb9389a Consistent spelling of "adapter" in the standard library
Change all occurrences of "(A|a)daptor" to "(A|a)dapter".
2021-07-30 17:23:07 +02:00
Ali Malik e43254aad1 Fix may not to appropriate might not or must not 2021-07-29 01:15:20 -04:00
Ian Jackson 03d7001564 docs: GlobalAlloc: Make example only require 4096-aligned static
Alignments > 4k are not supported,
  https://github.com/rust-lang/rust/issues/70022
  https://github.com/rust-lang/rust/issues/70144

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-07-21 12:37:59 +01:00
Ian Jackson 07e11e8495 docs: GlobalAlloc: completely replace example with one that works
Since this is an example, this could really do with some review from
someone familiar with unsafe stuff !

I made the example no longer `no_run` since it works for me.

Fixes #81847

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2021-07-20 14:49:35 +01:00
Jacob Pratt 3f14f4b3ce Use #[non_exhaustive] where appropriate
Due to the std/alloc split, it is not possible to make
`alloc::collections::TryReserveError::AllocError` non-exhaustive without
having an unstable, doc-hidden method to construct (which negates the
benefits from `#[non_exhaustive]`.
2021-06-24 04:16:11 -04:00
Oli Scherer 6f3635d87b Fix const stability since versions. 2021-03-15 14:39:18 +00:00
Yuki Okushi bc5669eef8 Rollup merge of #80189 - jyn514:convert-primitives, r=poliorcetics
Convert primitives in the standard library to intra-doc links

Blocked on https://github.com/rust-lang/rust/pull/80181. I forgot that this needs to wait for the beta bump so the standard library can be documented with `doc --stage 0`.

Notably I didn't convert `core::slice` because it's like 50 links and I got scared 😨
2021-03-02 21:23:12 +09:00
Vytautas Astrauskas cdfff98394 Try to clarify GlobalAlloc::realloc documentation comment. 2021-02-28 14:30:26 +01:00
Joshua Nelson 9a75f4fed1 Convert primitives to use intra-doc links 2021-02-25 20:31:53 -05:00
Mara Bos e077dffaec Rollup merge of #81767 - exrook:layout-error-stability, r=Mark-Simulacrum
Update LayoutError/LayoutErr stability attributes

`LayoutError` ended up not making it into 1.49.0, updating the stability attributes to reflect that.

I also pushed `LayoutErr` deprecation back a release to allow 2 releases before the deprecation comes into effect.

This change should be backported to beta.
2021-02-05 12:26:07 +01:00
Jacob Hughes 0c3a7d8b85 Update LayoutError/LayoutErr stability attributes 2021-02-04 19:02:57 -05:00
RustyYato d06384ac29 make Allocator object-safe
add test to ensure object-safety
This allows for runtime polymorphic allocators
2021-02-03 20:46:16 -05:00
bors 05dfaba442 Auto merge of #79827 - tmiasko:size-align, r=kennytm
Describe why `size_align` have not been inlined so far

although it is used only in one place.
2021-01-03 03:43:29 +00:00
bors 0b644e4196 Auto merge of #79045 - oli-obk:dont_rely_on_alloc_happening_for_soundness, r=TimDiekmann
Document that heap allocations are not guaranteed to happen, even if explicitly performed in the code

cc `@RalfJung`
2020-12-26 19:43:12 +00:00
oli efcd8a96c4 DIrect invocations of AllocRef::alloc cannot get optimized away 2020-12-26 17:16:50 +00:00
oli fba17e3f8d Adjust markdown text to be more like the rendered text 2020-12-26 17:14:49 +00:00
Oli Scherer 714feab059 Update library/core/src/alloc/mod.rs
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-12-26 18:06:04 +01:00
Oli Scherer 48c8ff59ec Update library/core/src/alloc/global.rs
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-12-26 18:05:55 +01:00
Tomasz Miąsko cf5bd26329 Describe why size_align have not been inlined so far 2020-12-10 00:00:00 +00:00
oli fda4c8d5c1 Update documentation to review comments 2020-12-04 17:28:22 +00:00
Tim Diekmann 9274b37d99 Rename AllocRef to Allocator and (de)alloc to (de)allocate 2020-12-04 14:47:15 +01:00
Chris Denton 9050d12714 Stabilize alloc::Layout const functions
Stabilizes #67521. In particular the following stable methods are stabilized as const fn:

* size
* align
* from_size_align
2020-11-22 21:43:30 +00:00
oli 58d62b8371 Inform tidy about the reason for the ignored rust code 2020-11-19 14:57:30 +00:00
oli dfca61a4c2 Elaborate on side effects 2020-11-19 14:28:28 +00:00
oli 3d1f676906 Fix tidy 2020-11-19 11:41:47 +00:00
oli 173892c776 Note that there are other optimizations than the one showcased 2020-11-19 11:09:10 +00:00
oli a2f938ac52 Document that heap allocations are not guaranteed to happen, even if explicitly performed in the code 2020-11-19 11:05:15 +00:00
Mara Bos 5bbf75da78 Rollup merge of #77691 - exrook:rename-layouterr, r=KodrAus
Rename/Deprecate LayoutErr in favor of LayoutError

Implements rust-lang/wg-allocators#73.

This patch renames LayoutErr to LayoutError, and uses a type alias to support users using the old name.

The new name will be instantly stable in release 1.49 (current nightly), the type alias will become deprecated in release 1.51 (so that when the current nightly is 1.51, 1.49 will be stable).

This is the only error type in `std` that ends in `Err` rather than `Error`, if this PR lands all stdlib error types will end in `Error` 🥰
2020-11-16 17:26:17 +01:00
Jacob Hughes 8ff0c14dc5 Change layouterr deprecation message 2020-10-27 04:48:37 -04:00
Joshua Nelson 65835d1059 Deny broken intra-doc links in linkchecker
Since rustdoc isn't warning about these links, check for them manually.
2020-10-15 20:22:16 -04:00
Jacob Hughes 0266c134a7 Deprecate LayoutErr 2020-10-08 01:19:21 -04:00