diff options
| author | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2018-12-13 09:34:23 +0100 |
|---|---|---|
| committer | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2019-01-01 20:05:02 +0100 |
| commit | f36c6559eabef7ad74818f8f7d952a474c64feaa (patch) | |
| tree | a9f310c1002f60ddf4ee24d0cc3002decac9ab23 | |
| parent | 135132891d69dde7ff4a2a6356f9fb92b004c1f7 (diff) | |
| download | rust-f36c6559eabef7ad74818f8f7d952a474c64feaa.tar.gz rust-f36c6559eabef7ad74818f8f7d952a474c64feaa.zip | |
Add `unwrap_usize` to `LazyConst`, too
21 files changed, 31 insertions, 34 deletions
diff --git a/src/librustc/mir/tcx.rs b/src/librustc/mir/tcx.rs index 6dda1933bba..c5b884525da 100644 --- a/src/librustc/mir/tcx.rs +++ b/src/librustc/mir/tcx.rs @@ -114,7 +114,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> { PlaceTy::Ty { ty: match ty.sty { ty::Array(inner, size) => { - let size = size.unwrap_evaluated().unwrap_usize(tcx); + let size = size.unwrap_usize(tcx); let len = size - (from as u64) - (to as u64); tcx.mk_array(inner, len) } diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 1f25925a8e1..171c53b7b20 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -543,7 +543,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } let element = self.layout_of(element)?; - let count = count.unwrap_evaluated().unwrap_usize(tcx); + let count = count.unwrap_usize(tcx); let size = element.size.checked_mul(count, dl) .ok_or(LayoutError::SizeOverflow(ty))?; diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 4a254fc1cd9..6fd3fbd7376 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -2024,13 +2024,6 @@ pub enum LazyConst<'tcx> { static_assert!(MEM_SIZE_OF_LAZY_CONST: ::std::mem::size_of::<LazyConst<'_>>() <= 24); impl<'tcx> LazyConst<'tcx> { - pub fn unwrap_evaluated(self) -> &'tcx Const<'tcx> { - match self { - LazyConst::Evaluated(c) => c, - LazyConst::Unevaluated(..) => bug!("unexpected unevaluated constant"), - } - } - pub fn map_evaluated<R>(self, f: impl FnOnce(&'tcx Const<'tcx>) -> Option<R>) -> Option<R> { match self { LazyConst::Evaluated(c) => f(c), @@ -2038,9 +2031,14 @@ impl<'tcx> LazyConst<'tcx> { } } - pub fn assert_usize(self, tcx: TyCtxt<'_, '_, 'tcx>) -> Option<u64> { + pub fn assert_usize(self, tcx: TyCtxt<'_, '_, '_>) -> Option<u64> { self.map_evaluated(|c| c.assert_usize(tcx)) } + + #[inline] + pub fn unwrap_usize(&self, tcx: TyCtxt<'_, '_, '_>) -> u64 { + self.assert_usize(tcx).expect("expected `LazyConst` to contain a usize") + } } /// Typed constant value. diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs index bec0cfa0d86..0fd04e9d203 100644 --- a/src/librustc_codegen_llvm/debuginfo/metadata.rs +++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs @@ -302,7 +302,7 @@ fn fixed_vec_metadata( let upper_bound = match array_or_slice_type.sty { ty::Array(_, len) => { - len.unwrap_evaluated().unwrap_usize(cx.tcx) as c_longlong + len.unwrap_usize(cx.tcx) as c_longlong } _ => -1 }; diff --git a/src/librustc_codegen_llvm/debuginfo/type_names.rs b/src/librustc_codegen_llvm/debuginfo/type_names.rs index c1309569c79..c8cbd735e85 100644 --- a/src/librustc_codegen_llvm/debuginfo/type_names.rs +++ b/src/librustc_codegen_llvm/debuginfo/type_names.rs @@ -88,7 +88,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, ty::Array(inner_type, len) => { output.push('['); push_debuginfo_type_name(cx, inner_type, true, output); - output.push_str(&format!("; {}", len.unwrap_evaluated().unwrap_usize(cx.tcx))); + output.push_str(&format!("; {}", len.unwrap_usize(cx.tcx))); output.push(']'); }, ty::Slice(inner_type) => { diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs index 166655f6a80..b88ec075653 100644 --- a/src/librustc_codegen_ssa/base.rs +++ b/src/librustc_codegen_ssa/base.rs @@ -171,7 +171,7 @@ pub fn unsized_info<'tcx, Cx: CodegenMethods<'tcx>>( let (source, target) = cx.tcx().struct_lockstep_tails(source, target); match (&source.sty, &target.sty) { (&ty::Array(_, len), &ty::Slice(_)) => { - cx.const_usize(len.unwrap_evaluated().unwrap_usize(cx.tcx())) + cx.const_usize(len.unwrap_usize(cx.tcx())) } (&ty::Dynamic(..), &ty::Dynamic(..)) => { // For now, upcasts are limited to changes in marker diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs index 58325e34432..35bd3a32e81 100644 --- a/src/librustc_codegen_ssa/mir/constant.rs +++ b/src/librustc_codegen_ssa/mir/constant.rs @@ -52,7 +52,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { .and_then(|c| { let field_ty = c.ty.builtin_index().unwrap(); let fields = match c.ty.sty { - ty::Array(_, n) => n.unwrap_evaluated().unwrap_usize(bx.tcx()), + ty::Array(_, n) => n.unwrap_usize(bx.tcx()), ref other => bug!("invalid simd shuffle type: {}", other), }; let values: Result<Vec<_>, ErrorHandled> = (0..fields).map(|field| { diff --git a/src/librustc_codegen_ssa/mir/rvalue.rs b/src/librustc_codegen_ssa/mir/rvalue.rs index 404ee86c6a1..9ca5414fa71 100644 --- a/src/librustc_codegen_ssa/mir/rvalue.rs +++ b/src/librustc_codegen_ssa/mir/rvalue.rs @@ -537,7 +537,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { if let mir::Place::Local(index) = *place { if let LocalRef::Operand(Some(op)) = self.locals[index] { if let ty::Array(_, n) = op.layout.ty.sty { - let n = n.unwrap_evaluated().unwrap_usize(bx.cx().tcx()); + let n = n.unwrap_usize(bx.cx().tcx()); return bx.cx().const_usize(n); } } diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index 3681c3d74eb..c11eae7edee 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -568,7 +568,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> { ProjectionElem::Subslice { from, to } => PlaceTy::Ty { ty: match base_ty.sty { ty::Array(inner, size) => { - let size = size.unwrap_evaluated().unwrap_usize(tcx); + let size = size.unwrap_usize(tcx); let min_size = (from as u64) + (to as u64); if let Some(rest_size) = size.checked_sub(min_size) { tcx.mk_array(inner, rest_size) diff --git a/src/librustc_mir/borrow_check/places_conflict.rs b/src/librustc_mir/borrow_check/places_conflict.rs index 88c27a869c0..ac7182abb36 100644 --- a/src/librustc_mir/borrow_check/places_conflict.rs +++ b/src/librustc_mir/borrow_check/places_conflict.rs @@ -388,7 +388,7 @@ fn place_element_conflict<'a, 'gcx: 'tcx, 'tcx>( (Place::Promoted(p1), Place::Promoted(p2)) => { if p1.0 == p2.0 { if let ty::Array(_, size) = p1.1.sty { - if size.unwrap_evaluated().unwrap_usize(tcx) == 0 { + if size.unwrap_usize(tcx) == 0 { // Ignore conflicts with promoted [T; 0]. debug!("place_element_conflict: IGNORE-LEN-0-PROMOTED"); return Overlap::Disjoint; diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 176c2f65643..0f5510a2921 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -635,7 +635,7 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>, }).collect() } ty::Array(ref sub_ty, len) if len.assert_usize(cx.tcx).is_some() => { - let len = len.unwrap_evaluated().unwrap_usize(cx.tcx); + let len = len.unwrap_usize(cx.tcx); if len != 0 && cx.is_uninhabited(sub_ty) { vec![] } else { @@ -1310,7 +1310,7 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>, )]), PatternKind::Array { .. } => match pcx.ty.sty { ty::Array(_, length) => Some(vec![ - Slice(length.unwrap_evaluated().unwrap_usize(cx.tcx)) + Slice(length.unwrap_usize(cx.tcx)) ]), _ => span_bug!(pat.span, "bad ty {:?} for array pattern", pcx.ty) }, @@ -1753,7 +1753,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>( // slices let (opt_ptr, n, ty) = match value.ty.builtin_deref(false).unwrap().ty.sty { ty::TyKind::Array(t, n) => - (value.to_ptr(), n.unwrap_evaluated().unwrap_usize(cx.tcx), t), + (value.to_ptr(), n.unwrap_usize(cx.tcx), t), ty::TyKind::Slice(t) => { match value.val { ConstValue::ScalarPair(ptr, n) => ( diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index d7eeaf43651..10d2d7bc1b1 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -650,7 +650,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> { ty::Array(_, len) => { // fixed-length array - let len = len.unwrap_evaluated().unwrap_usize(self.tcx); + let len = len.unwrap_usize(self.tcx); assert!(len >= prefix.len() as u64 + suffix.len() as u64); PatternKind::Array { prefix: prefix, slice: slice, suffix: suffix } } @@ -934,7 +934,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> { } ty::Array(_, n) => { PatternKind::Array { - prefix: (0..n.unwrap_evaluated().unwrap_usize(self.tcx)) + prefix: (0..n.unwrap_usize(self.tcx)) .map(|i| adt_subpattern(i as usize, None)) .collect(), slice: None, diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 37c5340e0a2..190a381cf52 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -307,7 +307,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> // u64 cast is from usize to u64, which is always good let val = Immediate::new_slice( ptr, - length.unwrap_evaluated().unwrap_usize(self.tcx.tcx), + length.unwrap_usize(self.tcx.tcx), self, ); self.write_immediate(val, dest) diff --git a/src/librustc_mir/monomorphize/item.rs b/src/librustc_mir/monomorphize/item.rs index 8cd8eb3ad64..e7f4451fdd7 100644 --- a/src/librustc_mir/monomorphize/item.rs +++ b/src/librustc_mir/monomorphize/item.rs @@ -295,7 +295,7 @@ impl<'a, 'tcx> DefPathBasedNames<'a, 'tcx> { ty::Array(inner_type, len) => { output.push('['); self.push_type_name(inner_type, output); - write!(output, "; {}", len.unwrap_evaluated().unwrap_usize(self.tcx)).unwrap(); + write!(output, "; {}", len.unwrap_usize(self.tcx)).unwrap(); output.push(']'); }, ty::Slice(inner_type) => { diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index 835a5dede74..73e26d63493 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -318,7 +318,7 @@ fn build_clone_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, match self_ty.sty { _ if is_copy => builder.copy_shim(), ty::Array(ty, len) => { - let len = len.unwrap_evaluated().unwrap_usize(tcx); + let len = len.unwrap_usize(tcx); builder.array_shim(dest, src, ty, len) } ty::Closure(def_id, substs) => { diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 209c26a0c6c..f35685b7b0c 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -712,7 +712,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { } else if let ty::Array(_, len) = ty.sty { // FIXME(eddyb) the `self.mode == Mode::Fn` condition // seems unnecessary, given that this is merely a ZST. - let len = len.unwrap_evaluated().unwrap_usize(self.tcx); + let len = len.unwrap_usize(self.tcx); if len == 0 && self.mode == Mode::Fn { forbidden_mut = false; } diff --git a/src/librustc_mir/transform/simplify_branches.rs b/src/librustc_mir/transform/simplify_branches.rs index 1d5bc4f9cc3..60a569e297d 100644 --- a/src/librustc_mir/transform/simplify_branches.rs +++ b/src/librustc_mir/transform/simplify_branches.rs @@ -51,9 +51,8 @@ impl MirPass for SimplifyBranches { }, TerminatorKind::Assert { target, cond: Operand::Constant(ref c), expected, .. - } if (c.literal.unwrap_evaluated().assert_bool(tcx) == Some(true)) == expected => { - TerminatorKind::Goto { target } - }, + } if (c.literal.map_evaluated(|e| e.assert_bool(tcx)) == Some(true)) == expected => + TerminatorKind::Goto { target }, TerminatorKind::FalseEdges { real_target, .. } => { TerminatorKind::Goto { target: real_target } }, diff --git a/src/librustc_mir/transform/uniform_array_move_out.rs b/src/librustc_mir/transform/uniform_array_move_out.rs index 172993daa9a..5ab9669baac 100644 --- a/src/librustc_mir/transform/uniform_array_move_out.rs +++ b/src/librustc_mir/transform/uniform_array_move_out.rs @@ -71,7 +71,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UniformArrayMoveOutVisitor<'a, 'tcx> { } else { let place_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx); if let ty::Array(item_ty, const_size) = place_ty.sty { - if let Some(size) = const_size.unwrap_evaluated().assert_usize(self.tcx) { + if let Some(size) = const_size.assert_usize(self.tcx) { assert!(size <= u32::max_value() as u64, "uniform array move out doesn't supported for array bigger then u32"); @@ -193,7 +193,7 @@ impl MirPass for RestoreSubsliceArrayMoveOut { let opt_size = opt_src_place.and_then(|src_place| { let src_ty = src_place.ty(mir, tcx).to_ty(tcx); if let ty::Array(_, ref size_o) = src_ty.sty { - size_o.unwrap_evaluated().assert_usize(tcx) + size_o.assert_usize(tcx) } else { None } diff --git a/src/librustc_mir/util/elaborate_drops.rs b/src/librustc_mir/util/elaborate_drops.rs index 221c2d55eec..8b55a4424ae 100644 --- a/src/librustc_mir/util/elaborate_drops.rs +++ b/src/librustc_mir/util/elaborate_drops.rs @@ -809,7 +809,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D> self.complete_drop(Some(DropFlagMode::Deep), succ, unwind) } ty::Array(ety, size) => { - let size = size.unwrap_evaluated().assert_usize(self.tcx()); + let size = size.assert_usize(self.tcx()); self.open_drop_for_array(ety, size) }, ty::Slice(ety) => self.open_drop_for_array(ety, None), diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index 13311a2efd5..b66c383edb5 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -377,7 +377,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let expected_ty = self.structurally_resolved_type(pat.span, expected); let (inner_ty, slice_ty) = match expected_ty.sty { ty::Array(inner_ty, size) => { - let size = size.unwrap_evaluated().unwrap_usize(tcx); + let size = size.unwrap_usize(tcx); let min_len = before.len() as u64 + after.len() as u64; if slice.is_none() { if min_len != size { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index d664eed0a34..4865b70687d 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3453,7 +3453,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } ty::Array(_, len) => { if let (Some(len), Ok(user_index)) = ( - len.unwrap_evaluated().assert_usize(self.tcx), + len.assert_usize(self.tcx), field.as_str().parse::<u64>() ) { let base = self.tcx.sess.source_map() |
