Simplify const propagator by removing unused code paths
This commit is contained in:
parent
411fcf58fd
commit
1561b4ad50
1 changed files with 52 additions and 71 deletions
|
@ -359,81 +359,62 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
|
||||||
&mut self,
|
&mut self,
|
||||||
block: BasicBlock,
|
block: BasicBlock,
|
||||||
kind: &TerminatorKind<'tcx>,
|
kind: &TerminatorKind<'tcx>,
|
||||||
_location: Location,
|
location: Location,
|
||||||
) {
|
) {
|
||||||
match kind {
|
self.super_terminator_kind(block, kind, location);
|
||||||
TerminatorKind::SwitchInt { discr: value, .. } |
|
if let TerminatorKind::Assert { expected, msg, cond, .. } = kind {
|
||||||
TerminatorKind::Yield { value, .. } |
|
if let Some(value) = self.eval_operand(cond) {
|
||||||
TerminatorKind::Assert { cond: value, .. } => {
|
if Value::ByVal(PrimVal::from_bool(*expected)) != value.0 {
|
||||||
match value {
|
let span = self.mir[block]
|
||||||
Operand::Constant(box Constant {
|
.terminator
|
||||||
literal: Literal::Value {
|
.as_ref()
|
||||||
value: &ty::Const {
|
.unwrap()
|
||||||
val: ConstVal::Value(_),
|
.source_info
|
||||||
..
|
.span;
|
||||||
},
|
let node_id = self
|
||||||
},
|
.tcx
|
||||||
..
|
.hir
|
||||||
}) => return,
|
.as_local_node_id(self.source.def_id)
|
||||||
_ => {},
|
.expect("some part of a failing const eval must be local");
|
||||||
}
|
let mut lint = self.tcx.struct_span_lint_node(
|
||||||
if let Some(value) = self.eval_operand(value) {
|
::rustc::lint::builtin::CONST_ERR,
|
||||||
if let TerminatorKind::Assert { expected, msg, .. } = kind {
|
node_id,
|
||||||
if Value::ByVal(PrimVal::from_bool(*expected)) != value.0 {
|
span,
|
||||||
let span = self.mir[block]
|
"constant evaluation error",
|
||||||
.terminator
|
);
|
||||||
.as_ref()
|
use rustc::mir::AssertMessage::*;
|
||||||
.unwrap()
|
match msg {
|
||||||
.source_info
|
GeneratorResumedAfterReturn =>
|
||||||
.span;
|
lint.span_label(span, "generator resumed after completion"),
|
||||||
let node_id = self
|
GeneratorResumedAfterPanic =>
|
||||||
.tcx
|
lint.span_label(span, "generator resumed after panicking"),
|
||||||
.hir
|
Math(ref err) => lint.span_label(span, err.description()),
|
||||||
.as_local_node_id(self.source.def_id)
|
BoundsCheck { ref len, ref index } => {
|
||||||
.expect("some part of a failing const eval must be local");
|
let len = self.eval_operand(len).expect("len must be const");
|
||||||
let mut lint = self.tcx.struct_span_lint_node(
|
let len = match len.0 {
|
||||||
::rustc::lint::builtin::CONST_ERR,
|
Value::ByVal(PrimVal::Bytes(n)) => n,
|
||||||
node_id,
|
_ => bug!("const len not primitive: {:?}", len),
|
||||||
|
};
|
||||||
|
let index = self
|
||||||
|
.eval_operand(index)
|
||||||
|
.expect("index must be const");
|
||||||
|
let index = match index.0 {
|
||||||
|
Value::ByVal(PrimVal::Bytes(n)) => n,
|
||||||
|
_ => bug!("const index not primitive: {:?}", index),
|
||||||
|
};
|
||||||
|
lint.span_label(
|
||||||
span,
|
span,
|
||||||
"constant evaluation error",
|
format!(
|
||||||
);
|
"index out of bounds: \
|
||||||
use rustc::mir::AssertMessage::*;
|
the len is {} but the index is {}",
|
||||||
match msg {
|
len,
|
||||||
GeneratorResumedAfterReturn =>
|
index,
|
||||||
lint.span_label(span, "generator resumed after completion"),
|
),
|
||||||
GeneratorResumedAfterPanic =>
|
)
|
||||||
lint.span_label(span, "generator resumed after panicking"),
|
},
|
||||||
Math(ref err) => lint.span_label(span, err.description()),
|
}.emit();
|
||||||
BoundsCheck { ref len, ref index } => {
|
|
||||||
let len = self.eval_operand(len).expect("len must be const");
|
|
||||||
let len = match len.0 {
|
|
||||||
Value::ByVal(PrimVal::Bytes(n)) => n,
|
|
||||||
_ => bug!("const len not primitive: {:?}", len),
|
|
||||||
};
|
|
||||||
let index = self
|
|
||||||
.eval_operand(index)
|
|
||||||
.expect("index must be const");
|
|
||||||
let index = match index.0 {
|
|
||||||
Value::ByVal(PrimVal::Bytes(n)) => n,
|
|
||||||
_ => bug!("const index not primitive: {:?}", index),
|
|
||||||
};
|
|
||||||
lint.span_label(
|
|
||||||
span,
|
|
||||||
format!(
|
|
||||||
"index out of bounds: \
|
|
||||||
the len is {} but the index is {}",
|
|
||||||
len,
|
|
||||||
index,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
}.emit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// FIXME: do this optimization for function calls
|
|
||||||
_ => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue