Skip to content

Commit 7f32c88

Browse files
committed
rustc: introduce ty::Const { ConstVal, Ty }.
1 parent 91d76b0 commit 7f32c88

File tree

31 files changed

+409
-194
lines changed

31 files changed

+409
-194
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ for ::middle::const_val::ConstVal<'gcx> {
276276
mem::discriminant(self).hash_stable(hcx, hasher);
277277

278278
match *self {
279-
Float(ref value) => {
279+
Integral(ref value) => {
280280
value.hash_stable(hcx, hasher);
281281
}
282-
Integral(ref value) => {
282+
Float(ref value) => {
283283
value.hash_stable(hcx, hasher);
284284
}
285285
Str(ref value) => {
@@ -324,6 +324,11 @@ impl_stable_hash_for!(struct ::middle::const_val::ByteArray<'tcx> {
324324
data
325325
});
326326

327+
impl_stable_hash_for!(struct ty::Const<'tcx> {
328+
ty,
329+
val
330+
});
331+
327332
impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
328333

329334
impl_stable_hash_for!(struct ty::GeneratorInterior<'tcx> { witness });

src/librustc/middle/const_val.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ use syntax_pos::Span;
3030

3131
use std::borrow::Cow;
3232

33-
pub type EvalResult<'tcx> = Result<&'tcx ConstVal<'tcx>, ConstEvalErr<'tcx>>;
33+
pub type EvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ConstEvalErr<'tcx>>;
3434

3535
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)]
3636
pub enum ConstVal<'tcx> {
37-
Float(ConstFloat),
3837
Integral(ConstInt),
38+
Float(ConstFloat),
3939
Str(InternedString),
4040
ByteStr(ByteArray<'tcx>),
4141
Bool(bool),
@@ -45,8 +45,6 @@ pub enum ConstVal<'tcx> {
4545
Aggregate(ConstAggregate<'tcx>),
4646
}
4747

48-
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx ConstVal<'tcx> {}
49-
5048
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, Eq, PartialEq)]
5149
pub struct ByteArray<'tcx> {
5250
pub data: &'tcx [u8],
@@ -56,10 +54,10 @@ impl<'tcx> serialize::UseSpecializedDecodable for ByteArray<'tcx> {}
5654

