Skip to content

Commit 46cce98

Browse files
committed
Use options instead of errors if the errors are never needed
1 parent 09b89ef commit 46cce98

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Not in interpret to make sure we do not use private implementation details
22

33
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};
75
use rustc_middle::mir;
86
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
97
use 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
}

compiler/rustc_const_eval/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn provide(providers: &mut Providers) {
5353
const_eval::eval_to_valtree(tcx, param_env, raw)
5454
};
5555
providers.try_destructure_mir_constant =
56-
|tcx, (cv, ty)| const_eval::try_destructure_mir_constant(tcx, cv, ty).ok();
56+
|tcx, (cv, ty)| const_eval::try_destructure_mir_constant(tcx, cv, ty);
5757
providers.valtree_to_const_val = |tcx, (ty, valtree)| {
5858
const_eval::valtree_to_const_value(tcx, ty::ParamEnv::empty().and(ty), valtree)
5959
};

0 commit comments

Comments
 (0)