@@ -7,6 +7,7 @@ use rustc_middle::ty::Ty;
77use rustc_middle:: ty:: TyCtxt ;
88use rustc_span:: def_id:: DefId ;
99use rustc_span:: Span ;
10+ use rustc_target:: abi;
1011
1112pub struct InstrumentCoverage ;
1213
@@ -25,7 +26,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
2526}
2627
2728// The first counter (start of the function) is index zero.
28- const INIT_FUNCTION_COUNTER : u128 = 0 ;
29+ const INIT_FUNCTION_COUNTER : u32 = 0 ;
2930
3031/// Injects calls to placeholder function `count_code_region()`.
3132// FIXME(richkadel): As a first step, counters are only injected at the top of each function.
@@ -35,7 +36,8 @@ pub fn instrument_coverage<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3536
3637 let count_code_region_fn =
3738 function_handle ( tcx, span, tcx. lang_items ( ) . count_code_region_fn ( ) . unwrap ( ) ) ;
38- let counter_index = const_int_operand ( tcx, span, tcx. types . u32 , INIT_FUNCTION_COUNTER ) ;
39+ let counter_index =
40+ const_int_operand ( tcx, span, tcx. types . u32 , Scalar :: from_u32 ( INIT_FUNCTION_COUNTER ) ) ;
3941
4042 let mut patch = MirPatch :: new ( body) ;
4143
@@ -77,17 +79,24 @@ fn const_int_operand<'tcx>(
7779 tcx : TyCtxt < ' tcx > ,
7880 span : Span ,
7981 ty : Ty < ' tcx > ,
80- val : u128 ,
82+ val : Scalar ,
8183) -> Operand < ' tcx > {
82- let param_env_and_ty = ty:: ParamEnv :: empty ( ) . and ( ty) ;
83- let size = tcx
84- . layout_of ( param_env_and_ty)
85- . unwrap_or_else ( |e| panic ! ( "could not compute layout for {:?}: {:?}" , ty, e) )
86- . size ;
84+ debug_assert ! ( {
85+ let param_env_and_ty = ty:: ParamEnv :: empty( ) . and( ty) ;
86+ let type_size = tcx
87+ . layout_of( param_env_and_ty)
88+ . unwrap_or_else( |e| panic!( "could not compute layout for {:?}: {:?}" , ty, e) )
89+ . size;
90+ let scalar_size = abi:: Size :: from_bytes( match val {
91+ Scalar :: Raw { size, .. } => size,
92+ _ => panic!( "Invalid scalar type {:?}" , val) ,
93+ } ) ;
94+ scalar_size == type_size
95+ } ) ;
8796 Operand :: Constant ( box Constant {
8897 span,
8998 user_ty : None ,
90- literal : ty:: Const :: from_scalar ( tcx, Scalar :: from_uint ( val, size ) , ty) ,
99+ literal : ty:: Const :: from_scalar ( tcx, val, ty) ,
91100 } )
92101}
93102
0 commit comments