Commit graph

2394 commits

Author SHA1 Message Date
Yuki Okushi
9f2ad0a061
Rollup merge of #90009 - woppopo:const_from_more, r=dtolnay
Make more `From` impls `const` (libcore)

Adding `const` to `From` implementations in the core. `rustc_const_unstable` attribute is not added to unstable implementations.

Tracking issue: #88674

<details>
<summary>Done</summary><div>

- `T` from `T`
- `T` from `!`
- `Option<T>` from `T`
- `Option<&T>` from `&Option<T>`
- `Option<&mut T>` from `&mut Option<T>`
- `Cell<T>` from `T`
- `RefCell<T>` from `T`
- `UnsafeCell<T>` from `T`
- `OnceCell<T>` from `T`
- `Poll<T>` from `T`
- `u32` from `char`
- `u64` from `char`
- `u128` from `char`
- `char` from `u8`
- `AtomicBool` from `bool`
- `AtomicPtr<T>` from `*mut T`
- `AtomicI(bits)` from `i(bits)`
- `AtomicU(bits)` from `u(bits)`
- `i(bits)` from `NonZeroI(bits)`
- `u(bits)` from `NonZeroU(bits)`
- `NonNull<T>` from `Unique<T>`
- `NonNull<T>` from `&T`
- `NonNull<T>` from `&mut T`
- `Unique<T>` from `&mut T`
- `Infallible` from `!`
- `TryIntError` from `!`
- `TryIntError` from `Infallible`
- `TryFromSliceError` from `Infallible`
- `FromResidual for Option<T>`
</div></details>

<details>
<summary>Remaining</summary><dev>

- `NonZero` from `NonZero`
These can't be made const at this time because these use Into::into.
https://github.com/rust-lang/rust/blob/master/library/core/src/convert/num.rs#L393

- `std`, `alloc`
There may still be many implementations that can be made `const`.
</div></details>
2021-10-20 04:35:15 +09:00
Yuki Okushi
f7024998c7
Rollup merge of #88860 - nbdd0121:panic, r=m-ou-se
Deduplicate panic_fmt

std's begin_panic_fmt and core's panic_fmt are duplicates. Merge them to declutter code and remove a lang item.
2021-10-20 04:35:14 +09:00
Yuki Okushi
84fe598f00
Rollup merge of #88789 - the8472:rm-zip-bound, r=JohnTitor
remove unnecessary bound on Zip specialization impl

I originally added this bound in an attempt to make the specialization
sound for owning iterators but it was never correct here and the correct
and [already implemented](497ee321af/library/alloc/src/vec/into_iter.rs (L220-L232)) solution is is to place it on the IntoIter
implementation.
2021-10-20 04:35:13 +09:00
Yuki Okushi
ca6798ab07
Rollup merge of #86479 - exphp-forks:float-debug-exponential, r=yaahc
Automatic exponential formatting in Debug

