Skip to content

Commit c65d544

Browse files
authored
Rollup merge of rust-lang#58518 - oli-obk:unreachable_result_errors, r=RalfJung
Use early unwraps instead of bubbling up errors just to unwrap in the end r? @RalfJung
2 parents c9f8304 + 5c0615b commit c65d544

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

src/librustc_codegen_ssa/mir/constant.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,36 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4949
constant: Result<ty::Const<'tcx>, ErrorHandled>,
5050
) -> (Bx::Value, Ty<'tcx>) {
5151
constant
52-
.and_then(|c| {
52+
.map(|c| {
5353
let field_ty = c.ty.builtin_index().unwrap();
5454
let fields = match c.ty.sty {
5555
ty::Array(_, n) => n.unwrap_usize(bx.tcx()),
5656
ref other => bug!("invalid simd shuffle type: {}", other),
5757
};
58-
let values: Result<Vec<_>, ErrorHandled> = (0..fields).map(|field| {
58+
let values: Vec<_> = (0..fields).map(|field| {
5959
let field = const_field(
6060
bx.tcx(),
6161
ty::ParamEnv::reveal_all(),
6262
None,
6363
mir::Field::new(field as usize),
6464
c,
65-
)?;
65+
);
6666
if let Some(prim) = field.val.try_to_scalar() {
6767
let layout = bx.layout_of(field_ty);
6868
let scalar = match layout.abi {
6969
layout::Abi::Scalar(ref x) => x,
7070
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
7171
};
72-
Ok(bx.scalar_to_backend(
72+
bx.scalar_to_backend(
7373
prim, scalar,
7474
bx.immediate_backend_type(layout),
75-
))
75+
)
7676
} else {
7777
bug!("simd shuffle field {:?}", field)
7878
}
7979
}).collect();
80-
let llval = bx.const_struct(&values?, false);
81-
Ok((llval, c.ty))
80+
let llval = bx.const_struct(&values, false);
81+
(llval, c.ty)
8282
})
8383
.unwrap_or_else(|_| {
8484
bx.tcx().sess.span_err(

src/librustc_mir/const_eval.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -466,45 +466,42 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
466466
}
467467

468468
/// Projects to a field of a (variant of a) const.
469+
// this function uses `unwrap` copiously, because an already validated constant must have valid
470+
// fields and can thus never fail outside of compiler bugs
469471
pub fn const_field<'a, 'tcx>(
470472
tcx: TyCtxt<'a, 'tcx, 'tcx>,
471473
param_env: ty::ParamEnv<'tcx>,
472474
variant: Option<VariantIdx>,
473475
field: mir::Field,
474476
value: ty::Const<'tcx>,
475-
) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
477+
) -> ty::Const<'tcx> {
476478
trace!("const_field: {:?}, {:?}", field, value);
477479
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env);
478-
let result = (|| {
479-
// get the operand again
480-
let op = ecx.const_to_op(value, None)?;
481-
// downcast
482-
let down = match variant {
483-
None => op,
484-
Some(variant) => ecx.operand_downcast(op, variant)?
485-
};
486-
// then project
487-
let field = ecx.operand_field(down, field.index() as u64)?;
488-
// and finally move back to the const world, always normalizing because
489-
// this is not called for statics.
490-
op_to_const(&ecx, field)
491-
})();
492-
result.map_err(|error| {
493-
let err = error_to_const_error(&ecx, error);
494-
err.report_as_error(ecx.tcx, "could not access field of constant");
495-
ErrorHandled::Reported
496-
})
480+
// get the operand again
481+
let op = ecx.const_to_op(value, None).unwrap();
482+
// downcast
483+
let down = match variant {
484+
None => op,
485+
Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
486+
};
487+
// then project
488+
let field = ecx.operand_field(down, field.index() as u64).unwrap();
489+
// and finally move back to the const world, always normalizing because
490+
// this is not called for statics.
491+
op_to_const(&ecx, field).unwrap()
497492
}
498493

494+
// this function uses `unwrap` copiously, because an already validated constant must have valid
495+
// fields and can thus never fail outside of compiler bugs
499496
pub fn const_variant_index<'a, 'tcx>(
500497
tcx: TyCtxt<'a, 'tcx, 'tcx>,
501498
param_env: ty::ParamEnv<'tcx>,
502499
val: ty::Const<'tcx>,
503-
) -> EvalResult<'tcx, VariantIdx> {
500+
) -> VariantIdx {
504501
trace!("const_variant_index: {:?}", val);
505502
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env);
506-
let op = ecx.const_to_op(val, None)?;
507-
Ok(ecx.read_discriminant(op)?.1)
503+
let op = ecx.const_to_op(val, None).unwrap();
504+
ecx.read_discriminant(op).unwrap().1
508505
}
509506

510507
pub fn error_to_const_error<'a, 'mir, 'tcx>(

src/librustc_mir/hair/pattern/_match.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,7 @@ impl<'tcx> Constructor<'tcx> {
440440
assert!(!adt.is_enum());
441441
VariantIdx::new(0)
442442
}
443-
&ConstantValue(c) => {
444-
crate::const_eval::const_variant_index(
445-
cx.tcx,
446-
cx.param_env,
447-
c,
448-
).unwrap()
449-
},
443+
&ConstantValue(c) => crate::const_eval::const_variant_index(cx.tcx, cx.param_env, c),
450444
_ => bug!("bad constructor {:?} for adt {:?}", self, adt)
451445
}
452446
}

src/librustc_mir/hair/pattern/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
937937
debug!("const_to_pat: cv={:#?} id={:?}", cv, id);
938938
let adt_subpattern = |i, variant_opt| {
939939
let field = Field::new(i);
940-
let val = const_field(
941-
self.tcx, self.param_env,
942-
variant_opt, field, cv,
943-
).expect("field access failed");
940+
let val = const_field(self.tcx, self.param_env, variant_opt, field, cv);
944941
self.const_to_pat(instance, val, id, span)
945942
};
946943
let adt_subpatterns = |n, variant_opt| {
@@ -979,9 +976,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
979976
PatternKind::Wild
980977
}
981978
ty::Adt(adt_def, substs) if adt_def.is_enum() => {
982-
let variant_index = const_variant_index(
983-
self.tcx, self.param_env, cv
984-
).expect("const_variant_index failed");
979+
let variant_index = const_variant_index(self.tcx, self.param_env, cv);
985980
let subpatterns = adt_subpatterns(
986981
adt_def.variants[variant_index].fields.len(),
987982
Some(variant_index),

0 commit comments

Comments
 (0)