Commit Graph

388 Commits

Author SHA1 Message Date
Manish Goregaokar 6556f26991 Rollup merge of #73678 - Keno:patch-1, r=LukasKalbertodt
Update Box::from_raw example to generalize better

I know very little about rust, so I saw the example here
```
use std::alloc::{alloc, Layout};

unsafe {
    let ptr = alloc(Layout::new::<i32>()) as *mut i32;
    *ptr = 5;
    let x = Box::from_raw(ptr);
}
```
and tried to generalize it by writing,
```
    let layout = Layout::new::<T>();
    let new_obj = unsafe {
        let ptr = alloc(layout) as *mut T;
        *ptr = obj;
        Box::from_raw(ptr)
    };
```
for some more complicated `T`, which ended up crashing with SIGSEGV,
because it tried to `drop_in_place` the previous object in `ptr` which is
of course garbage. I think that changing this example to use `.write` instead
would be a good idea to suggest the correct generalization. It is also more
consistent with other documentation items in this file, which use `.write`.
I also added a comment to explain it, but I'm not too attached to that,
and can see it being too verbose in this place.
2020-07-01 07:42:42 -07:00
David Wood 14ea7a777f lints: add improper_ctypes_definitions
This commit adds a new lint - `improper_ctypes_definitions` - which
functions identically to `improper_ctypes`, but on `extern "C" fn`
definitions (as opposed to `improper_ctypes`'s `extern "C" {}`
declarations).

Signed-off-by: David Wood <david@davidtw.co>
2020-06-24 12:09:35 +01:00
Keno Fischer 0c88dd663a Update Box::from_raw example to generalize better
I know very little about rust, so I saw this example and tried to generalize it by writing,
```
    let layout = Layout::new::<T>();
    let new_obj = unsafe {
        let ptr = alloc(layout) as *mut T;
        *ptr = obj;
        Box::from_raw(ptr)
    };
```
for some more complicated `T`, which ended up crashing with SIGSEGV,
because it tried to `drop_in_place` the previous object in `ptr` which is
of course garbage. I also added a comment that explains why `.write`
is used, but I think adding that comment is optional and may be too verbose
here. I do however think that changing this example is a good idea to
suggest the correct generalization. `.write` is also used in most of the rest
of the documentation here, even if the example is `i32`, so it would additionally
be more consistent.
2020-06-23 22:42:35 -04:00
Manish Goregaokar 55479de299 Rollup merge of #72709 - LeSeulArtichaut:unsafe-liballoc, r=nikomatsakis
`#[deny(unsafe_op_in_unsafe_fn)]` in liballoc

This PR proposes to make use of the new `unsafe_op_in_unsafe_fn` lint, i.e. no longer consider the body of an unsafe function as an unsafe block and require explicit unsafe block to perform unsafe operations.

This has been first (partly) suggested by @Mark-Simulacrum in https://github.com/rust-lang/rust/pull/69245#issuecomment-587817065

Tracking issue for the feature: #71668.
~~Blocked on #71862.~~
r? @Mark-Simulacrum cc @nikomatsakis can you confirm that those changes are desirable? Should I restrict it to only BTree for the moment?
2020-06-19 09:14:58 -07:00
LeSeulArtichaut 39e29ce4d0 #[deny(unsafe_op_in_unsafe_fn)] in liballoc 2020-06-19 13:47:01 +02:00
Josh Stone a7c2cf8f51 Reduce pointer casts in Box::into_boxed_slice
We only need to cast the pointer once to change `Box<T>` to an array
`Box<[T; 1]>`, then we can let unsized coercion return `Box<[T]>`.
2020-06-17 16:30:27 -07:00
Ralf Jung 8d64fd80ad Rollup merge of #72499 - mendess:master, r=dtolnay
Override Box::<[T]>::clone_from

Avoid dropping and reallocating when cloning to an existing box if the lengths are the same.

It would be nice if this could also be specialized for `Copy` but I don't know how that works since it's not on stable. Will gladly look into it if it's deemed as a good idea.

This is my first PR with code, hope I did everything right 😄
2020-05-30 13:45:06 +02:00
mendess a5734ca417 Override Box::<[T]>::clone_from 2020-05-23 14:08:53 +01:00
Ivan Tham a8ed9aa9f0 impl From<[T; N]> for Box<[T]>
Based on https://github.com/rust-lang/rust/pull/68692
2020-05-20 17:00:21 +08:00
bors 914adf04af Auto merge of #71447 - cuviper:unsized_cow, r=dtolnay
impl From<Cow> for Box, Rc, and Arc

These forward `Borrowed`/`Owned` values to existing `From` impls.

