Skip to content

Commit e694b63

Browse files
committed
Don't use ty::Const without immediately interning
1 parent 9d82107 commit e694b63

File tree

28 files changed

+114
-182
lines changed

28 files changed

+114
-182
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl ErrorHandled {
3838
}
3939

4040
pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
41-
pub type ConstEvalResult<'tcx> = Result<ty::Const<'tcx>, ErrorHandled>;
41+
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
4242

4343
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
4444
pub struct ConstEvalErr<'tcx> {

src/librustc/mir/mod.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,25 +1660,25 @@ impl<'tcx> TerminatorKind<'tcx> {
16601660
switch_ty,
16611661
..
16621662
} => {
1663-
let size = ty::tls::with(|tcx| {
1663+
ty::tls::with(|tcx| {
16641664
let param_env = ty::ParamEnv::empty();
16651665
let switch_ty = tcx.lift_to_global(&switch_ty).unwrap();
1666-
tcx.layout_of(param_env.and(switch_ty)).unwrap().size
1667-
});
1668-
values
1669-
.iter()
1670-
.map(|&u| {
1671-
(&ty::Const {
1672-
val: ConstValue::Scalar(
1673-
Scalar::Bits {
1674-
bits: u,
1675-
size: size.bytes() as u8,
1676-
}.into(),
1677-
),
1678-
ty: switch_ty,
1679-
}).to_string().into()
1680-
}).chain(iter::once("otherwise".into()))
1681-
.collect()
1666+
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
1667+
values
1668+
.iter()
1669+
.map(|&u| {
1670+
tcx.mk_const(ty::Const {
1671+
val: ConstValue::Scalar(
1672+
Scalar::Bits {
1673+
bits: u,
1674+
size: size.bytes() as u8,
1675+
}.into(),
1676+
),
1677+
ty: switch_ty,
1678+
}).to_string().into()
1679+
}).chain(iter::once("otherwise".into()))
1680+
.collect()
1681+
})
16821682
}
16831683
Call {
16841684
destination: Some(_),
@@ -2326,9 +2326,7 @@ impl<'tcx> Operand<'tcx> {
23262326
span,
23272327
ty,
23282328
user_ty: None,
2329-
literal: tcx.mk_const(
2330-
ty::Const::zero_sized(ty),
2331-
),
2329+
literal: ty::Const::zero_sized(tcx, ty),
23322330
})
23332331
}
23342332

src/librustc/traits/project.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
412412
};
413413
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
414414
let substs = tcx.lift_to_global(&substs).unwrap();
415-
let evaluated = tcx.mk_const(evaluated);
416415
let evaluated = evaluated.subst(tcx, substs);
417416
return evaluated;
418417
}
@@ -426,7 +425,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
426425
promoted: None
427426
};
428427
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
429-
return tcx.mk_const(evaluated);
428+
return evaluated;
430429
}
431430
}
432431
}

src/librustc/traits/query/normalize.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
202202
};
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204204
let substs = tcx.lift_to_global(&substs).unwrap();
205-
let evaluated = tcx.mk_const(evaluated);
206205
let evaluated = evaluated.subst(tcx, substs);
207206
return evaluated;
208207
}
@@ -216,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
216215
promoted: None,
217216
};
218217
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
219-
return tcx.mk_const(evaluated);
218+
return evaluated;
220219
}
221220
}
222221
}

src/librustc/ty/context.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::middle::lang_items;
2424
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2525
use crate::middle::stability;
2626
use crate::mir::{self, Mir, interpret, ProjectionKind};
27-
use crate::mir::interpret::{ConstValue, Allocation};
27+
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
2828
use crate::ty::subst::{Kind, InternalSubsts, SubstsRef, Subst};
2929
use crate::ty::ReprOptions;
3030
use crate::traits;
@@ -1000,7 +1000,10 @@ impl<'tcx> CommonConsts<'tcx> {
10001000
};
10011001

