@@ -7,6 +7,7 @@ use rustc_middle::ty::Ty;
7
7
use rustc_middle:: ty:: TyCtxt ;
8
8
use rustc_span:: def_id:: DefId ;
9
9
use rustc_span:: Span ;
10
+ use rustc_target:: abi;
10
11
11
12
pub struct InstrumentCoverage ;
12
13
@@ -25,7 +26,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
25
26
}
26
27
27
28
// The first counter (start of the function) is index zero.
28
- const INIT_FUNCTION_COUNTER : u128 = 0 ;
29
+ const INIT_FUNCTION_COUNTER : u32 = 0 ;
29
30
30
31
/// Injects calls to placeholder function `count_code_region()`.
31
32
// 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>) {
35
36
36
37
let count_code_region_fn =
37
38
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 ) ) ;
39
41
40
42
let mut patch = MirPatch :: new ( body) ;
41
43
@@ -77,17 +79,24 @@ fn const_int_operand<'tcx>(
77
79
tcx : TyCtxt < ' tcx > ,
78
80
span : Span ,
79
81
ty : Ty < ' tcx > ,
80
- val : u128 ,
82
+ val : Scalar ,
81
83
) -> 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
+ } ) ;
87
96
Operand :: Constant ( box Constant {
88
97
span,
89
98
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) ,
91
100
} )
92
101
}
93
102
0 commit comments