Commit Graph

68 Commits

Author SHA1 Message Date
bors
96ad8e5fbc Auto merge of #65013 - petertodd:2019-maybeuninit-debug, r=sfackler
Implement Debug for MaybeUninit

Precedent: `UnsafeCell` implements `Debug` even though it can't actually display the value. I noticed this omission while writing the following:

```
#[derive(Debug)]
 pub struct SliceInitializer<'a, T> {
    marker: PhantomData<&'a mut T>,
    uninit: &'a mut [MaybeUninit<T>],
    written: usize,
}
```

...which currently unergonomically fails to compile.

`UnsafeCell` does require `T: Debug`. Because of things like the above I think it'd be better to leave that requirement off. In fact, I'd also suggest removing that requirement for `UnsafeCell` too, which again I noticed in some low-level real world code.
2019-11-28 03:41:29 +00:00
David Tolnay
95e00bfed8 Format libcore with rustfmt
This commit applies rustfmt with default settings to files in
src/libcore *that are not involved in any currently open PR* to minimize
merge conflicts. The list of files involved in open PRs was determined
by querying GitHub's GraphQL API with this script:
https://gist.github.com/dtolnay/aa9c34993dc051a4f344d1b10e4487e8

With the list of files from the script in `outstanding_files`, the
relevant commands were:

    $ find src/libcore -name '*.rs' | xargs rustfmt --edition=2018
    $ rg libcore outstanding_files | xargs git checkout --

Repeating this process several months apart should get us coverage of
most of the rest of libcore.
2019-11-26 23:02:11 -08:00
Mazdak Farrokhzad
9b0427fc94 Rollup merge of #66411 - RalfJung:forget, r=sfackler
mem::forget docs: mention ManuallyDrop

Cc @SimonSapin @Centril
2019-11-24 03:16:59 +01:00
Adam Schwalm
3407c49c41 Clarify transmute_copy documentation example 2019-11-16 14:47:33 -06:00
Ralf Jung
7009e6d001 mem::forget docs: mention ManuallyDrop 2019-11-14 12:13:16 +01:00
Yuki Okushi
9db3fddfe9 Rollup merge of #66217 - RalfJung:diagnostic-items, r=Centril
invalid_value lint: use diagnostic items

This adjusts the invalid_value lint to use diagnostic items.

@Centril @oli-obk For some reason, this fails to recognize `transmute` -- somehow the diagnostic item is not found. Any idea why?

r? @Centril

Cc https://github.com/rust-lang/rust/issues/66075
2019-11-10 09:27:19 +09:00
Ralf Jung
769d52774b partially port invalid_value lint to diagnostic items 2019-11-09 10:34:16 +01:00
Yuki Okushi
a00c777b75 Rollup merge of #65580 - SimonSapin:maybeuninit-array, r=Amanieu
Add `MaybeUninit` methods `uninit_array`, `slice_get_ref`, `slice_get_mut`

Eventually these will hopefully become the idiomatic way to work with partially-initialized stack buffers.

All methods are unstable. Note that `uninit_array` takes a type-level `const usize` parameter, so it is blocked (at least in its current form) on const generics.

Example:

```rust
use std::mem::MaybeUninit;

let input = b"Foo";
let f = u8::to_ascii_uppercase;

let mut buffer: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array();
let vec;
let output = if let Some(buffer) = buffer.get_mut(..input.len()) {
    buffer.iter_mut().zip(input).for_each(|(a, b)| { a.write(f(b)); });
    unsafe { MaybeUninit::slice_get_ref(buffer) }
} else {
    vec = input.iter().map(f).collect::<Vec<u8>>();
    &vec
};

assert_eq!(output, b"FOO");
```
2019-11-08 13:42:14 +09:00
Peter Todd
8fad66b431 Implement Debug for MaybeUninit
Precedent: UnsafeCell implements Debug even though it can't actually
display the value.
2019-11-07 08:50:18 -05:00
Mazdak Farrokhzad
379b19c17f Rollup merge of #63793 - oli-obk:🧹, r=dtolnay
Have tidy ensure that we document all `unsafe` blocks in libcore

cc @rust-lang/libs

