summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2022-05-14Auto merge of #95602 - scottmcm:faster-array-intoiter-fold, r=the8472bors-1/+55
Fix `array::IntoIter::fold` to use the optimized `Range::fold` It was using `Iterator::by_ref` in the implementation, which ended up pessimizing it enough that, for example, it didn't vectorize when we tried it in the <https://rust-lang.zulipchat.com/#narrow/stream/257879-project-portable-simd/topic/Reducing.20sum.20into.20wider.20types> conversation. Demonstration that the codegen test doesn't pass on the current nightly: <https://rust.godbolt.org/z/Taxev5eMn>
2022-05-13Rollup merge of #97003 - nnethercote:rm-const_fn-attrs, r=fee1-deadMatthias Krüger-3/+0
Remove some unnecessary `rustc_allow_const_fn_unstable` attributes. r? `@fee1-dead`
2022-05-13Rollup merge of #96154 - lukaslueg:unreachablehint, r=scottmcmMatthias Krüger-15/+66
Expand core::hint::unreachable_unchecked() docs Rework the docs for `unreachable_unchecked`, encouraging deliberate use, and providing a better example for action at a distance. Fixes #95865
2022-05-13Slap #[inline] on all the ByRefSized methods, per the8472's suggestionScott McMurray-0/+11
2022-05-13Remove some unnecessary `rustc_allow_const_fn_unstable` attributes.Nicholas Nethercote-3/+0
2022-05-11Apply CR suggestions; add real tracking issueScott McMurray-8/+9
2022-05-11Add a debug check for ordering, and check for isize overflow in CTFEScott McMurray-0/+4
2022-05-11Rename `unsigned_offset_from` to `sub_ptr`Scott McMurray-15/+39
2022-05-11Add `unsigned_offset_from` on pointersScott McMurray-2/+136
Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
2022-05-10Rollup merge of #96674 - bstrie:vardoc, r=thomccDylan DPC-1/+2
docs: add link explaining variance to NonNull docs
2022-05-09Rollup merge of #96008 - ↵Matthias Krüger-21/+0
fmease:warn-on-useless-doc-hidden-on-assoc-impl-items, r=lcnr Warn on unused `#[doc(hidden)]` attributes on trait impl items [Zulip conversation](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/.E2.9C.94.20Validy.20checks.20for.20.60.23.5Bdoc.28hidden.29.5D.60). Whether an associated item in a trait impl is shown or hidden in the documentation entirely depends on the corresponding item in the trait declaration. Rustdoc completely ignores `#[doc(hidden)]` attributes on impl items. No error or warning is emitted: ```rust pub trait Tr { fn f(); } pub struct Ty; impl Tr for Ty { #[doc(hidden)] fn f() {} } // ^^^^^^^^^^^^^^ ignored by rustdoc and currently // no error or warning issued ``` This may lead users to the wrong belief that the attribute has an effect. In fact, several such cases are found in the standard library (I've removed all of them in this PR). There does not seem to exist any incentive to allow this in the future either: Impl'ing a trait for a type means the type *fully* conforms to its API. Users can add `#[doc(hidden)]` to the whole impl if they want to hide the implementation or add the attribute to the corresponding associated item in the trait declaration to hide the specific item. Hiding an implementation of an associated item does not make much sense: The associated item can still be found on the trait page. This PR emits the warn-by-default lint `unused_attribute` for this case with a future-incompat warning. `@rustbot` label T-compiler T-rustdoc A-lint
2022-05-09Rollup merge of #95483 - golddranks:improve_float_docs, r=joshtriplettMatthias Krüger-42/+136
Improve floating point documentation This is my attempt to improve/solve https://github.com/rust-lang/rust/issues/95468 and https://github.com/rust-lang/rust/issues/73328 . Added/refined explanations: - Refine the "NaN as a special value" top level explanation of f32 - Refine `const NAN` docstring: add an explanation about there being multitude of NaN bitpatterns and disclaimer about the portability/stability guarantees. - Refine `fn is_sign_positive` and `fn is_sign_negative` docstrings: add disclaimer about the sign bit of NaNs. - Refine `fn min` and `fn max` docstrings: explain the semantics and their relationship to the standard and libm better. - Refine `fn trunc` docstrings: explain the semantics slightly more. - Refine `fn powi` docstrings: add disclaimer that the rounding behaviour might be different from `powf`. - Refine `fn copysign` docstrings: add disclaimer about payloads of NaNs. - Refine `minimum` and `maximum`: add disclaimer that "propagating NaN" doesn't mean that propagating the NaN bit patterns is guaranteed. - Refine `max` and `min` docstrings: add "ignoring NaN" to bring the one-row explanation to parity with `minimum` and `maximum`. Cosmetic changes: - Reword `NaN` and `NAN` as plain "NaN", unless they refer to the specific `const NAN`. - Reword "a number" to `self` in function docstrings to clarify. - Remove "Returns NAN if the number is NAN" from `abs`, as this is told to be the default behavior in the top explanation.
2022-05-09Auto merge of #95960 - jhpratt:remove-rustc_deprecated, r=compiler-errorsbors-183/+109
Remove `#[rustc_deprecated]` This removes `#[rustc_deprecated]` and introduces diagnostics to help users to the right direction (that being `#[deprecated]`). All uses of `#[rustc_deprecated]` have been converted. CI is expected to fail initially; this requires #95958, which includes converting `stdarch`. I plan on following up in a short while (maybe a bootstrap cycle?) removing the diagnostics, as they're only intended to be short-term.
2022-05-08Auto merge of #96846 - matthiaskrgr:rollup-yxu9ot9, r=matthiaskrgrbors-2/+21
Rollup of 5 pull requests Successful merges: - #96617 (Fix incorrect syntax suggestion with `pub async fn`) - #96828 (Further elaborate the lack of guarantees from `Hasher`) - #96829 (Fix the `x.py clippy` command) - #96830 (Add and tweak const-generics tests) - #96835 (Add more eslint rules) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-05-08Warn on unused doc(hidden) on trait impl itemsLeón Orell Valerian Liehr-21/+0
2022-05-08Rollup merge of #96828 - scottmcm:clarify-hasher-write, r=AmanieuMatthias Krüger-2/+21
Further elaborate the lack of guarantees from `Hasher` I realized that I got too excited in #94598 by adding new methods, and forgot to do the documentation to really answer the core question in #94026. This PR just has that doc update. r? `@Amanieu`
2022-05-08Auto merge of #96302 - Serial-ATA:more-diagnostic-items, r=manishearthbors-0/+17
Add more diagnostic items This just adds a handful diagnostic items I noticed were missing. Would it be worth doing this for all of the remaining types? I'm willing to do it if it'd be helpful.
2022-05-07Further elaborate the lack of guarantees from `Hasher`Scott McMurray-2/+21
2022-05-07Rollup merge of #96336 - Nilstrieb:link-to-correct-as_mut-in-ptr-as_ref, ↵Matthias Krüger-1/+1
r=JohnTitor Link to correct `as_mut` in docs for `pointer::as_ref` It previously linked to the unstable const-mut-cast method instead of the `mut` counterpart for `as_ref`. Closes #96327
2022-05-07Fix a minor typo in the description of FormatterNikolaos Chatzikonstantinou-1/+1
2022-05-06Auto merge of #94598 - scottmcm:prefix-free-hasher-methods, r=Amanieubors-3/+153
Add a dedicated length-prefixing method to `Hasher` This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future. Fixes #94026 r? rust-lang/libs --- The core of this change is the following two new methods on `Hasher`: ```rust pub trait Hasher { /// Writes a length prefix into this hasher, as part of being prefix-free. /// /// If you're implementing [`Hash`] for a custom collection, call this before /// writing its contents to this `Hasher`. That way /// `(collection![1, 2, 3], collection![4, 5])` and /// `(collection![1, 2], collection![3, 4, 5])` will provide different /// sequences of values to the `Hasher` /// /// The `impl<T> Hash for [T]` includes a call to this method, so if you're /// hashing a slice (or array or vector) via its `Hash::hash` method, /// you should **not** call this yourself. /// /// This method is only for providing domain separation. If you want to /// hash a `usize` that represents part of the *data*, then it's important /// that you pass it to [`Hasher::write_usize`] instead of to this method. /// /// # Examples /// /// ``` /// #![feature(hasher_prefixfree_extras)] /// # // Stubs to make the `impl` below pass the compiler /// # struct MyCollection<T>(Option<T>); /// # impl<T> MyCollection<T> { /// # fn len(&self) -> usize { todo!() } /// # } /// # impl<'a, T> IntoIterator for &'a MyCollection<T> { /// # type Item = T; /// # type IntoIter = std::iter::Empty<T>; /// # fn into_iter(self) -> Self::IntoIter { todo!() } /// # } /// /// use std::hash::{Hash, Hasher}; /// impl<T: Hash> Hash for MyCollection<T> { /// fn hash<H: Hasher>(&self, state: &mut H) { /// state.write_length_prefix(self.len()); /// for elt in self { /// elt.hash(state); /// } /// } /// } /// ``` /// /// # Note to Implementers /// /// If you've decided that your `Hasher` is willing to be susceptible to /// Hash-DoS attacks, then you might consider skipping hashing some or all /// of the `len` provided in the name of increased performance. #[inline] #[unstable(feature = "hasher_prefixfree_extras", issue = "88888888")] fn write_length_prefix(&mut self, len: usize) { self.write_usize(len); } /// Writes a single `str` into this hasher. /// /// If you're implementing [`Hash`], you generally do not need to call this, /// as the `impl Hash for str` does, so you can just use that. /// /// This includes the domain separator for prefix-freedom, so you should /// **not** call `Self::write_length_prefix` before calling this. /// /// # Note to Implementers /// /// The default implementation of this method includes a call to /// [`Self::write_length_prefix`], so if your implementation of `Hasher` /// doesn't care about prefix-freedom and you've thus overridden /// that method to do nothing, there's no need to override this one. /// /// This method is available to be overridden separately from the others /// as `str` being UTF-8 means that it never contains `0xFF` bytes, which /// can be used to provide prefix-freedom cheaper than hashing a length. /// /// For example, if your `Hasher` works byte-by-byte (perhaps by accumulating /// them into a buffer), then you can hash the bytes of the `str` followed /// by a single `0xFF` byte. /// /// If your `Hasher` works in chunks, you can also do this by being careful /// about how you pad partial chunks. If the chunks are padded with `0x00` /// bytes then just hashing an extra `0xFF` byte doesn't necessarily /// provide prefix-freedom, as `"ab"` and `"ab\u{0}"` would likely hash /// the same sequence of chunks. But if you pad with `0xFF` bytes instead, /// ensuring at least one padding byte, then it can often provide /// prefix-freedom cheaper than hashing the length would. #[inline] #[unstable(feature = "hasher_prefixfree_extras", issue = "88888888")] fn write_str(&mut self, s: &str) { self.write_length_prefix(s.len()); self.write(s.as_bytes()); } } ``` With updates to the `Hash` implementations for slices and containers to call `write_length_prefix` instead of `write_usize`. `write_str` defaults to using `write_length_prefix` since, as was pointed out in the issue, the `write_u8(0xFF)` approach is insufficient for hashers that work in chunks, as those would hash `"a\u{0}"` and `"a"` to the same thing. But since `SipHash` works byte-wise (there's an internal buffer to accumulate bytes until a full chunk is available) it overrides `write_str` to continue to use the add-non-UTF-8-byte approach. --- Compatibility: Because the default implementation of `write_length_prefix` calls `write_usize`, the changed hash implementation for slices will do the same thing the old one did on existing `Hasher`s.
2022-05-06Clarify unreachable_unchecked docsLukas Lueg-9/+16
2022-05-06For now, don't change the details of hashing a `str`Scott McMurray-21/+52
We might want to change the default before stabilizing (or maybe even after), but for getting in the new unstable methods, leave it as-is for now. That way it won't break cargo and such.
2022-05-06Add a dedicated length-prefixing method to `Hasher`Scott McMurray-3/+122
This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future.
2022-05-05Rollup merge of #96639 - adpaco-aws:fix-offset-from-typo, r=scottmcmMichael Goulet-2/+2
Fix typo in `offset_from` documentation Small fix for what I think is a typo in the `offset_from` documentation. Someone reading this may understand that the distance in bytes is obtained by dividing the distance by `mem::size_of::<T>()`, but here we just want to define "units of T" in terms of bytes (i.e., units of T == bytes / `mem::size_of::<T>()`).
2022-05-05Rollup merge of #96174 - RalfJung:no-run-transmute, r=scottmcmMichael Goulet-1/+1
mark ptr-int-transmute test as no_run This causes [CI failures in Miri](https://github.com/rust-lang/miri-test-libstd/runs/6062500259?check_suite_focus=true) since ptr-int-transmutes are rejected there (when strict provenance is enabled).
2022-05-05Auto merge of #96520 - lcnr:general-incoherent-impls, r=petrochenkovbors-1/+1
generalize "incoherent impls" impl for user defined types To allow the move of `trait Error` into core. continues the work from #94963, finishes https://github.com/rust-lang/compiler-team/issues/487 r? `@petrochenkov` cc `@yaahc`
2022-05-05Rollup merge of #95359 - jhpratt:int_roundings, r=joshtriplettMatthias Krüger-10/+32
Update `int_roundings` methods from feedback This updates `#![feature(int_roundings)]` (#88581) from feedback. All methods now take `NonZeroX`. The documentation makes clear that they panic in debug mode and wrap in release mode. r? `@joshtriplett` `@rustbot` label +T-libs +T-libs-api +S-waiting-on-review
2022-05-05generalize "incoherent impls" impl for custom typeslcnr-1/+1
2022-05-05Auto merge of #96630 - m-ysk:fix/issue-88038, r=notriddlebors-1/+1
Include nonexported macro_rules! macros in the doctest target Fixes #88038 This PR aims to include nonexported `macro_rules!` macros in the doctest target. For more details, please see the above issue.
2022-05-04Update `int_roundings` methods from feedbackJacob Pratt-10/+32
2022-05-04Stabilize `bool::then_some`Josh Triplett-3/+1
2022-05-03Auto merge of #96280 - lygstate:ffi-fixes, r=joshtriplettbors-17/+46
library/core: Fixes implement of c_uint, c_long, c_ulong Fixes: aa670166243 ("make memcmp return a value of c_int_width instead of i32") Introduce c_num_definition to getting the cfg_if logic easier to maintain Add newlines for easier code reading Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
2022-05-03docs: add link explaining variance to NonNull docsbstrie-1/+2
2022-05-03ignore a doctest for the non-exported macroYoshiki Matsuda-1/+1
2022-05-03Update library/core/src/ffi/mod.rsYonggang Luo-1/+1
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-05-02This aligns the inline attributes of existing `__iterator_get_unchecked` ↵The 8472-0/+5
with those of `next()` on adapters that have both. It improves the performance of iterators using unchecked access when building in incremental mode (due to the larger CGU count?). It might negatively affect incremental compile times for better runtime results, but considering that the equivalent `next()` implementations also are `#[inline]` and usually are more complex this should be ok. ``` ./x.py bench library/core -i --stage 0 --test-args bench_trusted_random_access OLD: 119,172 ns/iter NEW: 17,714 ns/iter ```
2022-05-02Fix typo in `offset_from` documentationAdrian Palacios-2/+2
2022-05-02Fix nitsPyry Kontio-8/+8
2022-05-02Rollup merge of #96567 - alex-semenyuk:fix_docs_for_logs_func, r=Mark-SimulacrumYuki Okushi-6/+6
Fix docs for u32 and i32 logs func Closes #96545
2022-05-01Auto merge of #96521 - petrochenkov:docrules, r=notriddle,GuillaumeGomezbors-0/+2
rustdoc: Resolve doc links referring to `macro_rules` items cc https://github.com/rust-lang/rust/issues/81633 UPD: the fallback to considering *all* `macro_rules` in the crate for unresolved names is not removed in this PR, it will be removed separately and will be run through crater.
2022-04-30Add `do yeet` expressions to allow experimentation in nightlyScott McMurray-0/+41
Using an obviously-placeholder syntax. An RFC would still be needed before this could have any chance at stabilization, and it might be removed at any point. But I'd really like to have it in nightly at least to ensure it works well with try_trait_v2, especially as we refactor the traits.
2022-05-01Fix some links in the standard libraryVadim Petrochenkov-0/+2
2022-04-30Auto merge of #96348 - overdrivenpotato:inline-location, r=the8472bors-0/+4
Inline core::panic::Location methods This avoids the overhead of a function call when used.
2022-04-29Fix documentation for log functions intalexey semenyuk-3/+3
2022-04-29Fix documentation for log functions unsigned intalexey semenyuk-3/+3
2022-04-28Add more diagnostic itemsSerial-0/+17
2022-04-28Rollup merge of #96480 - user-simon:patch-1, r=Dylan-DPCDylan DPC-1/+1
Fixed grammatical error in example comment Added missing "we" in sentence.
2022-04-27Fixed grammatical error in example commentSimon-1/+1
2022-04-26Better error messages when collecting into `[T; n]`Michael Goulet-19/+13