Skip to content

Commit 837d6c7

Browse files
committed
Remove TryFrom impls that might become conditionally-infallible with a portability lint
#49305 (comment)
1 parent 09008cc commit 837d6c7

File tree

4 files changed

+100
-191
lines changed

4 files changed

+100
-191
lines changed

Diff for: src/libcore/iter/range.rs

+72-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ macro_rules! step_impl_unsigned {
9191
#[inline]
9292
#[allow(unreachable_patterns)]
9393
fn add_usize(&self, n: usize) -> Option<Self> {
94-
match <$t>::try_from(n) {
94+
match <$t>::private_try_from(n) {
9595
Ok(n_as_t) => self.checked_add(n_as_t),
9696
Err(_) => None,
9797
}
@@ -123,7 +123,7 @@ macro_rules! step_impl_signed {
123123
#[inline]
124124
#[allow(unreachable_patterns)]
125125
fn add_usize(&self, n: usize) -> Option<Self> {
126-
match <$unsigned>::try_from(n) {
126+
match <$unsigned>::private_try_from(n) {
127127
Ok(n_as_unsigned) => {
128128
// Wrapping in unsigned space handles cases like
129129
// `-120_i8.add_usize(200) == Some(80_i8)`,
@@ -461,3 +461,73 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
461461

462462
#[stable(feature = "fused", since = "1.26.0")]
463463
impl<A: Step> FusedIterator for ops::RangeInclusive<A> {}
464+
465+
/// Compensate removal of some impls per
466+
/// https://github.com/rust-lang/rust/pull/49305#issuecomment-376293243
467+
trait PrivateTryFromUsize: Sized {
468+
fn private_try_from(n: usize) -> Result<Self, ()>;
469+
}
470+
471+
impl<T> PrivateTryFromUsize for T where T: TryFrom<usize> {
472+
#[inline]
473+
fn private_try_from(n: usize) -> Result<Self, ()> {
474+
T::try_from(n).map_err(|_| ())
475+
}
476+
}
477+
478+
// no possible bounds violation
479+
macro_rules! try_from_unbounded {
480+
($($target:ty),*) => {$(
481+
impl PrivateTryFromUsize for $target {
482+
#[inline]
483+
fn private_try_from(value: usize) -> Result<Self, ()> {
484+
Ok(value as $target)
485+
}
486+
}
487+
)*}
488+
}
489+
490+
// unsigned to signed (only positive bound)
491+
macro_rules! try_from_upper_bounded {
492+
($($target:ty),*) => {$(
493+
impl PrivateTryFromUsize for $target {
494+
#[inline]
495+
fn private_try_from(u: usize) -> Result<$target, ()> {
496+
if u > (<$target>::max_value() as usize) {
497+
Err(())
498+
} else {
499+
Ok(u as $target)
500+
}
501+
}
502+
}
503+
)*}
504+
}
505+
506+
507+
#[cfg(target_pointer_width = "16")]
508+
mod ptr_try_from_impls {
509+
use super::PrivateTryFromUsize;
510+
511+
try_from_unbounded!(u16, u32, u64, u128);
512+
try_from_unbounded!(i32, i64, i128);
513+
}
514+
515+
#[cfg(target_pointer_width = "32")]
516+
mod ptr_try_from_impls {
517+
use super::PrivateTryFromUsize;
518+
519+
try_from_upper_bounded!(u16);
520+
try_from_unbounded!(u32, u64, u128);
521+
try_from_upper_bounded!(i32);
522+
try_from_unbounded!(i64, i128);
523+
}
524+
525+
#[cfg(target_pointer_width = "64")]
526+
mod ptr_try_from_impls {
527+
use super::PrivateTryFromUsize;
528+
529+
try_from_upper_bounded!(u16, u32);
530+
try_from_unbounded!(u64, u128);
531+
try_from_upper_bounded!(i32, i64);
532+
try_from_unbounded!(i128);
533+
}

Diff for: src/libcore/num/mod.rs

+10-60
Original file line numberDiff line numberDiff line change
@@ -3676,21 +3676,6 @@ impl From<!> for TryFromIntError {
36763676
}
36773677
}
36783678

3679-
// no possible bounds violation
3680-
macro_rules! try_from_unbounded {
3681-
($source:ty, $($target:ty),*) => {$(
3682-
#[stable(feature = "try_from", since = "1.26.0")]
3683-
impl TryFrom<$source> for $target {
3684-
type Error = TryFromIntError;
3685-
3686-
#[inline]
3687-
fn try_from(value: $source) -> Result<Self, Self::Error> {
3688-
Ok(value as $target)
3689-
}
3690-
}
3691-
)*}
3692-
}
3693-
36943679
// only negative bounds
36953680
macro_rules! try_from_lower_bounded {
36963681
($source:ty, $($target:ty),*) => {$(
@@ -3789,79 +3774,44 @@ try_from_both_bounded!(i128, u64, u32, u16, u8);
37893774
try_from_upper_bounded!(usize, isize);
37903775
try_from_lower_bounded!(isize, usize);
37913776

3777+
try_from_upper_bounded!(usize, u8);
3778+
try_from_upper_bounded!(usize, i8, i16);
3779+
try_from_both_bounded!(isize, u8);
3780+
try_from_both_bounded!(isize, i8);
3781+
37923782
#[cfg(target_pointer_width = "16")]
37933783
mod ptr_try_from_impls {
37943784
use super::TryFromIntError;
37953785
use convert::TryFrom;
37963786

3797-
try_from_upper_bounded!(usize, u8);
3798-
try_from_unbounded!(usize, u16, u32, u64, u128);
3799-
try_from_upper_bounded!(usize, i8, i16);
3800-
try_from_unbounded!(usize, i32, i64, i128);
3801-
3802-
try_from_both_bounded!(isize, u8);
3787+
// Fallible across platfoms, only implementation differs
38033788
try_from_lower_bounded!(isize, u16, u32, u64, u128);
3804-
try_from_both_bounded!(isize, i8);
3805-
try_from_unbounded!(isize, i16, i32, i64, i128);
3806-
3807-
rev!(try_from_upper_bounded, usize, u32, u64, u128);
38083789
rev!(try_from_lower_bounded, usize, i8, i16);
38093790
rev!(try_from_both_bounded, usize, i32, i64, i128);
3810-
3811-
rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
3812-
rev!(try_from_both_bounded, isize, i32, i64, i128);
38133791
}
38143792

38153793
#[cfg(target_pointer_width = "32")]
38163794
mod ptr_try_from_impls {
38173795
use super::TryFromIntError;
38183796
use convert::TryFrom;
38193797

3820-
try_from_upper_bounded!(usize, u8, u16);
3821-
try_from_unbounded!(usize, u32, u64, u128);
3822-
try_from_upper_bounded!(usize, i8, i16, i32);
3823-
try_from_unbounded!(usize, i64, i128);
3824-
3825-
try_from_both_bounded!(isize, u8, u16);
3798+
// Fallible across platfoms, only implementation differs
3799+
try_from_both_bounded!(isize, u16);
38263800
try_from_lower_bounded!(isize, u32, u64, u128);
3827-
try_from_both_bounded!(isize, i8, i16);
3828-
try_from_unbounded!(isize, i32, i64, i128);
3829-
3830-
rev!(try_from_unbounded, usize, u32);
3831-
rev!(try_from_upper_bounded, usize, u64, u128);
38323801
rev!(try_from_lower_bounded, usize, i8, i16, i32);
38333802
rev!(try_from_both_bounded, usize, i64, i128);
3834-
3835-
rev!(try_from_unbounded, isize, u16);
3836-
rev!(try_from_upper_bounded, isize, u32, u64, u128);
3837-
rev!(try_from_unbounded, isize, i32);
3838-
rev!(try_from_both_bounded, isize, i64, i128);
38393803
}
38403804

38413805
#[cfg(target_pointer_width = "64")]
38423806
mod ptr_try_from_impls {
38433807
use super::TryFromIntError;
38443808
use convert::TryFrom;
38453809

3846-
try_from_upper_bounded!(usize, u8, u16, u32);
3847-
try_from_unbounded!(usize, u64, u128);
3848-
try_from_upper_bounded!(usize, i8, i16, i32, i64);
3849-
try_from_unbounded!(usize, i128);
3850-
3851-
try_from_both_bounded!(isize, u8, u16, u32);
3810+
// Fallible across platfoms, only implementation differs
3811+
try_from_both_bounded!(isize, u16, u32);
38523812
try_from_lower_bounded!(isize, u64, u128);
3853-
try_from_both_bounded!(isize, i8, i16, i32);
3854-
try_from_unbounded!(isize, i64, i128);
3855-
3856-
rev!(try_from_unbounded, usize, u32, u64);
3857-
rev!(try_from_upper_bounded, usize, u128);
38583813
rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
38593814
rev!(try_from_both_bounded, usize, i128);
3860-
3861-
rev!(try_from_unbounded, isize, u16, u32);
3862-
rev!(try_from_upper_bounded, isize, u64, u128);
3863-
rev!(try_from_unbounded, isize, i32, i64);
3864-
rev!(try_from_both_bounded, isize, i128);
38653815
}
38663816

38673817
#[doc(hidden)]

Diff for: src/libcore/tests/num/mod.rs

-127
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ mod flt2dec;
3737
mod dec2flt;
3838
mod bignum;
3939

40-
41-
/// Adds the attribute to all items in the block.
42-
macro_rules! cfg_block {
43-
($(#[$attr:meta]{$($it:item)*})*) => {$($(
44-
#[$attr]
45-
$it
46-
)*)*}
47-
}
48-
4940
/// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
5041
/// support for larger/smaller pointer widths are added in the future.
5142
macro_rules! assume_usize_width {
@@ -318,42 +309,6 @@ assume_usize_width! {
318309

319310
test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
320311
test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
321-
322-
test_impl_try_from_always_ok! { test_try_usizeu64, usize, u64 }
323-
test_impl_try_from_always_ok! { test_try_usizeu128, usize, u128 }
324-
test_impl_try_from_always_ok! { test_try_usizei128, usize, i128 }
325-
326-
test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
327-
test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
328-
329-
cfg_block!(
330-
#[cfg(target_pointer_width = "16")] {
331-
test_impl_try_from_always_ok! { test_try_usizeu16, usize, u16 }
332-
test_impl_try_from_always_ok! { test_try_isizei16, isize, i16 }
333-
test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
334-
test_impl_try_from_always_ok! { test_try_usizei32, usize, i32 }
335-
test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
336-
test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
337-
}
338-
339-
#[cfg(target_pointer_width = "32")] {
340-
test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
341-
test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
342-
test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
343-
test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
344-
test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
345-
test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
346-
}
347-
348-
#[cfg(target_pointer_width = "64")] {
349-
test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
350-
test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
351-
test_impl_try_from_always_ok! { test_try_u32isize, u32, isize }
352-
test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
353-
test_impl_try_from_always_ok! { test_try_u64usize, u64, usize }
354-
test_impl_try_from_always_ok! { test_try_i64isize, i64, isize }
355-
}
356-
);
357312
}
358313

359314
/// Conversions where max of $source can be represented as $target,
@@ -402,24 +357,6 @@ assume_usize_width! {
402357
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
403358
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
404359
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
405-
406-
cfg_block!(
407-
#[cfg(target_pointer_width = "16")] {
408-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu16, isize, u16 }
409-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
410-
}
411-
412-
#[cfg(target_pointer_width = "32")] {
413-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
414-
415-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
416-
}
417-
418-
#[cfg(target_pointer_width = "64")] {
419-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
420-
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64usize, i64, usize }
421-
}
422-
);
423360
}
424361

425362
/// Conversions where max of $source can not be represented as $target,
@@ -461,29 +398,9 @@ test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
461398
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
462399

463400
assume_usize_width! {
464-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64isize, u64, isize }
465-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128isize, u128, isize }
466-
467401
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
468402
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
469403
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
470-
471-
cfg_block!(
472-
#[cfg(target_pointer_width = "16")] {
473-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16isize, u16, isize }
474-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
475-
}
476-
477-
#[cfg(target_pointer_width = "32")] {
478-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
479-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
480-
}
481-
482-
#[cfg(target_pointer_width = "64")] {
483-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
484-
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei64, usize, i64 }
485-
}
486-
);
487404
}
488405

