11use crate :: mir:: operand:: OperandRef ;
22use crate :: traits:: * ;
33use rustc:: mir;
4- use rustc:: mir:: interpret:: ErrorHandled ;
4+ use rustc:: mir:: interpret:: { ConstValue , ErrorHandled } ;
55use rustc:: ty:: layout:: { self , HasTyCtxt } ;
66use rustc:: ty:: { self , Ty } ;
77use rustc_index:: vec:: Idx ;
@@ -30,15 +30,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3030 }
3131 _ => {
3232 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) )
3435 }
3536 }
3637 }
3738
3839 pub fn eval_mir_constant (
3940 & mut self ,
4041 constant : & mir:: Constant < ' tcx > ,
41- ) -> Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > {
42+ ) -> Result < ConstValue < ' tcx > , ErrorHandled > {
4243 match constant. literal . val {
4344 ty:: ConstKind :: Unevaluated ( def_id, substs, promoted) => {
4445 let substs = self . monomorphize ( & substs) ;
@@ -55,7 +56,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5556 err
5657 } )
5758 }
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+ }
5968 }
6069 }
6170
@@ -65,21 +74,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6574 bx : & Bx ,
6675 span : Span ,
6776 ty : Ty < ' tcx > ,
68- constant : Result < & ' tcx ty :: Const < ' tcx > , ErrorHandled > ,
77+ constant : Result < ConstValue < ' tcx > , ErrorHandled > ,
6978 ) -> ( Bx :: Value , Ty < ' tcx > ) {
7079 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 {
7483 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) ,
7685 } ;
86+ let c = ty:: Const :: from_value ( bx. tcx ( ) , val, ty) ;
7787 let values: Vec < _ > = ( 0 ..fields)
7888 . map ( |field| {
7989 let field = bx. tcx ( ) . const_field (
8090 ty:: ParamEnv :: reveal_all ( ) . and ( ( & c, mir:: Field :: new ( field as usize ) ) ) ,
8191 ) ;
82- if let Some ( prim) = field. val . try_to_scalar ( ) {
92+ if let Some ( prim) = field. try_to_scalar ( ) {
8393 let layout = bx. layout_of ( field_ty) ;
8494 let scalar = match layout. abi {
8595 layout:: Abi :: Scalar ( ref x) => x,
0 commit comments