1
1
// Not in interpret to make sure we do not use private implementation details
2
2
3
3
use 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 } ;
7
5
use rustc_middle:: mir;
8
6
use rustc_middle:: mir:: interpret:: { EvalToValTreeResult , GlobalId } ;
9
7
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
@@ -91,20 +89,20 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
91
89
tcx : TyCtxt < ' tcx > ,
92
90
val : ConstValue < ' tcx > ,
93
91
ty : Ty < ' tcx > ,
94
- ) -> InterpResult < ' tcx , mir:: DestructuredConstant < ' tcx > > {
92
+ ) -> Option < mir:: DestructuredConstant < ' tcx > > {
95
93
let param_env = ty:: ParamEnv :: reveal_all ( ) ;
96
94
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 ( ) ?;
98
96
99
97
// We go to `usize` as we cannot allocate anything bigger anyway.
100
98
let ( field_count, variant, down) = match ty. kind ( ) {
101
99
ty:: Array ( _, len) => ( len. eval_target_usize ( tcx, param_env) as usize , None , op) ,
102
100
ty:: Adt ( def, _) if def. variants ( ) . is_empty ( ) => {
103
- throw_ub ! ( Unreachable )
101
+ return None ;
104
102
}
105
103
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 ( ) ?;
108
106
( def. variants ( ) [ variant] . fields . len ( ) , Some ( variant) , down)
109
107
}
110
108
ty:: Tuple ( substs) => ( substs. len ( ) , None , op) ,
@@ -113,12 +111,12 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
113
111
114
112
let fields_iter = ( 0 ..field_count)
115
113
. map ( |i| {
116
- let field_op = ecx. operand_field ( & down, i) ?;
114
+ let field_op = ecx. operand_field ( & down, i) . ok ( ) ?;
117
115
let val = op_to_const ( & ecx, & field_op) ;
118
- Ok ( ( val, field_op. layout . ty ) )
116
+ Some ( ( val, field_op. layout . ty ) )
119
117
} )
120
- . collect :: < InterpResult < ' tcx , Vec < _ > > > ( ) ?;
118
+ . collect :: < Option < Vec < _ > > > ( ) ?;
121
119
let fields = tcx. arena . alloc_from_iter ( fields_iter) ;
122
120
123
- Ok ( mir:: DestructuredConstant { variant, fields } )
121
+ Some ( mir:: DestructuredConstant { variant, fields } )
124
122
}
0 commit comments