Skip to content

Commit 8e9774f

Browse files
committed
Auto merge of #57475 - SimonSapin:signed, r=estebank
Add signed num::NonZeroI* types Multiple people have asked for them in #49137. Given that the unsigned ones already exist, they are very easy to add and not an additional maintenance burden.
2 parents 51cc3cd + e195ce6 commit 8e9774f

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

src/libcore/num/mod.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use ops;
1010
use str::FromStr;
1111

1212
macro_rules! impl_nonzero_fmt {
13-
( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
13+
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
1414
$(
15-
#[stable(feature = "nonzero", since = "1.28.0")]
15+
#[$stability]
1616
impl fmt::$Trait for $Ty {
1717
#[inline]
1818
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -31,7 +31,7 @@ macro_rules! doc_comment {
3131
}
3232

3333
macro_rules! nonzero_integers {
34-
( $( $Ty: ident($Int: ty); )+ ) => {
34+
( $( #[$stability: meta] $Ty: ident($Int: ty); )+ ) => {
3535
$(
3636
doc_comment! {
3737
concat!("An integer that is known not to equal zero.
@@ -41,10 +41,10 @@ For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!(
4141
4242
```rust
4343
use std::mem::size_of;
44-
assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int),
44+
assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int),
4545
">());
4646
```"),
47-
#[stable(feature = "nonzero", since = "1.28.0")]
47+
#[$stability]
4848
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
4949
#[repr(transparent)]
5050
#[rustc_layout_scalar_valid_range_start(1)]
@@ -57,14 +57,14 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
5757
/// # Safety
5858
///
5959
/// The value must not be zero.
60-
#[stable(feature = "nonzero", since = "1.28.0")]
60+
#[$stability]
6161
#[inline]
6262
pub const unsafe fn new_unchecked(n: $Int) -> Self {
6363
$Ty(n)
6464
}
6565

6666
/// Create a non-zero if the given value is not zero.
67-
#[stable(feature = "nonzero", since = "1.28.0")]
67+
#[$stability]
6868
#[inline]
6969
pub fn new(n: $Int) -> Option<Self> {
7070
if n != 0 {
@@ -75,7 +75,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
7575
}
7676

7777
/// Returns the value as a primitive type.
78-
#[stable(feature = "nonzero", since = "1.28.0")]
78+
#[$stability]
7979
#[inline]
8080
pub const fn get(self) -> $Int {
8181
self.0
@@ -91,19 +91,25 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
9191
}
9292

9393
impl_nonzero_fmt! {
94-
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
94+
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
9595
}
9696
)+
9797
}
9898
}
9999

100100
nonzero_integers! {
101-
NonZeroU8(u8);
102-
NonZeroU16(u16);
103-
NonZeroU32(u32);
104-
NonZeroU64(u64);
105-
NonZeroU128(u128);
106-
NonZeroUsize(usize);
101+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU8(u8);
102+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU16(u16);
103+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU32(u32);
104+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU64(u64);
105+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU128(u128);
106+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroUsize(usize);
107+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI8(i8);
108+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI16(i16);
109+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI32(i32);
110+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI64(i64);
111+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI128(i128);
112+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIsize(isize);
107113
}
108114

109115
/// Provides intentionally-wrapped arithmetic on `T`.

src/libcore/tests/nonzero.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::num::NonZeroU32;
1+
use core::num::{NonZeroU32, NonZeroI32};
22
use core::option::Option;
33
use core::option::Option::{Some, None};
44
use std::mem::size_of;
@@ -13,6 +13,7 @@ fn test_create_nonzero_instance() {
1313
#[test]
1414
fn test_size_nonzero_in_option() {
1515
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
16+
assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
1617
}
1718

1819
#[test]
@@ -118,3 +119,10 @@ fn test_from_nonzero() {
118119
let num: u32 = nz.into();
119120
assert_eq!(num, 1u32);
120121
}
122+
123+
#[test]
124+
fn test_from_signed_nonzero() {
125+
let nz = NonZeroI32::new(1).unwrap();
126+
let num: i32 = nz.into();
127+
assert_eq!(num, 1i32);
128+
}

src/librustc/traits/error_reporting.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
471471
}
472472

473473
fn report_similar_impl_candidates(&self,
474-
mut impl_candidates: Vec<ty::TraitRef<'tcx>>,
474+
impl_candidates: Vec<ty::TraitRef<'tcx>>,
475475
err: &mut DiagnosticBuilder<'_>)
476476
{
477477
if impl_candidates.is_empty() {
@@ -497,14 +497,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
497497
});
498498

499499
// Sort impl candidates so that ordering is consistent for UI tests.
500-
let normalized_impl_candidates = &mut impl_candidates[0..end]
500+
let mut normalized_impl_candidates = impl_candidates
501501
.iter()
502502
.map(normalize)
503503
.collect::<Vec<String>>();
504+
505+
// Sort before taking the `..end` range,
506+
// because the ordering of `impl_candidates` may not be deterministic:
507+
// https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507
504508
normalized_impl_candidates.sort();
505509

506510
err.help(&format!("the following implementations were found:{}{}",
507-
normalized_impl_candidates.join(""),
511+
normalized_impl_candidates[..end].join(""),
508512
if len > 5 {
509513
format!("\nand {} others", len - 4)
510514
} else {

src/test/ui/did_you_mean/issue-21659-show-relevant-trait-impls-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | f1.foo(1usize);
88
<Bar as Foo<i16>>
99
<Bar as Foo<i32>>
1010
<Bar as Foo<i8>>
11-
<Bar as Foo<u8>>
11+
<Bar as Foo<u16>>
1212
and 2 others
1313

1414
error: aborting due to previous error

src/test/ui/did_you_mean/issue-39802-show-5-trait-impls.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ LL | Foo::<i32>::bar(&true); //~ ERROR is not satisfied
4040
| ^^^^^^^^^^^^^^^ the trait `Foo<i32>` is not implemented for `bool`
4141
|
4242
= help: the following implementations were found:
43+
<bool as Foo<bool>>
44+
<bool as Foo<i8>>
4345
<bool as Foo<u16>>
4446
<bool as Foo<u32>>
45-
<bool as Foo<u64>>
46-
<bool as Foo<u8>>
4747
and 2 others
4848
note: required by `Foo::bar`
4949
--> $DIR/issue-39802-show-5-trait-impls.rs:2:5

src/test/ui/try-block/try-block-bad-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LL | Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>`
66
|
77
= help: the following implementations were found:
88
<i32 as std::convert::From<bool>>
9+
<i32 as std::convert::From<core::num::NonZeroI32>>
910
<i32 as std::convert::From<i16>>
1011
<i32 as std::convert::From<i8>>
11-
<i32 as std::convert::From<u16>>
12-
<i32 as std::convert::From<u8>>
12+
and 2 others
1313
= note: required by `std::convert::From::from`
1414

1515
error[E0271]: type mismatch resolving `<std::result::Result<i32, i32> as std::ops::Try>::Ok == &str`

0 commit comments

Comments
 (0)