Skip to content

Commit 57ebd28

Browse files
committed
rustc: use ConstVal::Unevaluated instead of mir::Literal::Item.
1 parent 74349fa commit 57ebd28

File tree

8 files changed

+33
-50
lines changed

8 files changed

+33
-50
lines changed

src/librustc/ich/impls_mir.rs

-4
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,6 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::L
493493
hasher: &mut StableHasher<W>) {
494494
mem::discriminant(self).hash_stable(hcx, hasher);
495495
match *self {
496-
mir::Literal::Item { def_id, substs } => {
497-
def_id.hash_stable(hcx, hasher);
498-
substs.hash_stable(hcx, hasher);
499-
}
500496
mir::Literal::Value { ref value } => {
501497
value.hash_stable(hcx, hasher);
502498
}

src/librustc/mir/mod.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,6 @@ newtype_index!(Promoted, "promoted");
14791479

14801480
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
14811481
pub enum Literal<'tcx> {
1482-
Item {
1483-
def_id: DefId,
1484-
substs: &'tcx Substs<'tcx>,
1485-
},
14861482
Value {
14871483
value: &'tcx ty::Const<'tcx>,
14881484
},
@@ -1502,9 +1498,6 @@ impl<'tcx> Debug for Literal<'tcx> {
15021498
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
15031499
use self::Literal::*;
15041500
match *self {
1505-
Item { def_id, substs } => {
1506-
ppaux::parameterized(fmt, substs, def_id, &[])
1507-
}
15081501
Value { value } => {
15091502
write!(fmt, "const ")?;
15101503
fmt_const_val(fmt, &value.val)
@@ -2002,17 +1995,16 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
20021995
impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
20031996
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
20041997
match *self {
2005-
Literal::Item { def_id, substs } => Literal::Item {
2006-
def_id,
2007-
substs: substs.fold_with(folder)
1998+
Literal::Value { value } => Literal::Value {
1999+
value: value.fold_with(folder)
20082000
},
2009-
_ => self.clone()
2001+
Literal::Promoted { index } => Literal::Promoted { index }
20102002
}
20112003
}
20122004
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
20132005
match *self {
2014-
Literal::Item { substs, .. } => substs.visit_with(visitor),
2015-
_ => false
2006+
Literal::Value { value } => value.visit_with(visitor),
2007+
Literal::Promoted { .. } => false
20162008
}
20172009
}
20182010
}

src/librustc/mir/visit.rs

-5
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,6 @@ macro_rules! make_mir_visitor {
724724
literal: & $($mutability)* Literal<'tcx>,
725725
location: Location) {
726726
match *literal {
727-
Literal::Item { ref $($mutability)* def_id,
728-
ref $($mutability)* substs } => {
729-
self.visit_def_id(def_id, location);
730-
self.visit_substs(substs, location);
731-
}
732727
Literal::Value { ref $($mutability)* value } => {
733728
self.visit_const(value, location);
734729
}

src/librustc_mir/hair/cx/expr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,11 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
643643

644644
Def::Const(def_id) |
645645
Def::AssociatedConst(def_id) => ExprKind::Literal {
646-
literal: Literal::Item {
647-
def_id,
648-
substs,
646+
literal: Literal::Value {
647+
value: cx.tcx.mk_const(ty::Const {
648+
val: ConstVal::Unevaluated(def_id, substs),
649+
ty: cx.tables().node_id_to_type(expr.hir_id)
650+
}),
649651
},
650652
},
651653

src/librustc_mir/transform/qualify_consts.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2020
use rustc::hir;
2121
use rustc::hir::map as hir_map;
2222
use rustc::hir::def_id::DefId;
23+
use rustc::middle::const_val::ConstVal;
2324
use rustc::traits::{self, Reveal};
2425
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
2526
use rustc::ty::cast::CastTy;
@@ -622,10 +623,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
622623
}
623624
}
624625
Operand::Constant(ref constant) => {
625-
if let Literal::Item { def_id, substs: _ } = constant.literal {
626+
if let Literal::Value {
627+
value: &ty::Const { val: ConstVal::Unevaluated(def_id, _), ty }
628+
} = constant.literal {
626629
// Don't peek inside trait associated constants.
627630
if self.tcx.trait_of_item(def_id).is_some() {
628-
self.add_type(constant.ty);
631+
self.add_type(ty);
629632
} else {
630633
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);
631634

@@ -635,7 +638,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
635638
// Just in case the type is more specific than
636639
// the definition, e.g. impl associated const
637640
// with type parameters, take it into account.
638-
self.qualif.restrict(constant.ty, self.tcx, self.param_env);
641+
self.qualif.restrict(ty, self.tcx, self.param_env);
639642
}
640643

641644
// Let `const fn` transitively have destructors,

src/librustc_passes/mir_stats.rs

-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
235235
location: Location) {
236236
self.record("Literal", literal);
237237
self.record(match *literal {
238-
Literal::Item { .. } => "Literal::Item",
239238
Literal::Value { .. } => "Literal::Value",
240239
Literal::Promoted { .. } => "Literal::Promoted",
241240
}, literal);

src/librustc_trans/collector.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
193193

194194
use rustc::hir::map as hir_map;
195195
use rustc::hir::def_id::DefId;
196+
use rustc::middle::const_val::ConstVal;
196197
use rustc::middle::lang_items::{ExchangeMallocFnLangItem};
197198
use rustc::traits;
198199
use rustc::ty::subst::Substs;
@@ -564,24 +565,17 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
564565
self.super_rvalue(rvalue, location);
565566
}
566567

567-
fn visit_constant(&mut self, constant: &mir::Constant<'tcx>, location: Location) {
568-
debug!("visiting constant {:?} @ {:?}", *constant, location);
568+
fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, location: Location) {
569+
debug!("visiting const {:?} @ {:?}", *constant, location);
569570

570-
if let ty::TyFnDef(..) = constant.ty.sty {
571-
// function definitions are zero-sized, and only generate
572-
// IR when they are called/reified.
573-
self.super_constant(constant, location);
574-
return
575-
}
576-
577-
if let mir::Literal::Item { def_id, substs } = constant.literal {
571+
if let ConstVal::Unevaluated(def_id, substs) = constant.val {
578572
let substs = self.scx.tcx().trans_apply_param_substs(self.param_substs,
579573
&substs);
580574
let instance = monomorphize::resolve(self.scx, def_id, substs);
581575
collect_neighbours(self.scx, instance, true, self.output);
582576
}
583577

584-
self.super_constant(constant, location);
578+
self.super_const(constant);
585579
}
586580

587581
fn visit_terminator_kind(&mut self,

src/librustc_trans/mir/constant.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,17 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
510510
mir::Operand::Constant(ref constant) => {
511511
let ty = self.monomorphize(&constant.ty);
512512
match constant.literal.clone() {
513-
mir::Literal::Item { def_id, substs } => {
514-
let substs = self.monomorphize(&substs);
515-
MirConstContext::trans_def(self.ccx, def_id, substs, IndexVec::new())
516-
}
517513
mir::Literal::Promoted { index } => {
518514
let mir = &self.mir.promoted[index];
519515
MirConstContext::new(self.ccx, mir, self.substs, IndexVec::new()).trans()
520516
}
521517
mir::Literal::Value { value } => {
522-
Ok(Const::from_constval(self.ccx, &value.val, ty))
518+
if let ConstVal::Unevaluated(def_id, substs) = value.val {
519+
let substs = self.monomorphize(&substs);
520+
MirConstContext::trans_def(self.ccx, def_id, substs, IndexVec::new())
521+
} else {
522+
Ok(Const::from_constval(self.ccx, &value.val, ty))
523+
}
523524
}
524525
}
525526
}
@@ -960,16 +961,17 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
960961
debug!("trans_constant({:?})", constant);
961962
let ty = self.monomorphize(&constant.ty);
962963
let result = match constant.literal.clone() {
963-
mir::Literal::Item { def_id, substs } => {
964-
let substs = self.monomorphize(&substs);
965-
MirConstContext::trans_def(bcx.ccx, def_id, substs, IndexVec::new())
966-
}
967964
mir::Literal::Promoted { index } => {
968965
let mir = &self.mir.promoted[index];
969966
MirConstContext::new(bcx.ccx, mir, self.param_substs, IndexVec::new()).trans()
970967
}
971968
mir::Literal::Value { value } => {
972-
Ok(Const::from_constval(bcx.ccx, &value.val, ty))
969+
if let ConstVal::Unevaluated(def_id, substs) = value.val {
970+
let substs = self.monomorphize(&substs);
971+
MirConstContext::trans_def(bcx.ccx, def_id, substs, IndexVec::new())
972+
} else {
973+
Ok(Const::from_constval(bcx.ccx, &value.val, ty))
974+
}
973975
}
974976
};
975977

0 commit comments

Comments
 (0)