2018-01-28 07:41:17 -06:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Propagates constants for early reporting of statically known
|
|
|
|
//! assertion failures
|
|
|
|
|
|
|
|
|
2018-03-06 05:43:02 -06:00
|
|
|
use rustc::hir::def::Def;
|
2018-01-28 07:41:17 -06:00
|
|
|
use rustc::mir::{Constant, Literal, Location, Place, Mir, Operand, Rvalue, Local};
|
|
|
|
use rustc::mir::{NullOp, StatementKind, Statement, BasicBlock, LocalKind};
|
2018-03-06 05:43:02 -06:00
|
|
|
use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem};
|
2018-01-30 06:57:13 -06:00
|
|
|
use rustc::mir::visit::{Visitor, PlaceContext};
|
2018-06-02 16:38:57 -05:00
|
|
|
use rustc::middle::const_val::{ConstVal, ConstEvalErr, ErrKind};
|
2018-01-28 07:41:17 -06:00
|
|
|
use rustc::ty::{TyCtxt, self, Instance};
|
2018-05-20 16:43:16 -05:00
|
|
|
use rustc::mir::interpret::{Value, Scalar, GlobalId, EvalResult};
|
2018-04-26 02:18:19 -05:00
|
|
|
use interpret::EvalContext;
|
|
|
|
use interpret::CompileTimeEvaluator;
|
2018-04-08 15:26:28 -05:00
|
|
|
use interpret::{eval_promoted, mk_borrowck_eval_cx, ValTy};
|
2018-01-28 07:41:17 -06:00
|
|
|
use transform::{MirPass, MirSource};
|
2018-04-26 02:18:19 -05:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
2018-01-28 07:41:17 -06:00
|
|
|
use rustc::ty::subst::Substs;
|
2018-01-29 08:12:45 -06:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2018-01-31 02:31:24 -06:00
|
|
|
use rustc::ty::ParamEnv;
|
2018-03-06 05:43:02 -06:00
|
|
|
use rustc::ty::layout::{
|
2018-06-02 16:34:25 -05:00
|
|
|
LayoutOf, TyLayout, LayoutError,
|
2018-03-06 05:43:02 -06:00
|
|
|
HasTyCtxt, TargetDataLayout, HasDataLayout,
|
|
|
|
};
|
2018-01-28 07:41:17 -06:00
|
|
|
|
|
|
|
pub struct ConstProp;
|
|
|
|
|
|
|
|
impl MirPass for ConstProp {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
source: MirSource,
|
|
|
|
mir: &mut Mir<'tcx>) {
|
2018-03-06 05:43:02 -06:00
|
|
|
// will be evaluated by miri and produce its errors there
|
|
|
|
if source.promoted.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match tcx.describe_def(source.def_id) {
|
|
|
|
// skip statics because they'll be evaluated by miri anyway
|
|
|
|
Some(Def::Static(..)) => return,
|
|
|
|
_ => {},
|
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
trace!("ConstProp starting for {:?}", source.def_id);
|
|
|
|
|
2018-01-29 08:12:45 -06:00
|
|
|
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
|
|
|
|
// constants, instead of just checking for const-folding succeeding.
|
|
|
|
// That would require an uniform one-def no-mutation analysis
|
|
|
|
// and RPO (or recursing when needing the value of a local).
|
2018-01-30 07:12:16 -06:00
|
|
|
let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
|
2018-01-29 03:38:05 -06:00
|
|
|
optimization_finder.visit_mir(mir);
|
2018-01-28 07:41:17 -06:00
|
|
|
|
|
|
|
trace!("ConstProp done for {:?}", source.def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Const<'tcx> = (Value, ty::Ty<'tcx>, Span);
|
|
|
|
|
|
|
|
/// Finds optimization opportunities on the MIR.
|
2018-01-30 07:12:16 -06:00
|
|
|
struct ConstPropagator<'b, 'a, 'tcx:'a+'b> {
|
2018-04-26 02:18:19 -05:00
|
|
|
ecx: EvalContext<'a, 'b, 'tcx, CompileTimeEvaluator>,
|
2018-01-28 07:41:17 -06:00
|
|
|
mir: &'b Mir<'tcx>,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
source: MirSource,
|
2018-01-29 08:12:45 -06:00
|
|
|
places: IndexVec<Local, Option<Const<'tcx>>>,
|
2018-01-30 02:40:46 -06:00
|
|
|
can_const_prop: IndexVec<Local, bool>,
|
2018-01-31 02:31:24 -06:00
|
|
|
param_env: ParamEnv<'tcx>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2018-02-05 13:07:20 -06:00
|
|
|
impl<'a, 'b, 'tcx> LayoutOf for &'a ConstPropagator<'a, 'b, 'tcx> {
|
|
|
|
type Ty = ty::Ty<'tcx>;
|
2018-03-06 05:43:02 -06:00
|
|
|
type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
|
|
|
|
|
|
|
|
fn layout_of(self, ty: ty::Ty<'tcx>) -> Self::TyLayout {
|
|
|
|
self.tcx.layout_of(self.param_env.and(ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'tcx> HasDataLayout for &'a ConstPropagator<'a, 'b, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'tcx> HasTyCtxt<'tcx> for &'a ConstPropagator<'a, 'b, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'tcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
|
2018-01-28 07:41:17 -06:00
|
|
|
fn new(
|
|
|
|
mir: &'b Mir<'tcx>,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
source: MirSource,
|
2018-01-30 07:12:16 -06:00
|
|
|
) -> ConstPropagator<'b, 'a, 'tcx> {
|
2018-01-31 02:31:24 -06:00
|
|
|
let param_env = tcx.param_env(source.def_id);
|
2018-04-26 02:18:19 -05:00
|
|
|
let substs = Substs::identity_for_item(tcx, source.def_id);
|
|
|
|
let instance = Instance::new(source.def_id, substs);
|
|
|
|
let ecx = mk_borrowck_eval_cx(tcx, instance, mir, DUMMY_SP).unwrap();
|
2018-01-30 07:12:16 -06:00
|
|
|
ConstPropagator {
|
2018-04-26 02:18:19 -05:00
|
|
|
ecx,
|
2018-01-28 07:41:17 -06:00
|
|
|
mir,
|
|
|
|
tcx,
|
|
|
|
source,
|
2018-01-31 02:31:24 -06:00
|
|
|
param_env,
|
2018-01-30 07:12:16 -06:00
|
|
|
can_const_prop: CanConstProp::check(mir),
|
2018-01-29 08:12:45 -06:00
|
|
|
places: IndexVec::from_elem(None, &mir.local_decls),
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:18:19 -05:00
|
|
|
fn use_ecx<F, T>(
|
|
|
|
&mut self,
|
2018-06-02 16:38:57 -05:00
|
|
|
source_info: SourceInfo,
|
2018-04-26 02:18:19 -05:00
|
|
|
f: F
|
|
|
|
) -> Option<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> EvalResult<'tcx, T>,
|
|
|
|
{
|
2018-06-02 16:38:57 -05:00
|
|
|
self.ecx.tcx.span = source_info.span;
|
|
|
|
let lint_root = match self.mir.source_scope_local_data {
|
|
|
|
ClearCrossCrate::Set(ref ivs) => {
|
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
//FIXME(#51314): remove this check
|
|
|
|
if source_info.scope.index() >= ivs.len() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
ivs[source_info.scope].lint_root
|
|
|
|
},
|
|
|
|
ClearCrossCrate::Clear => return None,
|
|
|
|
};
|
2018-04-26 02:18:19 -05:00
|
|
|
let r = match f(self) {
|
|
|
|
Ok(val) => Some(val),
|
2018-06-02 16:38:57 -05:00
|
|
|
Err(err) => {
|
|
|
|
let (frames, span) = self.ecx.generate_stacktrace(None);
|
|
|
|
let err = ConstEvalErr {
|
|
|
|
span,
|
|
|
|
kind: ErrKind::Miri(err, frames).into(),
|
|
|
|
};
|
|
|
|
err.report_as_lint(
|
|
|
|
self.ecx.tcx,
|
|
|
|
"this expression will panic at runtime",
|
|
|
|
lint_root,
|
|
|
|
);
|
2018-04-26 02:18:19 -05:00
|
|
|
None
|
|
|
|
},
|
|
|
|
};
|
|
|
|
self.ecx.tcx.span = DUMMY_SP;
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
fn const_eval(&mut self, cid: GlobalId<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
|
2018-01-31 02:31:24 -06:00
|
|
|
let value = match self.tcx.const_eval(self.param_env.and(cid)) {
|
|
|
|
Ok(val) => val,
|
2018-01-31 02:57:25 -06:00
|
|
|
Err(err) => {
|
2018-06-02 16:38:57 -05:00
|
|
|
err.report_as_error(
|
|
|
|
self.tcx.at(err.span),
|
|
|
|
"constant evaluation error",
|
|
|
|
);
|
2018-01-31 02:57:25 -06:00
|
|
|
return None;
|
|
|
|
},
|
2018-01-31 02:31:24 -06:00
|
|
|
};
|
|
|
|
let val = match value.val {
|
2018-04-26 02:18:19 -05:00
|
|
|
ConstVal::Value(v) => {
|
2018-06-02 16:38:57 -05:00
|
|
|
self.use_ecx(source_info, |this| this.ecx.const_value_to_value(v, value.ty))?
|
2018-04-26 02:18:19 -05:00
|
|
|
},
|
2018-01-31 02:31:24 -06:00
|
|
|
_ => bug!("eval produced: {:?}", value),
|
|
|
|
};
|
2018-06-02 16:38:57 -05:00
|
|
|
let val = (val, value.ty, source_info.span);
|
2018-01-31 02:31:24 -06:00
|
|
|
trace!("evaluated {:?} to {:?}", cid, val);
|
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
fn eval_constant(
|
|
|
|
&mut self,
|
|
|
|
c: &Constant<'tcx>,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
) -> Option<Const<'tcx>> {
|
2018-01-28 07:41:17 -06:00
|
|
|
match c.literal {
|
|
|
|
Literal::Value { value } => match value.val {
|
2018-04-26 02:18:19 -05:00
|
|
|
ConstVal::Value(v) => {
|
2018-06-02 16:38:57 -05:00
|
|
|
let v = self.use_ecx(source_info, |this| {
|
2018-04-26 02:18:19 -05:00
|
|
|
this.ecx.const_value_to_value(v, value.ty)
|
|
|
|
})?;
|
|
|
|
Some((v, value.ty, c.span))
|
|
|
|
},
|
2018-01-28 07:41:17 -06:00
|
|
|
ConstVal::Unevaluated(did, substs) => {
|
|
|
|
let instance = Instance::resolve(
|
|
|
|
self.tcx,
|
2018-01-31 02:31:24 -06:00
|
|
|
self.param_env,
|
2018-01-28 07:41:17 -06:00
|
|
|
did,
|
|
|
|
substs,
|
|
|
|
)?;
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
};
|
2018-06-02 16:38:57 -05:00
|
|
|
self.const_eval(cid, source_info)
|
2018-01-28 07:41:17 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// evaluate the promoted and replace the constant with the evaluated result
|
|
|
|
Literal::Promoted { index } => {
|
|
|
|
let generics = self.tcx.generics_of(self.source.def_id);
|
2018-04-12 11:51:08 -05:00
|
|
|
if generics.requires_monomorphization(self.tcx) {
|
2018-01-28 07:41:17 -06:00
|
|
|
// FIXME: can't handle code with generics
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let substs = Substs::identity_for_item(self.tcx, self.source.def_id);
|
|
|
|
let instance = Instance::new(self.source.def_id, substs);
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: Some(index),
|
|
|
|
};
|
2018-01-31 02:31:24 -06:00
|
|
|
// cannot use `const_eval` here, because that would require having the MIR
|
|
|
|
// for the current function available, but we're producing said MIR right now
|
2018-06-02 16:38:57 -05:00
|
|
|
let (value, _, ty) = self.use_ecx(source_info, |this| {
|
|
|
|
eval_promoted(&mut this.ecx, cid, this.mir, this.param_env)
|
|
|
|
})?;
|
2018-01-28 07:41:17 -06:00
|
|
|
let val = (value, ty, c.span);
|
|
|
|
trace!("evaluated {:?} to {:?}", c, val);
|
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:43:02 -06:00
|
|
|
fn eval_place(&mut self, place: &Place<'tcx>) -> Option<Const<'tcx>> {
|
|
|
|
match *place {
|
|
|
|
Place::Local(loc) => self.places[loc].clone(),
|
|
|
|
Place::Projection(ref proj) => match proj.elem {
|
|
|
|
ProjectionElem::Field(field, _) => {
|
|
|
|
trace!("field proj on {:?}", proj.base);
|
|
|
|
let (base, ty, span) = self.eval_place(&proj.base)?;
|
2018-06-04 07:50:29 -05:00
|
|
|
let valty = self.use_ecx(span, |this| {
|
2018-06-02 16:34:25 -05:00
|
|
|
this.ecx.read_field(base, None, field, ty)
|
2018-06-04 07:50:29 -05:00
|
|
|
})?;
|
|
|
|
Some((valty.value, valty.ty, span))
|
2018-03-06 05:43:02 -06:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
|
2018-01-28 07:41:17 -06:00
|
|
|
match *op {
|
2018-06-02 16:38:57 -05:00
|
|
|
Operand::Constant(ref c) => self.eval_constant(c, source_info),
|
2018-03-06 05:43:02 -06:00
|
|
|
Operand::Move(ref place) | Operand::Copy(ref place) => self.eval_place(place),
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn const_prop(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
place_ty: ty::Ty<'tcx>,
|
2018-01-29 08:12:45 -06:00
|
|
|
source_info: SourceInfo,
|
2018-01-28 07:41:17 -06:00
|
|
|
) -> Option<Const<'tcx>> {
|
2018-01-29 08:12:45 -06:00
|
|
|
let span = source_info.span;
|
2018-01-28 07:41:17 -06:00
|
|
|
match *rvalue {
|
|
|
|
// This branch exists for the sanity type check
|
|
|
|
Rvalue::Use(Operand::Constant(ref c)) => {
|
|
|
|
assert_eq!(c.ty, place_ty);
|
2018-06-02 16:38:57 -05:00
|
|
|
self.eval_constant(c, source_info)
|
2018-01-28 07:41:17 -06:00
|
|
|
},
|
|
|
|
Rvalue::Use(ref op) => {
|
2018-06-02 16:38:57 -05:00
|
|
|
self.eval_operand(op, source_info)
|
2018-01-28 07:41:17 -06:00
|
|
|
},
|
|
|
|
Rvalue::Repeat(..) |
|
|
|
|
Rvalue::Ref(..) |
|
|
|
|
Rvalue::Cast(..) |
|
|
|
|
Rvalue::Aggregate(..) |
|
|
|
|
Rvalue::NullaryOp(NullOp::Box, _) |
|
|
|
|
Rvalue::Discriminant(..) => None,
|
|
|
|
// FIXME(oli-obk): evaluate static/constant slice lengths
|
|
|
|
Rvalue::Len(_) => None,
|
|
|
|
Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
|
|
|
|
let param_env = self.tcx.param_env(self.source.def_id);
|
|
|
|
type_size_of(self.tcx, param_env, ty).map(|n| (
|
2018-05-22 03:28:46 -05:00
|
|
|
Value::Scalar(Scalar::Bits {
|
|
|
|
bits: n as u128,
|
|
|
|
defined: self.tcx.data_layout.pointer_size.bits() as u8,
|
|
|
|
}),
|
2018-01-28 07:41:17 -06:00
|
|
|
self.tcx.types.usize,
|
|
|
|
span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Rvalue::UnaryOp(op, ref arg) => {
|
|
|
|
let def_id = if self.tcx.is_closure(self.source.def_id) {
|
|
|
|
self.tcx.closure_base_def_id(self.source.def_id)
|
|
|
|
} else {
|
|
|
|
self.source.def_id
|
|
|
|
};
|
|
|
|
let generics = self.tcx.generics_of(def_id);
|
2018-04-12 11:51:08 -05:00
|
|
|
if generics.requires_monomorphization(self.tcx) {
|
2018-01-28 07:41:17 -06:00
|
|
|
// FIXME: can't handle code with generics
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
let val = self.eval_operand(arg, source_info)?;
|
|
|
|
let prim = self.use_ecx(source_info, |this| {
|
2018-05-22 12:30:16 -05:00
|
|
|
this.ecx.value_to_scalar(ValTy { value: val.0, ty: val.1 })
|
2018-04-26 02:18:19 -05:00
|
|
|
})?;
|
2018-06-02 16:38:57 -05:00
|
|
|
let val = self.use_ecx(source_info, |this| this.ecx.unary_op(op, prim, val.1))?;
|
2018-05-20 16:46:30 -05:00
|
|
|
Some((Value::Scalar(val), place_ty, span))
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
Rvalue::CheckedBinaryOp(op, ref left, ref right) |
|
|
|
|
Rvalue::BinaryOp(op, ref left, ref right) => {
|
|
|
|
trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
|
2018-06-02 16:38:57 -05:00
|
|
|
let right = self.eval_operand(right, source_info)?;
|
2018-01-28 07:41:17 -06:00
|
|
|
let def_id = if self.tcx.is_closure(self.source.def_id) {
|
|
|
|
self.tcx.closure_base_def_id(self.source.def_id)
|
|
|
|
} else {
|
|
|
|
self.source.def_id
|
|
|
|
};
|
|
|
|
let generics = self.tcx.generics_of(def_id);
|
2018-04-12 11:51:08 -05:00
|
|
|
if generics.requires_monomorphization(self.tcx) {
|
2018-01-28 07:41:17 -06:00
|
|
|
// FIXME: can't handle code with generics
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
let r = self.use_ecx(source_info, |this| {
|
2018-05-22 12:30:16 -05:00
|
|
|
this.ecx.value_to_scalar(ValTy { value: right.0, ty: right.1 })
|
2018-04-26 02:18:19 -05:00
|
|
|
})?;
|
2018-01-29 12:06:44 -06:00
|
|
|
if op == BinOp::Shr || op == BinOp::Shl {
|
2018-03-06 05:43:02 -06:00
|
|
|
let left_ty = left.ty(self.mir, self.tcx);
|
2018-05-23 10:45:50 -05:00
|
|
|
let left_bits = self
|
|
|
|
.tcx
|
|
|
|
.layout_of(self.param_env.and(left_ty))
|
|
|
|
.unwrap()
|
|
|
|
.size
|
|
|
|
.bits();
|
|
|
|
let right_size = self.tcx.layout_of(self.param_env.and(right.1)).unwrap().size;
|
2018-05-22 03:28:46 -05:00
|
|
|
if r.to_bits(right_size).ok().map_or(false, |b| b >= left_bits as u128) {
|
2018-05-28 09:37:48 -05:00
|
|
|
let source_scope_local_data = match self.mir.source_scope_local_data {
|
2018-01-29 12:06:44 -06:00
|
|
|
ClearCrossCrate::Set(ref data) => data,
|
|
|
|
ClearCrossCrate::Clear => return None,
|
|
|
|
};
|
2018-03-06 05:43:02 -06:00
|
|
|
let dir = if op == BinOp::Shr {
|
|
|
|
"right"
|
|
|
|
} else {
|
|
|
|
"left"
|
|
|
|
};
|
2018-05-28 09:37:48 -05:00
|
|
|
let node_id = source_scope_local_data[source_info.scope].lint_root;
|
2018-01-29 12:06:44 -06:00
|
|
|
self.tcx.lint_node(
|
|
|
|
::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
|
|
|
|
node_id,
|
|
|
|
span,
|
2018-03-06 05:43:02 -06:00
|
|
|
&format!("attempt to shift {} with overflow", dir));
|
2018-01-29 13:47:09 -06:00
|
|
|
return None;
|
2018-01-29 12:06:44 -06:00
|
|
|
}
|
2018-01-29 08:10:26 -06:00
|
|
|
}
|
2018-06-02 16:38:57 -05:00
|
|
|
let left = self.eval_operand(left, source_info)?;
|
|
|
|
let l = self.use_ecx(source_info, |this| {
|
2018-05-22 12:30:16 -05:00
|
|
|
this.ecx.value_to_scalar(ValTy { value: left.0, ty: left.1 })
|
2018-04-26 02:18:19 -05:00
|
|
|
})?;
|
2018-01-28 07:41:17 -06:00
|
|
|
trace!("const evaluating {:?} for {:?} and {:?}", op, left, right);
|
2018-06-02 16:38:57 -05:00
|
|
|
let (val, overflow) = self.use_ecx(source_info, |this| {
|
2018-04-26 02:18:19 -05:00
|
|
|
this.ecx.binary_op(op, l, left.1, r, right.1)
|
|
|
|
})?;
|
|
|
|
let val = if let Rvalue::CheckedBinaryOp(..) = *rvalue {
|
2018-05-20 16:46:30 -05:00
|
|
|
Value::ScalarPair(
|
2018-04-26 02:18:19 -05:00
|
|
|
val,
|
2018-05-20 16:43:16 -05:00
|
|
|
Scalar::from_bool(overflow),
|
2018-04-26 02:18:19 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
if overflow {
|
|
|
|
use rustc::mir::interpret::EvalErrorKind;
|
2018-06-02 16:38:57 -05:00
|
|
|
let err = EvalErrorKind::Overflow(op).into();
|
|
|
|
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
|
2018-04-26 02:18:19 -05:00
|
|
|
return None;
|
|
|
|
}
|
2018-05-20 16:46:30 -05:00
|
|
|
Value::Scalar(val)
|
2018-04-26 02:18:19 -05:00
|
|
|
};
|
|
|
|
Some((val, place_ty, span))
|
2018-01-28 07:41:17 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: ty::Ty<'tcx>) -> Option<u64> {
|
2018-01-31 08:45:59 -06:00
|
|
|
tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
struct CanConstProp {
|
2018-01-30 02:40:46 -06:00
|
|
|
can_const_prop: IndexVec<Local, bool>,
|
2018-01-28 07:41:17 -06:00
|
|
|
// false at the beginning, once set, there are not allowed to be any more assignments
|
2018-01-30 02:40:46 -06:00
|
|
|
found_assignment: IndexVec<Local, bool>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl CanConstProp {
|
2018-01-28 07:41:17 -06:00
|
|
|
/// returns true if `local` can be propagated
|
2018-01-30 07:12:16 -06:00
|
|
|
fn check(mir: &Mir) -> IndexVec<Local, bool> {
|
2018-01-28 07:41:17 -06:00
|
|
|
let mut cpv = CanConstProp {
|
2018-01-30 02:40:46 -06:00
|
|
|
can_const_prop: IndexVec::from_elem(true, &mir.local_decls),
|
|
|
|
found_assignment: IndexVec::from_elem(false, &mir.local_decls),
|
2018-01-28 07:41:17 -06:00
|
|
|
};
|
2018-01-30 02:40:46 -06:00
|
|
|
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
|
2018-01-30 09:38:14 -06:00
|
|
|
// cannot use args at all
|
|
|
|
// cannot use locals because if x < y { y - x } else { x - y } would
|
|
|
|
// lint for x != y
|
|
|
|
// FIXME(oli-obk): lint variables until they are used in a condition
|
|
|
|
// FIXME(oli-obk): lint if return value is constant
|
|
|
|
*val = mir.local_kind(local) == LocalKind::Temp;
|
2018-01-30 02:40:46 -06:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
cpv.visit_mir(mir);
|
|
|
|
cpv.can_const_prop
|
|
|
|
}
|
2018-01-30 02:40:46 -06:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl<'tcx> Visitor<'tcx> for CanConstProp {
|
2018-01-30 06:57:13 -06:00
|
|
|
fn visit_local(
|
2018-01-28 07:41:17 -06:00
|
|
|
&mut self,
|
2018-01-30 06:57:13 -06:00
|
|
|
&local: &Local,
|
|
|
|
context: PlaceContext<'tcx>,
|
|
|
|
_: Location,
|
2018-01-28 07:41:17 -06:00
|
|
|
) {
|
2018-01-30 06:57:13 -06:00
|
|
|
use rustc::mir::visit::PlaceContext::*;
|
|
|
|
match context {
|
|
|
|
// Constants must have at most one write
|
|
|
|
// FIXME(oli-obk): we could be more powerful here, if the multiple writes
|
|
|
|
// only occur in independent execution paths
|
|
|
|
Store => if self.found_assignment[local] {
|
|
|
|
self.can_const_prop[local] = false;
|
|
|
|
} else {
|
|
|
|
self.found_assignment[local] = true
|
2018-01-28 07:41:17 -06:00
|
|
|
},
|
2018-01-30 06:57:13 -06:00
|
|
|
// Reading constants is allowed an arbitrary number of times
|
|
|
|
Copy | Move |
|
|
|
|
StorageDead | StorageLive |
|
|
|
|
Validate |
|
2018-03-06 05:43:02 -06:00
|
|
|
Projection(_) |
|
2018-01-30 06:57:13 -06:00
|
|
|
Inspect => {},
|
|
|
|
_ => self.can_const_prop[local] = false,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
|
2018-01-28 07:41:17 -06:00
|
|
|
fn visit_constant(
|
|
|
|
&mut self,
|
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
trace!("visit_constant: {:?}", constant);
|
|
|
|
self.super_constant(constant, location);
|
2018-06-02 16:38:57 -05:00
|
|
|
let source_info = *self.mir.source_info(location);
|
|
|
|
self.eval_constant(constant, source_info);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
trace!("visit_statement: {:?}", statement);
|
|
|
|
if let StatementKind::Assign(ref place, ref rval) = statement.kind {
|
|
|
|
let place_ty = place
|
|
|
|
.ty(&self.mir.local_decls, self.tcx)
|
|
|
|
.to_ty(self.tcx);
|
2018-01-29 08:12:45 -06:00
|
|
|
if let Some(value) = self.const_prop(rval, place_ty, statement.source_info) {
|
2018-01-28 07:41:17 -06:00
|
|
|
if let Place::Local(local) = *place {
|
2018-03-06 05:43:02 -06:00
|
|
|
trace!("checking whether {:?} can be stored to {:?}", value, local);
|
2018-01-30 02:40:46 -06:00
|
|
|
if self.can_const_prop[local] {
|
2018-01-28 07:41:17 -06:00
|
|
|
trace!("storing {:?} to {:?}", value, local);
|
2018-01-29 08:12:45 -06:00
|
|
|
assert!(self.places[local].is_none());
|
|
|
|
self.places[local] = Some(value);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.super_statement(block, statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator_kind(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
kind: &TerminatorKind<'tcx>,
|
2018-01-30 07:28:45 -06:00
|
|
|
location: Location,
|
2018-01-28 07:41:17 -06:00
|
|
|
) {
|
2018-01-30 07:28:45 -06:00
|
|
|
self.super_terminator_kind(block, kind, location);
|
2018-06-02 16:38:57 -05:00
|
|
|
let source_info = *self.mir.source_info(location);
|
2018-01-30 07:28:45 -06:00
|
|
|
if let TerminatorKind::Assert { expected, msg, cond, .. } = kind {
|
2018-06-02 16:38:57 -05:00
|
|
|
if let Some(value) = self.eval_operand(cond, source_info) {
|
2018-03-06 05:43:02 -06:00
|
|
|
trace!("assertion on {:?} should be {:?}", value, expected);
|
2018-05-20 16:46:30 -05:00
|
|
|
if Value::Scalar(Scalar::from_bool(*expected)) != value.0 {
|
2018-03-06 05:43:02 -06:00
|
|
|
// poison all places this operand references so that further code
|
|
|
|
// doesn't use the invalid value
|
|
|
|
match cond {
|
|
|
|
Operand::Move(ref place) | Operand::Copy(ref place) => {
|
|
|
|
let mut place = place;
|
|
|
|
while let Place::Projection(ref proj) = *place {
|
|
|
|
place = &proj.base;
|
|
|
|
}
|
|
|
|
if let Place::Local(local) = *place {
|
|
|
|
self.places[local] = None;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Operand::Constant(_) => {}
|
|
|
|
}
|
2018-01-30 07:28:45 -06:00
|
|
|
let span = self.mir[block]
|
|
|
|
.terminator
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.source_info
|
|
|
|
.span;
|
|
|
|
let node_id = self
|
|
|
|
.tcx
|
|
|
|
.hir
|
|
|
|
.as_local_node_id(self.source.def_id)
|
|
|
|
.expect("some part of a failing const eval must be local");
|
2018-04-27 08:21:31 -05:00
|
|
|
use rustc::mir::interpret::EvalErrorKind::*;
|
2018-03-06 05:43:02 -06:00
|
|
|
let msg = match msg {
|
2018-04-27 08:21:31 -05:00
|
|
|
Overflow(_) |
|
|
|
|
OverflowNeg |
|
|
|
|
DivisionByZero |
|
|
|
|
RemainderByZero => msg.description().to_owned(),
|
2018-01-30 07:28:45 -06:00
|
|
|
BoundsCheck { ref len, ref index } => {
|
2018-06-02 16:38:57 -05:00
|
|
|
let len = self
|
|
|
|
.eval_operand(len, source_info)
|
|
|
|
.expect("len must be const");
|
2018-01-30 07:28:45 -06:00
|
|
|
let len = match len.0 {
|
2018-05-22 03:28:46 -05:00
|
|
|
Value::Scalar(Scalar::Bits { bits, ..}) => bits,
|
2018-01-30 07:28:45 -06:00
|
|
|
_ => bug!("const len not primitive: {:?}", len),
|
|
|
|
};
|
|
|
|
let index = self
|
2018-06-02 16:38:57 -05:00
|
|
|
.eval_operand(index, source_info)
|
2018-01-30 07:28:45 -06:00
|
|
|
.expect("index must be const");
|
|
|
|
let index = match index.0 {
|
2018-05-22 03:28:46 -05:00
|
|
|
Value::Scalar(Scalar::Bits { bits, .. }) => bits,
|
2018-01-30 07:28:45 -06:00
|
|
|
_ => bug!("const index not primitive: {:?}", index),
|
|
|
|
};
|
2018-03-06 05:43:02 -06:00
|
|
|
format!(
|
|
|
|
"index out of bounds: \
|
|
|
|
the len is {} but the index is {}",
|
|
|
|
len,
|
|
|
|
index,
|
2018-01-30 07:28:45 -06:00
|
|
|
)
|
|
|
|
},
|
2018-04-27 08:21:31 -05:00
|
|
|
// Need proper const propagator for these
|
|
|
|
_ => return,
|
2018-03-06 05:43:02 -06:00
|
|
|
};
|
|
|
|
self.tcx.lint_node(
|
|
|
|
::rustc::lint::builtin::CONST_ERR,
|
|
|
|
node_id,
|
|
|
|
span,
|
|
|
|
&msg,
|
|
|
|
);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|