5755
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
5856
pub enum ConstAggregate<'tcx> {
59-
Struct(&'tcx [(ast::Name, &'tcx ConstVal<'tcx>)]),
60-
Tuple(&'tcx [&'tcx ConstVal<'tcx>]),
61-
Array(&'tcx [&'tcx ConstVal<'tcx>]),
62-
Repeat(&'tcx ConstVal<'tcx>, u64),
57+
Struct(&'tcx [(ast::Name, &'tcx ty::Const<'tcx>)]),
58+
Tuple(&'tcx [&'tcx ty::Const<'tcx>]),
59+
Array(&'tcx [&'tcx ty::Const<'tcx>]),
60+
Repeat(&'tcx ty::Const<'tcx>, u64),
6361
}
6462

6563
impl<'tcx> Encodable for ConstAggregate<'tcx> {
@@ -259,7 +257,7 @@ pub fn eval_length(tcx: TyCtxt,
259257
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
260258
let substs = Substs::identity_for_item(tcx.global_tcx(), count_def_id);
261259
match tcx.at(count_expr.span).const_eval(param_env.and((count_def_id, substs))) {
262-
Ok(&Integral(Usize(count))) => {
260+
Ok(&ty::Const { val: Integral(Usize(count)), .. }) => {
263261
let val = count.as_u64(tcx.sess.target.uint_type);
264262
assert_eq!(val as usize as u64, val);
265263
Ok(val as usize)

src/librustc/mir/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,15 @@ impl<'tcx> Operand<'tcx> {
11871187
substs: &'tcx Substs<'tcx>,
11881188
span: Span,
11891189
) -> Self {
1190+
let ty = tcx.type_of(def_id).subst(tcx, substs);
11901191
Operand::Constant(box Constant {
11911192
span,
1192-
ty: tcx.type_of(def_id).subst(tcx, substs),
1193+
ty,
11931194
literal: Literal::Value {
1194-
value: tcx.mk_const(ConstVal::Function(def_id, substs))
1195+
value: tcx.mk_const(ty::Const {
1196+
val: ConstVal::Function(def_id, substs),
1197+
ty
1198+
})
11951199
},
11961200
})
11971201
}
@@ -1484,7 +1488,7 @@ pub enum Literal<'tcx> {
14841488
substs: &'tcx Substs<'tcx>,
14851489
},
14861490
Value {
1487-
value: &'tcx ConstVal<'tcx>,
1491+
value: &'tcx ty::Const<'tcx>,
14881492
},
14891493
Promoted {
14901494
// Index into the `promoted` vector of `Mir`.
@@ -1505,9 +1509,9 @@ impl<'tcx> Debug for Literal<'tcx> {
15051509
Item { def_id, substs } => {
15061510
ppaux::parameterized(fmt, substs, def_id, &[])
15071511
}
1508-
Value { ref value } => {
1512+
Value { value } => {
15091513
write!(fmt, "const ")?;
1510-
fmt_const_val(fmt, value)
1514+
fmt_const_val(fmt, &value.val)
15111515
}
15121516
Promoted { index } => {
15131517
write!(fmt, "{:?}", index)

src/librustc/mir/visit.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use middle::const_val::ConstVal;
1211
use hir::def_id::DefId;
1312
use ty::subst::Substs;
1413
use ty::{ClosureSubsts, Region, Ty, GeneratorInterior};
@@ -214,6 +213,18 @@ macro_rules! make_mir_visitor {
214213
self.super_ty(ty);
215214
}
216215

216+
fn visit_region(&mut self,
217+
region: & $($mutability)* ty::Region<'tcx>,
218+
_: Location) {
219+
self.super_region(region);
220+
}
221+
222+
fn visit_const(&mut self,
223+
constant: & $($mutability)* &'tcx ty::Const<'tcx>,
224+
_: Location) {
225+
self.super_const(constant);
226+
}
227+
217228
fn visit_substs(&mut self,
218229
substs: & $($mutability)* &'tcx Substs<'tcx>,
219230
_: Location) {
@@ -232,12 +243,6 @@ macro_rules! make_mir_visitor {
232243
self.super_generator_interior(interior);
233244
}
234245

235-
fn visit_const_val(&mut self,
236-
const_val: & $($mutability)* &'tcx ConstVal<'tcx>,
237-
_: Location) {
238-
self.super_const_val(const_val);
239-
}
240-
241246
fn visit_const_int(&mut self,
242247
const_int: &ConstInt,
243248
_: Location) {
@@ -515,9 +520,10 @@ macro_rules! make_mir_visitor {
515520
self.visit_const_usize(length, location);
516521
}
517522

518-
Rvalue::Ref(r, bk, ref $($mutability)* path) => {
523+
Rvalue::Ref(ref $($mutability)* r, bk, ref $($mutability)* path) => {
524+
self.visit_region(r, location);
519525
self.visit_lvalue(path, LvalueContext::Borrow {
520-
region: r,
526+
region: *r,
521527
kind: bk
522528
}, location);
523529
}
@@ -722,7 +728,7 @@ macro_rules! make_mir_visitor {
722728
self.visit_substs(substs, location);
723729
}
724730
Literal::Value { ref $($mutability)* value } => {
725-
self.visit_const_val(value, location);
731+
self.visit_const(value, location);
726732
}
727733
Literal::Promoted { index: _ } => {}
728734
}
@@ -747,6 +753,12 @@ macro_rules! make_mir_visitor {
747753
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
748754
}
749755

756+
fn super_region(&mut self, _region: & $($mutability)* ty::Region<'tcx>) {
757+
}
758+
759+
fn super_const(&mut self, _const: & $($mutability)* &'tcx ty::Const<'tcx>) {
760+
}
761+
750762
fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {
751763
}
752764

@@ -758,9 +770,6 @@ macro_rules! make_mir_visitor {
758770
_substs: & $($mutability)* ClosureSubsts<'tcx>) {
759771
}
760772

761-
fn super_const_val(&mut self, _const_val: & $($mutability)* &'tcx ConstVal<'tcx>) {
762-
}
763-
764773
fn super_const_int(&mut self, _const_int: &ConstInt) {
765774
}
766775

src/librustc/ty/context.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use hir::map as hir_map;
2121
use hir::map::DefPathHash;
2222
use lint::{self, Lint};
2323
use ich::{self, StableHashingContext, NodeIdHashingMode};
24-
use middle::const_val::ConstVal;
2524
use middle::free_region::FreeRegionMap;
2625
use middle::lang_items;
2726
use middle::resolve_lifetime;
@@ -33,7 +32,7 @@ use ty::ReprOptions;
3332
use traits;
3433
use ty::{self, Ty, TypeAndMut};
3534
use ty::{TyS, TypeVariants, Slice};
36-
use ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorInterior, Region};
35+
use ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorInterior, Region, Const};
3736
use hir::FreevarMap;
3837
use ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predicate};
3938
use ty::RegionKind;
@@ -110,7 +109,7 @@ pub struct CtxtInterners<'tcx> {
110109
region: RefCell<FxHashSet<Interned<'tcx, RegionKind>>>,
111110
existential_predicates: RefCell<FxHashSet<Interned<'tcx, Slice<ExistentialPredicate<'tcx>>>>>,
112111
predicates: RefCell<FxHashSet<Interned<'tcx, Slice<Predicate<'tcx>>>>>,
113-
const_: RefCell<FxHashSet<Interned<'tcx, ConstVal<'tcx>>>>,
112+
const_: RefCell<FxHashSet<Interned<'tcx, Const<'tcx>>>>,
114113
}
115114

116115
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
@@ -978,21 +977,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
978977
}
979978
}
980979

981-
pub fn alloc_constval_slice(self, values: &[&'tcx ConstVal<'gcx>])
982-
-> &'gcx [&'tcx ConstVal<'gcx>] {
980+
pub fn alloc_const_slice(self, values: &[&'tcx ty::Const<'tcx>])
981+
-> &'tcx [&'tcx ty::Const<'tcx>] {
983982
if values.is_empty() {
984983
&[]
985984
} else {
986-
self.global_interners.arena.alloc_slice(values)
985+
self.interners.arena.alloc_slice(values)
987986
}
988987
}
989988

990-
pub fn alloc_name_constval_slice(self, values: &[(ast::Name, &'tcx ConstVal<'gcx>)])
991-
-> &'gcx [(ast::Name, &'tcx ConstVal<'gcx>)] {
989+
pub fn alloc_name_const_slice(self, values: &[(ast::Name, &'tcx ty::Const<'tcx>)])
990+
-> &'tcx [(ast::Name, &'tcx ty::Const<'tcx>)] {
992991
if values.is_empty() {
993992
&[]
994993
} else {
995-
self.global_interners.arena.alloc_slice(values)
994+
self.interners.arena.alloc_slice(values)
996995
}
997996
}
998997

@@ -1202,13 +1201,10 @@ impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
12021201
}
12031202
}
12041203

1205-
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
1206-
type Lifted = &'tcx Substs<'tcx>;
1207-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
1208-
if self.len() == 0 {
1209-
return Some(Slice::empty());
1210-
}
1211-
if tcx.interners.arena.in_arena(&self[..] as *const _) {
1204+
impl<'a, 'tcx> Lift<'tcx> for Region<'a> {
1205+
type Lifted = Region<'tcx>;
1206+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Region<'tcx>> {
1207+
if tcx.interners.arena.in_arena(*self as *const _) {
12121208
return Some(unsafe { mem::transmute(*self) });
12131209
}
12141210
// Also try in the global tcx if we're not that.
@@ -1220,9 +1216,9 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
12201216
}
12211217
}
12221218

1223-
impl<'a, 'tcx> Lift<'tcx> for Region<'a> {
1224-
type Lifted = Region<'tcx>;
1225-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Region<'tcx>> {
1219+
impl<'a, 'tcx> Lift<'tcx> for &'a Const<'a> {
1220+
type Lifted = &'tcx Const<'tcx>;
1221+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Const<'tcx>> {
12261222
if tcx.interners.arena.in_arena(*self as *const _) {
12271223
return Some(unsafe { mem::transmute(*self) });
12281224
}
@@ -1235,6 +1231,24 @@ impl<'a, 'tcx> Lift<'tcx> for Region<'a> {
12351231
}
12361232
}
12371233

1234+
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
1235+
type Lifted = &'tcx Substs<'tcx>;
1236+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
1237+
if self.len() == 0 {
1238+
return Some(Slice::empty());
1239+
}
1240+
if tcx.interners.arena.in_arena(&self[..] as *const _) {
1241+
return Some(unsafe { mem::transmute(*self) });
1242+
}
1243+
// Also try in the global tcx if we're not that.
1244+
if !tcx.is_global() {
1245+
self.lift_to_tcx(tcx.global_tcx())
1246+
} else {
1247+
None
1248+
}
1249+
}
1250+
}
1251+
12381252
impl<'a, 'tcx> Lift<'tcx> for &'a Slice<Ty<'a>> {
12391253
type Lifted = &'tcx Slice<Ty<'tcx>>;
12401254
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
@@ -1522,8 +1536,8 @@ impl<'tcx: 'lcx, 'lcx> Borrow<[Predicate<'lcx>]>
15221536
}
15231537
}
15241538

