mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-30 04:56:25 +03:00
fix Eq+Hash for Constant
This commit is contained in:
+3
-2
@@ -85,7 +85,7 @@ fn eq(&self, other: &Constant) -> bool {
|
||||
(&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 don’t compare them
|
||||
@@ -119,7 +119,8 @@ fn hash<H>(&self, state: &mut H)
|
||||
c.hash(state);
|
||||
}
|
||||
Constant::Int(i) => {
|
||||
i.hash(state);
|
||||
i.to_u64_unchecked().hash(state);
|
||||
i.is_negative().hash(state);
|
||||
}
|
||||
Constant::Float(ref f, _) => {
|
||||
// don’t use the width here because of PartialEq implementation
|
||||
|
||||
+4
-4
@@ -55,11 +55,11 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
|
||||
|
||||
|
||||
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,
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user