Simplify code around reading/writing ConstVals

This commit is contained in:
Oliver Schneider 2018-01-26 16:44:10 +01:00 committed by Oliver Schneider
parent 4fbf1199ef
commit 5125d58fcf
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
3 changed files with 21 additions and 90 deletions
src
librustc_mir
borrow_check/nll/type_check
build
librustc_trans/mir

@ -24,9 +24,7 @@ use rustc::traits::{self, FulfillmentContext};
use rustc::ty::error::TypeError;
use rustc::ty::fold::TypeFoldable;
use rustc::ty::{self, ToPolyTraitRef, Ty, TyCtxt, TypeVariants};
use rustc::middle::const_val::ConstVal;
use rustc::mir::*;
use rustc::mir::interpret::{Value, PrimVal};
use rustc::mir::tcx::PlaceTy;
use rustc::mir::visit::{PlaceContext, Visitor};
use std::fmt;
@ -259,22 +257,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
// constraints on `'a` and `'b`. These constraints
// would be lost if we just look at the normalized
// value.
let did = match value.val {
ConstVal::Value(Value::ByVal(PrimVal::Ptr(p))) => {
self.tcx()
.interpret_interner
.get_fn(p.alloc_id)
.map(|instance| instance.def_id())
},
ConstVal::Value(Value::ByVal(PrimVal::Undef)) => {
match value.ty.sty {
ty::TyFnDef(ty_def_id, _) => Some(ty_def_id),
_ => None,
}
},
_ => None,
};
if let Some(def_id) = did {
if let ty::TyFnDef(def_id, substs) = value.ty.sty {
let tcx = self.tcx();
let type_checker = &mut self.cx;
@ -287,17 +270,6 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
// are transitioning to the miri-based system, we
// don't have a handy function for that, so for
// now we just ignore `value.val` regions.
let substs = match value.ty.sty {
ty::TyFnDef(ty_def_id, substs) => {
assert_eq!(def_id, ty_def_id);
substs
}
_ => span_bug!(
self.last_span,
"unexpected type for constant function: {:?}",
value.ty
),
};
let instantiated_predicates =
tcx.predicates_of(def_id).instantiate(tcx, substs);
@ -1029,35 +1001,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
fn is_box_free(&self, operand: &Operand<'tcx>) -> bool {
match operand {
&Operand::Constant(box Constant {
literal:
Literal::Value {
value:
&ty::Const {
val,
ty,
},
..
},
..
}) => match val {
ConstVal::Value(Value::ByVal(PrimVal::Ptr(p))) => {
let inst = self.tcx().interpret_interner.get_fn(p.alloc_id);
inst.map_or(false, |inst| {
Some(inst.def_id()) == self.tcx().lang_items().box_free_fn()
})
},
ConstVal::Value(Value::ByVal(PrimVal::Undef)) => {
match ty.sty {
ty::TyFnDef(ty_def_id, _) => {
Some(ty_def_id) == self.tcx().lang_items().box_free_fn()
}
_ => false,
}
match *operand {
Operand::Constant(ref c) => match c.ty.sty {
ty::TyFnDef(ty_def_id, _) => {
Some(ty_def_id) == self.tcx().lang_items().box_free_fn()
}
_ => false,
}
},
_ => false,
}
}

@ -54,30 +54,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
// Returns a zero literal operand for the appropriate type, works for
// bool, char and integers.
pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
let literal = match ty.sty {
ty::TyBool => {
self.hir.false_literal()
}
ty::TyChar => {
Literal::Value {
value: self.hir.tcx().mk_const(ty::Const {
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(0))),
ty
})
}
}
match ty.sty {
ty::TyBool |
ty::TyChar |
ty::TyUint(_) |
ty::TyInt(_) => {
Literal::Value {
value: self.hir.tcx().mk_const(ty::Const {
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(0))),
ty
})
}
}
ty::TyInt(_) => {}
_ => {
span_bug!(span, "Invalid type for zero_literal: `{:?}`", ty)
}
}
let literal = Literal::Value {
value: self.hir.tcx().mk_const(ty::Const {
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(0))),
ty
})
};
self.literal_operand(span, ty, literal)

@ -13,11 +13,9 @@
use rustc_data_structures::bitvec::BitVector;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::middle::const_val::ConstVal;
use rustc::mir::{self, Location, TerminatorKind, Literal};
use rustc::mir::{self, Location, TerminatorKind};
use rustc::mir::visit::{Visitor, PlaceContext};
use rustc::mir::traversal;
use rustc::mir::interpret::{Value, PrimVal};
use rustc::ty;
use rustc::ty::layout::LayoutOf;
use type_of::LayoutLlvmExt;
@ -112,19 +110,12 @@ impl<'mir, 'a, 'tcx> Visitor<'tcx> for LocalAnalyzer<'mir, 'a, 'tcx> {
location: Location) {
let check = match *kind {
mir::TerminatorKind::Call {
func: mir::Operand::Constant(box mir::Constant {
literal: Literal::Value {
value: &ty::Const { val, ty }, ..
}, ..
}),
func: mir::Operand::Constant(ref c),
ref args, ..
} => match val {
ConstVal::Value(Value::ByVal(PrimVal::Undef)) => match ty.sty {
ty::TyFnDef(did, _) => Some((did, args)),
_ => None,
},
} => match c.ty.sty {
ty::TyFnDef(did, _) => Some((did, args)),
_ => None,
}
},
_ => None,
};
if let Some((def_id, args)) = check {