1525-
impl<'tcx: 'lcx, 'lcx> Borrow<ConstVal<'lcx>> for Interned<'tcx, ConstVal<'tcx>> {
1526-
fn borrow<'a>(&'a self) -> &'a ConstVal<'lcx> {
1539+
impl<'tcx: 'lcx, 'lcx> Borrow<Const<'lcx>> for Interned<'tcx, Const<'tcx>> {
1540+
fn borrow<'a>(&'a self) -> &'a Const<'lcx> {
15271541
&self.0
15281542
}
15291543
}
@@ -1609,7 +1623,7 @@ direct_interners!('tcx,
16091623
_ => false
16101624
}
16111625
}) -> RegionKind,
1612-
const_: mk_const(/*|c: &Const| keep_local(&c.ty)*/ |_| false) -> ConstVal<'tcx>
1626+
const_: mk_const(|c: &Const| keep_local(&c.ty) || keep_local(&c.val)) -> Const<'tcx>
16131627
);
16141628

16151629
macro_rules! slice_interners {

src/librustc/ty/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub use self::sty::{InferTy, ParamTy, ProjectionTy, ExistentialPredicate};
6464
pub use self::sty::{ClosureSubsts, GeneratorInterior, TypeAndMut};
6565
pub use self::sty::{TraitRef, TypeVariants, PolyTraitRef};
6666
pub use self::sty::{ExistentialTraitRef, PolyExistentialTraitRef};
67-
pub use self::sty::{ExistentialProjection, PolyExistentialProjection};
67+
pub use self::sty::{ExistentialProjection, PolyExistentialProjection, Const};
6868
pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
6969
pub use self::sty::RegionKind;
7070
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};
@@ -1597,7 +1597,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
15971597
if let VariantDiscr::Explicit(expr_did) = v.discr {
15981598
let substs = Substs::identity_for_item(tcx.global_tcx(), expr_did);
15991599
match tcx.const_eval(param_env.and((expr_did, substs))) {
1600-
Ok(&ConstVal::Integral(v)) => {
1600+
Ok(&ty::Const { val: ConstVal::Integral(v), .. }) => {
16011601
discr = v;
16021602
}
16031603
err => {
@@ -1637,7 +1637,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16371637
ty::VariantDiscr::Explicit(expr_did) => {
16381638
let substs = Substs::identity_for_item(tcx.global_tcx(), expr_did);
16391639
match tcx.const_eval(param_env.and((expr_did, substs))) {
1640-
Ok(&ConstVal::Integral(v)) => {
1640+
Ok(&ty::Const { val: ConstVal::Integral(v), .. }) => {
16411641
explicit_value = v;
16421642
break;
16431643
}

0 commit comments

Comments
 (0)