fix Eq+Hash for Constant

This commit is contained in:
Oliver Schneider 2016-03-16 16:28:31 +01:00
parent 432d9fec38
commit 64110f16dd
3 changed files with 11 additions and 6 deletions

View file

@ -85,7 +85,7 @@ impl PartialEq for Constant {
(&Constant::Str(ref ls, ref lsty), &Constant::Str(ref rs, ref rsty)) => ls == rs && lsty == rsty,
(&Constant::Binary(ref l), &Constant::Binary(ref r)) => l == r,
(&Constant::Char(l), &Constant::Char(r)) => l == r,
(&Constant::Int(l), &Constant::Int(r)) => l == r,
(&Constant::Int(l), &Constant::Int(r)) => l.is_negative() == r.is_negative() && l.to_u64_unchecked() == r.to_u64_unchecked(),
(&Constant::Float(ref ls, _), &Constant::Float(ref rs, _)) => {
// we want `Fw32 == FwAny` and `FwAny == Fw64`, by transitivity we must have
// `Fw32 == Fw64` so dont compare them
@ -119,7 +119,8 @@ impl Hash for Constant {
c.hash(state);
}
Constant::Int(i) => {
i.hash(state);
i.to_u64_unchecked().hash(state);
i.is_negative().hash(state);
}
Constant::Float(ref f, _) => {
// dont use the width here because of PartialEq implementation

View file

@ -55,11 +55,11 @@ impl LateLintPass for IdentityOp {
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
if let Some(Constant::Int(v)) = constant_simple(e) {
if let Some(v @ Constant::Int(_)) = constant_simple(e) {
if match m {
0 => v == ConstInt::Infer(0),
-1 => v == ConstInt::InferSigned(-1),
1 => v == ConstInt::Infer(1),
0 => v == Constant::Int(ConstInt::Infer(0)),
-1 => v == Constant::Int(ConstInt::InferSigned(-1)),
1 => v == Constant::Int(ConstInt::Infer(1)),
_ => unreachable!(),
} {
span_lint(cx,

View file

@ -86,4 +86,8 @@ fn test_ops() {
assert_eq!(half_any, half32);
assert_eq!(half_any, half64);
assert_eq!(half32, half64); // for transitivity
assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::U8(0)));
assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::I8(0)));
assert_eq!(Constant::Int(ConstInt::InferSigned(-1)), Constant::Int(ConstInt::I8(-1)));
}