This commit is contained in:
Oliver Schneider 2017-03-03 14:46:33 +01:00
parent 268b15f288
commit 26270c7451
2 changed files with 19 additions and 31 deletions

View File

@ -53,21 +53,6 @@ pub enum Constant {
Tuple(Vec<Constant>), Tuple(Vec<Constant>),
} }
impl Constant {
/// Convert to `u64` if possible.
///
/// # panics
///
/// If the constant could not be converted to `u64` losslessly.
fn as_u64(&self) -> u64 {
if let Constant::Int(val) = *self {
val.to_u64().expect("negative constant can't be casted to `u64`")
} else {
panic!("Could not convert a `{:?}` to `u64`", self);
}
}
}
impl PartialEq for Constant { impl PartialEq for Constant {
fn eq(&self, other: &Constant) -> bool { fn eq(&self, other: &Constant) -> bool {
match (self, other) { match (self, other) {
@ -266,11 +251,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
ExprLit(ref lit) => Some(lit_to_constant(&lit.node, self.tcx, self.tables.expr_ty(e))), ExprLit(ref lit) => Some(lit_to_constant(&lit.node, self.tcx, self.tables.expr_ty(e))),
ExprArray(ref vec) => self.multi(vec).map(Constant::Vec), ExprArray(ref vec) => self.multi(vec).map(Constant::Vec),
ExprTup(ref tup) => self.multi(tup).map(Constant::Tuple), ExprTup(ref tup) => self.multi(tup).map(Constant::Tuple),
ExprRepeat(ref value, number_id) => { ExprRepeat(ref value, _) => {
let val = &self.tcx.hir.body(number_id).value; let n = match self.tables.expr_ty(e).sty {
self.binop_apply(value, ty::TyArray(_, n) => n,
val, _ => span_bug!(e.span, "typeck error"),
|v, n| Some(Constant::Repeat(Box::new(v), n.as_u64() as usize))) };
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
}, },
ExprUnary(op, ref operand) => { ExprUnary(op, ref operand) => {
self.expr(operand).and_then(|o| match op { self.expr(operand).and_then(|o| match op {
@ -375,15 +361,4 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
_ => None, _ => None,
} }
} }
fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
where F: Fn(Constant, Constant) -> Option<Constant>
{
if let (Some(lc), Some(rc)) = (self.expr(left), self.expr(right)) {
op(lc, rc)
} else {
None
}
}
} }

View File

@ -0,0 +1,13 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(clippy)]
fn main() {
match 1 {
1 => {}
2 => {
[0; 1];
}
_ => {}
}
}