11// Not in interpret to make sure we do not use private implementation details
22
33use crate :: errors:: MaxNumNodesInConstErr ;
4- use crate :: interpret:: {
5- intern_const_alloc_recursive, ConstValue , InternKind , InterpCx , InterpResult , Scalar ,
6- } ;
4+ use crate :: interpret:: { intern_const_alloc_recursive, ConstValue , InternKind , InterpCx , Scalar } ;
75use rustc_middle:: mir;
86use rustc_middle:: mir:: interpret:: { EvalToValTreeResult , GlobalId } ;
97use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
@@ -91,20 +89,20 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
9189 tcx : TyCtxt < ' tcx > ,
9290 val : ConstValue < ' tcx > ,
9391 ty : Ty < ' tcx > ,
94- ) -> InterpResult < ' tcx , mir:: DestructuredConstant < ' tcx > > {
92+ ) -> Option < mir:: DestructuredConstant < ' tcx > > {
9593 let param_env = ty:: ParamEnv :: reveal_all ( ) ;
9694 let ecx = mk_eval_cx ( tcx, DUMMY_SP , param_env, CanAccessStatics :: No ) ;
97- let op = ecx. const_val_to_op ( val, ty, None ) ?;
95+ let op = ecx. const_val_to_op ( val, ty, None ) . ok ( ) ?;
9896
9997 // We go to `usize` as we cannot allocate anything bigger anyway.
10098 let ( field_count, variant, down) = match ty. kind ( ) {
10199 ty:: Array ( _, len) => ( len. eval_target_usize ( tcx, param_env) as usize , None , op) ,
102100 ty:: Adt ( def, _) if def. variants ( ) . is_empty ( ) => {
103- throw_ub ! ( Unreachable )
101+ return None ;
104102 }
105103 ty:: Adt ( def, _) => {
106- let variant = ecx. read_discriminant ( & op) ?. 1 ;
107- let down = ecx. operand_downcast ( & op, variant) ?;
104+ let variant = ecx. read_discriminant ( & op) . ok ( ) ?. 1 ;
105+ let down = ecx. operand_downcast ( & op, variant) . ok ( ) ?;
108106 ( def. variants ( ) [ variant] . fields . len ( ) , Some ( variant) , down)
109107 }
110108 ty:: Tuple ( substs) => ( substs. len ( ) , None , op) ,
@@ -113,12 +111,12 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
113111
114112 let fields_iter = ( 0 ..field_count)
115113 . map ( |i| {
116- let field_op = ecx. operand_field ( & down, i) ?;
114+ let field_op = ecx. operand_field ( & down, i) . ok ( ) ?;
117115 let val = op_to_const ( & ecx, & field_op) ;
118- Ok ( ( val, field_op. layout . ty ) )
116+ Some ( ( val, field_op. layout . ty ) )
119117 } )
120- . collect :: < InterpResult < ' tcx , Vec < _ > > > ( ) ?;
118+ . collect :: < Option < Vec < _ > > > ( ) ?;
121119 let fields = tcx. arena . alloc_from_iter ( fields_iter) ;
122120
123- Ok ( mir:: DestructuredConstant { variant, fields } )
121+ Some ( mir:: DestructuredConstant { variant, fields } )
124122}
0 commit comments