From e5fb78482372a9fce3a276a07f99ff994384a343 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 21:32:56 +0100 Subject: [PATCH 01/14] Create intermediate enum ty::ConstKind. --- src/librustc/infer/canonical/canonicalizer.rs | 11 ++- src/librustc/infer/canonical/mod.rs | 5 +- .../infer/canonical/query_response.rs | 3 +- src/librustc/infer/combine.rs | 17 ++--- src/librustc/infer/freshen.rs | 17 ++--- src/librustc/infer/fudge.rs | 3 +- src/librustc/infer/higher_ranked/mod.rs | 3 +- src/librustc/infer/mod.rs | 3 +- src/librustc/infer/nll_relate/mod.rs | 7 +- src/librustc/infer/opaque_types/mod.rs | 3 +- src/librustc/infer/resolve.rs | 5 +- src/librustc/infer/unify_key.rs | 3 +- src/librustc/mir/interpret/value.rs | 46 +++--------- src/librustc/mir/mod.rs | 11 +-- src/librustc/ty/_match.rs | 5 +- src/librustc/ty/context.rs | 10 +-- src/librustc/ty/flags.rs | 15 ++-- src/librustc/ty/fold.rs | 11 ++- src/librustc/ty/mod.rs | 2 +- src/librustc/ty/print/obsolete.rs | 26 +++---- src/librustc/ty/print/pretty.rs | 30 ++++---- src/librustc/ty/relate.rs | 73 +++++++++++-------- src/librustc/ty/structural_impls.rs | 26 +++---- src/librustc/ty/sty.rs | 56 +++++++++++++- src/librustc/ty/subst.rs | 5 +- src/librustc/ty/util.rs | 3 +- src/librustc/ty/walk.rs | 3 +- src/librustc/ty/wf.rs | 3 +- 28 files changed, 205 insertions(+), 200 deletions(-) diff --git a/src/librustc/infer/canonical/canonicalizer.rs b/src/librustc/infer/canonical/canonicalizer.rs index 49a2c90bdbf..e782ec59295 100644 --- a/src/librustc/infer/canonical/canonicalizer.rs +++ b/src/librustc/infer/canonical/canonicalizer.rs @@ -10,7 +10,6 @@ OriginalQueryValues, }; use crate::infer::InferCtxt; -use crate::mir::interpret::ConstValue; use std::sync::atomic::Ordering; use crate::ty::fold::{TypeFoldable, TypeFolder}; use crate::ty::subst::GenericArg; @@ -441,7 +440,7 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { match ct.val { - ConstValue::Infer(InferConst::Var(vid)) => { + ty::ConstKind::Infer(InferConst::Var(vid)) => { debug!("canonical: const var found with vid {:?}", vid); match self.infcx.unwrap().probe_const_var(vid) { Ok(c) => { @@ -465,17 +464,17 @@ fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { } } } - ConstValue::Infer(InferConst::Fresh(_)) => { + ty::ConstKind::Infer(InferConst::Fresh(_)) => { bug!("encountered a fresh const during canonicalization") } - ConstValue::Bound(debruijn, _) => { + ty::ConstKind::Bound(debruijn, _) => { if debruijn >= self.binder_index { bug!("escaping bound type during canonicalization") } else { return ct; } } - ConstValue::Placeholder(placeholder) => { + ty::ConstKind::Placeholder(placeholder) => { return self.canonicalize_const_var( CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder), @@ -700,7 +699,7 @@ fn canonicalize_const_var( let var = self.canonical_var(info, const_var.into()); self.tcx().mk_const( ty::Const { - val: ConstValue::Bound(self.binder_index, var.into()), + val: ty::ConstKind::Bound(self.binder_index, var.into()), ty: self.fold_ty(const_var.ty), } ) diff --git a/src/librustc/infer/canonical/mod.rs b/src/librustc/infer/canonical/mod.rs index d833feeeb09..4e86cbb2cf6 100644 --- a/src/librustc/infer/canonical/mod.rs +++ b/src/librustc/infer/canonical/mod.rs @@ -24,7 +24,6 @@ use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind}; use crate::infer::{ConstVariableOrigin, ConstVariableOriginKind}; use crate::infer::region_constraints::MemberConstraint; -use crate::mir::interpret::ConstValue; use rustc_index::vec::IndexVec; use rustc_macros::HashStable; use rustc_serialize::UseSpecializedDecodable; @@ -447,7 +446,7 @@ fn instantiate_canonical_var( }; self.tcx.mk_const( ty::Const { - val: ConstValue::Placeholder(placeholder_mapped), + val: ty::ConstKind::Placeholder(placeholder_mapped), ty: self.tcx.types.err, // FIXME(const_generics) } ).into() @@ -510,7 +509,7 @@ pub fn make_identity(&self, tcx: TyCtxt<'tcx>) -> Self { GenericArgKind::Const(ct) => { tcx.mk_const(ty::Const { ty: ct.ty, - val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)), + val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)), }).into() } }) diff --git a/src/librustc/infer/canonical/query_response.rs b/src/librustc/infer/canonical/query_response.rs index 7ad6006012f..825e98cedb9 100644 --- a/src/librustc/infer/canonical/query_response.rs +++ b/src/librustc/infer/canonical/query_response.rs @@ -16,7 +16,6 @@ use crate::infer::region_constraints::{Constraint, RegionConstraintData}; use crate::infer::InferCtxtBuilder; use crate::infer::{InferCtxt, InferOk, InferResult}; -use crate::mir::interpret::ConstValue; use rustc_index::vec::Idx; use rustc_index::vec::IndexVec; use std::fmt::Debug; @@ -493,7 +492,7 @@ fn query_response_substitution_guess( } } GenericArgKind::Const(result_value) => { - if let ty::Const { val: ConstValue::Bound(debrujin, b), .. } = result_value { + if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value { // ...in which case we would set `canonical_vars[0]` to `Some(const X)`. // We only allow a `ty::INNERMOST` index in substitutions. diff --git a/src/librustc/infer/combine.rs b/src/librustc/infer/combine.rs index 51ae4e49493..a2f0531f0af 100644 --- a/src/librustc/infer/combine.rs +++ b/src/librustc/infer/combine.rs @@ -33,7 +33,6 @@ use super::unify_key::replace_if_possible; use crate::hir::def_id::DefId; -use crate::mir::interpret::ConstValue; use crate::ty::{IntType, UintType}; use crate::ty::{self, Ty, TyCtxt, InferConst}; use crate::ty::error::TypeError; @@ -137,8 +136,8 @@ pub fn super_combine_consts( let a_is_expected = relation.a_is_expected(); match (a.val, b.val) { - (ConstValue::Infer(InferConst::Var(a_vid)), - ConstValue::Infer(InferConst::Var(b_vid))) => { + (ty::ConstKind::Infer(InferConst::Var(a_vid)), + ty::ConstKind::Infer(InferConst::Var(b_vid))) => { self.const_unification_table .borrow_mut() .unify_var_var(a_vid, b_vid) @@ -147,16 +146,16 @@ pub fn super_combine_consts( } // All other cases of inference with other variables are errors. - (ConstValue::Infer(InferConst::Var(_)), ConstValue::Infer(_)) | - (ConstValue::Infer(_), ConstValue::Infer(InferConst::Var(_))) => { - bug!("tried to combine ConstValue::Infer/ConstValue::Infer(InferConst::Var)") + (ty::ConstKind::Infer(InferConst::Var(_)), ty::ConstKind::Infer(_)) | + (ty::ConstKind::Infer(_), ty::ConstKind::Infer(InferConst::Var(_))) => { + bug!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var)") } - (ConstValue::Infer(InferConst::Var(vid)), _) => { + (ty::ConstKind::Infer(InferConst::Var(vid)), _) => { return self.unify_const_variable(a_is_expected, vid, b); } - (_, ConstValue::Infer(InferConst::Var(vid))) => { + (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { return self.unify_const_variable(!a_is_expected, vid, a); } @@ -603,7 +602,7 @@ fn consts( assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be == match c.val { - ConstValue::Infer(InferConst::Var(vid)) => { + ty::ConstKind::Infer(InferConst::Var(vid)) => { let mut variable_table = self.infcx.const_unification_table.borrow_mut(); let var_value = variable_table.probe_value(vid); match var_value.val { diff --git a/src/librustc/infer/freshen.rs b/src/librustc/infer/freshen.rs index 1841bd9ea64..32b51da920d 100644 --- a/src/librustc/infer/freshen.rs +++ b/src/librustc/infer/freshen.rs @@ -31,7 +31,6 @@ //! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type //! inferencer knows "so far". -use crate::mir::interpret::ConstValue; use crate::ty::{self, Ty, TyCtxt, TypeFoldable}; use crate::ty::fold::TypeFolder; use crate::util::nodemap::FxHashMap; @@ -227,7 +226,7 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { match ct.val { - ConstValue::Infer(ty::InferConst::Var(v)) => { + ty::ConstKind::Infer(ty::InferConst::Var(v)) => { let opt_ct = self.infcx.const_unification_table .borrow_mut() .probe_value(v) @@ -240,7 +239,7 @@ fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { ct.ty, ); } - ConstValue::Infer(ty::InferConst::Fresh(i)) => { + ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => { if i >= self.const_freshen_count { bug!( "Encountered a freshend const with id {} \ @@ -252,16 +251,14 @@ fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { return ct; } - ConstValue::Bound(..) | - ConstValue::Placeholder(_) => { + ty::ConstKind::Bound(..) | + ty::ConstKind::Placeholder(_) => { bug!("unexpected const {:?}", ct) } - ConstValue::Param(_) | - ConstValue::Scalar(_) | - ConstValue::Slice { .. } | - ConstValue::ByRef { .. } | - ConstValue::Unevaluated(..) => {} + ty::ConstKind::Param(_) | + ty::ConstKind::Value(_) | + ty::ConstKind::Unevaluated(..) => {} } ct.super_fold_with(self) diff --git a/src/librustc/infer/fudge.rs b/src/librustc/infer/fudge.rs index e27766f4616..11f86a619b5 100644 --- a/src/librustc/infer/fudge.rs +++ b/src/librustc/infer/fudge.rs @@ -1,6 +1,5 @@ use crate::ty::{self, Ty, TyCtxt, TyVid, IntVid, FloatVid, RegionVid, ConstVid}; use crate::ty::fold::{TypeFoldable, TypeFolder}; -use crate::mir::interpret::ConstValue; use super::InferCtxt; use super::{RegionVariableOrigin, ConstVariableOrigin}; @@ -198,7 +197,7 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ConstValue::Infer(ty::InferConst::Var(vid)), ty } = ct { + if let ty::Const { val: ty::ConstKind::Infer(ty::InferConst::Var(vid)), ty } = ct { if self.const_vars.0.contains(&vid) { // This variable was created during the fudging. // Recreate it with a fresh variable here. diff --git a/src/librustc/infer/higher_ranked/mod.rs b/src/librustc/infer/higher_ranked/mod.rs index 542ac4931ec..49c095c69d6 100644 --- a/src/librustc/infer/higher_ranked/mod.rs +++ b/src/librustc/infer/higher_ranked/mod.rs @@ -7,7 +7,6 @@ use crate::infer::CombinedSnapshot; use crate::ty::relate::{Relate, RelateResult, TypeRelation}; use crate::ty::{self, Binder, TypeFoldable}; -use crate::mir::interpret::ConstValue; impl<'a, 'tcx> CombineFields<'a, 'tcx> { pub fn higher_ranked_sub( @@ -103,7 +102,7 @@ pub fn replace_bound_vars_with_placeholders( let fld_c = |bound_var: ty::BoundVar, ty| { self.tcx.mk_const( ty::Const { - val: ConstValue::Placeholder(ty::PlaceholderConst { + val: ty::ConstKind::Placeholder(ty::PlaceholderConst { universe: next_universe, name: bound_var, }), diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index e385d576b8c..49fb84a8260 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -14,7 +14,6 @@ use crate::middle::free_region::RegionRelations; use crate::middle::lang_items; use crate::middle::region; -use crate::mir::interpret::ConstValue; use crate::session::config::BorrowckMode; use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine}; use crate::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric}; @@ -1662,7 +1661,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = ct { + if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct { self.infcx.const_unification_table .borrow_mut() .probe_value(*vid) diff --git a/src/librustc/infer/nll_relate/mod.rs b/src/librustc/infer/nll_relate/mod.rs index d6f76e9ee34..1e0feb6a7da 100644 --- a/src/librustc/infer/nll_relate/mod.rs +++ b/src/librustc/infer/nll_relate/mod.rs @@ -29,7 +29,6 @@ use crate::ty::subst::GenericArg; use crate::ty::{self, Ty, TyCtxt, InferConst}; use crate::infer::{ConstVariableValue, ConstVarValue}; -use crate::mir::interpret::ConstValue; use rustc_data_structures::fx::FxHashMap; use std::fmt::Debug; @@ -626,7 +625,7 @@ fn consts( } match b.val { - ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { + ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { // Forbid inference variables in the RHS. bug!("unexpected inference var {:?}", b) } @@ -999,13 +998,13 @@ fn consts( _: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { match a.val { - ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { + ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { bug!( "unexpected inference variable encountered in NLL generalization: {:?}", a ); } - ConstValue::Infer(InferConst::Var(vid)) => { + ty::ConstKind::Infer(InferConst::Var(vid)) => { let mut variable_table = self.infcx.const_unification_table.borrow_mut(); let var_value = variable_table.probe_value(vid); match var_value.val.known() { diff --git a/src/librustc/infer/opaque_types/mod.rs b/src/librustc/infer/opaque_types/mod.rs index bd19a002fe8..dc54a273ed0 100644 --- a/src/librustc/infer/opaque_types/mod.rs +++ b/src/librustc/infer/opaque_types/mod.rs @@ -4,7 +4,6 @@ use crate::infer::outlives::free_region_map::FreeRegionRelations; use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin, TypeVariableOriginKind}; use crate::middle::region; -use crate::mir::interpret::ConstValue; use crate::traits::{self, PredicateObligation}; use crate::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::subst::{InternalSubsts, GenericArg, SubstsRef, GenericArgKind}; @@ -945,7 +944,7 @@ fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { trace!("checking const {:?}", ct); // Find a const parameter match ct.val { - ConstValue::Param(..) => { + ty::ConstKind::Param(..) => { // Look it up in the substitution list. match self.map.get(&ct.into()).map(|k| k.unpack()) { // Found it in the substitution list, replace with the parameter from the diff --git a/src/librustc/infer/resolve.rs b/src/librustc/infer/resolve.rs index 7c3a338366c..613f66d7ffd 100644 --- a/src/librustc/infer/resolve.rs +++ b/src/librustc/infer/resolve.rs @@ -1,6 +1,5 @@ use super::{InferCtxt, FixupError, FixupResult, Span}; use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; -use crate::mir::interpret::ConstValue; use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst}; use crate::ty::fold::{TypeFolder, TypeVisitor}; @@ -230,11 +229,11 @@ fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { } else { let c = self.infcx.shallow_resolve(c); match c.val { - ConstValue::Infer(InferConst::Var(vid)) => { + ty::ConstKind::Infer(InferConst::Var(vid)) => { self.err = Some(FixupError::UnresolvedConst(vid)); return self.tcx().consts.err; } - ConstValue::Infer(InferConst::Fresh(_)) => { + ty::ConstKind::Infer(InferConst::Fresh(_)) => { bug!("Unexpected const in full const resolver: {:?}", c); } _ => {} diff --git a/src/librustc/infer/unify_key.rs b/src/librustc/infer/unify_key.rs index b0b6d971c60..8ad6990a75d 100644 --- a/src/librustc/infer/unify_key.rs +++ b/src/librustc/infer/unify_key.rs @@ -1,5 +1,4 @@ use crate::ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt, InferConst}; -use crate::mir::interpret::ConstValue; use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable}; use rustc_data_structures::unify::InPlace; use syntax_pos::{Span, DUMMY_SP}; @@ -180,7 +179,7 @@ pub fn replace_if_possible( mut table: RefMut<'_, UnificationTable>>>, c: &'tcx ty::Const<'tcx> ) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = c { + if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = c { match table.probe_value(*vid).val.known() { Some(c) => c, None => c, diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 7b29fb26e74..e5778528e38 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -2,10 +2,7 @@ use rustc_macros::HashStable; use rustc_apfloat::{Float, ieee::{Double, Single}}; -use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef}; -use crate::ty::PlaceholderConst; -use crate::hir::def_id::DefId; -use crate::ty::{BoundVar, DebruijnIndex}; +use crate::ty::{Ty, layout::{HasDataLayout, Size}}; use super::{InterpResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate}; @@ -23,18 +20,6 @@ pub struct RawConst<'tcx> { #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash, HashStable)] pub enum ConstValue<'tcx> { - /// A const generic parameter. - Param(ParamConst), - - /// Infer the value of the const. - Infer(InferConst<'tcx>), - - /// Bound const variable, used only when preparing a trait query. - Bound(DebruijnIndex, BoundVar), - - /// A placeholder const - universally quantified higher-ranked const. - Placeholder(PlaceholderConst), - /// Used only for types with `layout::abi::Scalar` ABI and ZSTs. /// /// Not using the enum `Value` to encode that this must not be `Undef`. @@ -55,10 +40,6 @@ pub enum ConstValue<'tcx> { /// Offset into `alloc` offset: Size, }, - - /// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other - /// variants when the code is monomorphic enough for that. - Unevaluated(DefId, SubstsRef<'tcx>), } #[cfg(target_arch = "x86_64")] @@ -68,26 +49,21 @@ impl<'tcx> ConstValue<'tcx> { #[inline] pub fn try_to_scalar(&self) -> Option { match *self { - ConstValue::Param(_) | - ConstValue::Infer(_) | - ConstValue::Bound(..) | - ConstValue::Placeholder(_) | ConstValue::ByRef { .. } | - ConstValue::Unevaluated(..) | ConstValue::Slice { .. } => None, ConstValue::Scalar(val) => Some(val), } } - - #[inline] - pub fn try_to_bits(&self, size: Size) -> Option { - self.try_to_scalar()?.to_bits(size).ok() - } - - #[inline] - pub fn try_to_ptr(&self) -> Option { - self.try_to_scalar()?.to_ptr().ok() - } +// +// #[inline] +// pub fn try_to_bits(&self, size: Size) -> Option { +// self.try_to_scalar()?.to_bits(size).ok() +// } +// +// #[inline] +// pub fn try_to_ptr(&self) -> Option { +// self.try_to_scalar()?.to_ptr().ok() +// } } /// A `Scalar` represents an immediate, primitive value existing outside of a diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index a3ddfec765f..066998a1718 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -7,7 +7,7 @@ use crate::hir::def::{CtorKind, Namespace}; use crate::hir::def_id::DefId; use crate::hir::{self, InlineAsm as HirInlineAsm}; -use crate::mir::interpret::{ConstValue, PanicInfo, Scalar}; +use crate::mir::interpret::{PanicInfo, Scalar}; use crate::mir::visit::MirVisitable; use crate::ty::adjustment::PointerCast; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; @@ -1506,10 +1506,11 @@ pub fn fmt_successor_labels(&self) -> Vec> { values .iter() .map(|&u| { - tcx.mk_const(ty::Const { - val: ConstValue::Scalar(Scalar::from_uint(u, size).into()), - ty: switch_ty, - }) + ty::Const::from_scalar( + tcx, + Scalar::from_uint(u, size).into(), + switch_ty, + ) .to_string() .into() }) diff --git a/src/librustc/ty/_match.rs b/src/librustc/ty/_match.rs index a0d22789dae..ea8a6ff2b2e 100644 --- a/src/librustc/ty/_match.rs +++ b/src/librustc/ty/_match.rs @@ -1,7 +1,6 @@ use crate::ty::{self, Ty, TyCtxt, InferConst}; use crate::ty::error::TypeError; use crate::ty::relate::{self, Relate, TypeRelation, RelateResult}; -use crate::mir::interpret::ConstValue; /// A type "A" *matches* "B" if the fresh types in B could be /// substituted with values so as to make it equal to A. Matching is @@ -92,11 +91,11 @@ fn consts( } match (a.val, b.val) { - (_, ConstValue::Infer(InferConst::Fresh(_))) => { + (_, ty::ConstKind::Infer(InferConst::Fresh(_))) => { return Ok(a); } - (ConstValue::Infer(_), _) | (_, ConstValue::Infer(_)) => { + (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => { return Err(TypeError::ConstMismatch(relate::expected_found(self, &a, &b))); } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 04e0f6f4b56..ba014e3002b 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -885,7 +885,7 @@ pub fn is_identity(&self) -> bool { }, GenericArgKind::Const(ct) => match ct.val { - ConstValue::Bound(debruijn, b) => { + ty::ConstKind::Bound(debruijn, b) => { // We only allow a `ty::INNERMOST` index in substitutions. assert_eq!(debruijn, ty::INNERMOST); cvar == b @@ -986,7 +986,7 @@ fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonCons CommonConsts { err: mk_const(ty::Const { - val: ConstValue::Scalar(Scalar::zst()), + val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())), ty: types.err, }), } @@ -2534,7 +2534,7 @@ pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> { #[inline] pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx Const<'tcx> { self.mk_const(ty::Const { - val: ConstValue::Infer(InferConst::Var(v)), + val: ty::ConstKind::Infer(InferConst::Var(v)), ty, }) } @@ -2561,7 +2561,7 @@ pub fn mk_const_infer( ty: Ty<'tcx>, ) -> &'tcx ty::Const<'tcx> { self.mk_const(ty::Const { - val: ConstValue::Infer(ic), + val: ty::ConstKind::Infer(ic), ty, }) } @@ -2579,7 +2579,7 @@ pub fn mk_const_param( ty: Ty<'tcx> ) -> &'tcx Const<'tcx> { self.mk_const(ty::Const { - val: ConstValue::Param(ParamConst { index, name }), + val: ty::ConstKind::Param(ParamConst { index, name }), ty, }) } diff --git a/src/librustc/ty/flags.rs b/src/librustc/ty/flags.rs index d4b7f37b120..aee0ec7806b 100644 --- a/src/librustc/ty/flags.rs +++ b/src/librustc/ty/flags.rs @@ -1,6 +1,5 @@ use crate::ty::subst::{SubstsRef, GenericArgKind}; use crate::ty::{self, Ty, TypeFlags, InferConst}; -use crate::mir::interpret::ConstValue; #[derive(Debug)] pub struct FlagComputation { @@ -232,29 +231,27 @@ fn add_region(&mut self, r: ty::Region<'_>) { fn add_const(&mut self, c: &ty::Const<'_>) { self.add_ty(c.ty); match c.val { - ConstValue::Unevaluated(_, substs) => { + ty::ConstKind::Unevaluated(_, substs) => { self.add_substs(substs); self.add_flags(TypeFlags::HAS_PROJECTION); }, - ConstValue::Infer(infer) => { + ty::ConstKind::Infer(infer) => { self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_CT_INFER); match infer { InferConst::Fresh(_) => {} InferConst::Var(_) => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX), } } - ConstValue::Bound(debruijn, _) => self.add_binder(debruijn), - ConstValue::Param(_) => { + ty::ConstKind::Bound(debruijn, _) => self.add_binder(debruijn), + ty::ConstKind::Param(_) => { self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES); self.add_flags(TypeFlags::HAS_PARAMS); } - ConstValue::Placeholder(_) => { + ty::ConstKind::Placeholder(_) => { self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES); self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER); } - ConstValue::Scalar(_) => {} - ConstValue::Slice { .. } => {} - ConstValue::ByRef { .. } => {} + ty::ConstKind::Value(_) => {} } } diff --git a/src/librustc/ty/fold.rs b/src/librustc/ty/fold.rs index bacf3d42f04..f3480ce5739 100644 --- a/src/librustc/ty/fold.rs +++ b/src/librustc/ty/fold.rs @@ -32,7 +32,6 @@ //! looking for, and does not need to visit anything else. use crate::hir::def_id::DefId; -use crate::mir::interpret::ConstValue; use crate::ty::{self, Binder, Ty, TyCtxt, TypeFlags, flags::FlagComputation}; use std::collections::BTreeMap; @@ -521,7 +520,7 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ConstValue::Bound(debruijn, bound_const), ty } = *ct { + if let ty::Const { val: ty::ConstKind::Bound(debruijn, bound_const), ty } = *ct { if debruijn == self.current_index { let fld_c = &mut self.fld_c; let ct = fld_c(bound_const, ty); @@ -568,7 +567,7 @@ pub fn replace_late_bound_regions( let fld_t = |bound_ty| self.mk_ty(ty::Bound(ty::INNERMOST, bound_ty)); let fld_c = |bound_ct, ty| { self.mk_const(ty::Const { - val: ConstValue::Bound(ty::INNERMOST, bound_ct), + val: ty::ConstKind::Bound(ty::INNERMOST, bound_ct), ty, }) }; @@ -801,7 +800,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ConstValue::Bound(debruijn, bound_ct), ty } = *ct { + if let ty::Const { val: ty::ConstKind::Bound(debruijn, bound_ct), ty } = *ct { if self.amount == 0 || debruijn < self.current_index { ct } else { @@ -813,7 +812,7 @@ fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { } }; self.tcx.mk_const(ty::Const { - val: ConstValue::Bound(debruijn, bound_ct), + val: ty::ConstKind::Bound(debruijn, bound_ct), ty, }) } @@ -919,7 +918,7 @@ fn visit_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> bool { // const, as it has types/regions embedded in a lot of other // places. match ct.val { - ConstValue::Bound(debruijn, _) if debruijn >= self.outer_index => true, + ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => true, _ => ct.super_visit_with(self), } } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index e4ed1cd198e..8aa212ce310 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -63,7 +63,7 @@ pub use self::sty::{ClosureSubsts, GeneratorSubsts, UpvarSubsts, TypeAndMut}; pub use self::sty::{TraitRef, TyKind, PolyTraitRef}; pub use self::sty::{ExistentialTraitRef, PolyExistentialTraitRef}; -pub use self::sty::{ExistentialProjection, PolyExistentialProjection, Const}; +pub use self::sty::{ExistentialProjection, PolyExistentialProjection, Const, ConstKind}; pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region}; pub use self::sty::RegionKind; pub use self::sty::{TyVid, IntVid, FloatVid, ConstVid, RegionVid}; diff --git a/src/librustc/ty/print/obsolete.rs b/src/librustc/ty/print/obsolete.rs index 0389218b61d..7eb774849b1 100644 --- a/src/librustc/ty/print/obsolete.rs +++ b/src/librustc/ty/print/obsolete.rs @@ -6,7 +6,6 @@ //! FIXME(eddyb) implement a custom `PrettyPrinter` for this. use rustc::hir::def_id::DefId; -use rustc::mir::interpret::ConstValue; use rustc::ty::subst::SubstsRef; use rustc::ty::{self, Const, Instance, Ty, TyCtxt}; use rustc::{bug, hir}; @@ -170,21 +169,16 @@ pub fn push_type_name(&self, t: Ty<'tcx>, output: &mut String, debug: bool) { // If `debug` is true, usually-unprintable consts (such as `Infer`) will be printed, // as well as the unprintable types of constants (see `push_type_name` for more details). pub fn push_const_name(&self, c: &Const<'tcx>, output: &mut String, debug: bool) { - match c.val { - ConstValue::Scalar(..) | ConstValue::Slice { .. } | ConstValue::ByRef { .. } => { - // FIXME(const_generics): we could probably do a better job here. - write!(output, "{:?}", c).unwrap() - } - _ => { - if debug { - write!(output, "{:?}", c).unwrap() - } else { - bug!( - "DefPathBasedNames: trying to create const name for unexpected const: {:?}", - c, - ); - } - } + if let ty::ConstKind::Value(_) = c.val { + // FIXME(const_generics): we could probably do a better job here. + write!(output, "{:?}", c).unwrap() + } else if debug { + write!(output, "{:?}", c).unwrap() + } else { + bug!( + "DefPathBasedNames: trying to create const name for unexpected const: {:?}", + c, + ); } output.push_str(": "); self.push_type_name(c.ty, output, debug); diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index fdd3a1faaa9..c70bb2552cc 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -699,7 +699,7 @@ fn pretty_print_type( p!(write("["), print(ty), write("; ")); if self.tcx().sess.verbose() { p!(write("{:?}", sz)); - } else if let ConstValue::Unevaluated(..) = sz.val { + } else if let ty::ConstKind::Unevaluated(..) = sz.val { // do not try to evalute unevaluated constants. If we are const evaluating an // array length anon const, rustc will (with debug assertions) print the // constant's path. Which will end up here again. @@ -865,7 +865,7 @@ fn pretty_print_const( match (ct.val, &ct.ty.kind) { (_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)), - (ConstValue::Unevaluated(did, substs), _) => { + (ty::ConstKind::Unevaluated(did, substs), _) => { match self.tcx().def_kind(did) { | Some(DefKind::Static) | Some(DefKind::Const) @@ -882,15 +882,15 @@ fn pretty_print_const( }, } }, - (ConstValue::Infer(..), _) => p!(write("_: "), print(ct.ty)), - (ConstValue::Param(ParamConst { name, .. }), _) => p!(write("{}", name)), - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Bool) => + (ty::ConstKind::Infer(..), _) => p!(write("_: "), print(ct.ty)), + (ty::ConstKind::Param(ParamConst { name, .. }), _) => p!(write("{}", name)), + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Bool) => p!(write("{}", if data == 0 { "false" } else { "true" })), - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F32)) => + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F32)) => p!(write("{}f32", Single::from_bits(data))), - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F64)) => + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F64)) => p!(write("{}f64", Double::from_bits(data))), - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => { + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Uint(ui)) => { let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size(); let max = truncate(u128::max_value(), bit_size); @@ -901,7 +901,7 @@ fn pretty_print_const( p!(write("{}{}", data, ui_str)) }; }, - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Int(i)) => { + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Int(i)) => { let bit_size = Integer::from_attr(&self.tcx(), SignedInt(*i)) .size().bits() as u128; let min = 1u128 << (bit_size - 1); @@ -918,10 +918,10 @@ fn pretty_print_const( _ => p!(write("{}{}", sign_extend(data, size) as i128, i_str)) } }, - (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) => + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Char) => p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap())), - (ConstValue::Scalar(_), ty::RawPtr(_)) => p!(write("{{pointer}}")), - (ConstValue::Scalar(Scalar::Ptr(ptr)), ty::FnPtr(_)) => { + (ty::ConstKind::Value(ConstValue::Scalar(_)), ty::RawPtr(_)) => p!(write("{{pointer}}")), + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::FnPtr(_)) => { let instance = { let alloc_map = self.tcx().alloc_map.lock(); alloc_map.unwrap_fn(ptr.alloc_id) @@ -931,14 +931,14 @@ fn pretty_print_const( _ => { let printed = if let ty::Ref(_, ref_ty, _) = ct.ty.kind { let byte_str = match (ct.val, &ref_ty.kind) { - (ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => { + (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::Array(t, n)) if *t == u8 => { let n = n.eval_usize(self.tcx(), ty::ParamEnv::empty()); Some(self.tcx() .alloc_map.lock() .unwrap_memory(ptr.alloc_id) .get_bytes(&self.tcx(), ptr, Size::from_bytes(n)).unwrap()) }, - (ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => { + (ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Slice(t)) if *t == u8 => { // The `inspect` here is okay since we checked the bounds, and there are // no relocations (we have an active slice reference here). We don't use // this result to affect interpreter execution. @@ -956,7 +956,7 @@ fn pretty_print_const( } p!(write("\"")); true - } else if let (ConstValue::Slice { data, start, end }, ty::Str) = + } else if let (ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Str) = (ct.val, &ref_ty.kind) { // The `inspect` here is okay since we checked the bounds, and there are no diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index 9b5cdc489a8..49a25347912 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -561,51 +561,62 @@ pub fn super_relate_consts>( // and those that derive both `PartialEq` and `Eq`, corresponding // to `structural_match` types. let new_const_val = match (eagerly_eval(a), eagerly_eval(b)) { - (ConstValue::Infer(_), _) | (_, ConstValue::Infer(_)) => { + (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => { // The caller should handle these cases! bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b) } - (ConstValue::Param(a_p), ConstValue::Param(b_p)) if a_p.index == b_p.index => { + (ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index == b_p.index => { return Ok(a); } - (ConstValue::Placeholder(p1), ConstValue::Placeholder(p2)) if p1 == p2 => { + (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) if p1 == p2 => { return Ok(a); } - (ConstValue::Scalar(a_val), ConstValue::Scalar(b_val)) if a.ty == b.ty => { - if a_val == b_val { - Ok(ConstValue::Scalar(a_val)) - } else if let ty::FnPtr(_) = a.ty.kind { - let alloc_map = tcx.alloc_map.lock(); - let a_instance = alloc_map.unwrap_fn(a_val.to_ptr().unwrap().alloc_id); - let b_instance = alloc_map.unwrap_fn(b_val.to_ptr().unwrap().alloc_id); - if a_instance == b_instance { - Ok(ConstValue::Scalar(a_val)) - } else { - Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) + (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => { + let new_val = match (a_val, b_val) { + (ConstValue::Scalar(a_val), ConstValue::Scalar(b_val)) if a.ty == b.ty => { + if a_val == b_val { + Ok(ConstValue::Scalar(a_val)) + } else if let ty::FnPtr(_) = a.ty.kind { + let alloc_map = tcx.alloc_map.lock(); + let a_instance = alloc_map.unwrap_fn(a_val.to_ptr().unwrap().alloc_id); + let b_instance = alloc_map.unwrap_fn(b_val.to_ptr().unwrap().alloc_id); + if a_instance == b_instance { + Ok(ConstValue::Scalar(a_val)) + } else { + Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) + } + } else { + Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) + } } - } else { - Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) - } - } - (a_val @ ConstValue::Slice { .. }, b_val @ ConstValue::Slice { .. }) => { - let a_bytes = get_slice_bytes(&tcx, a_val); - let b_bytes = get_slice_bytes(&tcx, b_val); - if a_bytes == b_bytes { - Ok(a_val) - } else { - Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) - } - } + (a_val @ ConstValue::Slice { .. }, b_val @ ConstValue::Slice { .. }) => { + let a_bytes = get_slice_bytes(&tcx, a_val); + let b_bytes = get_slice_bytes(&tcx, b_val); + if a_bytes == b_bytes { + Ok(a_val) + } else { + Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))) + } + } - // FIXME(const_generics): handle `ConstValue::ByRef`. + // FIXME(const_generics): handle `ConstValue::ByRef`. + + _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))), + }; + + match new_val { + Ok(val) => Ok(ty::ConstKind::Value(val)), + Err(err) => Err(err), + } + }, // FIXME(const_generics): this is wrong, as it is a projection - (ConstValue::Unevaluated(a_def_id, a_substs), - ConstValue::Unevaluated(b_def_id, b_substs)) if a_def_id == b_def_id => { + (ty::ConstKind::Unevaluated(a_def_id, a_substs), + ty::ConstKind::Unevaluated(b_def_id, b_substs)) if a_def_id == b_def_id => { let substs = relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?; - Ok(ConstValue::Unevaluated(a_def_id, &substs)) + Ok(ty::ConstKind::Unevaluated(a_def_id, &substs)) } _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))), }; diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 5d78d563e9a..dd92898e434 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -5,7 +5,6 @@ use crate::hir::def::Namespace; use crate::mir::ProjectionKind; -use crate::mir::interpret::ConstValue; use crate::ty::{self, Lift, Ty, TyCtxt, InferConst}; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::print::{FmtPrinter, Printer}; @@ -1378,26 +1377,25 @@ fn visit_with>(&self, visitor: &mut V) -> bool { } } -impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> { +impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> { fn super_fold_with>(&self, folder: &mut F) -> Self { match *self { - ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)), - ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)), - ConstValue::Unevaluated(did, substs) - => ConstValue::Unevaluated(did, substs.fold_with(folder)), - ConstValue::ByRef { .. } | ConstValue::Bound(..) | ConstValue::Placeholder(..) - | ConstValue::Scalar(..) | ConstValue::Slice { .. } => *self, - + ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)), + ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)), + ty::ConstKind::Unevaluated(did, substs) + => ty::ConstKind::Unevaluated(did, substs.fold_with(folder)), + ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) + | ty::ConstKind::Placeholder(..) => *self, } } fn super_visit_with>(&self, visitor: &mut V) -> bool { match *self { - ConstValue::Infer(ic) => ic.visit_with(visitor), - ConstValue::Param(p) => p.visit_with(visitor), - ConstValue::Unevaluated(_, substs) => substs.visit_with(visitor), - ConstValue::ByRef { .. } | ConstValue::Bound(..) | ConstValue::Placeholder(_) - | ConstValue::Scalar(_) | ConstValue::Slice { .. } => false, + ty::ConstKind::Infer(ic) => ic.visit_with(visitor), + ty::ConstKind::Param(p) => p.visit_with(visitor), + ty::ConstKind::Unevaluated(_, substs) => substs.visit_with(visitor), + ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) + | ty::ConstKind::Placeholder(_) => false, } } } diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 51cf7550c30..bb2fc9f0131 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -2257,17 +2257,17 @@ pub fn is_trivially_sized(&self, tcx: TyCtxt<'tcx>) -> bool { pub struct Const<'tcx> { pub ty: Ty<'tcx>, - pub val: ConstValue<'tcx>, + pub val: ConstKind<'tcx>, } #[cfg(target_arch = "x86_64")] -static_assert_size!(Const<'_>, 40); +static_assert_size!(Const<'_>, 48); impl<'tcx> Const<'tcx> { #[inline] pub fn from_scalar(tcx: TyCtxt<'tcx>, val: Scalar, ty: Ty<'tcx>) -> &'tcx Self { tcx.mk_const(Self { - val: ConstValue::Scalar(val), + val: ConstKind::Value(ConstValue::Scalar(val)), ty, }) } @@ -2317,7 +2317,7 @@ pub fn eval( // FIXME(const_generics): this doesn't work right now, // because it tries to relate an `Infer` to a `Param`. match self.val { - ConstValue::Unevaluated(did, substs) => { + ConstKind::Unevaluated(did, substs) => { // if `substs` has no unresolved components, use and empty param_env let (param_env, substs) = param_env.with_reveal_all().and(substs).into_parts(); // try to resolve e.g. associated constants to their definition on an impl @@ -2363,6 +2363,54 @@ pub fn eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 { impl<'tcx> rustc_serialize::UseSpecializedDecodable for &'tcx Const<'tcx> {} +/// Represents a constant in Rust. +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, + RustcEncodable, RustcDecodable, Hash, HashStable)] +pub enum ConstKind<'tcx> { + /// A const generic parameter. + Param(ParamConst), + + /// Infer the value of the const. + Infer(InferConst<'tcx>), + + /// Bound const variable, used only when preparing a trait query. + Bound(DebruijnIndex, BoundVar), + + /// A placeholder const - universally quantified higher-ranked const. + Placeholder(ty::PlaceholderConst), + + /// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other + /// variants when the code is monomorphic enough for that. + Unevaluated(DefId, SubstsRef<'tcx>), + + /// Used to hold computed value. + Value(ConstValue<'tcx>), +} + +#[cfg(target_arch = "x86_64")] +static_assert_size!(ConstKind<'_>, 40); + +impl<'tcx> ConstKind<'tcx> { + #[inline] + pub fn try_to_scalar(&self) -> Option { + if let ConstKind::Value(val) = self { + val.try_to_scalar() + } else { + None + } + } + + #[inline] + pub fn try_to_bits(&self, size: ty::layout::Size) -> Option { + self.try_to_scalar()?.to_bits(size).ok() + } + + //#[inline] + //pub fn try_to_ptr(&self) -> Option { + // self.try_to_scalar()?.to_ptr().ok() + //} +} + /// An inference variable for a const, for use in const generics. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash, HashStable)] diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 29721979099..8ba8622704a 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -4,7 +4,6 @@ use crate::infer::canonical::Canonical; use crate::ty::{self, Lift, List, Ty, TyCtxt, ParamConst}; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; -use crate::mir::interpret::ConstValue; use crate::ty::sty::{ClosureSubsts, GeneratorSubsts}; use rustc_serialize::{self, Encodable, Encoder, Decodable, Decoder}; @@ -234,7 +233,7 @@ pub fn bound_vars_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> ty::GenericParamDefKind::Const => { tcx.mk_const(ty::Const { - val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)), + val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)), ty: tcx.type_of(def_id), }).into() } @@ -578,7 +577,7 @@ fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { return c; } - if let ConstValue::Param(p) = c.val { + if let ty::ConstKind::Param(p) = c.val { self.const_for_param(p, c) } else { c.super_fold_with(self) diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index d46320abff2..76afba220ce 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -12,7 +12,6 @@ use crate::ty::query::TyCtxtAt; use crate::ty::TyKind::*; use crate::ty::layout::{Integer, IntegerExt}; -use crate::mir::interpret::ConstValue; use crate::util::common::ErrorReported; use crate::middle::lang_items; @@ -566,7 +565,7 @@ pub fn destructor_constraints(self, def: &'tcx ty::AdtDef) !impl_generics.type_param(pt, self).pure_wrt_drop } GenericArgKind::Const(&ty::Const { - val: ConstValue::Param(ref pc), + val: ty::ConstKind::Param(ref pc), .. }) => { !impl_generics.const_param(pc, self).pure_wrt_drop diff --git a/src/librustc/ty/walk.rs b/src/librustc/ty/walk.rs index f5b1902e3cc..8d0f9a47162 100644 --- a/src/librustc/ty/walk.rs +++ b/src/librustc/ty/walk.rs @@ -3,7 +3,6 @@ use crate::ty::{self, Ty}; use smallvec::{self, SmallVec}; -use crate::mir::interpret::ConstValue; // The TypeWalker's stack is hot enough that it's worth going to some effort to // avoid heap allocations. @@ -75,7 +74,7 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) { ty::Placeholder(..) | ty::Bound(..) | ty::Foreign(..) => { } ty::Array(ty, len) => { - if let ConstValue::Unevaluated(_, substs) = len.val { + if let ty::ConstKind::Unevaluated(_, substs) = len.val { stack.extend(substs.types().rev()); } stack.push(len.ty); diff --git a/src/librustc/ty/wf.rs b/src/librustc/ty/wf.rs index f9e7a8030a6..aa0456b78af 100644 --- a/src/librustc/ty/wf.rs +++ b/src/librustc/ty/wf.rs @@ -8,7 +8,6 @@ use syntax::symbol::{kw, Ident}; use syntax_pos::Span; use crate::middle::lang_items; -use crate::mir::interpret::ConstValue; /// Returns the set of obligations needed to make `ty` well-formed. /// If `ty` contains unresolved inference variables, this may include @@ -363,7 +362,7 @@ fn compute_projection(&mut self, data: ty::ProjectionTy<'tcx>) { /// Pushes the obligations required for an array length to be WF /// into `self.out`. fn compute_array_len(&mut self, constant: ty::Const<'tcx>) { - if let ConstValue::Unevaluated(def_id, substs) = constant.val { + if let ty::ConstKind::Unevaluated(def_id, substs) = constant.val { let obligations = self.nominal_obligations(def_id, substs); self.out.extend(obligations); From ee33e0279a66526f8edf6b71e5d47fc5d9fbd0bc Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 21:55:19 +0100 Subject: [PATCH 02/14] Refactor pretty_print_const. --- src/librustc/ty/print/pretty.rs | 59 +++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index c70bb2552cc..594550dd967 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -861,8 +861,6 @@ fn pretty_print_const( return Ok(self); } - let u8 = self.tcx().types.u8; - match (ct.val, &ct.ty.kind) { (_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)), (ty::ConstKind::Unevaluated(did, substs), _) => { @@ -884,13 +882,38 @@ fn pretty_print_const( }, (ty::ConstKind::Infer(..), _) => p!(write("_: "), print(ct.ty)), (ty::ConstKind::Param(ParamConst { name, .. }), _) => p!(write("{}", name)), - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Bool) => + (ty::ConstKind::Value(value), _) => return self.pretty_print_const_value(value, ct.ty), + + _ => { + // fallback + p!(write("{:?} : ", ct.val), print(ct.ty)) + } + }; + Ok(self) + } + + fn pretty_print_const_value( + mut self, + ct: ConstValue<'tcx>, + ty: Ty<'tcx>, + ) -> Result { + define_scoped_cx!(self); + + if self.tcx().sess.verbose() { + p!(write("ConstValue({:?}: {:?})", ct, ty)); + return Ok(self); + } + + let u8 = self.tcx().types.u8; + + match (ct, &ty.kind) { + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Bool) => p!(write("{}", if data == 0 { "false" } else { "true" })), - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F32)) => + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F32)) => p!(write("{}f32", Single::from_bits(data))), - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F64)) => + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F64)) => p!(write("{}f64", Double::from_bits(data))), - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Uint(ui)) => { + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => { let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size(); let max = truncate(u128::max_value(), bit_size); @@ -901,13 +924,13 @@ fn pretty_print_const( p!(write("{}{}", data, ui_str)) }; }, - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Int(i)) => { + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Int(i)) => { let bit_size = Integer::from_attr(&self.tcx(), SignedInt(*i)) .size().bits() as u128; let min = 1u128 << (bit_size - 1); let max = min - 1; - let ty = self.tcx().lift(&ct.ty).unwrap(); + let ty = self.tcx().lift(&ty).unwrap(); let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty)) .unwrap() .size; @@ -918,10 +941,10 @@ fn pretty_print_const( _ => p!(write("{}{}", sign_extend(data, size) as i128, i_str)) } }, - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Char) => + (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) => p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap())), - (ty::ConstKind::Value(ConstValue::Scalar(_)), ty::RawPtr(_)) => p!(write("{{pointer}}")), - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::FnPtr(_)) => { + (ConstValue::Scalar(_), ty::RawPtr(_)) => p!(write("{{pointer}}")), + (ConstValue::Scalar(Scalar::Ptr(ptr)), ty::FnPtr(_)) => { let instance = { let alloc_map = self.tcx().alloc_map.lock(); alloc_map.unwrap_fn(ptr.alloc_id) @@ -929,16 +952,16 @@ fn pretty_print_const( p!(print_value_path(instance.def_id(), instance.substs)); }, _ => { - let printed = if let ty::Ref(_, ref_ty, _) = ct.ty.kind { - let byte_str = match (ct.val, &ref_ty.kind) { - (ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::Array(t, n)) if *t == u8 => { + let printed = if let ty::Ref(_, ref_ty, _) = ty.kind { + let byte_str = match (ct, &ref_ty.kind) { + (ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => { let n = n.eval_usize(self.tcx(), ty::ParamEnv::empty()); Some(self.tcx() .alloc_map.lock() .unwrap_memory(ptr.alloc_id) .get_bytes(&self.tcx(), ptr, Size::from_bytes(n)).unwrap()) }, - (ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Slice(t)) if *t == u8 => { + (ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => { // The `inspect` here is okay since we checked the bounds, and there are // no relocations (we have an active slice reference here). We don't use // this result to affect interpreter execution. @@ -956,8 +979,8 @@ fn pretty_print_const( } p!(write("\"")); true - } else if let (ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Str) = - (ct.val, &ref_ty.kind) + } else if let (ConstValue::Slice { data, start, end }, ty::Str) = + (ct, &ref_ty.kind) { // The `inspect` here is okay since we checked the bounds, and there are no // relocations (we have an active `str` reference here). We don't use this @@ -975,7 +998,7 @@ fn pretty_print_const( }; if !printed { // fallback - p!(write("{:?} : ", ct.val), print(ct.ty)) + p!(write("{:?} : ", ct), print(ty)) } } }; From ff6b7cd7c13c172b829b0dc12edfc2a20f7c3c32 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:11:31 +0100 Subject: [PATCH 03/14] Rename in librustc_traits. --- src/librustc_traits/chalk_context/mod.rs | 3 +-- src/librustc_traits/chalk_context/resolvent_ops.rs | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/librustc_traits/chalk_context/mod.rs b/src/librustc_traits/chalk_context/mod.rs index 8d136a1b65c..d9f0b8550bb 100644 --- a/src/librustc_traits/chalk_context/mod.rs +++ b/src/librustc_traits/chalk_context/mod.rs @@ -37,7 +37,6 @@ use rustc::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use rustc::ty::query::Providers; use rustc::ty::subst::{GenericArg, GenericArgKind}; -use rustc::mir::interpret::ConstValue; use syntax_pos::DUMMY_SP; use std::fmt::{self, Debug}; @@ -286,7 +285,7 @@ fn is_trivial_substitution( _ => false, }, GenericArgKind::Const(ct) => match ct.val { - ConstValue::Bound(debruijn, bound_ct) => { + ty::ConstKind::Bound(debruijn, bound_ct) => { debug_assert_eq!(debruijn, ty::INNERMOST); cvar == bound_ct } diff --git a/src/librustc_traits/chalk_context/resolvent_ops.rs b/src/librustc_traits/chalk_context/resolvent_ops.rs index 49d76681196..b8893d65ced 100644 --- a/src/librustc_traits/chalk_context/resolvent_ops.rs +++ b/src/librustc_traits/chalk_context/resolvent_ops.rs @@ -19,7 +19,6 @@ use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::GenericArg; use rustc::ty::relate::{Relate, RelateResult, TypeRelation}; -use rustc::mir::interpret::ConstValue; use syntax_pos::DUMMY_SP; use super::{ChalkInferenceContext, ChalkArenas, ChalkExClause, ConstrainedSubst}; @@ -287,7 +286,7 @@ fn consts( a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - if let ty::Const { val: ConstValue::Bound(debruijn, bound_ct), .. } = a { + if let ty::Const { val: ty::ConstKind::Bound(debruijn, bound_ct), .. } = a { if *debruijn == self.binder_index { self.unify_free_answer_var(*bound_ct, b.into())?; return Ok(b); @@ -296,8 +295,8 @@ fn consts( match (a, b) { ( - ty::Const { val: ConstValue::Bound(a_debruijn, a_bound), .. }, - ty::Const { val: ConstValue::Bound(b_debruijn, b_bound), .. }, + ty::Const { val: ty::ConstKind::Bound(a_debruijn, a_bound), .. }, + ty::Const { val: ty::ConstKind::Bound(b_debruijn, b_bound), .. }, ) => { assert_eq!(a_debruijn, b_debruijn); assert_eq!(a_bound, b_bound); From 028c83cdf6d6d0054bafaaf85af39bca393a104c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:11:43 +0100 Subject: [PATCH 04/14] Rename in librustc_typecheck. --- src/librustc_typeck/astconv.rs | 5 ++--- src/librustc_typeck/check/mod.rs | 2 +- src/librustc_typeck/check/wfcheck.rs | 5 ++--- src/librustc_typeck/constrained_generic_params.rs | 3 +-- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 5c661513382..ee3ebd691e4 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -18,7 +18,6 @@ use rustc::ty::{GenericParamDef, GenericParamDefKind}; use rustc::ty::subst::{self, Subst, InternalSubsts, SubstsRef}; use rustc::ty::wf::object_region_bounds; -use rustc::mir::interpret::ConstValue; use rustc_target::spec::abi; use crate::require_c_abi_if_c_variadic; use smallvec::SmallVec; @@ -2226,7 +2225,7 @@ pub fn ast_const_to_const( let def_id = tcx.hir().local_def_id(ast_const.hir_id); let mut const_ = ty::Const { - val: ConstValue::Unevaluated( + val: ty::ConstKind::Unevaluated( def_id, InternalSubsts::identity_for_item(tcx, def_id), ), @@ -2243,7 +2242,7 @@ pub fn ast_const_to_const( let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(hir_id)]; let name = tcx.hir().name(hir_id); - const_.val = ConstValue::Param(ty::ParamConst::new(index, name)); + const_.val = ty::ConstKind::Param(ty::ParamConst::new(index, name)); } tcx.mk_const(const_) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 72b5018589c..2363dbc5303 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1688,7 +1688,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: DefId, span: Span) }; let param_env = ty::ParamEnv::reveal_all(); if let Ok(static_) = tcx.const_eval(param_env.and(cid)) { - let alloc = if let ConstValue::ByRef { alloc, .. } = static_.val { + let alloc = if let ty::ConstKind::Value(ConstValue::ByRef { alloc, .. }) = static_.val { alloc } else { bug!("Matching on non-ByRef static") diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 5b25d8f25a9..fd97493b591 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -6,7 +6,6 @@ use rustc::ty::{self, Ty, TyCtxt, GenericParamDefKind, TypeFoldable, ToPredicate}; use rustc::ty::subst::{Subst, InternalSubsts}; use rustc::util::nodemap::{FxHashSet, FxHashMap}; -use rustc::mir::interpret::ConstValue; use rustc::middle::lang_items; use rustc::infer::opaque_types::may_define_opaque_type; @@ -536,7 +535,7 @@ fn visit_region(&mut self, _: ty::Region<'tcx>) -> bool { } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool { - if let ConstValue::Param(param) = c.val { + if let ty::ConstKind::Param(param) = c.val { self.params.insert(param.index); } c.super_visit_with(self) @@ -705,7 +704,7 @@ fn check_opaque_types<'fcx, 'tcx>( } ty::subst::GenericArgKind::Const(ct) => match ct.val { - ConstValue::Param(_) => {} + ty::ConstKind::Param(_) => {} _ => { tcx.sess .struct_span_err( diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 1fdf49fde55..0523de56512 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -1,7 +1,6 @@ use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::fold::{TypeFoldable, TypeVisitor}; use rustc::util::nodemap::FxHashSet; -use rustc::mir::interpret::ConstValue; use syntax::source_map::Span; #[derive(Clone, PartialEq, Eq, Hash, Debug)] @@ -77,7 +76,7 @@ fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool { } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool { - if let ConstValue::Param(data) = c.val { + if let ty::ConstKind::Param(data) = c.val { self.parameters.push(Parameter::from(data)); } false From 7378c25f2593086a496e66e638c305ded2e39e7a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:11:51 +0100 Subject: [PATCH 05/14] Rename in librustc_mir. --- .../borrow_check/nll/type_check/mod.rs | 4 +- src/librustc_mir/const_eval.rs | 8 ++-- src/librustc_mir/hair/constant.rs | 2 +- src/librustc_mir/hair/cx/expr.rs | 8 ++-- src/librustc_mir/hair/pattern/_match.rs | 40 +++++++++++++++---- src/librustc_mir/hair/pattern/mod.rs | 7 ++-- src/librustc_mir/interpret/intrinsics.rs | 4 +- src/librustc_mir/interpret/operand.rs | 22 +++++----- src/librustc_mir/monomorphize/collector.rs | 8 ++-- .../transform/check_consts/qualifs.rs | 3 +- src/librustc_mir/transform/promote_consts.rs | 3 +- src/librustc_mir/transform/qualify_consts.rs | 3 +- 12 files changed, 66 insertions(+), 46 deletions(-) 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 37fc0b09e5b..cdfbca65e7e 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::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 @@ fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) { ); } } 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 707ad151182..326953d9f30 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 b9e75a576ca..1abdcde10ab 100644 --- a/src/librustc_mir/hair/constant.rs +++ b/src/librustc_mir/hair/constant.rs @@ -56,7 +56,7 @@ 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 2ff690b7ccc..92c9c702c7a 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -5,7 +5,7 @@ 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 982330baf9c..9a1fbf71e6f 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -313,7 +313,10 @@ fn fold_pattern(&mut self, pat: &Pat<'tcx>) -> Pat<'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 @@ fn fold_pattern(&mut self, pat: &Pat<'tcx>) -> Pat<'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 @@ fn from_const( ) -> 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 range_borders(r: IntRange<'_>) -> impl Iterator { 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 f613f6f4b99..3d97ed6bf9b 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 39f10d8e604..b48cac082e3 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -54,11 +54,11 @@ fn numeric_intrinsic<'tcx, Tag>( "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 79762b87b0a..4d2ccdc20da 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -545,23 +545,27 @@ pub(super) fn eval_operands( 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 @@ pub(super) fn eval_operands( 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 49cdd914234..49f27a8117e 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 496a5679067..595ef2aad49 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 @@ fn in_operand( 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 48a58f1d0ee..a7f48162f9b 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 @@ fn validate_operand(&self, operand: &Operand<'tcx>) -> Result<(), Unpromotable> 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 39720af4cb5..0373fbc55f7 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -14,7 +14,6 @@ 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 @@ fn in_operand(cx: &ConstCx<'_, 'tcx>, operand: &Operand<'tcx>) -> bool { 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) From c67125260ba28c5f1a113794b7198033ab408c28 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:12:17 +0100 Subject: [PATCH 06/14] Rename in librustc_codegen_utils. --- src/librustc_codegen_utils/symbol_names/legacy.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_codegen_utils/symbol_names/legacy.rs b/src/librustc_codegen_utils/symbol_names/legacy.rs index 66e1b6d949e..1597f98771c 100644 --- a/src/librustc_codegen_utils/symbol_names/legacy.rs +++ b/src/librustc_codegen_utils/symbol_names/legacy.rs @@ -253,7 +253,7 @@ fn print_const( ct: &'tcx ty::Const<'tcx>, ) -> Result { // only print integers - if let ConstValue::Scalar(Scalar::Raw { .. }) = ct.val { + if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { .. })) = ct.val { if ct.ty.is_integral() { return self.pretty_print_const(ct); } From 25fab7ecf5192406de74bbfa9978e3ef53681386 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:12:26 +0100 Subject: [PATCH 07/14] Rename in librustc_codegen_ssa. --- src/librustc_codegen_ssa/mir/constant.rs | 2 +- src/librustc_codegen_ssa/mir/operand.rs | 12 ++++++------ src/librustc_codegen_ssa/mir/place.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs index d06359ab0ce..181787e3985 100644 --- a/src/librustc_codegen_ssa/mir/constant.rs +++ b/src/librustc_codegen_ssa/mir/constant.rs @@ -14,7 +14,7 @@ pub fn eval_mir_constant( constant: &mir::Constant<'tcx>, ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> { match constant.literal.val { - mir::interpret::ConstValue::Unevaluated(def_id, ref substs) => { + ty::ConstKind::Unevaluated(def_id, ref substs) => { let substs = self.monomorphize(substs); let instance = ty::Instance::resolve( self.cx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs, diff --git a/src/librustc_codegen_ssa/mir/operand.rs b/src/librustc_codegen_ssa/mir/operand.rs index ba5e47aeede..78d09f834c6 100644 --- a/src/librustc_codegen_ssa/mir/operand.rs +++ b/src/librustc_codegen_ssa/mir/operand.rs @@ -75,12 +75,12 @@ pub fn from_const>( return OperandRef::new_zst(bx, layout); } - let val = match val.val { - ConstValue::Unevaluated(..) => bug!("unevaluated constant in `OperandRef::from_const`"), - ConstValue::Param(_) => bug!("encountered a ConstValue::Param in codegen"), - ConstValue::Infer(_) => bug!("encountered a ConstValue::Infer in codegen"), - ConstValue::Bound(..) => bug!("encountered a ConstValue::Bound in codegen"), - ConstValue::Placeholder(_) => bug!("encountered a ConstValue::Placeholder in codegen"), + let val_val = match val.val { + ty::ConstKind::Value(val_val) => val_val, + _ => bug!("encountered bad ConstKind in codegen"), + }; + + let val = match val_val { ConstValue::Scalar(x) => { let scalar = match layout.abi { layout::Abi::Scalar(ref x) => x, diff --git a/src/librustc_codegen_ssa/mir/place.rs b/src/librustc_codegen_ssa/mir/place.rs index 3e7c4ef49fb..d515f114c77 100644 --- a/src/librustc_codegen_ssa/mir/place.rs +++ b/src/librustc_codegen_ssa/mir/place.rs @@ -480,7 +480,7 @@ pub fn codegen_place( let layout = cx.layout_of(self.monomorphize(&ty)); match bx.tcx().const_eval(param_env.and(cid)) { Ok(val) => match val.val { - mir::interpret::ConstValue::ByRef { alloc, offset } => { + ty::ConstKind::Value(mir::interpret::ConstValue::ByRef { alloc, offset }) => { bx.cx().from_const_alloc(layout, alloc, offset) } _ => bug!("promoteds should have an allocation: {:?}", val), From 1f2c18cdbd755afc722fa2a38c97a6dc6d134453 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:19:46 +0100 Subject: [PATCH 08/14] Rename in librustc_codegen_llvm. --- src/librustc_codegen_llvm/consts.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index fd7054a5a0a..bcf0154e3f8 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -88,9 +88,9 @@ pub fn codegen_static_initializer( let static_ = cx.tcx.const_eval(param_env.and(cid))?; let alloc = match static_.val { - ConstValue::ByRef { + ty::ConstKind::Value(ConstValue::ByRef { alloc, offset, - } if offset.bytes() == 0 => { + }) if offset.bytes() == 0 => { alloc }, _ => bug!("static const eval returned {:#?}", static_), From 3ebfa1856e3d839068131be8794821c4e3d029ed Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 8 Nov 2019 23:19:55 +0100 Subject: [PATCH 09/14] Rename in librustdoc. --- src/librustdoc/clean/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 3d42d5bb0ca..4d8d0040cdc 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -16,7 +16,7 @@ use rustc::middle::resolve_lifetime as rl; use rustc::middle::lang_items; use rustc::middle::stability; -use rustc::mir::interpret::{GlobalId, ConstValue}; +use rustc::mir::interpret::GlobalId; use rustc::hir; use rustc::hir::def::{CtorKind, DefKind, Res}; use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; @@ -3075,7 +3075,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Type { ty::Slice(ty) => Slice(box ty.clean(cx)), ty::Array(ty, n) => { let mut n = cx.tcx.lift(&n).expect("array lift failed"); - if let ConstValue::Unevaluated(def_id, substs) = n.val { + if let ty::ConstKind::Unevaluated(def_id, substs) = n.val { let param_env = cx.tcx.param_env(def_id); let cid = GlobalId { instance: ty::Instance::new(def_id, substs), @@ -4234,7 +4234,7 @@ fn name_from_pat(p: &hir::Pat) -> String { fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String { match n.val { - ConstValue::Unevaluated(def_id, _) => { + ty::ConstKind::Unevaluated(def_id, _) => { if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) { print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id)) } else { From f9ff78bdd546b2a420fbb0056cc9d1a6a0e40132 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 9 Nov 2019 00:06:37 +0100 Subject: [PATCH 10/14] Remove cruft. --- src/librustc/mir/interpret/value.rs | 10 ---------- src/librustc/ty/sty.rs | 5 ----- 2 files changed, 15 deletions(-) diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index e5778528e38..a0367154b75 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -54,16 +54,6 @@ pub fn try_to_scalar(&self) -> Option { ConstValue::Scalar(val) => Some(val), } } -// -// #[inline] -// pub fn try_to_bits(&self, size: Size) -> Option { -// self.try_to_scalar()?.to_bits(size).ok() -// } -// -// #[inline] -// pub fn try_to_ptr(&self) -> Option { -// self.try_to_scalar()?.to_ptr().ok() -// } } /// A `Scalar` represents an immediate, primitive value existing outside of a diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index bb2fc9f0131..8f6fc02ab4b 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -2404,11 +2404,6 @@ pub fn try_to_scalar(&self) -> Option { pub fn try_to_bits(&self, size: ty::layout::Size) -> Option { self.try_to_scalar()?.to_bits(size).ok() } - - //#[inline] - //pub fn try_to_ptr(&self) -> Option { - // self.try_to_scalar()?.to_ptr().ok() - //} } /// An inference variable for a const, for use in const generics. From 6b56dced4c4fa1ac9d6d7629c816ad9b06c466d0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 9 Nov 2019 11:30:51 +0100 Subject: [PATCH 11/14] Fix tidy. --- src/librustc_codegen_ssa/mir/place.rs | 4 +++- src/librustc_mir/hair/pattern/_match.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/librustc_codegen_ssa/mir/place.rs b/src/librustc_codegen_ssa/mir/place.rs index d515f114c77..d4e33ee8b6f 100644 --- a/src/librustc_codegen_ssa/mir/place.rs +++ b/src/librustc_codegen_ssa/mir/place.rs @@ -480,7 +480,9 @@ pub fn codegen_place( let layout = cx.layout_of(self.monomorphize(&ty)); match bx.tcx().const_eval(param_env.and(cid)) { Ok(val) => match val.val { - ty::ConstKind::Value(mir::interpret::ConstValue::ByRef { alloc, offset }) => { + ty::ConstKind::Value(mir::interpret::ConstValue::ByRef { + alloc, offset + }) => { bx.cx().from_const_alloc(layout, alloc, offset) } _ => bug!("promoteds should have an allocation: {:?}", val), diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 9a1fbf71e6f..9894fa64779 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -327,7 +327,9 @@ fn fold_pattern(&mut self, pat: &Pat<'tcx>) -> Pat<'tcx> { span: pat.span, kind: box PatKind::Constant { value: self.tcx.mk_const(Const { - val: ty::ConstKind::Value(self.fold_const_value_deref(*val, rty, crty)), + val: ty::ConstKind::Value( + self.fold_const_value_deref(*val, rty, crty) + ), ty: rty, }), }, @@ -1256,7 +1258,9 @@ fn from_const( ) -> Option> { if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, value.ty) { let ty = value.ty; - let val = if let ty::ConstKind::Value(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 From 4caa66f25ded7ae6fbd062332f3642b93a2d40a8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 9 Nov 2019 11:43:32 +0100 Subject: [PATCH 12/14] Centril review. --- src/librustc/ty/relate.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index 49a25347912..28bd43b3ee5 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -605,10 +605,7 @@ pub fn super_relate_consts>( _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))), }; - match new_val { - Ok(val) => Ok(ty::ConstKind::Value(val)), - Err(err) => Err(err), - } + new_val.map(ty::ConstKind::Value) }, // FIXME(const_generics): this is wrong, as it is a projection From a1af690ab38fc3f3c96c978311735a0f6026e328 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 12 Nov 2019 20:09:31 +0100 Subject: [PATCH 13/14] Bless symbol-names. --- src/test/ui/symbol-names/impl1.legacy.stderr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/symbol-names/impl1.legacy.stderr b/src/test/ui/symbol-names/impl1.legacy.stderr index b27df10e750..c9495b597eb 100644 --- a/src/test/ui/symbol-names/impl1.legacy.stderr +++ b/src/test/ui/symbol-names/impl1.legacy.stderr @@ -46,13 +46,13 @@ error: def-path(bar::::baz) LL | #[rustc_def_path] | ^^^^^^^^^^^^^^^^^ -error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$_$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17h61b0fcb05ebeeb79E) +error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$_$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17h92c563325b7ff21aE) --> $DIR/impl1.rs:61:13 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; _] as impl1::main::{{closure}}::Bar>::method::h61b0fcb05ebeeb79) +error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; _] as impl1::main::{{closure}}::Bar>::method::h92c563325b7ff21a) --> $DIR/impl1.rs:61:13 | LL | #[rustc_symbol_name] From 552fa6479846951e242df412a08c86212beb1d0f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 12 Nov 2019 22:02:55 +0100 Subject: [PATCH 14/14] Bless mir-dump test. --- src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs b/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs index 85ed8d55b24..34d4a534ad9 100644 --- a/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs +++ b/src/test/mir-opt/const_prop/const_prop_fails_gracefully.rs @@ -25,7 +25,7 @@ fn main() { // ... // _4 = const Scalar(AllocId(1).0x0) : &i32; // _3 = const Scalar(AllocId(1).0x0) : &i32; -// _2 = const Scalar(AllocId(1).0x0) : *const i32; +// _2 = const Value(Scalar(AllocId(1).0x0)) : *const i32; // ... // _1 = move _2 as usize (Misc); // ...