10021002
CommonConsts {
1003-
err: mk_const(ty::Const::zero_sized(types.err)),
1003+
err: mk_const(ty::Const {
1004+
val: ConstValue::Scalar(Scalar::Bits { bits: 0, size: 0 }),
1005+
ty: types.err,
1006+
}),
10041007
}
10051008
}
10061009
}
@@ -1822,14 +1825,6 @@ nop_list_lift!{ProjectionKind => ProjectionKind}
18221825
// this is the impl for `&'a InternalSubsts<'a>`
18231826
nop_list_lift!{Kind<'a> => Kind<'tcx>}
18241827

1825-
impl<'a, 'tcx> Lift<'tcx> for &'a mir::interpret::Allocation {
1826-
type Lifted = &'tcx mir::interpret::Allocation;
1827-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
1828-
assert!(tcx.global_arenas.const_allocs.in_arena(*self as *const _));
1829-
Some(unsafe { mem::transmute(*self) })
1830-
}
1831-
}
1832-
18331828
pub mod tls {
18341829
use super::{GlobalCtxt, TyCtxt, ptr_eq};
18351830

@@ -2594,9 +2589,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25942589

25952590
#[inline]
25962591
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2597-
self.mk_ty(Array(ty, self.mk_const(
2598-
ty::Const::from_usize(self.global_tcx(), n)
2599-
)))
2592+
self.mk_ty(Array(ty, ty::Const::from_usize(self.global_tcx(), n)))
26002593
}
26012594

26022595
#[inline]

src/librustc/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ define_print_and_forward_display! {
15361536
p!(print_def_path(self.def_id, self.substs));
15371537
}
15381538

