changed const to consts to avoid keyword, added test, fixed a lot of bugs

This commit is contained in:
llogiq 2015-08-14 17:14:54 +02:00
parent 6aa36e9deb
commit f23af0cfd5
3 changed files with 136 additions and 84 deletions

View File

@ -1,8 +1,21 @@
#[cfg(test)]
use rustc::lint::Context; use rustc::lint::Context;
use rustc::middle::const_eval::lookup_const_by_id;
use syntax::ast::*;
use syntax::ptr::P;
use rustc::middle::const_eval::lookup_const_by_id;
use rustc::middle::def::PathResolution;
use rustc::middle::def::Def::*;
use syntax::ast::*;
use syntax::parse::token::InternedString;
use syntax::ptr::P;
use std::rc::Rc;
use std::ops::Deref;
use self::ConstantVariant::*;
use self::FloatWidth::*;
#[cfg(not(test))]
pub struct Context;
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum FloatWidth { pub enum FloatWidth {
Fw32, Fw32,
Fw64, Fw64,
@ -25,20 +38,31 @@ pub struct Constant {
} }
impl Constant { impl Constant {
fn new(variant: ConstantVariant) -> Constant { pub fn new(variant: ConstantVariant) -> Constant {
Constant { constant: variant, needed_resolution: false } Constant { constant: variant, needed_resolution: false }
} }
fn new_resolved(variant: ConstantVariant) -> Constant { pub fn new_resolved(variant: ConstantVariant) -> Constant {
Constant { constant: variant, needed_resolution: true } Constant { constant: variant, needed_resolution: true }
} }
// convert this constant to a f64, if possible
pub fn as_float(&self) -> Option<f64> {
match &self.constant {
&ConstantByte(b) => Some(b as f64),
&ConstantFloat(ref s, _) => s.parse().ok(),
&ConstantInt(i, ty) => Some(if is_negative(ty) {
-(i as f64) } else { i as f64 }),
_ => None
}
}
} }
/// a Lit_-like enum to fold constant `Expr`s into /// a Lit_-like enum to fold constant `Expr`s into
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Debug, Clone)]
pub enum ConstantVariant { pub enum ConstantVariant {
/// a String "abc" /// a String "abc"
ConstantStr(&'static str, StrStyle), ConstantStr(String, StrStyle),
/// a Binary String b"abc" /// a Binary String b"abc"
ConstantBinary(Rc<Vec<u8>>), ConstantBinary(Rc<Vec<u8>>),
/// a single byte b'a' /// a single byte b'a'
@ -48,15 +72,15 @@ pub enum ConstantVariant {
/// an integer /// an integer
ConstantInt(u64, LitIntType), ConstantInt(u64, LitIntType),
/// a float with given type /// a float with given type
ConstantFloat(Cow<'static, str>, FloatWidth), ConstantFloat(String, FloatWidth),
/// true or false /// true or false
ConstantBool(bool), ConstantBool(bool),
/// an array of constants /// an array of constants
ConstantVec(Vec<Constant>), ConstantVec(Box<Vec<Constant>>),
/// also an array, but with only one constant, repeated N times /// also an array, but with only one constant, repeated N times
ConstantRepeat(Constant, usize), ConstantRepeat(Box<ConstantVariant>, usize),
/// a tuple of constants /// a tuple of constants
ConstantTuple(Vec<Constant>), ConstantTuple(Box<Vec<Constant>>),
} }
impl ConstantVariant { impl ConstantVariant {
@ -76,34 +100,32 @@ impl ConstantVariant {
/// simple constant folding: Insert an expression, get a constant or none. /// simple constant folding: Insert an expression, get a constant or none.
pub fn constant(cx: &Context, e: &Expr) -> Option<Constant> { pub fn constant(cx: &Context, e: &Expr) -> Option<Constant> {
match e { match &e.node {
&ExprParen(ref inner) => constant(cx, inner), &ExprParen(ref inner) => constant(cx, inner),
&ExprPath(_, _) => fetch_path(cx, e), &ExprPath(_, _) => fetch_path(cx, e),
&ExprBlock(ref block) => constant_block(cx, inner), &ExprBlock(ref block) => constant_block(cx, block),
&ExprIf(ref cond, ref then, ref otherwise) => &ExprIf(ref cond, ref then, ref otherwise) =>
constant_if(cx, cond, then, otherwise), constant_if(cx, &*cond, &*then, &*otherwise),
&ExprLit(ref lit) => Some(lit_to_constant(lit)), &ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
&ExprVec(ref vec) => constant_vec(cx, vec), &ExprVec(ref vec) => constant_vec(cx, &vec[..]),
&ExprTup(ref tup) => constant_tup(cx, tup), &ExprTup(ref tup) => constant_tup(cx, &tup[..]),
&ExprRepeat(ref value, ref number) => &ExprRepeat(ref value, ref number) =>
constant_binop_apply(cx, value, number,|v, n| Constant { constant_binop_apply(cx, value, number,|v, n|
constant: ConstantRepeat(v, n.constant.as_u64()), Some(ConstantRepeat(Box::new(v), n.as_u64() as usize))),
needed_resolution: v.needed_resolution || n.needed_resolution
}),
&ExprUnary(op, ref operand) => constant(cx, operand).and_then( &ExprUnary(op, ref operand) => constant(cx, operand).and_then(
|o| match op { |o| match op {
UnNot => UnNot =>
if let ConstantBool(b) = o.variant { if let ConstantBool(b) = o.constant {
Some(Constant{ Some(Constant{
needed_resolution: o.needed_resolution, needed_resolution: o.needed_resolution,
constant: ConstantBool(!b), constant: ConstantBool(!b),
}) })
} else { None }, } else { None },
UnNeg => constant_negate(o), UnNeg => constant_negate(o),
UnUniq | UnDeref => o, UnUniq | UnDeref => Some(o),
}), }),
&ExprBinary(op, ref left, ref right) => &ExprBinary(op, ref left, ref right) =>
constant_binop(op, left, right), constant_binop(cx, op, left, right),
//TODO: add other expressions //TODO: add other expressions
_ => None, _ => None,
} }
@ -111,82 +133,93 @@ pub fn constant(cx: &Context, e: &Expr) -> Option<Constant> {
fn lit_to_constant(lit: &Lit_) -> Constant { fn lit_to_constant(lit: &Lit_) -> Constant {
match lit { match lit {
&LitStr(ref is, style) => Constant::new(ConstantStr(&*is, style)), &LitStr(ref is, style) =>
Constant::new(ConstantStr(is.to_string(), style)),
&LitBinary(ref blob) => Constant::new(ConstantBinary(blob.clone())), &LitBinary(ref blob) => Constant::new(ConstantBinary(blob.clone())),
&LitByte(b) => Constant::new(ConstantByte(b)), &LitByte(b) => Constant::new(ConstantByte(b)),
&LitChar(c) => Constant::new(ConstantChar(c)), &LitChar(c) => Constant::new(ConstantChar(c)),
&LitInt(value, ty) => Constant::new(ConstantInt(value, ty)), &LitInt(value, ty) => Constant::new(ConstantInt(value, ty)),
&LitFloat(ref is, ty) => &LitFloat(ref is, ty) => {
Constant::new(ConstantFloat(Cow::Borrowed(&*is), ty.into())), Constant::new(ConstantFloat(is.to_string(), ty.into()))
&LitFloatUnsuffixed(InternedString) => },
Constant::new(ConstantFloat(Cow::Borrowed(&*is), FwAny)), &LitFloatUnsuffixed(ref is) => {
Constant::new(ConstantFloat(is.to_string(), FwAny))
},
&LitBool(b) => Constant::new(ConstantBool(b)), &LitBool(b) => Constant::new(ConstantBool(b)),
} }
} }
/// create `Some(ConstantVec(..))` of all constants, unless there is any /// create `Some(ConstantVec(..))` of all constants, unless there is any
/// non-constant part /// non-constant part
fn constant_vec(cx: &Context, vec: &[&Expr]) -> Option<Constant> { fn constant_vec<E: Deref<Target=Expr> + Sized>(cx: &Context, vec: &[E]) -> Option<Constant> {
let mut parts = Vec::new(); let mut parts = Vec::new();
let mut resolved = false; let mut resolved = false;
for opt_part in vec { for opt_part in vec.iter() {
match constant(cx, opt_part) { match constant(cx, opt_part) {
Some(ref p) => { Some(p) => {
resolved |= p.needed_resolution; resolved |= (&p).needed_resolution;
parts.push(p) parts.push(p)
}, },
None => { return None; }, None => { return None; },
} }
} }
Some(Constant { Some(Constant {
constant: ConstantVec(parts), constant: ConstantVec(Box::new(parts)),
needed_resolution: resolved needed_resolution: resolved
}) })
} }
fn constant_tup(cx: &Context, tup: &[&Expr]) -> Option<Constant> { fn constant_tup<E: Deref<Target=Expr> + Sized>(cx: &Context, tup: &[E]) -> Option<Constant> {
let mut parts = Vec::new(); let mut parts = Vec::new();
let mut resolved = false; let mut resolved = false;
for opt_part in vec { for opt_part in tup.iter() {
match constant(cx, opt_part) { match constant(cx, opt_part) {
Some(ref p) => { Some(p) => {
resolved |= p.needed_resolution; resolved |= (&p).needed_resolution;
parts.push(p) parts.push(p)
}, },
None => { return None; }, None => { return None; },
} }
} }
Some(Constant { Some(Constant {
constant: ConstantTuple(parts), constant: ConstantTuple(Box::new(parts)),
needed_resolution: resolved needed_resolution: resolved
}) })
} }
#[cfg(test)]
fn fetch_path(_cx: &Context, _expr: &Expr) -> Option<Constant> { None }
/// lookup a possibly constant expression from a ExprPath /// lookup a possibly constant expression from a ExprPath
#[cfg(not(test))]
fn fetch_path(cx: &Context, e: &Expr) -> Option<Constant> { fn fetch_path(cx: &Context, e: &Expr) -> Option<Constant> {
if let Some(&PathResolution { base_def: DefConst(id), ..}) = if let Some(&PathResolution { base_def: DefConst(id), ..}) =
cx.tcx.def_map.borrow().get(&e.id) { cx.tcx.def_map.borrow().get(&e.id) {
lookup_const_by_id(cx.tcx, id, None).map( lookup_const_by_id(cx.tcx, id, None).and_then(
|l| Constant::new_resolved(constant(cx, l).constant)) |l| constant(cx, l).map(|c| Constant::new_resolved(c.constant)))
} else { None } } else { None }
} }
/// A block can only yield a constant if it only has one constant expression /// A block can only yield a constant if it only has one constant expression
fn constant_block(cx: &Context, block: &Block) -> Option<Constant> { fn constant_block(cx: &Context, block: &Block) -> Option<Constant> {
if block.stmts.is_empty() { if block.stmts.is_empty() {
block.expr.map(|b| constant(cx, b)) block.expr.as_ref().and_then(|b| constant(cx, &*b))
} else { None } } else { None }
} }
fn constant_if(cx: &Context, cond: &Expr, then: &Expr, otherwise: &Expr) -> fn constant_if(cx: &Context, cond: &Expr, then: &Block, otherwise:
Option<Constant> { &Option<P<Expr>>) -> Option<Constant> {
if let Some(Constant{ constant: ConstantBool(b), needed_resolution: res }) = if let Some(Constant{ constant: ConstantBool(b), needed_resolution: res }) =
constant(cx, cond) { constant(cx, cond) {
let part = constant(cx, if b { then } else { otherwise }); if b {
Some(Constant { constant_block(cx, then)
constant: part.constant, } else {
needed_resolution: res || part.needed_resolution, otherwise.as_ref().and_then(|expr| constant(cx, &*expr))
}) }.map(|part|
Constant {
constant: part.constant,
needed_resolution: res || part.needed_resolution,
})
} else { None } } else { None }
} }
@ -194,14 +227,15 @@ fn constant_negate(o: Constant) -> Option<Constant> {
Some(Constant{ Some(Constant{
needed_resolution: o.needed_resolution, needed_resolution: o.needed_resolution,
constant: match o.constant { constant: match o.constant {
&ConstantInt(value, ty) => ConstantInt(value, ty) =>
ConstantInt(value, match ty { ConstantInt(value, match ty {
SignedIntLit(ity, sign) => SignedIntLit(ity, sign) =>
SignedIntLit(ity, neg_sign(sign)), SignedIntLit(ity, neg_sign(sign)),
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)), UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
_ => { return None; }, _ => { return None; },
}), }),
&LitFloat(ref is, ref ty) => ConstantFloat(neg_float_str(is), ty), ConstantFloat(ref is, ty) =>
ConstantFloat(neg_float_str(is.to_string()), ty),
_ => { return None; }, _ => { return None; },
} }
}) })
@ -214,11 +248,11 @@ fn neg_sign(s: Sign) -> Sign {
} }
} }
fn neg_float_str(s: &InternedString) -> Cow<'static, str> { fn neg_float_str(s: String) -> String {
if s.startsWith('-') { if s.starts_with('-') {
Cow::Borrowed(s[1..]) s[1..].to_owned()
} else { } else {
Cow::Owned(format!("-{}", &*s)) format!("-{}", &*s)
} }
} }
@ -229,19 +263,19 @@ fn is_negative(ty: LitIntType) -> bool {
} }
} }
fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option(LitIntType) { fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option<LitIntType> {
match (l, r) { match (l, r) {
(SignedIntLit(lty, _), SignedIntLit(rty, _)) => if lty == rty { (SignedIntLit(lty, _), SignedIntLit(rty, _)) => if lty == rty {
Some(SignedIntLit(lty, s)) } else { None }, Some(SignedIntLit(lty, s)) } else { None },
(UnsignedIntLit(lty), UnsignedIntLit(rty)) => (UnsignedIntLit(lty), UnsignedIntLit(rty)) =>
if Sign == Plus && lty == rty { if s == Plus && lty == rty {
Some(UnsignedIntLit(lty)) Some(UnsignedIntLit(lty))
} else { None }, } else { None },
(UnsuffixedIntLit(_), UnsuffixedIntLit(_)) => UnsuffixedIntLit(s), (UnsuffixedIntLit(_), UnsuffixedIntLit(_)) => Some(UnsuffixedIntLit(s)),
(SignedIntLit(lty, _), UnsuffixedIntLit(_)) => SignedIntLit(lty, s), (SignedIntLit(lty, _), UnsuffixedIntLit(_)) => Some(SignedIntLit(lty, s)),
(UnsignedIntLit(lty), UnsuffixedIntLit(rs)) => if rs == Plus { (UnsignedIntLit(lty), UnsuffixedIntLit(rs)) => if rs == Plus {
Some(UnsignedIntLit(lty)) } else { None }, Some(UnsignedIntLit(lty)) } else { None },
(UnsuffixedIntLit(_), SignedIntLit(rty, _)) => SignedIntLit(rty, s), (UnsuffixedIntLit(_), SignedIntLit(rty, _)) => Some(SignedIntLit(rty, s)),
(UnsuffixedIntLit(ls), UnsignedIntLit(rty)) => if ls == Plus { (UnsuffixedIntLit(ls), UnsignedIntLit(rty)) => if ls == Plus {
Some(UnsignedIntLit(rty)) } else { None }, Some(UnsignedIntLit(rty)) } else { None },
_ => None, _ => None,
@ -312,7 +346,7 @@ fn constant_binop(cx: &Context, op: BinOp, left: &Expr, right: &Expr)
} }
fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) -> fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) ->
Some(Constant) { Option<ConstantVariant> {
if neg > pos { if neg > pos {
unify_int_type(nty, pty, Minus).map(|ty| ConstantInt(neg - pos, ty)) unify_int_type(nty, pty, Minus).map(|ty| ConstantInt(neg - pos, ty))
} else { } else {
@ -320,37 +354,43 @@ fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) ->
} }
} }
fn sub_int(l: u64, lty: LitIntType, r: u64, rty: LitIntType, neg: Bool) -> fn sub_int(l: u64, lty: LitIntType, r: u64, rty: LitIntType, neg: bool) ->
Option<Constant> { Option<ConstantVariant> {
unify_int_type(lty, rty, if neg { Minus } else { Plus }).and_then( unify_int_type(lty, rty, if neg { Minus } else { Plus }).and_then(
|ty| l64.checked_sub(r64).map(|v| ConstantInt(v, ty))) |ty| l.checked_sub(r).map(|v| ConstantInt(v, ty)))
} }
fn constant_binop_apply<F>(cx: &Context, left: &Expr, right: &Expr, op: F) fn constant_binop_apply<F>(cx: &Context, left: &Expr, right: &Expr, op: F)
-> Option<Constant> -> Option<Constant>
where F: FnMut(ConstantVariant, ConstantVariant) -> Option<ConstantVariant> { where F: Fn(ConstantVariant, ConstantVariant) -> Option<ConstantVariant> {
constant(cx, left).and_then(|l| constant(cx, right).and_then( if let (Some(Constant { constant: lc, needed_resolution: ln }),
|r| Constant { Some(Constant { constant: rc, needed_resolution: rn })) =
needed_resolution: l.needed_resolution || r.needed_resolution, (constant(cx, left), constant(cx, right)) {
constant: op(l.constant, r.constant) op(lc, rc).map(|c|
})) Constant {
needed_resolution: ln || rn,
constant: c,
})
} else { None }
} }
fn constant_short_circuit(cx: &Context, left: &Expr, right: &Expr, b: bool) -> fn constant_short_circuit(cx: &Context, left: &Expr, right: &Expr, b: bool) ->
Option<Constant> { Option<Constant> {
let leftconst = constant(cx, left); constant(cx, left).and_then(|left|
if let ConstantBool(lbool) = leftconst.constant { if let &ConstantBool(lbool) = &left.constant {
if l == b { if lbool == b {
Some(leftconst) Some(left)
} else { } else {
let rightconst = constant(cx, right); constant(cx, right).and_then(|right|
if let ConstantBool(rbool) = rightconst.constant { if let ConstantBool(_) = right.constant {
Some(Constant { Some(Constant {
constant: rightconst.constant, constant: right.constant,
needed_resolution: leftconst.needed_resolution || needed_resolution: left.needed_resolution ||
rightconst.needed_resolution, right.needed_resolution,
}) })
} else { None } } else { None }
} )
} else { None } }
} else { None }
)
} }

View File

@ -15,6 +15,7 @@ use rustc::lint::LintPassObject;
#[macro_use] #[macro_use]
pub mod utils; pub mod utils;
pub mod consts;
pub mod types; pub mod types;
pub mod misc; pub mod misc;
pub mod eq_op; pub mod eq_op;

11
tests/consts.rs Normal file
View File

@ -0,0 +1,11 @@
extern crate clippy;
use clippy::consts;
use syntax::ast::*;
#[test]
fn test_lit() {
assert_eq!(ConstantBool(true), constant(&Context,
Expr{ node_id: 1, node: ExprLit(LitBool(true)), span: default() }));
}