@@ -168,12 +168,20 @@ pub(crate) fn codegen_int_binop<'tcx>(
168168 BinOp :: BitXor => b. bxor ( lhs, rhs) ,
169169 BinOp :: BitAnd => b. band ( lhs, rhs) ,
170170 BinOp :: BitOr => b. bor ( lhs, rhs) ,
171- BinOp :: Shl => fx. bcx . ins ( ) . ishl ( lhs, rhs) ,
171+ BinOp :: Shl => {
172+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
173+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
174+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
175+ fx. bcx . ins ( ) . ishl ( lhs, actual_shift)
176+ }
172177 BinOp :: Shr => {
178+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
179+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
180+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
173181 if signed {
174- fx. bcx . ins ( ) . sshr ( lhs, rhs )
182+ fx. bcx . ins ( ) . sshr ( lhs, actual_shift )
175183 } else {
176- fx. bcx . ins ( ) . ushr ( lhs, rhs )
184+ fx. bcx . ins ( ) . ushr ( lhs, actual_shift )
177185 }
178186 }
179187 // Compare binops handles by `codegen_binop`.
@@ -295,7 +303,10 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
295303 }
296304 }
297305 BinOp :: Shl => {
298- let val = fx. bcx . ins ( ) . ishl ( lhs, rhs) ;
306+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
307+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
308+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
309+ let val = fx. bcx . ins ( ) . ishl ( lhs, actual_shift) ;
299310 let ty = fx. bcx . func . dfg . value_type ( val) ;
300311 let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
301312 let has_overflow = fx
@@ -305,10 +316,13 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
305316 ( val, has_overflow)
306317 }
307318 BinOp :: Shr => {
319+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
320+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
321+ let actual_shift = clif_intcast ( fx, actual_shift, types:: I8 , false ) ;
308322 let val = if !signed {
309- fx. bcx . ins ( ) . ushr ( lhs, rhs )
323+ fx. bcx . ins ( ) . ushr ( lhs, actual_shift )
310324 } else {
311- fx. bcx . ins ( ) . sshr ( lhs, rhs )
325+ fx. bcx . ins ( ) . sshr ( lhs, actual_shift )
312326 } ;
313327 let ty = fx. bcx . func . dfg . value_type ( val) ;
314328 let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
0 commit comments