489406
/// Conversions where min/max of $source can not be represented as $target.
@@ -543,34 +460,6 @@ test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
543460

544461
assume_usize_width! {
545462
test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
546-
test_impl_try_from_same_sign_err! { test_try_u128usize, u128, usize }
547-
test_impl_try_from_same_sign_err! { test_try_i128isize, i128, isize }
548-
549-
cfg_block!(
550-
#[cfg(target_pointer_width = "16")] {
551-
test_impl_try_from_same_sign_err! { test_try_u32usize, u32, usize }
552-
test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
553-
554-
test_impl_try_from_same_sign_err! { test_try_i32isize, i32, isize }
555-
test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
556-
}
557-
558-
#[cfg(target_pointer_width = "32")] {
559-
test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
560-
test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
561-
562-
test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
563-
test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
564-
}
565-
566-
#[cfg(target_pointer_width = "64")] {
567-
test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
568-
test_impl_try_from_same_sign_err! { test_try_usizeu32, usize, u32 }
569-
570-
test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
571-
test_impl_try_from_same_sign_err! { test_try_isizei32, isize, i32 }
572-
}
573-
);
574463
}
575464

576465
/// Conversions where neither the min nor the max of $source can be represented by
@@ -615,22 +504,6 @@ test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
615504
assume_usize_width! {
616505
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
617506
test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
618-
619-
cfg_block! {
620-
#[cfg(target_pointer_width = "16")] {
621-
test_impl_try_from_signed_to_unsigned_err! { test_try_i32usize, i32, usize }
622-
test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
623-
}
624-
#[cfg(target_pointer_width = "32")] {
625-
test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
626-
627-
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
628-
}
629-
#[cfg(target_pointer_width = "64")] {
630-
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
631-
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu32, isize, u32 }
632-
}
633-
}
634507
}
635508

636509
macro_rules! test_float {

0 commit comments

Comments
 (0)