Context: See [this comment from the libs team](https://github.com/rust-lang/rfcs/pull/2729#issuecomment-853454204)

---

Makes `"{:?}"` switch to exponential for floats based on magnitude. The libs team suggested exploring this idea in the discussion thread for RFC rust-lang/rfcs#2729. (**note:** this is **not** an implementation of the RFC; it is an implementation of one of the alternatives)

Thresholds chosen were 1e-4 and 1e16.  Justification described [here](https://github.com/rust-lang/rfcs/pull/2729#issuecomment-864482954).

**This will require a crater run.**

---

As mentioned in the commit message of 8731d4dfb4, this behavior will not apply when a precision is supplied, because I wanted to preserve the following existing and useful behavior of `{:.PREC?}` (which recursively applies `{:.PREC}` to floats in a struct):

```rust
assert_eq!(
    format!("{:.2?}", [100.0, 0.000004]),
    "[100.00, 0.00]",
)
```

I looked around and am not sure where there are any tests that actually use this in the test suite, though?

All things considered, I'm surprised that this change did not seem to break even a single existing test in `x.py test --stage 2`.  (even when I tried a smaller threshold of 1e6)
2021-10-20 04:35:10 +09:00
Gary Guo
9370156957 Deduplicate panic_fmt
std's begin_panic_fmt and core's panic_fmt are duplicates.
Merge them to declutter code and remove a lang item.
2021-10-19 15:02:21 +01:00
bors
2f22e63cc4 Auto merge of #90037 - matthiaskrgr:rollup-cdfhxtn, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #89766 (RustWrapper: adapt for an LLVM API change)
 - #89867 (Fix macro_rules! duplication when reexported in the same module)
 - #89941 (removing TLS support in x86_64-unknown-none-hermitkernel)
 - #89956 (Suggest a case insensitive match name regardless of levenshtein distance)
 - #89988 (Do not promote values with const drop that need to be dropped)
 - #89997 (Add test for issue #84957 - `str.as_bytes()` in a `const` expression)
 - #90002 (⬆️ rust-analyzer)
 - #90034 (Tiny tweak to Iterator::unzip() doc comment example.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2021-10-19 05:04:38 +00:00
bors
cd8b56f528 Auto merge of #89905 - matthiaskrgr:rev_89709_entirely, r=michaelwoerister
Revert "Auto merge of #89709 - clemenswasser:apply_clippy_suggestions…

…_2, r=petrochenkov"

The PR had some unforseen perf regressions that are not as easy to find.
Revert the PR for now.

This reverts commit 6ae8912a3e, reversing
changes made to 86d6d2b738.
2021-10-19 02:03:21 +00:00
moxian
1519ca99d8 Tiny tweak to Iterator::unzip() doc comment example.
It's easier to figure out what it's doing and which output
elements map to which input ones if the matrix we are dealing
with is rectangular 2x3 rather than square 2x2.
2021-10-19 00:03:51 +00:00
woppopo
7936ecff48 Make more From impls const 2021-10-18 19:19:28 +09:00
Matthias Krüger
f044a84f5d
Rollup merge of #89977 - woppopo:result_const_as_mut, r=oli-obk
Make Result::as_mut const

Adding `const` for `Result::as_mut`.

Tracking issue: #82814
2021-10-17 18:19:00 +02:00
Matthias Krüger
1520fffecc
Rollup merge of #89945 - JohnTitor:we-now-specialize-clone-from-slice, r=the8472
Remove a mention to `copy_from_slice` from `clone_from_slice` doc

Fixes #84736
I think removing it would be the best but I'm happy to clarify it instead if someone would like.
2021-10-17 18:18:57 +02:00
woppopo
ea28abee28 Make Result::as_mut const 2021-10-17 18:39:54 +09:00
Yuki Okushi
0029af7930
Rollup merge of #89953 - woppopo:option_const_as_mut, r=oli-obk
Make Option::as_mut const

Adding `const` for `Option::as_mut`.

Tracking issue: #67441
2021-10-17 07:52:21 +09:00
Yuki Okushi
9614da27cd
Rollup merge of #89507 - lopopolo:lopopolo/ordering-repr-i8, r=joshtriplett
Add `#[repr(i8)]` to `Ordering`

Followup to #89491 to allow `Ordering` to auto-derive `AsRepr` once the proposal to add `AsRepr` (#81642) lands.

cc ``@joshtriplett``
2021-10-17 07:52:17 +09:00
woppopo
d1f7608699 Add #![cfg_attr(bootstrap, feature(const_panic))] to library/core/tests/lib.rs 2021-10-17 00:32:01 +09:00
woppopo
00dba3a693 Make Option::as_mut const 2021-10-17 00:02:42 +09:00
Yuki Okushi
1df185ac02
Remove a mention to copy_from_slice from clone_from_slice doc 2021-10-16 17:30:34 +09:00
Matthias Krüger
8e20470425
Rollup merge of #89925 - gilescope:update-docs-atomic-usage, r=m-ou-se
updating docs to mention usage of AtomicBool

Mouse mentioned we should point out that atomic bool is used by the std lib these days. ( https://github.com/m-ou-se/getrandom/pull/1 )
2021-10-16 08:02:28 +02:00
Matthias Krüger
9ae0804859
Rollup merge of #89509 - jhpratt:stabilize-const_unreachable_unchecked, r=oli-obk
Stabilize `unreachable_unchecked` as `const fn`

Closes #53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With #89508, that is no longer an issue.

````@rustbot```` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
2021-10-16 08:02:20 +02:00
Giles Cope
d3bddf3ea1
updating docs to reflect current situation 2021-10-15 20:43:52 +01:00
bors
1dafe6d1c3 Auto merge of #88540 - ibraheemdev:swap-unchecked, r=kennytm
add `slice::swap_unchecked`

An unsafe version of `slice::swap` that does not do bounds checking.
2021-10-15 09:35:45 +00:00
Matthias Krüger
4457014398 Revert "Auto merge of #89709 - clemenswasser:apply_clippy_suggestions_2, r=petrochenkov"
The PR had some unforseen perf regressions that are not as easy to find.
Revert the PR for now.

This reverts commit 6ae8912a3e, reversing
changes made to 86d6d2b738.
2021-10-15 11:28:23 +02:00
Matthias Krüger
686857f5bf
Rollup merge of #89873 - askoufis:patch-1, r=Mark-Simulacrum
Add missing word to `FromStr` trait documentation

The doc comment is getting a bit wide, let me know if I should restructure it/add a new line.
2021-10-14 16:06:46 +02:00
Ibraheem Ahmed
cf12732a38
don't duplicate slice panic_bounds_check 2021-10-14 09:31:34 -04:00
bors
c34ac8747c Auto merge of #89247 - fee1-dead:const-eval-select, r=oli-obk
Add `const_eval_select` intrinsic

Adds an intrinsic that calls a given function when evaluated at compiler time, but generates a call to another function when called at runtime.

See https://github.com/rust-lang/const-eval/issues/7 for previous discussion.

r? `@oli-obk.`
2021-10-14 10:06:30 +00:00
Deadbeef
26b78ccd31
Fix const stability 2021-10-14 07:07:34 +00:00
Deadbeef
6770dbd4b5
Avoid tupling at the callee 2021-10-14 06:18:53 +00:00
Adam Skoufis
4b59b35b76
Add missing word to FromStr trait docs 2021-10-14 13:47:54 +11:00
Yuki Okushi
59ebfdd7e0
Rollup merge of #89817 - m-ou-se:int-log-10-inline, r=the8472
Add #[inline] to int log10 functions.
2021-10-13 21:55:16 +09:00
Yuki Okushi
c1bde6e4b6
Rollup merge of #89794 - jkugelman:must-use-to_value-conversions, r=joshtriplett
Add #[must_use] to to_value conversions

`NonNull<T>::cast` snuck in when I wasn't looking. What a scamp!

Parent issue: #89692

r? ````@joshtriplett````
2021-10-13 21:55:13 +09:00
Yuki Okushi
f759fff447
Rollup merge of #89781 - Wilfred:patch-2, r=JohnTitor
Add missing words in `Infallible` docs

This sentence was previously incomplete.
2021-10-13 21:55:11 +09:00
bors
ef4b3069ba Auto merge of #89774 - the8472:inline-mut-iter-next, r=m-ou-se
inline next() on &mut Iterator impl

In [#87431](https://github.com/rust-lang/rust/pull/87431/files#diff-79a6b417b85ecf4f1a4ef2235135fedf540199caf6e9e1d154ac6a413b40a757R132-R136)   I found that `(&mut range).fold` doesn't optimize well because the default impl for for `fold` on `&mut Iterator` doesn't inline `next`. In that particular case it was worked around by using `try_fold` which takes a `&mut self` instead of `self`.

Let's see if this can be fixed more broadly.
2021-10-12 23:59:48 +00:00
Mara Bos
a6bb1fb641 Add #[inline] to int log10 functions. 2021-10-12 15:21:14 +02:00
the8472
7017410e5d
Rollup merge of #89799 - ast-ral:ready-method-spellck, r=joshtriplett
fix minor spelling error in Poll::ready docs

Fixes minor spelling error in the proposed `Poll::ready` docs. Not that my opinion matters, but +1 on the original PR (#89651), it reads much nicer to me than the `ready!` macro.
2021-10-12 14:53:11 +02:00
the8472
a1bdd48106
Rollup merge of #89796 - jkugelman:must-use-non-mutating-verb-methods, r=joshtriplett
Add #[must_use] to non-mutating verb methods

These are methods that could be misconstrued to mutate their input, similar to #89694. I gave each one a different custom message.

I wrote that `upgrade` and `downgrade` don't modify the input pointers. Logically they don't, but technically they do...

Parent issue: #89692

r? ```@joshtriplett```
2021-10-12 14:53:10 +02:00
the8472
b55a3c5d15
Rollup merge of #89778 - jkugelman:must-use-as_type-conversions, r=joshtriplett
Add #[must_use] to as_type conversions

Clippy missed these:

```rust
alloc::string::String   fn as_mut_str(&mut self) -> &mut str;
core::mem::NonNull<T>   unsafe fn as_uninit_mut<'a>(&mut self) -> &'a MaybeUninit<T>;
str                     unsafe fn as_bytes_mut(&mut self) -> &mut [u8];
str                     fn as_mut_ptr(&mut self) -> *mut u8;
```

Parent issue: #89692

r? ````@joshtriplett````
2021-10-12 14:53:08 +02:00
bors
9475e609b8 Auto merge of #89770 - jkugelman:must-use-from-and-into, r=joshtriplett
Add #[must_use] to From::from and Into::into

Risk of churn: **High**
Magic 8-Ball says: **Outlook not so good**

I figured I'd put this out there. If we don't do it now maybe we save it for a rainy day.

Parent issue: #89692

r? `@joshtriplett`
2021-10-12 09:43:37 +00:00
bors
02f2b31e61 Auto merge of #89769 - jkugelman:must-use-maybe-uninit-new, r=joshtriplett
Add #[must_use] to MaybeUninit::new

As discussed in https://github.com/rust-lang/rust/pull/89729#issuecomment-939775659.

Parent issue: #89692

r? `@joshtriplett`
2021-10-12 07:02:53 +00:00
Deadbeef
5387b6542f
Add const_eval_select intrinsic 2021-10-12 05:42:23 +00:00
ast-ral
5100630dcd
fix minor spelling error in Poll::ready docs 2021-10-11 21:00:02 -07:00
bors
ffdf18d144 Auto merge of #88788 - falk-hueffner:speedup-int-log10-branchless, r=joshtriplett
Speedup int log10 branchless

This is achieved with a branchless bit-twiddling implementation of the case x < 100_000, and using this as building block.

Benchmark on an Intel i7-8700K (Coffee Lake):

```
name                                   old ns/iter  new ns/iter  diff ns/iter   diff %  speedup
num::int_log::u8_log10_predictable     165          169                     4    2.42%   x 0.98
num::int_log::u8_log10_random          438          423                   -15   -3.42%   x 1.04
num::int_log::u8_log10_random_small    438          423                   -15   -3.42%   x 1.04
num::int_log::u16_log10_predictable    633          417                  -216  -34.12%   x 1.52
num::int_log::u16_log10_random         908          471                  -437  -48.13%   x 1.93
num::int_log::u16_log10_random_small   945          471                  -474  -50.16%   x 2.01
num::int_log::u32_log10_predictable    1,496        1,340                -156  -10.43%   x 1.12
num::int_log::u32_log10_random         1,076        873                  -203  -18.87%   x 1.23
num::int_log::u32_log10_random_small   1,145        874                  -271  -23.67%   x 1.31
num::int_log::u64_log10_predictable    4,005        3,171                -834  -20.82%   x 1.26
num::int_log::u64_log10_random         1,247        1,021                -226  -18.12%   x 1.22
num::int_log::u64_log10_random_small   1,265        921                  -344  -27.19%   x 1.37
num::int_log::u128_log10_predictable   39,667       39,579                -88   -0.22%   x 1.00
num::int_log::u128_log10_random        6,456        6,696                 240    3.72%   x 0.96
num::int_log::u128_log10_random_small  4,108        3,903                -205   -4.99%   x 1.05
```

Benchmark on an M1 Mac Mini:

```
name                                   old ns/iter  new ns/iter  diff ns/iter   diff %  speedup
num::int_log::u8_log10_predictable     143          130                   -13   -9.09%   x 1.10
num::int_log::u8_log10_random          375          325                   -50  -13.33%   x 1.15
num::int_log::u8_log10_random_small    376          325                   -51  -13.56%   x 1.16
num::int_log::u16_log10_predictable    500          322                  -178  -35.60%   x 1.55
num::int_log::u16_log10_random         794          405                  -389  -48.99%   x 1.96
num::int_log::u16_log10_random_small   1,035        405                  -630  -60.87%   x 2.56
num::int_log::u32_log10_predictable    1,144        894                  -250  -21.85%   x 1.28
num::int_log::u32_log10_random         832          786                   -46   -5.53%   x 1.06
num::int_log::u32_log10_random_small   832          787                   -45   -5.41%   x 1.06
num::int_log::u64_log10_predictable    2,681        2,057                -624  -23.27%   x 1.30
num::int_log::u64_log10_random         1,015        806                  -209  -20.59%   x 1.26
num::int_log::u64_log10_random_small   1,004        795                  -209  -20.82%   x 1.26
num::int_log::u128_log10_predictable   56,825       56,526               -299   -0.53%   x 1.01
num::int_log::u128_log10_random        9,056        8,861                -195   -2.15%   x 1.02
num::int_log::u128_log10_random_small  1,528        1,527                  -1   -0.07%   x 1.00
```

The 128 bit case remains ridiculously slow because llvm fails to optimize division by a constant 128-bit value to multiplications. This could be worked around but it seems preferable to fix this in llvm.

From u32 up, table lookup (like suggested [here](https://github.com/rust-lang/rust/issues/70887#issuecomment-881099813)) is still faster, but requires a hardware `leading_zeros` to be viable, and might clog up the cache.
2021-10-12 03:18:54 +00:00
John Kugelman
c3f0577002 Add #[must_use] to non-mutating verb methods 2021-10-11 21:21:32 -04:00
John Kugelman
0cf84c8c19 Add #[must_use] to to_value conversions 2021-10-11 19:37:16 -04:00
John Kugelman
f9692b5619 Add #[must_use] to From::from and Into::into 2021-10-11 18:10:30 -04:00
Matthias Krüger
f94a325496
Rollup merge of #89785 - nbdd0121:master, r=Mark-Simulacrum
Fix ICE when compiling nightly std/rustc on beta compiler

Fix #89775

#89479 renames a lot of diagnostic items, but it happens that the beta compiler assumes that there must be DefId with `rustc_diagnostic_item = "send_trait"`, causing an ICE when compiling stage 0 std or stage 1 compiler. So gate it with `cfg(bootstrap)`.

The unwrap is also removed, so that existence of the diagnostic item is not required. I ripgreped the code base and this seems the only place where `unwrap` is called on the return value of `get_diagnostic_item`.
2021-10-11 23:45:53 +02:00
Matthias Krüger
d3984e16bf
Rollup merge of #89651 - ibraheemdev:poll-ready, r=dtolnay
Add `Poll::ready` and revert stabilization of `task::ready!`

This PR adds an inherent `ready` method to `Poll` that can be used with the `?` operator as an alternative to the `task::ready!` macro:
```rust
let val = ready!(fut.poll(cx));
let val = fut.poll(cx).ready()?;
```

I think this form is a nice, non-breaking middle ground between changing the `impl Try for Poll`, and adding a separate macro. It looks better than `ready!` in my opinion, and it composes well:

```rust
let elem = ready!(fut.poll(cx)).pop().unwrap();
let elem = fut.poll(cx).ready()?.pop().unwrap();
```

The planned stabilization of `ready!` in 1.56 has been reverted because I think this alternate approach is worth considering.

r? rust-lang/libs
2021-10-11 23:45:48 +02:00
The8472
f1c588f1ef use fold instead of try_fold now that .by_ref().next() has been inlined 2021-10-11 23:36:04 +02:00
Ibraheem Ahmed
c517a0de3e add slice::swap tests 2021-10-11 16:16:20 -04:00
Ibraheem Ahmed
2a8ff8df54 improve slice::swap panic message 2021-10-11 16:14:31 -04:00
ibraheemdev
33ecc33268 use swap_unchecked in slice::reverse 2021-10-11 16:14:31 -04:00