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 37fc0b09e5b9..cdfbca65e7ef 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -27,7 +27,7 @@ use rustc::infer::canonical::QueryRegionConstraints; use rustc::infer::outlives::env::RegionBoundPairs; use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin}; use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; -use rustc::mir::interpret::{ConstValue, PanicInfo}; +use rustc::mir::interpret::PanicInfo; use rustc::mir::tcx::PlaceTy; use rustc::mir::visit::{PlaceContext, Visitor, NonMutatingUseContext}; use rustc::mir::*; @@ -309,7 +309,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { ); } } else { - if let ConstValue::Unevaluated(def_id, substs) = constant.literal.val { + if let ty::ConstKind::Unevaluated(def_id, substs) = constant.literal.val { if let Err(terr) = self.cx.fully_perform_op( location.to_locations(), ConstraintCategory::Boring, diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 707ad1511826..326953d9f302 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -128,7 +128,7 @@ fn op_to_const<'tcx>( } }, }; - ecx.tcx.mk_const(ty::Const { val, ty: op.layout.ty }) + ecx.tcx.mk_const(ty::Const { val: ty::ConstKind::Value(val), ty: op.layout.ty }) } // Returns a pointer to where the result lives @@ -519,7 +519,7 @@ pub fn const_caller_location<'tcx>( intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap(); let loc_const = ty::Const { ty: loc_ty, - val: ConstValue::Scalar(loc_place.ptr.into()), + val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())), }; tcx.mk_const(loc_const) @@ -580,10 +580,10 @@ fn validate_and_turn_into_const<'tcx>( if tcx.is_static(def_id) || cid.promoted.is_some() { let ptr = mplace.ptr.to_ptr()?; Ok(tcx.mk_const(ty::Const { - val: ConstValue::ByRef { + val: ty::ConstKind::Value(ConstValue::ByRef { alloc: ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id), offset: ptr.offset, - }, + }), ty: mplace.layout.ty, })) } else { diff --git a/src/librustc_mir/hair/constant.rs b/src/librustc_mir/hair/constant.rs index b9e75a576cad..1abdcde10ab0 100644 --- a/src/librustc_mir/hair/constant.rs +++ b/src/librustc_mir/hair/constant.rs @@ -56,7 +56,7 @@ crate fn lit_to_const<'tcx>( LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)), LitKind::Err(_) => unreachable!(), }; - Ok(tcx.mk_const(ty::Const { val: lit, ty })) + Ok(tcx.mk_const(ty::Const { val: ty::ConstKind::Value(lit), ty })) } fn parse_float<'tcx>( diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 2ff690b7ccc9..92c9c702c7a8 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -5,7 +5,7 @@ use crate::hair::cx::to_ref::ToRef; use crate::hair::util::UserAnnotatedTyHelpers; use rustc_index::vec::Idx; use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind}; -use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue}; +use rustc::mir::interpret::{GlobalId, ErrorHandled}; use rustc::ty::{self, AdtKind, Ty}; use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast}; use rustc::ty::subst::{InternalSubsts, SubstsRef}; @@ -692,7 +692,7 @@ fn make_mirror_unadjusted<'a, 'tcx>( // and not the beginning of discriminants (which is always `0`) let substs = InternalSubsts::identity_for_item(cx.tcx(), did); let lhs = mk_const(cx.tcx().mk_const(ty::Const { - val: ConstValue::Unevaluated(did, substs), + val: ty::ConstKind::Unevaluated(did, substs), ty: var_ty, })); let bin = ExprKind::Binary { @@ -914,7 +914,7 @@ fn convert_path_expr<'a, 'tcx>( let local_def_id = cx.tcx.hir().local_def_id(hir_id); let index = generics.param_def_id_to_index[&local_def_id]; let name = cx.tcx.hir().name(hir_id); - let val = ConstValue::Param(ty::ParamConst::new(index, name)); + let val = ty::ConstKind::Param(ty::ParamConst::new(index, name)); ExprKind::Literal { literal: cx.tcx.mk_const( ty::Const { @@ -932,7 +932,7 @@ fn convert_path_expr<'a, 'tcx>( debug!("convert_path_expr: (const) user_ty={:?}", user_ty); ExprKind::Literal { literal: cx.tcx.mk_const(ty::Const { - val: ConstValue::Unevaluated(def_id, substs), + val: ty::ConstKind::Unevaluated(def_id, substs), ty: cx.tables().node_type(expr.hir_id), }), user_ty, diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 982330baf9c9..9a1fbf71e6f4 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -313,7 +313,10 @@ impl PatternFolder<'tcx> for LiteralExpander<'tcx> { ( &ty::Ref(_, rty, _), &PatKind::Constant { - value: Const { val, ty: ty::TyS { kind: ty::Ref(_, crty, _), .. } }, + value: Const { + val: ty::ConstKind::Value(val), + ty: ty::TyS { kind: ty::Ref(_, crty, _), .. } + }, }, ) => Pat { ty: pat.ty, @@ -324,13 +327,21 @@ impl PatternFolder<'tcx> for LiteralExpander<'tcx> { span: pat.span, kind: box PatKind::Constant { value: self.tcx.mk_const(Const { - val: self.fold_const_value_deref(*val, rty, crty), + val: ty::ConstKind::Value(self.fold_const_value_deref(*val, rty, crty)), ty: rty, }), }, }, }, }, + + ( + &ty::Ref(_, rty, _), + &PatKind::Constant { + value: Const { val, ty: ty::TyS { kind: ty::Ref(_, crty, _), .. } }, + }, + ) => bug!("cannot deref {:#?}, {} -> {}", val, crty, rty), + (_, &PatKind::Binding { subpattern: Some(ref s), .. }) => s.fold_with(self), _ => pat.super_fold_with(self), } @@ -1245,7 +1256,7 @@ impl<'tcx> IntRange<'tcx> { ) -> Option> { if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, value.ty) { let ty = value.ty; - let val = if let ConstValue::Scalar(Scalar::Raw { data, size }) = value.val { + let val = if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, size })) = value.val { // For this specific pattern we can skip a lot of effort and go // straight to the result, after doing a bit of checking. (We // could remove this branch and just use the next branch, which @@ -1781,7 +1792,19 @@ fn slice_pat_covered_by_const<'tcx>( suffix: &[Pat<'tcx>], param_env: ty::ParamEnv<'tcx>, ) -> Result { - let data: &[u8] = match (const_val.val, &const_val.ty.kind) { + let const_val_val = if let ty::ConstKind::Value(val) = const_val.val { + val + } else { + bug!( + "slice_pat_covered_by_const: {:#?}, {:#?}, {:#?}, {:#?}", + const_val, + prefix, + slice, + suffix, + ) + }; + + let data: &[u8] = match (const_val_val, &const_val.ty.kind) { (ConstValue::ByRef { offset, alloc, .. }, ty::Array(t, n)) => { assert_eq!(*t, tcx.types.u8); let n = n.eval_usize(tcx, param_env); @@ -2054,7 +2077,8 @@ fn split_grouped_constructors<'p, 'tcx>( max_fixed_len = cmp::max(max_fixed_len, n.eval_usize(tcx, param_env)) } - (ConstValue::Slice { start, end, .. }, ty::Slice(_)) => { + (ty::ConstKind::Value(ConstValue::Slice { start, end, .. }), + ty::Slice(_)) => { max_fixed_len = cmp::max(max_fixed_len, (end - start) as u64) } _ => {} @@ -2253,17 +2277,17 @@ fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>( // is when they are subslices of nonzero slices. let (alloc, offset, n, ty) = match value.ty.kind { ty::Array(t, n) => match value.val { - ConstValue::ByRef { offset, alloc, .. } => { + ty::ConstKind::Value(ConstValue::ByRef { offset, alloc, .. }) => { (alloc, offset, n.eval_usize(cx.tcx, cx.param_env), t) } _ => span_bug!(pat.span, "array pattern is {:?}", value,), }, ty::Slice(t) => { match value.val { - ConstValue::Slice { data, start, end } => { + ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => { (data, Size::from_bytes(start as u64), (end - start) as u64, t) } - ConstValue::ByRef { .. } => { + ty::ConstKind::Value(ConstValue::ByRef { .. }) => { // FIXME(oli-obk): implement `deref` for `ConstValue` return None; } diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index f613f6f4b99b..3d97ed6bf9b9 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -1197,9 +1197,10 @@ pub fn compare_const_vals<'tcx>( if let ty::Str = ty.kind { match (a.val, b.val) { - (ConstValue::Slice { .. }, ConstValue::Slice { .. }) => { - let a_bytes = get_slice_bytes(&tcx, a.val); - let b_bytes = get_slice_bytes(&tcx, b.val); + (ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }), + ty::ConstKind::Value(b_val @ ConstValue::Slice { .. })) => { + let a_bytes = get_slice_bytes(&tcx, a_val); + let b_bytes = get_slice_bytes(&tcx, b_val); return from_bool(a_bytes == b_bytes); } _ => (), diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index 39f10d8e6045..b48cac082e31 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -54,11 +54,11 @@ crate fn eval_nullary_intrinsic<'tcx>( "type_name" => { let alloc = type_name::alloc_type_name(tcx, tp_ty); tcx.mk_const(ty::Const { - val: ConstValue::Slice { + val: ty::ConstKind::Value(ConstValue::Slice { data: alloc, start: 0, end: alloc.len(), - }, + }), ty: tcx.mk_static_str(), }) }, diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 79762b87b0a8..4d2ccdc20da6 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -545,23 +545,27 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Scalar::Raw { data, size } => Scalar::Raw { data, size }, }; // Early-return cases. - match val.val { - ConstValue::Param(_) => + let val_val = match val.val { + ty::ConstKind::Param(_) => throw_inval!(TooGeneric), - ConstValue::Unevaluated(def_id, substs) => { + ty::ConstKind::Unevaluated(def_id, substs) => { let instance = self.resolve(def_id, substs)?; return Ok(OpTy::from(self.const_eval_raw(GlobalId { instance, promoted: None, })?)); } - _ => {} - } + ty::ConstKind::Infer(..) | + ty::ConstKind::Bound(..) | + ty::ConstKind::Placeholder(..) => + bug!("eval_const_to_op: Unexpected ConstKind {:?}", val), + ty::ConstKind::Value(val_val) => val_val, + }; // Other cases need layout. let layout = from_known_layout(layout, || { self.layout_of(val.ty) })?; - let op = match val.val { + let op = match val_val { ConstValue::ByRef { alloc, offset } => { let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc); // We rely on mutability being set correctly in that allocation to prevent writes @@ -583,12 +587,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self, )) } - ConstValue::Param(..) | - ConstValue::Infer(..) | - ConstValue::Bound(..) | - ConstValue::Placeholder(..) | - ConstValue::Unevaluated(..) => - bug!("eval_const_to_op: Unexpected ConstValue {:?}", val), }; Ok(OpTy { op, layout }) } diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index 49cdd9142345..49f27a8117ec 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -1284,15 +1284,15 @@ fn collect_const<'tcx>( ); match substituted_constant.val { - ConstValue::Scalar(Scalar::Ptr(ptr)) => + ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))) => collect_miri(tcx, ptr.alloc_id, output), - ConstValue::Slice { data: alloc, start: _, end: _ } | - ConstValue::ByRef { alloc, .. } => { + ty::ConstKind::Value(ConstValue::Slice { data: alloc, start: _, end: _ }) | + ty::ConstKind::Value(ConstValue::ByRef { alloc, .. }) => { for &((), id) in alloc.relocations().values() { collect_miri(tcx, id, output); } } - ConstValue::Unevaluated(def_id, substs) => { + ty::ConstKind::Unevaluated(def_id, substs) => { let instance = ty::Instance::resolve(tcx, param_env, def_id, diff --git a/src/librustc_mir/transform/check_consts/qualifs.rs b/src/librustc_mir/transform/check_consts/qualifs.rs index 496a56790679..595ef2aad49d 100644 --- a/src/librustc_mir/transform/check_consts/qualifs.rs +++ b/src/librustc_mir/transform/check_consts/qualifs.rs @@ -1,7 +1,6 @@ //! A copy of the `Qualif` trait in `qualify_consts.rs` that is suitable for the new validator. use rustc::mir::*; -use rustc::mir::interpret::ConstValue; use rustc::ty::{self, Ty}; use syntax_pos::DUMMY_SP; @@ -118,7 +117,7 @@ pub trait Qualif { Operand::Move(ref place) => Self::in_place(cx, per_local, place.as_ref()), Operand::Constant(ref constant) => { - if let ConstValue::Unevaluated(def_id, _) = constant.literal.val { + if let ty::ConstKind::Unevaluated(def_id, _) = constant.literal.val { // Don't peek inside trait associated constants. if cx.tcx.trait_of_item(def_id).is_some() { Self::in_any_value_of_ty(cx, constant.literal.ty) diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index 48a58f1d0ee5..a7f48162f9b1 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -14,7 +14,6 @@ use rustc::hir::def_id::DefId; use rustc::mir::*; -use rustc::mir::interpret::ConstValue; use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor}; use rustc::mir::traversal::ReversePostorder; use rustc::ty::{self, List, TyCtxt, TypeFoldable}; @@ -584,7 +583,7 @@ impl<'tcx> Validator<'_, 'tcx> { Operand::Move(place) => self.validate_place(place.as_ref()), Operand::Constant(constant) => { - if let ConstValue::Unevaluated(def_id, _) = constant.literal.val { + if let ty::ConstKind::Unevaluated(def_id, _) = constant.literal.val { if self.tcx.trait_of_item(def_id).is_some() { // Don't peek inside trait associated constants. // (see below what we do for other consts, for now) diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 39720af4cb5d..0373fbc55f7a 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -14,7 +14,6 @@ use rustc::ty::{self, TyCtxt, Ty, TypeFoldable}; use rustc::ty::cast::CastTy; use rustc::ty::query::Providers; use rustc::mir::*; -use rustc::mir::interpret::ConstValue; use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext, NonMutatingUseContext}; use rustc::middle::lang_items; use rustc::session::config::nightly_options; @@ -251,7 +250,7 @@ trait Qualif { Operand::Move(ref place) => Self::in_place(cx, place.as_ref()), Operand::Constant(ref constant) => { - if let ConstValue::Unevaluated(def_id, _) = constant.literal.val { + if let ty::ConstKind::Unevaluated(def_id, _) = constant.literal.val { // Don't peek inside trait associated constants. if cx.tcx.trait_of_item(def_id).is_some() { Self::in_any_value_of_ty(cx, constant.literal.ty).unwrap_or(false)