@@ -3701,39 +3701,21 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
3701
3701
3702
3702
if let Some ( ( source_ty, target_ty) ) = from_trait_impl {
3703
3703
// Optimize From::from calls with constant arguments to avoid creating intermediate types.
3704
+ // Since From is only implemented for safe conversions (widening conversions that preserve
3705
+ // the numeric value), we can directly create a constant of the target type for primitive
3706
+ // numeric types.
3704
3707
if let [ arg] = args {
3705
3708
if let Some ( const_val) = self . builder . lookup_const_scalar ( * arg) {
3706
- use rustc_middle:: ty:: { FloatTy , IntTy , UintTy } ;
3707
-
3708
3709
let optimized_result = match ( source_ty. kind ( ) , target_ty. kind ( ) ) {
3709
- // Unsigned integer widening conversions
3710
- (
3711
- ty:: Uint ( UintTy :: U8 ) ,
3712
- ty:: Uint ( UintTy :: U16 | UintTy :: U32 | UintTy :: U64 | UintTy :: U128 ) ,
3713
- )
3714
- | (
3715
- ty:: Uint ( UintTy :: U16 ) ,
3716
- ty:: Uint ( UintTy :: U32 | UintTy :: U64 | UintTy :: U128 ) ,
3717
- )
3718
- | ( ty:: Uint ( UintTy :: U32 ) , ty:: Uint ( UintTy :: U64 | UintTy :: U128 ) )
3719
- | ( ty:: Uint ( UintTy :: U64 ) , ty:: Uint ( UintTy :: U128 ) )
3720
- // Signed integer widening conversions
3721
- | (
3722
- ty:: Int ( IntTy :: I8 ) ,
3723
- ty:: Int ( IntTy :: I16 | IntTy :: I32 | IntTy :: I64 | IntTy :: I128 ) ,
3724
- )
3725
- | ( ty:: Int ( IntTy :: I16 ) , ty:: Int ( IntTy :: I32 | IntTy :: I64 | IntTy :: I128 ) )
3726
- | ( ty:: Int ( IntTy :: I32 ) , ty:: Int ( IntTy :: I64 | IntTy :: I128 ) )
3727
- | ( ty:: Int ( IntTy :: I64 ) , ty:: Int ( IntTy :: I128 ) ) => {
3710
+ // Integer widening conversions
3711
+ ( ty:: Uint ( _) , ty:: Uint ( _) ) | ( ty:: Int ( _) , ty:: Int ( _) ) => {
3728
3712
Some ( self . constant_int ( result_type, const_val) )
3729
3713
}
3730
-
3731
- // Float widening conversions: f32->f64
3732
- ( ty:: Float ( FloatTy :: F32 ) , ty:: Float ( FloatTy :: F64 ) ) => {
3714
+ // Float widening conversions
3715
+ ( ty:: Float ( _) , ty:: Float ( _) ) => {
3733
3716
let float_val = f32:: from_bits ( const_val as u32 ) as f64 ;
3734
3717
Some ( self . constant_float ( result_type, float_val) )
3735
3718
}
3736
-
3737
3719
// No optimization for narrowing conversions or unsupported types
3738
3720
_ => None ,
3739
3721
} ;
0 commit comments