I documented a few and added ignore flags on the other files. We can incrementally document the files, but won't regress any files this way.
2019-11-07 14:27:20 +01:00
Simon Sapin
639c4f779c MaybeUninit::uninit_array docs: better example 2019-11-07 12:05:01 +01:00
Simon Sapin
05c14bcc31 Apply docs suggestions from review
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
2019-11-07 12:05:01 +01:00
Simon Sapin
1906c6f714 Add MaybeUninit methods uninit_array, slice_get_ref, slice_get_mut 2019-11-07 12:05:00 +01:00
Yuki Okushi
12ffe5ffdd Rollup merge of #66044 - RalfJung:uninit-lint, r=oli-obk
Improve uninit/zeroed lint

* Also warn when creating a raw pointer with a NULL vtable.
* Also identify `MaybeUninit::uninit().assume_init()` and `MaybeUninit::zeroed().assume_init()` as dangerous.
2019-11-07 09:20:36 +09:00
Oliver Scherer
97633f814d Silence a deprecation warning 2019-11-06 11:04:42 +01:00
Oliver Scherer
954fc71962 Halloween... time to get rid of 👻 2019-11-06 11:04:42 +01:00
Oliver Scherer
02f9167f94 Have tidy ensure that we document all unsafe blocks in libcore 2019-11-06 11:04:42 +01:00
Daniel Henry-Mantilla
67f2200f4a Minor style improvements
Co-Authored-By: Ralf Jung <post@ralfj.de>
2019-11-04 19:47:31 +01:00
Ralf Jung
bb37d00787 more robust method checking through DefId and diagnostic_item 2019-11-04 10:11:58 +01:00
Daniel Henry-Mantilla
d9087cb388 Added a panic-on-uninhabited guard on get_ref and get_mut 2019-10-30 15:45:01 +01:00
Daniel Henry-Mantilla
2ebf5e6e2f Fix doctests 2019-10-30 15:44:55 +01:00
Daniel Henry-Mantilla
60671268c8 Improved MaybeUninit::get_{ref,mut} documentation 2019-10-29 23:56:04 +01:00
Tyler Mandry
7167a59241 Rollup merge of #65016 - lzutao:inline-mem-constfn, r=oli-obk
Always inline `mem::{size_of,align_of}` in debug builds

Those two are const fn and do not have any arguments. Inlining
helps reducing generated code size in debug builds.

See also #64996.
2019-10-18 13:48:10 -07:00
Jon Gjengset
45aca119a6 Stabilize mem::take (mem_take)
Tracking issue: https://github.com/rust-lang/rust/issues/61129
2019-10-08 18:04:18 -04:00
Lzu Tao
a87b44dbea Always inline mem::{size_of,align_of} in debug builds
Those two are const fn and do not have any arguments. Inlining
helps reducing generated code size in debug builds.
2019-10-02 18:36:06 +00:00
Lzu Tao
cdf1852a61 Add missing links for mem::needs_drop 2019-09-30 16:12:01 +00:00
Nils Liberg
fd505d7fd5 Improve wording in documentation of MaybeUninit 2019-09-28 11:49:45 +02:00
Andrew Banchich
8acf95886b update test
Use assert_eq and assert_ne over comparison operators.
2019-09-23 16:09:36 -04:00
bors
9e9a136fce Auto merge of #63575 - Centril:rollup-anlv9g5, r=Centril
Rollup of 11 pull requests

Successful merges:

 - #62984 (Add lint for excess trailing semicolons)
 - #63075 (Miri: Check that a ptr is aligned and inbounds already when evaluating `*`)
 - #63490 (libsyntax: cleanup and refactor `pat.rs`)
 - #63507 (When needing type annotations in local bindings, account for impl Trait and closures)
 - #63509 (Point at the right enclosing scope when using `await` in non-async fn)
 - #63528 (syntax: Remove `DummyResult::expr_only`)
 - #63537 (expand: Unimplement `MutVisitor` on `MacroExpander`)
 - #63542 (Add NodeId for Arm, Field and FieldPat)
 - #63543 (Merge Variant and Variant_)
 - #63560 (move test that shouldn't be in test/run-pass/)
 - #63570 (Adjust tracking issues for `MaybeUninit<T>` gates)

Failed merges:

r? @ghost
2019-08-15 00:32:05 +00:00
Mazdak Farrokhzad
24693d70d6 Adjust tracking issues for MaybeUninit<T> gates 2019-08-14 20:07:37 +02:00
Mark Rousskov
2601c86487 Handle cfg(bootstrap) throughout 2019-08-14 05:39:53 -04:00
Mark Rousskov
061245e2b1 Rollup merge of #63346 - RalfJung:zeroed-lint, r=eddyb
Lint on some incorrect uses of mem::zeroed / mem::uninitialized

