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,15 +30,16 @@ 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
+ let ty = self . monomorphize ( & constant. literal . ty ) ;
34
+ Ok ( OperandRef :: from_const ( bx, val. clone ( ) , ty) )
34
35
}
35
36
}
36
37
}
37
38
38
39
pub fn eval_mir_constant (
39
40
& mut self ,
40
41
constant : & mir:: Constant < ' tcx > ,
41
- ) -> Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > {
42
+ ) -> Result < ConstValue < ' tcx > , ErrorHandled > {
42
43
match constant. literal . val {
43
44
ty:: ConstKind :: Unevaluated ( def_id, substs, promoted) => {
44
45
let substs = self . monomorphize ( & substs) ;
@@ -55,7 +56,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
55
56
err
56
57
} )
57
58
}
58
- _ => Ok ( self . monomorphize ( & constant. literal ) ) ,
59
+ ty:: ConstKind :: Value ( value) => Ok ( value) ,
60
+ _ => {
61
+ let const_ = self . monomorphize ( & constant. literal ) ;
62
+ if let ty:: ConstKind :: Value ( value) = const_. val {
63
+ Ok ( value)
64
+ } else {
65
+ span_bug ! ( constant. span, "encountered bad ConstKind in codegen: {:?}" , const_) ;
66
+ }
67
+ }
59
68
}
60
69
}
61
70
@@ -65,21 +74,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
65
74
bx : & Bx ,
66
75
span : Span ,
67
76
ty : Ty < ' tcx > ,
68
- constant : Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > ,
77
+ constant : Result < ConstValue < ' tcx > , ErrorHandled > ,
69
78
) -> ( Bx :: Value , Ty < ' tcx > ) {
70
79
constant
71
- . map ( |c | {
72
- let field_ty = c . ty . builtin_index ( ) . unwrap ( ) ;
73
- let fields = match c . ty . kind {
80
+ . map ( |val | {
81
+ let field_ty = ty. builtin_index ( ) . unwrap ( ) ;
82
+ let fields = match ty. kind {
74
83
ty:: Array ( _, n) => n. eval_usize ( bx. tcx ( ) , ty:: ParamEnv :: reveal_all ( ) ) ,
75
- _ => bug ! ( "invalid simd shuffle type: {}" , c . ty) ,
84
+ _ => bug ! ( "invalid simd shuffle type: {}" , ty) ,
76
85
} ;
86
+ let c = ty:: Const :: from_value ( bx. tcx ( ) , val, ty) ;
77
87
let values: Vec < _ > = ( 0 ..fields)
78
88
. map ( |field| {
79
89
let field = bx. tcx ( ) . const_field (
80
90
ty:: ParamEnv :: reveal_all ( ) . and ( ( & c, mir:: Field :: new ( field as usize ) ) ) ,
81
91
) ;
82
- if let Some ( prim) = field. val . try_to_scalar ( ) {
92
+ if let Some ( prim) = field. try_to_scalar ( ) {
83
93
let layout = bx. layout_of ( field_ty) ;
84
94
let scalar = match layout. abi {
85
95
layout:: Abi :: Scalar ( ref x) => x,
0 commit comments