@@ -3701,39 +3701,23 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
37013701
37023702 if let Some ( ( source_ty, target_ty) ) = from_trait_impl {
37033703 // 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.
37043707 if let [ arg] = args {
37053708 if let Some ( const_val) = self . builder . lookup_const_scalar ( * arg) {
3706- use rustc_middle:: ty:: { FloatTy , IntTy , UintTy } ;
3707-
3709+ use rustc_middle:: ty:: FloatTy ;
37083710 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 ) ) => {
3711+ // Integer widening conversions
3712+ ( ty:: Uint ( _) , ty:: Uint ( _) ) | ( ty:: Int ( _) , ty:: Int ( _) ) => {
37283713 Some ( self . constant_int ( result_type, const_val) )
37293714 }
3730-
3731- // Float widening conversions: f32->f64
3715+ // Float widening conversions
3716+ // TODO(@LegNeato): Handle more float types
37323717 ( ty:: Float ( FloatTy :: F32 ) , ty:: Float ( FloatTy :: F64 ) ) => {
37333718 let float_val = f32:: from_bits ( const_val as u32 ) as f64 ;
37343719 Some ( self . constant_float ( result_type, float_val) )
37353720 }
3736-
37373721 // No optimization for narrowing conversions or unsupported types
37383722 _ => None ,
37393723 } ;
0 commit comments