From e694b63cd147512ad5e73ff4f9bee057e0c27c19 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Wed, 3 Apr 2019 15:29:31 +0200 Subject: [PATCH] Don't use `ty::Const` without immediately interning --- src/librustc/mir/interpret/error.rs | 2 +- src/librustc/mir/mod.rs | 38 ++++++++------- src/librustc/traits/project.rs | 3 +- src/librustc/traits/query/normalize.rs | 3 +- src/librustc/ty/context.rs | 19 +++----- src/librustc/ty/print/pretty.rs | 2 +- src/librustc/ty/structural_impls.rs | 46 +------------------ src/librustc/ty/sty.rs | 19 ++++---- src/librustc_codegen_ssa/mir/constant.rs | 6 +-- src/librustc_codegen_ssa/mir/operand.rs | 2 +- src/librustc_mir/build/matches/mod.rs | 4 +- src/librustc_mir/build/matches/test.rs | 11 ++--- src/librustc_mir/build/misc.rs | 4 +- src/librustc_mir/const_eval.rs | 14 +++--- src/librustc_mir/hair/constant.rs | 8 ++-- src/librustc_mir/hair/cx/expr.rs | 23 ++++------ src/librustc_mir/hair/cx/mod.rs | 12 ++--- src/librustc_mir/hair/pattern/_match.rs | 12 ++--- src/librustc_mir/hair/pattern/mod.rs | 24 +++++----- src/librustc_mir/interpret/operand.rs | 4 +- src/librustc_mir/monomorphize/collector.rs | 4 +- src/librustc_mir/shim.rs | 12 ++--- src/librustc_mir/transform/const_prop.rs | 2 +- src/librustc_mir/transform/elaborate_drops.rs | 4 +- src/librustc_mir/transform/generator.rs | 4 +- src/librustc_mir/util/elaborate_drops.rs | 4 +- src/librustc_typeck/check/mod.rs | 2 +- src/librustdoc/clean/mod.rs | 8 +++- 28 files changed, 114 insertions(+), 182 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 5c6fc6f49f0..9c2fd399ee0 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -38,7 +38,7 @@ impl ErrorHandled { } pub type ConstEvalRawResult<'tcx> = Result, ErrorHandled>; -pub type ConstEvalResult<'tcx> = Result, ErrorHandled>; +pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>; #[derive(Clone, Debug, RustcEncodable, RustcDecodable)] pub struct ConstEvalErr<'tcx> { diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index e44493dec2c..84aff8101a0 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -1660,25 +1660,25 @@ impl<'tcx> TerminatorKind<'tcx> { switch_ty, .. } => { - let size = ty::tls::with(|tcx| { + ty::tls::with(|tcx| { let param_env = ty::ParamEnv::empty(); let switch_ty = tcx.lift_to_global(&switch_ty).unwrap(); - tcx.layout_of(param_env.and(switch_ty)).unwrap().size - }); - values - .iter() - .map(|&u| { - (&ty::Const { - val: ConstValue::Scalar( - Scalar::Bits { - bits: u, - size: size.bytes() as u8, - }.into(), - ), - ty: switch_ty, - }).to_string().into() - }).chain(iter::once("otherwise".into())) - .collect() + let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; + values + .iter() + .map(|&u| { + tcx.mk_const(ty::Const { + val: ConstValue::Scalar( + Scalar::Bits { + bits: u, + size: size.bytes() as u8, + }.into(), + ), + ty: switch_ty, + }).to_string().into() + }).chain(iter::once("otherwise".into())) + .collect() + }) } Call { destination: Some(_), @@ -2326,9 +2326,7 @@ impl<'tcx> Operand<'tcx> { span, ty, user_ty: None, - literal: tcx.mk_const( - ty::Const::zero_sized(ty), - ), + literal: ty::Const::zero_sized(tcx, ty), }) } diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs index 67e76f7625c..b1c4e409112 100644 --- a/src/librustc/traits/project.rs +++ b/src/librustc/traits/project.rs @@ -412,7 +412,6 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a, }; if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { let substs = tcx.lift_to_global(&substs).unwrap(); - let evaluated = tcx.mk_const(evaluated); let evaluated = evaluated.subst(tcx, substs); return evaluated; } @@ -426,7 +425,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a, promoted: None }; if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { - return tcx.mk_const(evaluated); + return evaluated; } } } diff --git a/src/librustc/traits/query/normalize.rs b/src/librustc/traits/query/normalize.rs index 9940249da8b..d09a9c10786 100644 --- a/src/librustc/traits/query/normalize.rs +++ b/src/librustc/traits/query/normalize.rs @@ -202,7 +202,6 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx }; if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { let substs = tcx.lift_to_global(&substs).unwrap(); - let evaluated = tcx.mk_const(evaluated); let evaluated = evaluated.subst(tcx, substs); return evaluated; } @@ -216,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx promoted: None, }; if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { - return tcx.mk_const(evaluated); + return evaluated; } } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index d83b2ce842a..a56fe7d7003 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -24,7 +24,7 @@ use crate::middle::lang_items; use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault}; use crate::middle::stability; use crate::mir::{self, Mir, interpret, ProjectionKind}; -use crate::mir::interpret::{ConstValue, Allocation}; +use crate::mir::interpret::{ConstValue, Allocation, Scalar}; use crate::ty::subst::{Kind, InternalSubsts, SubstsRef, Subst}; use crate::ty::ReprOptions; use crate::traits; @@ -1000,7 +1000,10 @@ impl<'tcx> CommonConsts<'tcx> { }; CommonConsts { - err: mk_const(ty::Const::zero_sized(types.err)), + err: mk_const(ty::Const { + val: ConstValue::Scalar(Scalar::Bits { bits: 0, size: 0 }), + ty: types.err, + }), } } } @@ -1822,14 +1825,6 @@ nop_list_lift!{ProjectionKind => ProjectionKind} // this is the impl for `&'a InternalSubsts<'a>` nop_list_lift!{Kind<'a> => Kind<'tcx>} -impl<'a, 'tcx> Lift<'tcx> for &'a mir::interpret::Allocation { - type Lifted = &'tcx mir::interpret::Allocation; - fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option { - assert!(tcx.global_arenas.const_allocs.in_arena(*self as *const _)); - Some(unsafe { mem::transmute(*self) }) - } -} - pub mod tls { use super::{GlobalCtxt, TyCtxt, ptr_eq}; @@ -2594,9 +2589,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { #[inline] pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> { - self.mk_ty(Array(ty, self.mk_const( - ty::Const::from_usize(self.global_tcx(), n) - ))) + self.mk_ty(Array(ty, ty::Const::from_usize(self.global_tcx(), n))) } #[inline] diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index ca89346b4b4..7b363ad6ac4 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -1536,7 +1536,7 @@ define_print_and_forward_display! { p!(print_def_path(self.def_id, self.substs)); } - ty::Const<'tcx> { + &'tcx ty::Const<'tcx> { match (self.val, &self.ty.sty) { | (ConstValue::Unevaluated(..), _) | (ConstValue::Infer(..), _) diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 35d0359dbcf..86883a14f68 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -6,7 +6,7 @@ use crate::hir::def::Namespace; use crate::mir::ProjectionKind; use crate::mir::interpret::ConstValue; -use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid, InferConst}; +use crate::ty::{self, Lift, Ty, TyCtxt, InferConst}; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::print::{FmtPrinter, Printer}; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; @@ -14,7 +14,6 @@ use smallvec::SmallVec; use crate::mir::interpret; use std::fmt; -use std::marker::PhantomData; use std::rc::Rc; impl fmt::Debug for ty::GenericParamDef { @@ -295,9 +294,6 @@ CloneTypeFoldableAndLiftImpls! { (), bool, usize, - u32, - crate::ty::BoundVar, - crate::ty::DebruijnIndex, crate::ty::layout::VariantIdx, u64, String, @@ -314,8 +310,6 @@ CloneTypeFoldableAndLiftImpls! { ::rustc_target::spec::abi::Abi, crate::mir::Local, crate::mir::Promoted, - crate::mir::interpret::Scalar, - crate::mir::interpret::Pointer, crate::traits::Reveal, crate::ty::adjustment::AutoBorrowMutability, crate::ty::AdtKind, @@ -793,44 +787,6 @@ BraceStructLiftImpl! { } } -BraceStructLiftImpl! { - impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> { - type Lifted = ty::Const<'tcx>; - val, ty - } -} - -EnumLiftImpl! { - impl<'a, 'tcx> Lift<'tcx> for interpret::ConstValue<'a> { - type Lifted = interpret::ConstValue<'tcx>; - (interpret::ConstValue::Unevaluated)(a, b), - (interpret::ConstValue::Param)(a), - (interpret::ConstValue::Infer)(a), - (interpret::ConstValue::Scalar)(a), - (interpret::ConstValue::Slice)(a, b), - (interpret::ConstValue::ByRef)(a, b), - } -} - -EnumLiftImpl! { - impl<'a, 'tcx> Lift<'tcx> for ty::InferConst<'a> { - type Lifted = ty::InferConst<'tcx>; - (ty::InferConst::Var)(a), - (ty::InferConst::Fresh)(a), - (ty::InferConst::Canonical)(a, b), - } -} - -impl<'a, 'tcx> Lift<'tcx> for ConstVid<'a> { - type Lifted = ConstVid<'tcx>; - fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option { - Some(ConstVid { - index: self.index, - phantom: PhantomData, - }) - } -} - /////////////////////////////////////////////////////////////////////////// // TypeFoldable implementations. // diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 0b5a6fce5cb..b2ac40f845a 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -2212,13 +2212,14 @@ static_assert_size!(Const<'_>, 48); impl<'tcx> Const<'tcx> { #[inline] pub fn from_scalar( + tcx: TyCtxt<'_, '_, 'tcx>, val: Scalar, ty: Ty<'tcx>, - ) -> Self { - Self { + ) -> &'tcx Self { + tcx.mk_const(Self { val: ConstValue::Scalar(val), ty, - } + }) } #[inline] @@ -2226,28 +2227,28 @@ impl<'tcx> Const<'tcx> { tcx: TyCtxt<'_, '_, 'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>, - ) -> Self { + ) -> &'tcx Self { let ty = tcx.lift_to_global(&ty).unwrap(); let size = tcx.layout_of(ty).unwrap_or_else(|e| { panic!("could not compute layout for {:?}: {:?}", ty, e) }).size; let truncated = truncate(bits, size); assert_eq!(truncated, bits, "from_bits called with untruncated value"); - Self::from_scalar(Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value) + Self::from_scalar(tcx, Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value) } #[inline] - pub fn zero_sized(ty: Ty<'tcx>) -> Self { - Self::from_scalar(Scalar::Bits { bits: 0, size: 0 }, ty) + pub fn zero_sized(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) -> &'tcx Self { + Self::from_scalar(tcx, Scalar::Bits { bits: 0, size: 0 }, ty) } #[inline] - pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> Self { + pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> &'tcx Self { Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool)) } #[inline] - pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> Self { + pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> &'tcx Self { Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize)) } diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs index 3f8dc420cf4..eaa1dd186a5 100644 --- a/src/librustc_codegen_ssa/mir/constant.rs +++ b/src/librustc_codegen_ssa/mir/constant.rs @@ -13,7 +13,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { pub fn eval_mir_constant( &mut self, constant: &mir::Constant<'tcx>, - ) -> Result, ErrorHandled> { + ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> { match constant.literal.val { mir::interpret::ConstValue::Unevaluated(def_id, ref substs) => { let substs = self.monomorphize(substs); @@ -26,7 +26,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { }; self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid)) }, - _ => Ok(*self.monomorphize(&constant.literal)), + _ => Ok(self.monomorphize(&constant.literal)), } } @@ -36,7 +36,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { bx: &Bx, span: Span, ty: Ty<'tcx>, - constant: Result, ErrorHandled>, + constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>, ) -> (Bx::Value, Ty<'tcx>) { constant .map(|c| { diff --git a/src/librustc_codegen_ssa/mir/operand.rs b/src/librustc_codegen_ssa/mir/operand.rs index 3db05b7214d..75f0c6a6c53 100644 --- a/src/librustc_codegen_ssa/mir/operand.rs +++ b/src/librustc_codegen_ssa/mir/operand.rs @@ -67,7 +67,7 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> { pub fn from_const>( bx: &mut Bx, - val: ty::Const<'tcx> + val: &'tcx ty::Const<'tcx> ) -> Result { let layout = bx.layout_of(val.ty); diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index 58ca35abcb1..8c2ef082c33 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -737,12 +737,12 @@ enum TestKind<'tcx> { SwitchInt { switch_ty: Ty<'tcx>, options: Vec, - indices: FxHashMap, usize>, + indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>, }, // test for equality Eq { - value: ty::Const<'tcx>, + value: &'tcx ty::Const<'tcx>, ty: Ty<'tcx>, }, diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index a5834b02ffc..0995a2f7fdf 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -98,7 +98,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { candidate: &Candidate<'pat, 'tcx>, switch_ty: Ty<'tcx>, options: &mut Vec, - indices: &mut FxHashMap, usize>) + indices: &mut FxHashMap<&'tcx ty::Const<'tcx>, usize>) -> bool { let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) { @@ -305,7 +305,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { } let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap(); let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]); - let method = self.hir.tcx().mk_const(method); let re_erased = self.hir.tcx().lifetimes.re_erased; // take the argument by reference @@ -371,8 +370,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { TestKind::Range(PatternRange { ref lo, ref hi, ty, ref end }) => { // Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons. - let lo = self.literal_operand(test.span, ty.clone(), lo.clone()); - let hi = self.literal_operand(test.span, ty.clone(), hi.clone()); + let lo = self.literal_operand(test.span, ty, lo); + let hi = self.literal_operand(test.span, ty, hi); let val = Operand::Copy(place.clone()); let fail = self.cfg.start_new_block(); @@ -724,7 +723,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { fn const_range_contains( &self, range: PatternRange<'tcx>, - value: ty::Const<'tcx>, + value: &'tcx ty::Const<'tcx>, ) -> Option { use std::cmp::Ordering::*; @@ -744,7 +743,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { fn values_not_contained_in_range( &self, range: PatternRange<'tcx>, - indices: &FxHashMap, usize>, + indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>, ) -> Option { for &val in indices.keys() { if self.const_range_contains(range, val)? { diff --git a/src/librustc_mir/build/misc.rs b/src/librustc_mir/build/misc.rs index d71a13dec5a..daf59d7a135 100644 --- a/src/librustc_mir/build/misc.rs +++ b/src/librustc_mir/build/misc.rs @@ -27,13 +27,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { pub fn literal_operand(&mut self, span: Span, ty: Ty<'tcx>, - literal: ty::Const<'tcx>) + literal: &'tcx ty::Const<'tcx>) -> Operand<'tcx> { let constant = box Constant { span, ty, user_ty: None, - literal: self.hir.tcx().mk_const(literal), + literal, }; Operand::Constant(constant) } diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 866b6492d10..2bfc10f8e8c 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -65,7 +65,7 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>( fn mplace_to_const<'tcx>( ecx: &CompileTimeEvalContext<'_, '_, 'tcx>, mplace: MPlaceTy<'tcx>, -) -> ty::Const<'tcx> { +) -> &'tcx ty::Const<'tcx> { let MemPlace { ptr, align, meta } = *mplace; // extract alloc-offset pair assert!(meta.is_none()); @@ -79,13 +79,13 @@ fn mplace_to_const<'tcx>( // interned this? I thought that is the entire point of that `FinishStatic` stuff? let alloc = ecx.tcx.intern_const_alloc(alloc); let val = ConstValue::ByRef(ptr, alloc); - ty::Const { val, ty: mplace.layout.ty } + ecx.tcx.mk_const(ty::Const { val, ty: mplace.layout.ty }) } fn op_to_const<'tcx>( ecx: &CompileTimeEvalContext<'_, '_, 'tcx>, op: OpTy<'tcx>, -) -> ty::Const<'tcx> { +) -> &'tcx ty::Const<'tcx> { // We do not normalize just any data. Only non-union scalars and slices. let normalize = match op.layout.abi { layout::Abi::Scalar(..) => op.layout.ty.ty_adt_def().map_or(true, |adt| !adt.is_union()), @@ -104,7 +104,7 @@ fn op_to_const<'tcx>( Err(Immediate::ScalarPair(a, b)) => ConstValue::Slice(a.not_undef().unwrap(), b.to_usize(ecx).unwrap()), }; - ty::Const { val, ty: op.layout.ty } + ecx.tcx.mk_const(ty::Const { val, ty: op.layout.ty }) } // Returns a pointer to where the result lives @@ -450,8 +450,8 @@ pub fn const_field<'a, 'tcx>( param_env: ty::ParamEnv<'tcx>, variant: Option, field: mir::Field, - value: ty::Const<'tcx>, -) -> ty::Const<'tcx> { + value: &'tcx ty::Const<'tcx>, +) -> &'tcx ty::Const<'tcx> { trace!("const_field: {:?}, {:?}", field, value); let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env); // get the operand again @@ -473,7 +473,7 @@ pub fn const_field<'a, 'tcx>( pub fn const_variant_index<'a, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, param_env: ty::ParamEnv<'tcx>, - val: ty::Const<'tcx>, + val: &'tcx ty::Const<'tcx>, ) -> VariantIdx { trace!("const_variant_index: {:?}", val); let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env); diff --git a/src/librustc_mir/hair/constant.rs b/src/librustc_mir/hair/constant.rs index caadc6055b5..5ed16abb9a6 100644 --- a/src/librustc_mir/hair/constant.rs +++ b/src/librustc_mir/hair/constant.rs @@ -14,7 +14,7 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>( tcx: TyCtxt<'a, 'gcx, 'tcx>, ty: Ty<'tcx>, neg: bool, -) -> Result, LitToConstError> { +) -> Result<&'tcx ty::Const<'tcx>, LitToConstError> { use syntax::ast::*; let trunc = |n| { @@ -39,10 +39,10 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>( LitKind::Err(ref s) => { let s = s.as_str(); let id = tcx.allocate_bytes(s.as_bytes()); - return Ok(ty::Const { + return Ok(tcx.mk_const(ty::Const { val: ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64), ty: tcx.types.err, - }); + })); }, LitKind::ByteStr(ref data) => { let id = tcx.allocate_bytes(data); @@ -71,7 +71,7 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>( LitKind::Bool(b) => ConstValue::Scalar(Scalar::from_bool(b)), LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)), }; - Ok(ty::Const { val: lit, ty }) + Ok(tcx.mk_const(ty::Const { val: 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 d623f149988..ca457b0ac7c 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -329,9 +329,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, } hir::ExprKind::Lit(ref lit) => ExprKind::Literal { - literal: cx.tcx.mk_const( - cx.const_eval_literal(&lit.node, expr_ty, lit.span, false) - ), + literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, false), user_ty: None, }, @@ -429,9 +427,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, } else { if let hir::ExprKind::Lit(ref lit) = arg.node { ExprKind::Literal { - literal: cx.tcx.mk_const( - cx.const_eval_literal(&lit.node, expr_ty, lit.span, true) - ), + literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, true), user_ty: None, } } else { @@ -680,7 +676,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, ty: var_ty, span: expr.span, kind: ExprKind::Literal { - literal: cx.tcx.mk_const(literal), + literal, user_ty: None }, }.to_ref(); @@ -694,10 +690,10 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, // in case we are offsetting from a computed discriminant // and not the beginning of discriminants (which is always `0`) let substs = InternalSubsts::identity_for_item(cx.tcx(), did); - let lhs = mk_const(ty::Const { + let lhs = mk_const(cx.tcx().mk_const(ty::Const { val: ConstValue::Unevaluated(did, substs), ty: var_ty, - }); + })); let bin = ExprKind::Binary { op: BinOp::Add, lhs, @@ -837,9 +833,7 @@ fn method_callee<'a, 'gcx, 'tcx>( ty, span, kind: ExprKind::Literal { - literal: cx.tcx().mk_const( - ty::Const::zero_sized(ty) - ), + literal: ty::Const::zero_sized(cx.tcx(), ty), user_ty, }, } @@ -902,9 +896,10 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, let user_ty = user_substs_applied_to_res(cx, expr.hir_id, res); debug!("convert_path_expr: user_ty={:?}", user_ty); ExprKind::Literal { - literal: cx.tcx.mk_const(ty::Const::zero_sized( + literal: ty::Const::zero_sized( + cx.tcx, cx.tables().node_type(expr.hir_id), - )), + ), user_ty, } } diff --git a/src/librustc_mir/hair/cx/mod.rs b/src/librustc_mir/hair/cx/mod.rs index e8070b21bb8..c993a02a6de 100644 --- a/src/librustc_mir/hair/cx/mod.rs +++ b/src/librustc_mir/hair/cx/mod.rs @@ -106,7 +106,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { } pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> { - self.tcx.mk_const(ty::Const::from_usize(self.tcx, value)) + ty::Const::from_usize(self.tcx, value) } pub fn bool_ty(&mut self) -> Ty<'tcx> { @@ -118,11 +118,11 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { } pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> { - self.tcx.mk_const(ty::Const::from_bool(self.tcx, true)) + ty::Const::from_bool(self.tcx, true) } pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> { - self.tcx.mk_const(ty::Const::from_bool(self.tcx, false)) + ty::Const::from_bool(self.tcx, false) } pub fn const_eval_literal( @@ -131,7 +131,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { ty: Ty<'tcx>, sp: Span, neg: bool, - ) -> ty::Const<'tcx> { + ) -> &'tcx ty::Const<'tcx> { trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg); match lit_to_const(lit, self.tcx, ty, neg) { @@ -166,14 +166,14 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { method_name: &str, self_ty: Ty<'tcx>, params: &[Kind<'tcx>]) - -> (Ty<'tcx>, ty::Const<'tcx>) { + -> (Ty<'tcx>, &'tcx ty::Const<'tcx>) { let method_name = Symbol::intern(method_name); let substs = self.tcx.mk_substs_trait(self_ty, params); for item in self.tcx.associated_items(trait_def_id) { if item.kind == ty::AssociatedKind::Method && item.ident.name == method_name { let method_ty = self.tcx.type_of(item.def_id); let method_ty = method_ty.subst(self.tcx, substs); - return (method_ty, ty::Const::zero_sized(method_ty)); + return (method_ty, ty::Const::zero_sized(self.tcx, method_ty)); } } diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index fd4416fc2b7..83e17514c08 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -255,10 +255,10 @@ impl<'a, 'tcx> PatternFolder<'tcx> for LiteralExpander<'a, 'tcx> { subpattern: Pattern { ty: rty, span: pat.span, - kind: box PatternKind::Constant { value: Const { - val: self.fold_const_value_deref(val, rty, crty), + kind: box PatternKind::Constant { value: self.tcx.mk_const(Const { + val: self.fold_const_value_deref(*val, rty, crty), ty: rty, - } }, + }) }, } } } @@ -423,7 +423,7 @@ enum Constructor<'tcx> { /// Enum variants. Variant(DefId), /// Literal values. - ConstantValue(ty::Const<'tcx>), + ConstantValue(&'tcx ty::Const<'tcx>), /// Ranges of literal values (`2...5` and `2..5`). ConstantRange(u128, u128, Ty<'tcx>, RangeEnd), /// Array patterns of length n. @@ -1424,7 +1424,7 @@ fn constructor_sub_pattern_tys<'a, 'tcx: 'a>(cx: &MatchCheckCtxt<'a, 'tcx>, fn slice_pat_covered_by_const<'tcx>( tcx: TyCtxt<'_, 'tcx, '_>, _span: Span, - const_val: ty::Const<'tcx>, + const_val: &'tcx ty::Const<'tcx>, prefix: &[Pattern<'tcx>], slice: &Option>, suffix: &[Pattern<'tcx>] @@ -1824,7 +1824,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>( &cx.tcx, ptr, layout.size, ).ok()?; let scalar = scalar.not_undef().ok()?; - let value = ty::Const::from_scalar(scalar, ty); + let value = ty::Const::from_scalar(cx.tcx, scalar, ty); let pattern = Pattern { ty, span: pat.span, diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index a3e43d64819..4cc42e3ee0e 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -152,7 +152,7 @@ pub enum PatternKind<'tcx> { }, Constant { - value: ty::Const<'tcx>, + value: &'tcx ty::Const<'tcx>, }, Range(PatternRange<'tcx>), @@ -176,8 +176,8 @@ pub enum PatternKind<'tcx> { #[derive(Copy, Clone, Debug, PartialEq)] pub struct PatternRange<'tcx> { - pub lo: ty::Const<'tcx>, - pub hi: ty::Const<'tcx>, + pub lo: &'tcx ty::Const<'tcx>, + pub hi: &'tcx ty::Const<'tcx>, pub ty: Ty<'tcx>, pub end: RangeEnd, } @@ -291,15 +291,15 @@ impl<'tcx> fmt::Display for Pattern<'tcx> { write!(f, "{}", subpattern) } PatternKind::Constant { value } => { - write!(f, "{}", &value) + write!(f, "{}", value) } PatternKind::Range(PatternRange { lo, hi, ty: _, end }) => { - write!(f, "{}", &lo)?; + write!(f, "{}", lo)?; match end { RangeEnd::Included => write!(f, "..=")?, RangeEnd::Excluded => write!(f, "..")?, } - write!(f, "{}", &hi) + write!(f, "{}", hi) } PatternKind::Slice { ref prefix, ref slice, ref suffix } | PatternKind::Array { ref prefix, ref slice, ref suffix } => { @@ -942,7 +942,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> { fn const_to_pat( &self, instance: ty::Instance<'tcx>, - cv: ty::Const<'tcx>, + cv: &'tcx ty::Const<'tcx>, id: hir::HirId, span: Span, ) -> Pattern<'tcx> { @@ -1205,7 +1205,7 @@ impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> { PatternKind::Constant { value } => PatternKind::Constant { - value: value.fold_with(folder) + value, }, PatternKind::Range(PatternRange { lo, @@ -1213,8 +1213,8 @@ impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> { ty, end, }) => PatternKind::Range(PatternRange { - lo: lo.fold_with(folder), - hi: hi.fold_with(folder), + lo, + hi, ty: ty.fold_with(folder), end, }), @@ -1242,8 +1242,8 @@ impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> { pub fn compare_const_vals<'a, 'gcx, 'tcx>( tcx: TyCtxt<'a, 'gcx, 'tcx>, - a: ty::Const<'tcx>, - b: ty::Const<'tcx>, + a: &'tcx ty::Const<'tcx>, + b: &'tcx ty::Const<'tcx>, ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>, ) -> Option { trace!("compare_const_vals: {:?}, {:?}", a, b); diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 9c2b491925f..9481dd3cb8f 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -500,7 +500,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> Move(ref place) => self.eval_place_to_op(place, layout)?, - Constant(ref constant) => self.eval_const_to_op(*constant.literal, layout)?, + Constant(ref constant) => self.eval_const_to_op(constant.literal, layout)?, }; trace!("{:?}: {:?}", mir_op, *op); Ok(op) @@ -520,7 +520,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> // in patterns via the `const_eval` module crate fn eval_const_to_op( &self, - val: ty::Const<'tcx>, + val: &'tcx ty::Const<'tcx>, layout: Option>, ) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> { let op = match val.val { diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index 91cf0fbb9b4..e0d6784789e 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -609,7 +609,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> { fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, location: Location) { debug!("visiting const {:?} @ {:?}", *constant, location); - collect_const(self.tcx, **constant, self.param_substs, self.output); + collect_const(self.tcx, *constant, self.param_substs, self.output); self.super_const(constant); } @@ -1248,7 +1248,7 @@ fn def_id_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn collect_const<'a, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, - constant: ty::Const<'tcx>, + constant: &'tcx ty::Const<'tcx>, param_substs: SubstsRef<'tcx>, output: &mut Vec>, ) { diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index b9224d973fe..8be660ae17a 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -458,9 +458,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { span: self.span, ty: func_ty, user_ty: None, - literal: tcx.mk_const( - ty::Const::zero_sized(func_ty), - ), + literal: ty::Const::zero_sized(tcx, func_ty), }); let ref_loc = self.make_place( @@ -520,9 +518,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { span: self.span, ty: self.tcx.types.usize, user_ty: None, - literal: self.tcx.mk_const( - ty::Const::from_usize(self.tcx, value), - ), + literal: ty::Const::from_usize(self.tcx, value), } } @@ -762,9 +758,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span, ty, user_ty: None, - literal: tcx.mk_const( - ty::Const::zero_sized(ty) - ), + literal: ty::Const::zero_sized(tcx, ty), }), vec![rcvr]) } diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 8f3dd72c4f2..05518ca48d3 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -282,7 +282,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> { c: &Constant<'tcx>, ) -> Option> { self.ecx.tcx.span = c.span; - match self.ecx.eval_const_to_op(*c.literal, None) { + match self.ecx.eval_const_to_op(c.literal, None) { Ok(op) => { Some(op) }, diff --git a/src/librustc_mir/transform/elaborate_drops.rs b/src/librustc_mir/transform/elaborate_drops.rs index 023a61588c4..6320cb44248 100644 --- a/src/librustc_mir/transform/elaborate_drops.rs +++ b/src/librustc_mir/transform/elaborate_drops.rs @@ -533,9 +533,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> { span, ty: self.tcx.types.bool, user_ty: None, - literal: self.tcx.mk_const( - ty::Const::from_bool(self.tcx, val), - ), + literal: ty::Const::from_bool(self.tcx, val), }))) } diff --git a/src/librustc_mir/transform/generator.rs b/src/librustc_mir/transform/generator.rs index 780b3c9686b..ab2f2933a96 100644 --- a/src/librustc_mir/transform/generator.rs +++ b/src/librustc_mir/transform/generator.rs @@ -757,9 +757,7 @@ fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span: mir.span, ty: tcx.types.bool, user_ty: None, - literal: tcx.mk_const( - ty::Const::from_bool(tcx, false), - ), + literal: ty::Const::from_bool(tcx, false), }), expected: true, msg: message, diff --git a/src/librustc_mir/util/elaborate_drops.rs b/src/librustc_mir/util/elaborate_drops.rs index 98ca7c32675..ac5ebc5e251 100644 --- a/src/librustc_mir/util/elaborate_drops.rs +++ b/src/librustc_mir/util/elaborate_drops.rs @@ -975,9 +975,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D> span: self.source_info.span, ty: self.tcx().types.usize, user_ty: None, - literal: self.tcx().mk_const( - ty::Const::from_usize(self.tcx(), val.into()) - ), + literal: ty::Const::from_usize(self.tcx(), val.into()), }) } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 655bf5722ae..b5bb62a0f46 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4441,7 +4441,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if element_ty.references_error() { tcx.types.err } else if let Ok(count) = count { - tcx.mk_ty(ty::Array(t, tcx.mk_const(count))) + tcx.mk_ty(ty::Array(t, count)) } else { tcx.types.err } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f344c529c59..fb0caa55ffb 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2956,7 +2956,7 @@ impl<'tcx> Clean for Ty<'tcx> { ty::Str => Primitive(PrimitiveType::Str), ty::Slice(ty) => Slice(box ty.clean(cx)), ty::Array(ty, n) => { - let mut n = *cx.tcx.lift(&n).expect("array lift failed"); + let mut n = cx.tcx.lift(&n).expect("array lift failed"); if let ConstValue::Unevaluated(def_id, substs) = n.val { let param_env = cx.tcx.param_env(def_id); let cid = GlobalId { @@ -4126,7 +4126,7 @@ fn name_from_pat(p: &hir::Pat) -> String { } } -fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String { +fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String { match n.val { ConstValue::Unevaluated(def_id, _) => { if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) { @@ -4141,6 +4141,10 @@ fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String { if s.ends_with("usize") { let n = s.len() - "usize".len(); s.truncate(n); + if s.ends_with(": ") { + let n = s.len() - ": ".len(); + s.truncate(n); + } } s },