about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2023-05-20Update documentation for `drop_in_place()`Patrick Walton-6/+24
2023-05-20Auto merge of #111646 - Voultapher:restore-branchless-code-gen-for-merge, ↵bors-26/+12
r=cuviper Use code with reliable branchless code-gen for slice::sort merge The recent LLVM 16 update changes code-gen to be not branchless anymore, in the slice::sort implementation merge function. This improves performance by 30% for random patterns, restoring the performance to the state with LLVM 15. Fixes #111559
2023-05-20Auto merge of #111778 - Dylan-DPC:rollup-107ig9h, r=Dylan-DPCbors-1/+1
Rollup of 10 pull requests Successful merges: - #111491 (Dont check `must_use` on nested `impl Future` from fn) - #111606 (very minor cleanups) - #111619 (Add timings for MIR passes to profiling report) - #111652 (Better diagnostic for `use Self::..`) - #111665 (Add more tests for the offset_of macro) - #111708 (Give a more useful location for where a span_bug was delayed) - #111715 (Fix doc comment for `ConstParamTy` derive) - #111723 (style: do not overwrite obligations) - #111743 (Improve cgu merging debug output) - #111762 (fix: emit error when fragment is `MethodReceiverExpr` and items is empty) r? `@ghost` `@rustbot` modify labels: rollup
2023-05-20optimize next_chunk impls for Filter and FilterMapThe 8472-1/+116
2023-05-20Rollup merge of #111715 - juntyr:const-param-ty-derive-fix, r=NilstriebDylan DPC-1/+1
Fix doc comment for `ConstParamTy` derive See https://github.com/rust-lang/rust/pull/111670#discussion_r1196453888 Thanks ````@Nilstrieb```` for the pointer :)
2023-05-20Auto merge of #111524 - scottmcm:escape-using-ascii, r=cuviperbors-50/+65
`ascii::Char`-ify the escaping code in `core` This means that `EscapeIterInner::as_str` no longer needs unsafe code, because the type system ensures the internal buffer is only ASCII, and thus valid UTF-8. Come to think of it, this also gives it a (non-guaranteed) niche. cc `@BurntSushi` as potentially interested `ascii::Char` tracking issue: #110998
2023-05-19Auto merge of #111453 - scottmcm:constify-as-slice, r=Mark-Simulacrumbors-6/+6
constify `slice_as_chunks` (unstable) Tracking issue: #74985 Nothing complicated required; just adding `const` to the declarations.
2023-05-19Add slice::{split_,}{first,last}_chunk{,_mut}ltdk-0/+258
2023-05-19tidyIbraheem Ahmed-2/+1
2023-05-19remove unstable `Poll::ready`Ibraheem Ahmed-94/+3
2023-05-19constify `slice_as_chunks` (unstable)Scott McMurray-6/+6
Tracking issue: 74985 Nothing complicated required; just adding `const` to the declarations.
2023-05-19Auto merge of #111590 - dtolnay:panictemporaries, r=bjorn3bors-12/+12
Shorten even more panic temporary lifetimes Followup to #104134. As pointed out by `@bjorn3` in https://github.com/rust-lang/rust/pull/104134#pullrequestreview-1425585948, there are other cases in the panic macros which would also benefit from dropping their non-Send temporaries as soon as possible, avoiding pointlessly holding them across an await point. For the tests added in this PR, here are the failures you get today on master without the macro changes in this PR: <details> <summary>tests/ui/macros/panic-temporaries-2018.rs</summary> ```console error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries-2018.rs:52:18 | LL | require_send(panic_display()); | ^^^^^^^^^^^^^^^ future returned by `panic_display` is not `Send` | = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries-2018.rs:35:31 | LL | f(panic!("{}", NOT_SEND)).await; | -------- ^^^^^- `NOT_SEND` is later dropped here | | | | | await occurs here, with `NOT_SEND` maybe used later | has type `NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries-2018.rs:48:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries-2018.rs:52:18 | LL | require_send(panic_display()); | ^^^^^^^^^^^^^^^ future returned by `panic_display` is not `Send` | = help: within `NotSend`, the trait `Sync` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries-2018.rs:35:31 | LL | f(panic!("{}", NOT_SEND)).await; | ---------------------- ^^^^^- the value is later dropped here | | | | | await occurs here, with the value maybe used later | has type `&NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries-2018.rs:48:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries-2018.rs:53:18 | LL | require_send(panic_str()); | ^^^^^^^^^^^ future returned by `panic_str` is not `Send` | = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries-2018.rs:40:36 | LL | f(panic!((NOT_SEND, "...").1)).await; | -------- ^^^^^- `NOT_SEND` is later dropped here | | | | | await occurs here, with `NOT_SEND` maybe used later | has type `NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries-2018.rs:48:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries-2018.rs:54:18 | LL | require_send(unreachable_display()); | ^^^^^^^^^^^^^^^^^^^^^ future returned by `unreachable_display` is not `Send` | = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries-2018.rs:45:31 | LL | f(unreachable!(NOT_SEND)).await; | -------- ^^^^^- `NOT_SEND` is later dropped here | | | | | await occurs here, with `NOT_SEND` maybe used later | has type `NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries-2018.rs:48:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries-2018.rs:54:18 | LL | require_send(unreachable_display()); | ^^^^^^^^^^^^^^^^^^^^^ future returned by `unreachable_display` is not `Send` | = help: within `NotSend`, the trait `Sync` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries-2018.rs:45:31 | LL | f(unreachable!(NOT_SEND)).await; | ---------------------- ^^^^^- the value is later dropped here | | | | | await occurs here, with the value maybe used later | has type `&NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries-2018.rs:48:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: aborting due to 5 previous errors ``` </details> <details> <summary>tests/ui/macros/panic-temporaries.rs</summary> ```console error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries.rs:42:18 | LL | require_send(panic_display()); | ^^^^^^^^^^^^^^^ future returned by `panic_display` is not `Send` | = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries.rs:35:31 | LL | f(panic!("{}", NOT_SEND)).await; | -------- ^^^^^- `NOT_SEND` is later dropped here | | | | | await occurs here, with `NOT_SEND` maybe used later | has type `NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries.rs:38:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: future cannot be sent between threads safely --> tests/ui/macros/panic-temporaries.rs:42:18 | LL | require_send(panic_display()); | ^^^^^^^^^^^^^^^ future returned by `panic_display` is not `Send` | = help: within `NotSend`, the trait `Sync` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> tests/ui/macros/panic-temporaries.rs:35:31 | LL | f(panic!("{}", NOT_SEND)).await; | ---------------------- ^^^^^- the value is later dropped here | | | | | await occurs here, with the value maybe used later | has type `&NotSend` which is not `Send` note: required by a bound in `require_send` --> tests/ui/macros/panic-temporaries.rs:38:25 | LL | fn require_send(_: impl Send) {} | ^^^^ required by this bound in `require_send` error: aborting due to 2 previous errors ``` </details> r? bjorn3
2023-05-18Option::map_or_else: Show an example of integrating with ResultColin Walters-1/+20
Moving this from https://github.com/rust-lang/libs-team/issues/59 where an API addition was rejected. But I think it's valuable to add this example to the documentation at least.
2023-05-18Fix doc comment for `ConstParamTy` deriveJuniper Tyree-1/+1
See https://github.com/rust-lang/rust/pull/111670#discussion_r1196453888 Thanks @Nilstrieb for the pointer :)
2023-05-18Document `Pin` memory layoutJules Bertholet-0/+2
2023-05-17Rollup merge of #111654 - JoJoJet:unsafe-cell-from-mut-lib, r=joshtriplettDylan DPC-0/+21
Add a conversion from `&mut T` to `&mut UnsafeCell<T>` Provides a safe way of downgrading an exclusive reference into an alias-able `&UnsafeCell<T>` reference. ACP: https://github.com/rust-lang/libs-team/issues/198.
2023-05-17Rollup merge of #111649 - Nilstrieb:derive-const-param-ty, r=BoxyUwUDylan DPC-0/+8
Add derive for `core::marker::ConstParamTy` This makes it easier to implement it for a type, just like `Copy`. `@BoxyUwU` half asked me to add it
2023-05-17Rollup merge of #111043 - jmillikin:cstr-is-empty, r=dtolnayDylan DPC-3/+2
Stabilize feature `cstr_is_empty` Fixes #102444 ACP: https://github.com/rust-lang/libs-team/issues/106
2023-05-16add `UnsafeCell::from_mut`JoJoJet-0/+21
2023-05-16Add derive for `core::marker::ConstParamTy`Nilstrieb-0/+8
This makes it easier to implement it for a type, just like `Copy`.
2023-05-16Hide repr attribute from doc of types without guaranteed reprDavid Tolnay-9/+9
2023-05-16Use code with reliable branchless code-gen for slice::sort mergeLukas Bergdoll-26/+12
The recent LLVM 16 update changes code-gen to be not branchless anymore, in the slice::sort implementation merge function. This improves performance by 30% for random patterns, restoring the performance to the state with LLVM 15.
2023-05-16Auto merge of #111044 - jmillikin:nonzero-negation, r=dtolnaybors-18/+12
Stabilize feature `nonzero_negation_ops` Fixes #102443 ACP: https://github.com/rust-lang/libs-team/issues/105
2023-05-15Mark internal functions and traits unsafeLegionMammal978-24/+45
2023-05-15Give better error when collecting into `&[T]`Chayim Refael Friedman-0/+10
2023-05-15Rollup merge of #111587 - cbeuw:copy-for-deref, r=oli-obkMatthias Krüger-1/+2
Custom MIR: Support `Rvalue::CopyForDeref` r? `@oli-obk` or `@tmiasko` or `@JakobDegen`
2023-05-15Rollup merge of #108356 - gftea:master, r=workingjubileeMatthias Krüger-1/+3
improve doc test for UnsafeCell::raw_get improve docs of public API `UnsafeCell::raw_get`
2023-05-15Shorten lifetime of even more panic temporariesDavid Tolnay-12/+12
2023-05-15DocumentationAndy Wang-1/+1
2023-05-15Add CopyForDeref to custom MIRAndy Wang-0/+1
2023-05-15Rollup merge of #111581 - scottmcm:fix-pattern-comment, r=workingjubileeMatthias Krüger-4/+4
Fix some misleading and copy-pasted `Pattern` examples These examples were listed twice and also were confusable with doing a substring match instead of a any-of-set match.
2023-05-15Rollup merge of #102673 - lukas-code:infered-lifetimes, r=ehussMatthias Krüger-1/+1
Update doc for `PhantomData` to match code example After https://github.com/rust-lang/rust/pull/106621, there is no longer a `T: 'a` annotation in the doc example, so update the text to match the code.
2023-05-14Fix some misleading and copy-pasted `Pattern` examplesScott McMurray-4/+4
These examples were listed twice and also were confusable with doing a substring match instead of a any-of-set match.
2023-05-15Auto merge of #108273 - tspiteri:const_slice_split_at_not_mut, r=dtolnaybors-1/+2
Stabilize const slice::split_at This stabilizes the use of the following method in const context: ```rust impl<T> [T] { pub const fn split_at(&self, mid: usize) -> (&[T], &[T]); } ``` cc tracking issue #101158
2023-05-14Auto merge of #92048 - Urgau:num-midpoint, r=scottmcmbors-0/+207
Add midpoint function for all integers and floating numbers This pull-request adds the `midpoint` function to `{u,i}{8,16,32,64,128,size}`, `NonZeroU{8,16,32,64,size}` and `f{32,64}`. This new function is analog to the [C++ midpoint](https://en.cppreference.com/w/cpp/numeric/midpoint) function, and basically compute `(a + b) / 2` with a rounding towards ~~`a`~~ negative infinity in the case of integers. Or simply said: `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a sufficiently-large signed integral type. Note that unlike the C++ function this pull-request does not implement this function on pointers (`*const T` or `*mut T`). This could be implemented in a future pull-request if desire. ### Implementation For `f32` and `f64` the implementation in based on the `libcxx` [one](https://github.com/llvm/llvm-project/blob/18ab892ff7e9032914ff7fdb07685d5945c84fef/libcxx/include/__numeric/midpoint.h#L65-L77). I originally tried many different approach but all of them failed or lead me with a poor version of the `libcxx`. Note that `libstdc++` has a very similar one; Microsoft STL implementation is also basically the same as `libcxx`. It unfortunately doesn't seems like a better way exist. For unsigned integers I created the macro `midpoint_impl!`, this macro has two branches: - The first one take `$SelfT` and is used when there is no unsigned integer with at least the double of bits. The code simply use this formula `a + (b - a) / 2` with the arguments in the correct order and signs to have the good rounding. - The second branch is used when a `$WideT` (at least double of bits as `$SelfT`) is provided, using a wider number means that no overflow can occur, this greatly improve the codegen (no branch and less instructions). For signed integers the code basically forwards the signed numbers to the unsigned version of midpoint by mapping the signed numbers to their unsigned numbers (`ex: i8 [-128; 127] to [0; 255]`) and vice versa. I originally created a version that worked directly on the signed numbers but the code was "ugly" and not understandable. Despite this mapping "overhead" the codegen is better than my most optimized version on signed integers. ~~Note that in the case of unsigned numbers I tried to be smart and used `#[cfg(target_pointer_width = "64")]` to determine if using the wide version was better or not by looking at the assembly on godbolt. This was applied to `u32`, `u64` and `usize` and doesn't change the behavior only the assembly code generated.~~
2023-05-14explain that `PhantomData<&'a T>` infers `T: 'a`Lukas Markeffsky-1/+1
2023-05-14Shorten lifetime of panic temporaries in panic_fmt caseDavid Tolnay-6/+10
2023-05-14Auto merge of #111425 - Bryanskiy:privacy_ef, r=petrochenkovbors-0/+6
Populate effective visibilities in `rustc_privacy` (take 2) Same as https://github.com/rust-lang/rust/pull/110907 + regressions fixes. Fixes https://github.com/rust-lang/rust/issues/111359. r? `@petrochenkov`
2023-05-13Auto merge of #111447 - scottmcm:remove-more-assumes, r=thomccbors-4/+0
Remove useless `assume`s from `slice::iter(_mut)` You were right in https://github.com/rust-lang/rust/pull/111395#discussion_r1190312704, r? `@the8472` LLVM already removes these assumes while optimizing, as can be seen in <https://rust.godbolt.org/z/KTfWKbdEM>.
2023-05-12`ascii::Char`-ify the escaping codeScott McMurray-50/+65
This means that `EscapeIterInner::as_str` no longer needs unsafe code, because the type system ensures the internal buffer is only ASCII, and thus valid UTF-8.
2023-05-12Remove useless `assume`s from `slice::iter(_mut)`Scott McMurray-4/+0
2023-05-13Auto merge of #103413 - RalfJung:phantom-dropck, r=lcnrbors-10/+73
PhantomData: fix documentation wrt interaction with dropck As far as I could find out, the `PhantomData`-dropck interaction *only* affects code using `may_dangle`. The documentation in the standard library has not been updated for 8 years and thus stems from a time when Rust still used "parametric dropck", before [RFC 1238](https://rust-lang.github.io/rfcs/1238-nonparametric-dropck.html). Back then what the docs said was correct, but with `may_dangle` dropck it stopped being entirely accurate and these days, with NLL, it is actively misleading. Fixes https://github.com/rust-lang/rust/issues/102810 Fixes https://github.com/rust-lang/rust/issues/70841 Cc `@nikomatsakis` I hope what I am saying here is right.^^
2023-05-12hedge for future changesRalf Jung-4/+7
Co-authored-by: lcnr <rust@lcnr.de>
2023-05-12Auto merge of #109732 - Urgau:uplift_drop_forget_ref_lints, r=davidtwcobors-3/+4
Uplift `clippy::{drop,forget}_{ref,copy}` lints This PR aims at uplifting the `clippy::drop_ref`, `clippy::drop_copy`, `clippy::forget_ref` and `clippy::forget_copy` lints. Those lints are/were declared in the correctness category of clippy because they lint on useless and most probably is not what the developer wanted. ## `drop_ref` and `forget_ref` The `drop_ref` and `forget_ref` lint checks for calls to `std::mem::drop` or `std::mem::forget` with a reference instead of an owned value. ### Example ```rust let mut lock_guard = mutex.lock(); std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex // still locked operation_that_requires_mutex_to_be_unlocked(); ``` ### Explanation Calling `drop` or `forget` on a reference will only drop the reference itself, which is a no-op. It will not call the `drop` or `forget` method on the underlying referenced value, which is likely what was intended. ## `drop_copy` and `forget_copy` The `drop_copy` and `forget_copy` lint checks for calls to `std::mem::forget` or `std::mem::drop` with a value that derives the Copy trait. ### Example ```rust let x: i32 = 42; // i32 implements Copy std::mem::forget(x) // A copy of x is passed to the function, leaving the // original unaffected ``` ### Explanation Calling `std::mem::forget` [does nothing for types that implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the value will be copied and moved into the function on invocation. ----- Followed the instructions for uplift a clippy describe here: https://github.com/rust-lang/rust/pull/99696#pullrequestreview-1134072751 cc `@m-ou-se` (as T-libs-api leader because the uplifting was discussed in a recent meeting)
2023-05-11Auto merge of #111395 - scottmcm:slice-iter-zst-experiment, r=the8472bors-35/+51
Simplify the implementation of iterators over slices of ZSTs Currently, slice iterators over ZSTs store `end = start.wrapping_byte_add(len)`. That's slightly convenient for `is_empty`, but kinda annoying for pretty much everything else -- see bugs like #42789, for example. This PR instead changes it to just `end = ptr::invalid(len)` instead. That's easier to think about (IMHO, at least) as well as easier to represent. `next` is still to big to get inlined into the mir-opt/pre-codegen/ tests, but if I bump the inline threshold to force it to show the whole thing, this implementation is also less MIR: ``` > git diff --numstat 241 370 tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir 255 329 tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir 184 216 tests/mir-opt/pre-codegen/slice_iter.slice_iter_mut_next_back.PreCodegen.after.mir 182 254 tests/mir-opt/pre-codegen/slice_iter.slice_iter_next.PreCodegen.after.mir ``` (That's ≈70 lines less for `Iter::next`, for example.) r? `@ghost` ~~Built atop #111282, so draft until that lands.~~
2023-05-11Populate effective visibilities in rustc_privacyBryanskiy-0/+6
2023-05-10Simplify the implementation of iterators over slices of ZSTsScott McMurray-35/+51
Currently, slice iterators over ZSTs store `end = start.wrapping_byte_add(len)`. That's slightly convenient for `is_empty`, but kinda annoying for pretty much everything else -- see bugs like 42789, for example. This PR instead changes it to just `end = ptr::invalid(len)` instead. That's easier to think about (IMHO, at least) as well as easier to represent.
2023-05-10Allow the drop_copy lint in some library examplesUrgau-0/+1
2023-05-10Remove useless drop of copy typeUrgau-3/+3
2023-05-10Rollup merge of #111408 - TomMD:patch-1, r=workingjubileeMatthias Krüger-1/+1
Fix incorrect implication of transmuting slices transmute<&[u8]> would be useful and as a beginner it is confusing to see documents casually confuse the types of &[u8] and [u8; SZ]