Cc https://github.com/rust-lang/rust/issues/62825 and https://internals.rust-lang.org/t/make-mem-uninitialized-and-mem-zeroed-panic-for-some-types-where-0-is-a-niche/10605

This does not yet handle `NonNull`/`NonZero*`, but it is a start.

I also improved some doc issues I hit on the way, and added a useful helper to `TyS`.

EDIT: I added the relnotes label mostly as a proposal -- I think this is worth mentioning, but leave the decision up to the release team.
2019-08-11 15:18:40 -04:00
Ralf Jung
c5a63566d6 allow the lint if a few UB-demonstrating doc tests 2019-08-11 12:04:49 +02:00
bors
2b78e10ac1 Auto merge of #63343 - ishitatsuyuki:revert-62150, r=RalfJung
Back out #62150

Ref: #62825

cc @RalfJung
2019-08-11 09:58:01 +00:00
Tatsuyuki Ishi
2358e3eff6 Revert "Rollup merge of #62150 - alex:mem-uninit-refactor, r=RalfJung"
This reverts commit 1d45156866, reversing
changes made to 0f92eb8a4a.
2019-08-10 22:16:35 +09:00
Mazdak Farrokhzad
fb1f57e0e5 Rollup merge of #63034 - tmandry:reduce-generator-size-regressions, r=cramertj
Fix generator size regressions due to optimization

I tested the generator optimizations in #60187 and #61922 on the Fuchsia
build, and noticed that some small generators (about 8% of the async fns
in our build) increased in size slightly.

This is because in #60187 we split the fields into two groups, a
"prefix" non-overlap region and an overlap region, and lay them out
separately. This can introduce unnecessary padding bytes between the two
groups.

In every single case in the Fuchsia build, it was due to there being
only a single variant being used in the overlap region. This means that
we aren't doing any overlapping, period. So it's better to combine the
two regions into one and lay out all the fields at once, which is what
this change does.

r? @cramertj
cc @eddyb @Zoxc
2019-08-07 01:39:25 +02:00
Ralf Jung
1821414b7b clarify 2019-08-06 10:41:48 +02:00
Ralf Jung
1b9eb4ac4c be clear that 1-init Vec being valid (but not safe) is not a stable guarantee 2019-08-06 09:47:42 +02:00
Ralf Jung
e1875742d0 assume_init: warn about valid != safe 2019-08-05 20:13:59 +02:00
Mazdak Farrokhzad
4520a39b94 Rollup merge of #63215 - gnzlbg:patch-6, r=Centril
Clarify semantics of mem::zeroed

Clarifies the semantics of `mem::zeroed`.

r? @Centril

cc @RalfJung
2019-08-03 00:09:14 +02:00
gnzlbg
3725e3542f Consistency. 2019-08-02 15:07:19 +02:00
gnzlbg
13b4afe4ba Remove trailing whitespace
I had one job...
2019-08-02 14:12:12 +02:00
gnzlbg
57f94237e1 Clarify semantics of mem::zeroed 2019-08-02 13:39:07 +02:00
Bruce Mitchener
86633b6389 Fix typos in doc comments. 2019-08-02 01:36:36 +07:00
Tyler Mandry
6fae7f8071 Wrap promoted generator fields in MaybeUninit
This prevents uninhabited fields from "infecting" the abi and
largest_niche of the generator layout.

This fixes a latent bug, where an uninhabited field could be promoted to
the generator prefix and cause the entire generator to become
uninhabited.
2019-07-29 12:17:49 -07:00
Mazdak Farrokhzad
fbfd542277 Rollup merge of #62360 - Aaron1011:patch-2, r=RalfJung
Document that ManuallyDrop::drop should not called more than once

Double dropping is unsound (e.g. https://github.com/rust-lang/rust/issues/60977). This commit documents the fact that `ManuallyDrop::drop` should not be called multiple times on the same instance, as it might not be immediately obvious that this counts as a use of uninitialized data.
2019-07-28 21:19:52 +02:00
Aaron Hill
a93f4abe24 Update wording 2019-07-27 18:42:47 -04:00
Mika Lehtinen
a44f43e8b5 Fix typo in mem::uninitialized doc 2019-07-24 11:34:30 +03:00
Ralf Jung
f3abbf7103 tidy is being silly 2019-07-21 12:59:51 +02:00
Ralf Jung
4b47e78a16 use a const to hack around promotion limitations 2019-07-21 12:28:18 +02:00