Skip to content

Commit

Permalink
rustc: use ConstVal::Unevaluated instead of mir::Literal::Item.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 11, 2017
1 parent 74349fa commit 57ebd28
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 50 deletions.
4 changes: 0 additions & 4 deletions src/librustc/ich/impls_mir.rs
Expand Up @@ -493,10 +493,6 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::L
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
mir::Literal::Item { def_id, substs } => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
}
mir::Literal::Value { ref value } => {
value.hash_stable(hcx, hasher);
}
Expand Down
18 changes: 5 additions & 13 deletions src/librustc/mir/mod.rs
Expand Up @@ -1479,10 +1479,6 @@ newtype_index!(Promoted, "promoted");

#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum Literal<'tcx> {
Item {
def_id: DefId,
substs: &'tcx Substs<'tcx>,
},
Value {
value: &'tcx ty::Const<'tcx>,
},
Expand All @@ -1502,9 +1498,6 @@ impl<'tcx> Debug for Literal<'tcx> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
use self::Literal::*;
match *self {
Item { def_id, substs } => {
ppaux::parameterized(fmt, substs, def_id, &[])
}
Value { value } => {
write!(fmt, "const ")?;
fmt_const_val(fmt, &value.val)
Expand Down Expand Up @@ -2002,17 +1995,16 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
match *self {
Literal::Item { def_id, substs } => Literal::Item {
def_id,
substs: substs.fold_with(folder)
Literal::Value { value } => Literal::Value {
value: value.fold_with(folder)
},
_ => self.clone()
Literal::Promoted { index } => Literal::Promoted { index }
}
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match *self {
Literal::Item { substs, .. } => substs.visit_with(visitor),
_ => false
Literal::Value { value } => value.visit_with(visitor),
Literal::Promoted { .. } => false
}
}
}
5 changes: 0 additions & 5 deletions src/librustc/mir/visit.rs
Expand Up @@ -724,11 +724,6 @@ macro_rules! make_mir_visitor {
literal: & $($mutability)* Literal<'tcx>,
location: Location) {
match *literal {
Literal::Item { ref $($mutability)* def_id,
ref $($mutability)* substs } => {
self.visit_def_id(def_id, location);
self.visit_substs(substs, location);
}
Literal::Value { ref $($mutability)* value } => {
self.visit_const(value, location);
}
Expand Down
8 changes: 5 additions & 3 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -643,9 +643,11 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,

Def::Const(def_id) |
Def::AssociatedConst(def_id) => ExprKind::Literal {
literal: Literal::Item {
def_id,
substs,
literal: Literal::Value {
value: cx.tcx.mk_const(ty::Const {
val: ConstVal::Unevaluated(def_id, substs),
ty: cx.tables().node_id_to_type(expr.hir_id)
}),
},
},

Expand Down
9 changes: 6 additions & 3 deletions src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -20,6 +20,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc::hir;
use rustc::hir::map as hir_map;
use rustc::hir::def_id::DefId;
use rustc::middle::const_val::ConstVal;
use rustc::traits::{self, Reveal};
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
use rustc::ty::cast::CastTy;
Expand Down Expand Up @@ -622,10 +623,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
}
}
Operand::Constant(ref constant) => {
if let Literal::Item { def_id, substs: _ } = constant.literal {
if let Literal::Value {
value: &ty::Const { val: ConstVal::Unevaluated(def_id, _), ty }
} = constant.literal {
// Don't peek inside trait associated constants.
if self.tcx.trait_of_item(def_id).is_some() {
self.add_type(constant.ty);
self.add_type(ty);
} else {
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);

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

// Let `const fn` transitively have destructors,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_passes/mir_stats.rs
Expand Up @@ -235,7 +235,6 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
location: Location) {
self.record("Literal", literal);
self.record(match *literal {
Literal::Item { .. } => "Literal::Item",
Literal::Value { .. } => "Literal::Value",
Literal::Promoted { .. } => "Literal::Promoted",
}, literal);
Expand Down
16 changes: 5 additions & 11 deletions src/librustc_trans/collector.rs
Expand Up @@ -193,6 +193,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;

use rustc::hir::map as hir_map;
use rustc::hir::def_id::DefId;
use rustc::middle::const_val::ConstVal;
use rustc::middle::lang_items::{ExchangeMallocFnLangItem};
use rustc::traits;
use rustc::ty::subst::Substs;
Expand Down Expand Up @@ -564,24 +565,17 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
self.super_rvalue(rvalue, location);
}

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

if let ty::TyFnDef(..) = constant.ty.sty {
// function definitions are zero-sized, and only generate
// IR when they are called/reified.
self.super_constant(constant, location);
return
}

if let mir::Literal::Item { def_id, substs } = constant.literal {
if let ConstVal::Unevaluated(def_id, substs) = constant.val {
let substs = self.scx.tcx().trans_apply_param_substs(self.param_substs,
&substs);
let instance = monomorphize::resolve(self.scx, def_id, substs);
collect_neighbours(self.scx, instance, true, self.output);
}

self.super_constant(constant, location);
self.super_const(constant);
}

fn visit_terminator_kind(&mut self,
Expand Down
22 changes: 12 additions & 10 deletions src/librustc_trans/mir/constant.rs
Expand Up @@ -510,16 +510,17 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
mir::Operand::Constant(ref constant) => {
let ty = self.monomorphize(&constant.ty);
match constant.literal.clone() {
mir::Literal::Item { def_id, substs } => {
let substs = self.monomorphize(&substs);
MirConstContext::trans_def(self.ccx, def_id, substs, IndexVec::new())
}
mir::Literal::Promoted { index } => {
let mir = &self.mir.promoted[index];
MirConstContext::new(self.ccx, mir, self.substs, IndexVec::new()).trans()
}
mir::Literal::Value { value } => {
Ok(Const::from_constval(self.ccx, &value.val, ty))
if let ConstVal::Unevaluated(def_id, substs) = value.val {
let substs = self.monomorphize(&substs);
MirConstContext::trans_def(self.ccx, def_id, substs, IndexVec::new())
} else {
Ok(Const::from_constval(self.ccx, &value.val, ty))
}
}
}
}
Expand Down Expand Up @@ -960,16 +961,17 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
debug!("trans_constant({:?})", constant);
let ty = self.monomorphize(&constant.ty);
let result = match constant.literal.clone() {
mir::Literal::Item { def_id, substs } => {
let substs = self.monomorphize(&substs);
MirConstContext::trans_def(bcx.ccx, def_id, substs, IndexVec::new())
}
mir::Literal::Promoted { index } => {
let mir = &self.mir.promoted[index];
MirConstContext::new(bcx.ccx, mir, self.param_substs, IndexVec::new()).trans()
}
mir::Literal::Value { value } => {
Ok(Const::from_constval(bcx.ccx, &value.val, ty))
if let ConstVal::Unevaluated(def_id, substs) = value.val {
let substs = self.monomorphize(&substs);
MirConstContext::trans_def(bcx.ccx, def_id, substs, IndexVec::new())
} else {
Ok(Const::from_constval(bcx.ccx, &value.val, ty))
}
}
};

Expand Down

0 comments on commit 57ebd28

Please sign in to comment.