1
1
use crate :: mir:: operand:: OperandRef ;
2
2
use crate :: traits:: * ;
3
3
use rustc:: mir;
4
- use rustc:: mir:: interpret:: ErrorHandled ;
4
+ use rustc:: mir:: interpret:: { ConstValue , ErrorHandled } ;
5
5
use rustc:: ty:: layout:: { self , HasTyCtxt } ;
6
6
use rustc:: ty:: { self , Ty } ;
7
7
use rustc_index:: vec:: Idx ;
@@ -30,27 +30,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
30
30
}
31
31
_ => {
32
32
let val = self . eval_mir_constant ( constant) ?;
33
- Ok ( OperandRef :: from_const ( bx, & val) )
33
+ Ok ( OperandRef :: from_const ( bx, val. clone ( ) , constant . literal . ty ) )
34
34
}
35
35
}
36
36
}
37
37
38
38
pub fn eval_mir_constant (
39
39
& mut self ,
40
40
constant : & mir:: Constant < ' tcx > ,
41
- ) -> Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > {
41
+ ) -> Result < ConstValue < ' tcx > , ErrorHandled > {
42
42
match constant. literal . val {
43
43
ty:: ConstKind :: Unevaluated ( def_id, substs, promoted) => {
44
44
let substs = self . monomorphize ( & substs) ;
45
45
self . cx
46
46
. tcx ( )
47
47
. const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , def_id, substs, promoted, None )
48
- . map ( |val| {
49
- self . cx . tcx ( ) . mk_const ( ty:: Const {
50
- val : ty:: ConstKind :: Value ( val) ,
51
- ty : constant. literal . ty ,
52
- } )
53
- } )
54
48
. map_err ( |err| {
55
49
if promoted. is_none ( ) {
56
50
self . cx
@@ -61,7 +55,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
61
55
err
62
56
} )
63
57
}
64
- _ => Ok ( self . monomorphize ( & constant. literal ) ) ,
58
+ ty:: ConstKind :: Value ( value) => Ok ( value) ,
59
+ _ => {
60
+ let const_ = self . monomorphize ( & constant. literal ) ;
61
+ if let ty:: ConstKind :: Value ( value) = const_. val {
62
+ Ok ( value)
63
+ } else {
64
+ bug ! ( "encountered bad ConstKind in codegen" ) ;
65
+ }
66
+ }
65
67
}
66
68
}
67
69
@@ -71,21 +73,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
71
73
bx : & Bx ,
72
74
span : Span ,
73
75
ty : Ty < ' tcx > ,
74
- constant : Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > ,
76
+ constant : Result < ConstValue < ' tcx > , ErrorHandled > ,
75
77
) -> ( Bx :: Value , Ty < ' tcx > ) {
76
78
constant
77
- . map ( |c | {
78
- let field_ty = c . ty . builtin_index ( ) . unwrap ( ) ;
79
- let fields = match c . ty . kind {
79
+ . map ( |val | {
80
+ let field_ty = ty. builtin_index ( ) . unwrap ( ) ;
81
+ let fields = match ty. kind {
80
82
ty:: Array ( _, n) => n. eval_usize ( bx. tcx ( ) , ty:: ParamEnv :: reveal_all ( ) ) ,
81
- _ => bug ! ( "invalid simd shuffle type: {}" , c . ty) ,
83
+ _ => bug ! ( "invalid simd shuffle type: {}" , ty) ,
82
84
} ;
85
+ let c = bx. tcx ( ) . mk_const ( ty:: Const { val : ty:: ConstKind :: Value ( val) , ty } ) ;
83
86
let values: Vec < _ > = ( 0 ..fields)
84
87
. map ( |field| {
85
88
let field = bx. tcx ( ) . const_field (
86
89
ty:: ParamEnv :: reveal_all ( ) . and ( ( & c, mir:: Field :: new ( field as usize ) ) ) ,
87
90
) ;
88
- if let Some ( prim) = field. val . try_to_scalar ( ) {
91
+ if let Some ( prim) = field. try_to_scalar ( ) {
89
92
let layout = bx. layout_of ( field_ty) ;
90
93
let scalar = match layout. abi {
91
94
layout:: Abi :: Scalar ( ref x) => x,
0 commit comments