Skip to content

Commit fc02736

Browse files
committed
Auto merge of #39408 - ollie27:i128_try_from, r=alexcrichton
Fix TryFrom for i128/u128 Another case of `as` cast silent truncation being error prone. This also adds a few missing TryFrom tests to libcoretest. cc #33417 cc #35118
2 parents 9c8cdb2 + a2de6e2 commit fc02736

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/libcore/num/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2626,8 +2626,8 @@ macro_rules! cross_sign_from_int_impl {
26262626
type Err = TryFromIntError;
26272627

26282628
fn try_from(u: $unsigned) -> Result<$signed, TryFromIntError> {
2629-
let max = <$signed as FromStrRadixHelper>::max_value() as u64;
2630-
if u as u64 > max {
2629+
let max = <$signed as FromStrRadixHelper>::max_value() as u128;
2630+
if u as u128 > max {
26312631
Err(TryFromIntError(()))
26322632
} else {
26332633
Ok(u as $signed)
@@ -2640,8 +2640,8 @@ macro_rules! cross_sign_from_int_impl {
26402640
type Err = TryFromIntError;
26412641

26422642
fn try_from(u: $signed) -> Result<$unsigned, TryFromIntError> {
2643-
let max = <$unsigned as FromStrRadixHelper>::max_value() as u64;
2644-
if u < 0 || u as u64 > max {
2643+
let max = <$unsigned as FromStrRadixHelper>::max_value() as u128;
2644+
if u < 0 || u as u128 > max {
26452645
Err(TryFromIntError(()))
26462646
} else {
26472647
Ok(u as $unsigned)

src/libcoretest/num/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,35 @@ test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
366366
test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
367367
test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
368368
test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
369+
370+
macro_rules! test_impl_try_from_signed_to_unsigned_err {
371+
($fn_name:ident, $source:ty, $target:ty) => {
372+
#[test]
373+
fn $fn_name() {
374+
let max = <$source>::max_value();
375+
let min = <$source>::min_value();
376+
let zero: $source = 0;
377+
let t_max = <$target>::max_value();
378+
let t_min = <$target>::min_value();
379+
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
380+
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
381+
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
382+
zero as $target);
383+
assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
384+
.unwrap(),
385+
t_max as $target);
386+
assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
387+
.unwrap(),
388+
t_min as $target);
389+
}
390+
}
391+
}
392+
393+
test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
394+
395+
test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
396+
test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
397+
398+
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
399+
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
400+
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }

0 commit comments

Comments
 (0)