Don't use `ty::Const` without immediately interning

This commit is contained in:
Oliver Scherer 2019-04-03 15:29:31 +02:00
parent 9d82107f16
commit e694b63cd1
28 changed files with 114 additions and 182 deletions

View File

@ -38,7 +38,7 @@ impl ErrorHandled {
}
pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
pub type ConstEvalResult<'tcx> = Result<ty::Const<'tcx>, ErrorHandled>;
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct ConstEvalErr<'tcx> {

View File

@ -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),
})
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<Self::Lifted> {
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]

View File

@ -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(..), _)

View File

@ -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<Self::Lifted> {
Some(ConstVid {
index: self.index,
phantom: PhantomData,
})
}
}
///////////////////////////////////////////////////////////////////////////
// TypeFoldable implementations.
//

View File

@ -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))
}

View File

@ -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<ty::Const<'tcx>, 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<ty::Const<'tcx>, ErrorHandled>,
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
) -> (Bx::Value, Ty<'tcx>) {
constant
.map(|c| {

View File

@ -67,7 +67,7 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
bx: &mut Bx,
val: ty::Const<'tcx>
val: &'tcx ty::Const<'tcx>
) -> Result<Self, ErrorHandled> {
let layout = bx.layout_of(val.ty);

View File

@ -737,12 +737,12 @@ enum TestKind<'tcx> {
SwitchInt {
switch_ty: Ty<'tcx>,
options: Vec<u128>,
indices: FxHashMap<ty::Const<'tcx>, usize>,
indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
},
// test for equality
Eq {
value: ty::Const<'tcx>,
value: &'tcx ty::Const<'tcx>,
ty: Ty<'tcx>,
},

View File

@ -98,7 +98,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
candidate: &Candidate<'pat, 'tcx>,
switch_ty: Ty<'tcx>,
options: &mut Vec<u128>,
indices: &mut FxHashMap<ty::Const<'tcx>, 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<bool> {
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<ty::Const<'tcx>, usize>,
indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>,
) -> Option<bool> {
for &val in indices.keys() {
if self.const_range_contains(range, val)? {

View File

@ -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)
}

View File

@ -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<VariantIdx>,
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);

View File

@ -14,7 +14,7 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>(
tcx: TyCtxt<'a, 'gcx, 'tcx>,
ty: Ty<'tcx>,
neg: bool,
) -> Result<ty::Const<'tcx>, 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>(

View File

@ -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,
}
}

View File

@ -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));
}
}

View File

@ -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<Pattern<'tcx>>,
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,

View File

@ -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<Ordering> {
trace!("compare_const_vals: {:?}, {:?}", a, b);

View File

@ -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<TyLayout<'tcx>>,
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
let op = match val.val {

View File

@ -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<MonoItem<'tcx>>,
) {

View File

@ -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])
}

View File

@ -282,7 +282,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
c: &Constant<'tcx>,
) -> Option<Const<'tcx>> {
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)
},

View File

@ -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),
})))
}

View File

@ -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,

View File

@ -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()),
})
}

View File

@ -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
}

View File

@ -2956,7 +2956,7 @@ impl<'tcx> Clean<Type> 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
},