summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2022-09-19Revert "Auto merge of #95295 - CAD97:layout-isize, r=scottmcm"Pietro Albini-14/+20
This reverts commit 4ec97d991b1bd86dc89fee761d79ac8e85239a08, reversing changes made to 95e77648e466c780a9adce2fa3d3eb5e423dc04e.
2022-09-19Revert "Auto merge of #99136 - CAD97:layout-faster, r=scottmcm"Pietro Albini-38/+21
This reverts commit 87588a2afd9ca903366f0deaf84d805f34469384, reversing changes made to c80dde43f992f3eb419899a34551b84c6301f8e8.
2022-08-03Auto merge of #100023 - saethlin:send-sync-chunksmut, r=m-ou-sebors-0/+24
Add back Send and Sync impls on ChunksMut iterators Fixes https://github.com/rust-lang/rust/issues/100014 These were accidentally removed in #94247 because the representation was changed from `&mut [T]` to `*mut T`, which has `!Send + !Sync`.
2022-08-03Rollup merge of #99614 - RalfJung:transmute-is-not-memcpy, r=thomccDylan DPC-13/+18
do not claim that transmute is like memcpy Saying transmute is like memcpy is not a well-formed statement, since memcpy is by-ref whereas transmute is by-val. The by-val nature of transmute inherently means that padding is lost along the way. (This is not specific to transmute, this is how all by-value operations work.) So adjust the docs to clarify this aspect. Cc `@workingjubilee`
2022-08-02wordsmithingRalf Jung-2/+4
2022-08-02Auto merge of #92268 - jswrenn:transmute, r=oli-obkbors-0/+47
Initial implementation of transmutability trait. *T'was the night before Christmas and all through the codebase, not a miri was stirring — no hint of `unsafe`!* This PR provides an initial, **incomplete** implementation of *[MCP 411: Lang Item for Transmutability](https://github.com/rust-lang/compiler-team/issues/411)*. The `core::mem::BikeshedIntrinsicFrom` trait provided by this PR is implemented on-the-fly by the compiler for types `Src` and `Dst` when the bits of all possible values of type `Src` are safely reinterpretable as a value of type `Dst`. What this PR provides is: - [x] [support for transmutations involving primitives](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/primitives) - [x] [support for transmutations involving arrays](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/arrays) - [x] [support for transmutations involving structs](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/structs) - [x] [support for transmutations involving enums](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/enums) - [x] [support for transmutations involving unions](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/unions) - [x] [support for weaker validity checks](https://github.com/jswrenn/rust/blob/transmute/src/test/ui/transmutability/unions/should_permit_intersecting_if_validity_is_assumed.rs) (i.e., `Assume::VALIDITY`) - [x] visibility checking What isn't yet implemented: - [ ] transmutability options passed using the `Assume` struct - [ ] [support for references](https://github.com/jswrenn/rust/blob/transmute/src/test/ui/transmutability/references.rs) - [ ] smarter error messages These features will be implemented in future PRs.
2022-08-01Add back Send and Sync impls on ChunksMut iteratorsBen Kimock-0/+24
These were accidentally removed in #94247 because the representation was changed from &mut [T] to *mut T, which has !Send + !Sync.
2022-07-31Add validation to const fn CStr creationBlackHoleFox-13/+34
2022-07-31typoRalf Jung-1/+1
Co-authored-by: Jubilee <46493976+workingjubilee@users.noreply.github.com>
2022-07-29Rollup merge of #99781 - workingjubilee:demo-string-from-cstr, r=thomccYuki Okushi-3/+3
Use String::from_utf8_lossy in CStr demo Fixes rust-lang/rust#99755.
2022-07-28Rollup merge of #99689 - dtolnay:write, r=Mark-SimulacrumDylan DPC-8/+6
Revert `write!` and `writeln!` to late drop temporaries Closes (on master, but not on beta) #99684 by reverting the `write!` and `writeln!` parts of #96455. argument position | before<br>#94868 | after<br>#94868 | after<br>#96455 | after<br>this PR | desired<br>(unimplementable) --- |:---:|:---:|:---:|:---:|:---: `write!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late** `write!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺* `writeln!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late** `writeln!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺* `print!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺* `println!("…", $tmp)` | *early⸺* | **⸺late** | *early⸺* | *early⸺* | *early⸺* `eprint!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺* `eprintln!("…", $tmp)` | *early⸺* | **⸺late**| *early⸺* | *early⸺* | *early⸺* `panic!("…", $tmp)` | *early⸺* | *early⸺* | *early⸺* | *early⸺* | *early⸺* "Late drop" refers to dropping temporaries at the nearest semicolon **outside** of the macro invocation. "Early drop" refers to dropping temporaries inside of the macro invocation.
2022-07-28add more docs regarding ideographic numbersVincenzo Palazzo-1/+11
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2022-07-28Auto merge of #99182 - RalfJung:mitigate-uninit, r=scottmcmbors-1/+12
mem::uninitialized: mitigate many incorrect uses of this function Alternative to https://github.com/rust-lang/rust/pull/98966: fill memory with `0x01` rather than leaving it uninit. This is definitely bitewise valid for all `bool` and nonnull types, and also those `Option<&T>` that we started putting `noundef` on. However it is still invalid for `char` and some enums, and on references the `dereferenceable` attribute is still violated, so the generated LLVM IR still has UB -- but in fewer cases, and `dereferenceable` is hopefully less likely to cause problems than clearly incorrect range annotations. This can make using `mem::uninitialized` a lot slower, but that function has been deprecated for years and we keep telling everyone to move to `MaybeUninit` because it is basically impossible to use `mem::uninitialized` correctly. For the cases where that hasn't helped (and all the old code out there that nobody will ever update), we can at least mitigate the effect of using this API. Note that this is *not* in any way a stable guarantee -- it is still UB to call `mem::uninitialized::<bool>()`, and Miri will call it out as such. This is somewhat similar to https://github.com/rust-lang/rust/pull/87032, which proposed to make `uninitialized` return a buffer filled with 0x00. However - That PR also proposed to reduce the situations in which we panic, which I don't think we should do at this time. - The 0x01 bit pattern means that nonnull requirements are satisfied, which (due to references) is the most common validity invariant. `@5225225` I hope I am using `cfg(sanitize)` the right way; I was not sure for which ones to test here. Cc https://github.com/rust-lang/rust/issues/66151 Fixes https://github.com/rust-lang/rust/issues/87675
2022-07-27safe transmute: reference tracking issueJack Wrenn-3/+3
ref: https://github.com/rust-lang/rust/pull/92268#discussion_r925266769
2022-07-27safe transmute: add `rustc_on_unimplemented` to `BikeshedIntrinsicFrom`Jack Wrenn-0/+4
ref: https://github.com/rust-lang/rust/pull/92268#discussion_r925266583
2022-07-27Initial (incomplete) implementation of transmutability trait.Jack Wrenn-0/+43
This initial implementation handles transmutations between types with specified layouts, except when references are involved. Co-authored-by: Igor null <m1el.2027@gmail.com>
2022-07-27Rollup merge of #94247 - saethlin:chunksmut-aliasing, r=the8472Guillaume Gomez-72/+122
Fix slice::ChunksMut aliasing Fixes https://github.com/rust-lang/rust/issues/94231, details in that issue. cc `@RalfJung` This isn't done just yet, all the safety comments are placeholders. But otherwise, it seems to work. I don't really like this approach though. There's a lot of unsafe code where there wasn't before, but as far as I can tell the only other way to uphold the aliasing requirement imposed by `__iterator_get_unchecked` is to use raw slices, which I think require the same amount of unsafe code. All that would do is tie the `len` and `ptr` fields together. Oh I just looked and I'm pretty sure that `ChunksExactMut`, `RChunksMut`, and `RChunksExactMut` also need to be patched. Even more reason to put up a draft.
2022-07-27Rollup merge of #99704 - fee1-dead-contrib:add_self_tilde_const_trait, r=oli-obkYuki Okushi-2/+3
Add `Self: ~const Trait` to traits with `#[const_trait]` r? `@oli-obk`
2022-07-26Clarify safety commentsBen Kimock-59/+47
2022-07-26Explain how *mut [T] helps, and how we rely on the check in split_at_mutBen Kimock-20/+55
2022-07-26Force the Cow into a StringJubilee Young-3/+3
2022-07-26Use String::from_utf8_lossy in CStr demoJubilee Young-1/+1
2022-07-26Rollup merge of #99757 - asquared31415:patch-1, r=Dylan-DPCMatthias Krüger-1/+1
Make `transmute_copy` docs read better
2022-07-26Add `Self: ~const Trait` to traits with `#[const_trait]`Deadbeef-2/+3
2022-07-26Make `transmute_copy` docs read betterasquared31415-1/+1
2022-07-26Rollup merge of #99692 - RalfJung:too-far, r=oli-obkDylan DPC-1/+1
interpret, ptr_offset_from: refactor and test too-far-apart check We didn't have any tests for the "too far apart" message, and indeed that check mostly relied on the in-bounds check and was otherwise probably not entirely correct... so I rewrote that check, and it is before the in-bounds check so we can test it separately.
2022-07-26Rollup merge of #99084 - RalfJung:write_bytes, r=thomccYuki Okushi-37/+14
clarify how write_bytes can lead to UB due to invalid values Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/330 Cc ``@5225225``
2022-07-26Rollup merge of #92390 - fee1-dead-contrib:const_cmp, r=oli-obkYuki Okushi-19/+56
Constify a few `(Partial)Ord` impls Only a few `impl`s are constified for now, as #92257 has not landed in the bootstrap compiler yet and quite a few impls would need that fix. This unblocks #92228, which unblocks marking iterator methods as `default_method_body_is_const`.
2022-07-24interpret, ptr_offset_from: refactor and test too-far-apart checkRalf Jung-1/+1
2022-07-24add miri-track-caller to some intrinsic-exposing methodsRalf Jung-0/+38
2022-07-24Revert write! and writeln! to late drop temporariesDavid Tolnay-8/+6
2022-07-24add const hack commentDeadbeef-0/+6
2022-07-24Add issue numbersDeadbeef-17/+17
2022-07-24Constify a few const `(Partial)Ord` implsDeadbeef-19/+50
2022-07-24Auto merge of #98674 - RalfJung:miri-stacktrace-pruning, r=Mark-Simulacrumbors-0/+75
miri: prune some atomic operation and raw pointer details from stacktrace Since Miri removes `track_caller` frames from the stacktrace, adding that attribute can help make backtraces more readable (similar to how it makes panic locations better). I made them only show up with `cfg(miri)` to make sure the extra arguments induced by `track_caller` do not cause any runtime performance trouble. This is also testing the waters for whether the libs team is okay with having these attributes in their code, or whether you'd prefer if we find some other way to do this. If you are fine with this, we will probably want to add it to a lot more functions (all the other atomic operations, to start). Before: ``` error: Undefined Behavior: Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) --> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:2594:23 | 2594 | SeqCst => intrinsics::atomic_load_seqcst(dst), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `std::sync::atomic::atomic_load::<usize>` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:2594:23 = note: inside `std::sync::atomic::AtomicUsize::load` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:1719:26 note: inside closure at ../miri/tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 --> ../miri/tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 | 22 | (&*c.0).load(Ordering::SeqCst) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` After: ``` error: Undefined Behavior: Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) --> tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 | 22 | (&*c.0).load(Ordering::SeqCst) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside closure at tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 ```
2022-07-23say some more things about how transmute is UBRalf Jung-11/+13
2022-07-23Auto merge of #93397 - joshtriplett:sort-floats, r=Amanieubors-0/+60
Add `[f32]::sort_floats` and `[f64]::sort_floats` It's inconvenient to sort a slice or Vec of floats, compared to sorting integers. To simplify numeric code, add a convenience method to `[f32]` and `[f64]` to sort them using `sort_unstable_by` with `total_cmp`.
2022-07-22do not claim that transmute is like memcpyRalf Jung-2/+3
2022-07-22adjust UnsafeCell documentationRalf Jung-9/+18
2022-07-22Auto merge of #99491 - workingjubilee:sync-psimd, r=workingjubileebors-0/+1
Sync in portable-simd subtree r? `@ghost`
2022-07-22Rollup merge of #99579 - CleanCut:expect-warning, r=joshtriplettDylan DPC-0/+9
Add same warning to Result::expect as Result::unwrap I was reading a recent blog post by Jimmy Hartzell and [he noted](https://www.thecodedmessage.com/posts/2022-07-14-programming-unwrap/#context): > I will however note that the documentation of `unwrap` comes with [a warning not to use it](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap). The warning is framed in terms of the fact that `unwrap` may panic, but the [documentation of `expect`](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect), where this is equally true, does not come with such a warning. It _is_ equally true. Let's add the same warning to `expect`. This PR is a copy-and-paste of the warning text from the docstring for `unwrap`.
2022-07-22Rollup merge of #98174 - Kixunil:rename_ptr_as_mut_const_to_cast, r=scottmcmDylan DPC-4/+6
Rename `<*{mut,const} T>::as_{const,mut}` to `cast_` This renames the methods to use the `cast_` prefix instead of `as_` to make it more readable and avoid confusion with `<*mut T>::as_mut()` which is `unsafe` and returns a reference. Sorry, didn't notice ACP process exists, opened https://github.com/rust-lang/libs-team/issues/51 See #92675
2022-07-22Auto merge of #99420 - RalfJung:vtable, r=oli-obkbors-2/+37
make vtable pointers entirely opaque This implements the scheme discussed in https://github.com/rust-lang/unsafe-code-guidelines/issues/338: vtable pointers should be considered entirely opaque and not even readable by Rust code, similar to function pointers. - We have a new kind of `GlobalAlloc` that symbolically refers to a vtable. - Miri uses that kind of allocation when generating a vtable. - The codegen backends, upon encountering such an allocation, call `vtable_allocation` to obtain an actually dataful allocation for this vtable. - We need new intrinsics to obtain the size and align from a vtable (for some `ptr::metadata` APIs), since direct accesses are UB now. I had to touch quite a bit of code that I am not very familiar with, so some of this might not make much sense... r? `@oli-obk`
2022-07-21add same warning to Result::expect as Result::unwrapNathan Stocks-0/+9
2022-07-21Rollup merge of #99454 - benluelo:control-flow/continue-combinators, r=scottmcmMatthias Krüger-0/+35
Add map_continue and continue_value combinators to ControlFlow As suggested in this comment: https://github.com/rust-lang/rust/issues/75744#issuecomment-1188549494 Related tracking issue: https://github.com/rust-lang/rust/issues/75744 r? ``````@scottmcm``````
2022-07-21Rename `<*{mut,const} T>::as_{const,mut}` to `cast_`Martin Habovstiak-4/+6
This renames the methods to use the `cast_` prefix instead of `as_` to make it more readable and avoid confusion with `<*mut T>::as_mut()` which is `unsafe` and returns a reference. See #92675
2022-07-20Introduce core::simd trait imports in testsJubilee Young-0/+1
2022-07-20various nits from reviewRalf Jung-2/+1
2022-07-20use extern type for extra opaquenessRalf Jung-7/+8
2022-07-20incorporate some review feedbackRalf Jung-0/+3