rust/library/core/tests
bors 2fab321435 Auto merge of #79134 - ohadravid:nzint-div, r=dtolnay
Add `impl Div<NonZeroU{0}> for u{0}` which cannot panic

Dividing an unsigned int by a `NonZeroUxx` requires a user to write (for example, in [this SO question](https://stackoverflow.com/questions/64855738/how-to-inform-the-optimizer-that-nonzerou32get-will-never-return-zero)):

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y.get()
}
```

which generates a panicking-checked-div [assembly](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(fontScale:14,j:1,lang:rust,selection:(endColumn:2,endLineNumber:6,positionColumn:2,positionLineNumber:6,selectionStartColumn:2,selectionStartLineNumber:6,startColumn:2,startLineNumber:6),source:%27pub+fn+div(x:+u32,+y:+u32)+-%3E+u32+%7B%0A++++x+/+y%0A%7D%0Apub+fn+safe_div(x:+u32,+y:+std::num::NonZeroU32)+-%3E+u32+%7B%0A++++x+/+y.get()+//+an+unchecked+division+expected%0A%7D%27),l:%275%27,n:%270%27,o:%27Rust+source+%231%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27),(g:!((h:compiler,i:(compiler:r1470,filters:(b:%270%27,binary:%271%27,commentOnly:%270%27,demangle:%270%27,directives:%270%27,execute:%271%27,intel:%270%27,libraryCode:%271%27,trim:%271%27),fontScale:14,j:1,lang:rust,libs:!(),options:%27-O%27,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:%275%27,n:%270%27,o:%27rustc+1.47.0+(Editor+%231,+Compiler+%231)+Rust%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27)),l:%272%27,n:%270%27,o:%27%27,t:%270%27)),version:4).
Avoiding the `panic` currently requires `unsafe` code.

This PR adds an `impl Div<NonZeroU{0}> for u{0}` (and `impl Rem<NonZeroU{0}> for u{0}`) which calls the `unchecked_div` (and `unchecked_rem`) intrinsic without any additional checks,
making the following code compile:

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y
}

pub fn safe_rem(x: u32, y: std::num::NonZeroU32) -> u32 {
    x % y
}
```

The doc is set to match the regular div impl [docs](https://doc.rust-lang.org/beta/src/core/ops/arith.rs.html#460).

I've marked these as stable because (as I understand it) trait impls are automatically stable. I'm happy to change it to unstable if needed.

Following `@dtolnay` template from a similar issue:
this adds the following **stable** impls, which rely on dividing unsigned integers by nonzero integers being well defined and previously would have involved unsafe code to encode that knowledge:
```
impl Div<NonZeroU8> for u8 {
    type Output = u8;
}

impl Rem<NonZeroU8> for u8 {
    type Output = u8;
}
```
and equivalent for u16, u32, u64, u128, usize, but **not** for i8, i16, i32, i64, i128, isize (since -1/MIN is undefined).

r? `@dtolnay`
2020-12-27 13:11:06 +00:00
..
fmt
hash
num Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
alloc.rs
any.rs
array.rs Rollup merge of #76454 - poliorcetics:ui-to-unit-test-1, r=matklad 2020-09-28 18:39:39 +02:00
ascii.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
atomic.rs Migrate standard library away from compare_and_swap 2020-12-22 12:19:46 +01:00
bool.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
cell.rs Constantify UnsafeCell::into_inner and related 2020-11-04 11:41:57 +01:00
char.rs
clone.rs
cmp.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
intrinsics.rs move test to intergrated test in library/core 2020-09-24 14:46:57 +00:00
iter.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
lazy.rs Make OnceCell<T> transparent to dropck 2020-08-18 00:34:54 +02:00
lib.rs write_slice(_cloned) 2020-12-15 12:21:33 -08:00
macros.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
manually_drop.rs
mem.rs Fix unused import error on wasm 2020-12-18 14:53:55 -08:00
nonzero.rs Added impl Rem<NonZeroU{0}> for u{0} which cannot panic 2020-12-17 18:42:19 +02:00
ops.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
option.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
pattern.rs
pin.rs Make some methods of Pin<&mut T> unstable const 2020-09-18 19:23:50 +02:00
ptr.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
result.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
slice.rs Check for exhaustion in SliceIndex for RangeInclusive 2020-10-20 17:18:08 -07:00
str_lossy.rs
str.rs
task.rs Move const tests for Poll to library\core 2020-09-04 01:04:34 +02:00
time.rs Fixup tests: Duration::MIN -> ::ZERO 2020-10-27 13:57:51 -07:00
tuple.rs
unicode.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00