- `Box<T>` is a fundamental type, so it would be a breaking change to add a blanket impl. Therefore, `From<Cow>` is only implemented for `[T]`, `str`, `CStr`, `OsStr`, and `Path`.
- For `Rc<T>` and `Arc<T>`, `From<Cow>` is implemented for everything that implements `From` the borrowed and owned types separately.
2020-05-19 08:08:48 +00:00
Dylan DPC 398d3eeca1 Rollup merge of #71421 - elichai:2020-04-boxed-slice, r=sfackler
Add a function to turn Box<T> into Box<[T]>

Hi,
I think this is very useful, as currently it's not possible in safe rust to do this without re-allocating.
an alternative implementation of the same function can be:
```rust
pub fn into_boxed_slice<T>(boxed: Box<T>) -> Box<[T]> {
    unsafe {
        let slice = slice::from_raw_parts_mut(Box::into_raw(boxed), 1);
        Box::from_raw(slice)
    }
}
```

The only thing that makes me a little uncomfortable is this line :
> The alignment of array types is greater or equal to the alignment of its element type

from https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html

But then I see:
> The alignment of &T, &mut T, *const T and *mut T are the same, and are at least the word size.
> The alignment of &[T] is the word size.

from https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html#representation

So I do believe this is valid(FWIW it also passes in miri https://play.rust-lang.org/?gist=c002b99364ee6b29862aeb3565a91c19)
2020-04-26 21:02:32 +02:00
Elichai Turkel 6f31f05aaf Add a function to turn Box<T> into Box<[T]> (into_boxed_slice) 2020-04-26 15:42:39 +03:00
Josh Stone b0fb57bd8d impl From<Cow> for boxed slices and strings
These forward `Borrowed`/`Owned` values to existing `Box::from` impls.

- `From<Cow<'_, [T]>> for Box<[T]>`
- `From<Cow<'_, str>> for Box<str>`
- `From<Cow<'_, CStr>> for Box<CStr>`
- `From<Cow<'_, OsStr>> for Box<OsStr>`
- `From<Cow<'_, Path>> for Box<Path>`
2020-04-22 13:03:05 -07:00
Simon Sapin 7709d205dd Implement Box::into_raw based on Box::leak
… instead of the other way around.
2020-04-16 17:20:53 +02:00
Simon Sapin 9a1c7dba32 Apply suggestions from code review
Co-Authored-By: Ralf Jung <post@ralfj.de>
2020-04-15 18:32:56 +02:00
Simon Sapin cdb6bef4fb Deprecate Box::into_raw_non_null
Per https://github.com/rust-lang/rust/issues/47336#issuecomment-586589016
2020-04-15 16:18:33 +02:00
Trevor Spiteri 2b718e8d9c use ManuallyDrop instead of forget inside collections
This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
2020-04-04 14:30:33 +02:00
Tim Diekmann bf6a46db31 Make fields in MemoryBlock public 2020-03-28 20:22:07 +01:00
Tim Diekmann 2526accdd3 Fix issues from review and unsoundness of RawVec::into_box 2020-03-26 17:11:47 +01:00
Tim Diekmann 56cbf2f22a Overhaul of the AllocRef trait to match allocator-wg's latest consens 2020-03-26 17:10:54 +01:00
Jonas Schievink f53f9a88f1 Bump the bootstrap compiler 2020-03-15 19:43:25 +01:00
Yuki Okushi 4699b29a04 Rollup merge of #69609 - TimDiekmann:excess, r=Amanieu
Remove `usable_size` APIs

This removes the usable size APIs:
- remove `usable_size` (obv)
- change return type of allocating methods to include the allocated size
- remove `_excess` API

r? @Amanieu
closes rust-lang/wg-allocators#17
2020-03-03 17:50:06 +09:00
Tim Diekmann d8e3557dba Remove usable_size APIs 2020-03-03 00:08:24 +01:00
Yuki Okushi 4e0bea326e Stabilize boxed_slice_try_from 2020-02-28 13:28:09 +09:00
Tim Diekmann 76aa29ff5e Preparation for allocator aware Box 2020-02-11 13:16:20 +01:00
Dylan DPC 2d8f6389d0 Rollup merge of #68524 - jonas-schievink:generator-resume-arguments, r=Zoxc
Generator Resume Arguments

cc https://github.com/rust-lang/rust/issues/43122 and https://github.com/rust-lang/rust/issues/56974

Blockers:
* [x] Fix miscompilation when resume argument is live across a yield point (https://github.com/rust-lang/rust/pull/68524#issuecomment-578459069)
* [x] Fix 10% compile time regression in `await-call-tree` benchmarks (https://github.com/rust-lang/rust/pull/68524#issuecomment-578487162)
  * [x] Fix remaining 1-3% regression (https://github.com/rust-lang/rust/pull/68524#issuecomment-579566255) - resolved (https://github.com/rust-lang/rust/pull/68524#issuecomment-581144901)
* [x] Make dropck rules account for resume arguments (https://github.com/rust-lang/rust/pull/68524#issuecomment-578541137)

Follow-up work:
* Change async/await desugaring to make use of this feature
* Rewrite [`box_region.rs`](https://github.com/rust-lang/rust/blob/3d8778d767f0dde6fe2bc9459f21ead8e124d8cb/src/librustc_data_structures/box_region.rs) to use resume arguments (this shows up in profiles too)
2020-02-06 22:38:33 +01:00
Jonas Schievink 044fe0f558 Add a resume type parameter to Generator 2020-02-02 13:20:57 +01:00
hman523 346920c3c8 Fixed issue 68593 2020-01-31 13:41:07 -06:00
Tim Diekmann 7ca25db816 Rename Alloc to AllocRef 2020-01-27 21:39:51 +01:00
Josh Stone 81a6709cf9 Simplify Clone for Box<[T]>
The bespoke `BoxBuilder` was basically a very simple `Vec`. Instead,
let's clone to a real `Vec`, with all of its specialization for the
task, then convert back to `Box<[T]>`.
2019-12-23 07:35:39 -07:00
Mark Rousskov a06baa56b9 Format the world 2019-12-22 17:42:47 -05:00
Ross MacArthur f7256d28d1 Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
Yuki Okushi 9860a4eeb7 Rollup merge of #62514 - stephaneyfx:box-ffi, r=nikomatsakis
Clarify `Box<T>` representation and its use in FFI

This officializes what was only shown as a code example in [the unsafe code guidelines](https://rust-lang.github.io/unsafe-code-guidelines/layout/function-pointers.html?highlight=box#use) and follows [the discussion](https://github.com/rust-lang/unsafe-code-guidelines/issues/157) in the corresponding repository.

It is also related to [the issue](https://github.com/rust-lang/rust/issues/52976) regarding marking `Box<T>` `#[repr(transparent)]`.

If the statement this PR adds is incorrect or a more in-depth discussion is warranted, I apologize. Should it be the case, the example in the unsafe code guidelines should be amended and some document should make it clear that it is not sound/supported.
2019-12-12 10:09:15 +09:00
Nicholas Matsakis fafa489798 clarify that Box<T> should only be used when defined *in Rust* 2019-12-11 10:33:36 -05:00
Stephane Raux cb1cc1181e Fix description based on review 2019-12-10 22:29:25 -08:00
Stephane Raux 1a26df7727 Remove trailing whitespace 2019-12-10 00:05:37 -08:00
Stephane Raux fe6ddd5d15 Specify behavior when passed a null pointer 2019-12-09 23:20:49 -08:00
Stephane Raux ead1159490 Use Niko's wording 2019-12-09 23:20:49 -08:00
Emilio Cobos Álvarez b12e142bc5 alloc: Add new_zeroed() versions like new_uninit().
MaybeUninit has both uninit() and zeroed(), it seems reasonable to have the same
surface on Box/Rc/Arc.

Needs tests.
2019-11-05 19:27:42 +01:00
Stephane Raux 812ec6a3bf Update FFI example
- Use meaningful names
- Clarify comments
- Fix C function declaration
2019-11-01 04:34:12 -07:00
Simon Sapin ca1cfdab78 Uninitialized boxes: check for zero-size allocation based on Layout::size 2019-10-16 20:32:58 +02:00
Simon Sapin 23d3ff1b97 Fix zero-size uninitialized boxes
Requesting a zero-size allocation is not allowed,
return a dangling pointer instead.

CC https://github.com/rust-lang/rust/issues/63291#issuecomment-538692745
2019-10-06 23:48:55 +02:00
Jonas Schievink 02f36e52a6 Hide the Iterator specialization behind a trait 2019-10-05 15:33:25 +02:00
Jonas Schievink 2cd5030ef5 Deny specializing items not in the parent impl 2019-10-05 15:33:24 +02:00
Lzu Tao 6c1b447f2e Remove unneeded fn main blocks from docs 2019-10-01 11:55:46 +00:00
Stephane Raux aea94230c4 Update Box representation comment based on reviews 2019-08-25 23:25:56 -07:00
Simon Sapin ba0328327c Doc nits
Co-Authored-By: Ralf Jung <post@ralfj.de>
2019-08-17 15:42:05 +02:00
Simon Sapin 78264f5e3c Add tracking issue numbers 2019-08-16 17:11:18 +02:00
Simon Sapin dab967afdc Use alloc::Global in Box::new_uninit 2019-08-16 17:11:18 +02:00
Simon Sapin 4eeb623e9e Fix intra-rustdoc links 2019-08-16 17:11:18 +02:00