1539-
ty::Const<'tcx> {
1539+
&'tcx ty::Const<'tcx> {
15401540
match (self.val, &self.ty.sty) {
15411541
| (ConstValue::Unevaluated(..), _)
15421542
| (ConstValue::Infer(..), _)

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
use crate::hir::def::Namespace;
77
use crate::mir::ProjectionKind;
88
use crate::mir::interpret::ConstValue;
9-
use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid, InferConst};
9+
use crate::ty::{self, Lift, Ty, TyCtxt, InferConst};
1010
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1111
use crate::ty::print::{FmtPrinter, Printer};
1212
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1313
use smallvec::SmallVec;
1414
use crate::mir::interpret;
1515

1616
use std::fmt;
17-
use std::marker::PhantomData;
1817
use std::rc::Rc;
1918

2019
impl fmt::Debug for ty::GenericParamDef {
@@ -295,9 +294,6 @@ CloneTypeFoldableAndLiftImpls! {
295294
(),
296295
bool,
297296
usize,
298-
u32,
299-
crate::ty::BoundVar,
300-
crate::ty::DebruijnIndex,
301297
crate::ty::layout::VariantIdx,
302298
u64,
303299
String,
@@ -314,8 +310,6 @@ CloneTypeFoldableAndLiftImpls! {
314310
::rustc_target::spec::abi::Abi,
315311
crate::mir::Local,
316312
crate::mir::Promoted,
317-
crate::mir::interpret::Scalar,
318-
crate::mir::interpret::Pointer,
319313
crate::traits::Reveal,
320314
crate::ty::adjustment::AutoBorrowMutability,
321315
crate::ty::AdtKind,
@@ -793,44 +787,6 @@ BraceStructLiftImpl! {
793787
}
794788
}
795789

796-
BraceStructLiftImpl! {
797-
impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
798-
type Lifted = ty::Const<'tcx>;
799-
val, ty
800-
}
801-
}
802-
803-
EnumLiftImpl! {
804-
impl<'a, 'tcx> Lift<'tcx> for interpret::ConstValue<'a> {
805-
type Lifted = interpret::ConstValue<'tcx>;
806-
(interpret::ConstValue::Unevaluated)(a, b),
807-
(interpret::ConstValue::Param)(a),
808-
(interpret::ConstValue::Infer)(a),
809-
(interpret::ConstValue::Scalar)(a),
810-
(interpret::ConstValue::Slice)(a, b),
811-
(interpret::ConstValue::ByRef)(a, b),
812-
}
813-
}
814-
815-
EnumLiftImpl! {
816-
impl<'a, 'tcx> Lift<'tcx> for ty::InferConst<'a> {
817-
type Lifted = ty::InferConst<'tcx>;
818-
(ty::InferConst::Var)(a),
819-
(ty::InferConst::Fresh)(a),
820-
(ty::InferConst::Canonical)(a, b),
821-
}
822-
}
823-
824-
impl<'a, 'tcx> Lift<'tcx> for ConstVid<'a> {
825-
type Lifted = ConstVid<'tcx>;
826-
fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
827-
Some(ConstVid {
828-
index: self.index,
829-
phantom: PhantomData,
830-
})
831-
}
832-
}
833-
834790
///////////////////////////////////////////////////////////////////////////
835791
// TypeFoldable implementations.
836792
//

src/librustc/ty/sty.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,42 +2212,43 @@ static_assert_size!(Const<'_>, 48);
22122212
impl<'tcx> Const<'tcx> {
22132213
#[inline]
22142214
pub fn from_scalar(
2215+
tcx: TyCtxt<'_, '_, 'tcx>,
22152216
val: Scalar,
22162217
ty: Ty<'tcx>,
2217-
) -> Self {
2218-
Self {
2218+
) -> &'tcx Self {
2219+
tcx.mk_const(Self {
22192220
val: ConstValue::Scalar(val),
22202221
ty,
2221-
}
2222+
})
22222223
}
22232224

22242225
#[inline]
22252226
pub fn from_bits(
22262227
tcx: TyCtxt<'_, '_, 'tcx>,
22272228
bits: u128,
22282229
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
2229-
) -> Self {
2230+
) -> &'tcx Self {
22302231
let ty = tcx.lift_to_global(&ty).unwrap();
22312232
let size = tcx.layout_of(ty).unwrap_or_else(|e| {
22322233
panic!("could not compute layout for {:?}: {:?}", ty, e)
22332234
}).size;
22342235
let truncated = truncate(bits, size);
22352236
assert_eq!(truncated, bits, "from_bits called with untruncated value");
2236-
Self::from_scalar(Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value)
2237+
Self::from_scalar(tcx, Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value)
22372238
}
22382239

22392240
#[inline]
2240-
pub fn zero_sized(ty: Ty<'tcx>) -> Self {
2241-
Self::from_scalar(Scalar::Bits { bits: 0, size: 0 }, ty)
2241+
pub fn zero_sized(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
2242+
Self::from_scalar(tcx, Scalar::Bits { bits: 0, size: 0 }, ty)
22422243
}
22432244

22442245
#[inline]
2245-
pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> Self {
2246+
pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> &'tcx Self {
22462247
Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool))
22472248
}
22482249

22492250
#[inline]
2250-
pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> Self {
2251+
pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> &'tcx Self {
22512252
Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize))
22522253
}
22532254

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1313
pub fn eval_mir_constant(
1414
&mut self,
1515
constant: &mir::Constant<'tcx>,
16-
) -> Result<ty::Const<'tcx>, ErrorHandled> {
16+
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
1717
match constant.literal.val {
1818
mir::interpret::ConstValue::Unevaluated(def_id, ref substs) => {
1919
let substs = self.monomorphize(substs);
@@ -26,7 +26,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2626
};
2727
self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid))
2828
},
29-
_ => Ok(*self.monomorphize(&constant.literal)),
29+
_ => Ok(self.monomorphize(&constant.literal)),
3030
}
3131
}
3232

@@ -36,7 +36,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3636
bx: &Bx,
3737
span: Span,
3838
ty: Ty<'tcx>,
39-
constant: Result<ty::Const<'tcx>, ErrorHandled>,
39+
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
4040
) -> (Bx::Value, Ty<'tcx>) {
4141
constant
4242
.map(|c| {

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
6767

6868
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
6969
bx: &mut Bx,
70-
val: ty::Const<'tcx>
70+
val: &'tcx ty::Const<'tcx>
7171
) -> Result<Self, ErrorHandled> {
7272
let layout = bx.layout_of(val.ty);
7373

0 commit comments

Comments
 (0)