Skip to content

Commit eada860

Browse files
committed
Small fixes in #802
1 parent 51e63a1 commit eada860

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/types.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc::ty;
55
use rustc_front::hir::*;
66
use rustc_front::intravisit::{FnKind, Visitor, walk_ty};
77
use rustc_front::util::{is_comparison_binop, binop_to_string};
8+
use std::cmp::Ordering;
89
use syntax::ast::{IntTy, UintTy, FloatTy};
910
use syntax::codemap::Span;
1011
use utils::*;
@@ -803,8 +804,6 @@ enum FullInt {
803804
U(u64),
804805
}
805806

806-
use std::cmp::Ordering;
807-
808807
impl FullInt {
809808
#[allow(cast_sign_loss)]
810809
fn cmp_s_u(s: i64, u: u64) -> Ordering {
@@ -843,8 +842,7 @@ impl Ord for FullInt {
843842

844843
fn numeric_cast_precast_bounds<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(FullInt, FullInt)> {
845844
use rustc::ty::TypeVariants::{TyInt, TyUint};
846-
use syntax::ast::UintTy;
847-
use syntax::ast::IntTy;
845+
use syntax::ast::{IntTy, UintTy};
848846
use std::*;
849847

850848
if let ExprCast(ref cast_exp,_) = expr.node {
@@ -912,21 +910,21 @@ fn upcast_comparison_bounds_err(
912910
lhs_bounds: Option<(FullInt, FullInt)>, lhs: &Expr, rhs: &Expr, invert: bool) {
913911
use utils::comparisons::*;
914912

915-
if let Some(nlb) = lhs_bounds {
913+
if let Some((lb, ub)) = lhs_bounds {
916914
if let Some(norm_rhs_val) = node_as_const_fullint(cx, rhs) {
917915
if rel == Rel::Eq || rel == Rel::Ne {
918-
if norm_rhs_val < nlb.0 || norm_rhs_val > nlb.0 {
916+
if norm_rhs_val < lb || norm_rhs_val > ub {
919917
err_upcast_comparison(cx, &span, lhs, rel == Rel::Ne);
920918
}
921919
} else if match rel {
922-
Rel::Lt => if invert { norm_rhs_val < nlb.0 } else { nlb.1 < norm_rhs_val },
923-
Rel::Le => if invert { norm_rhs_val <= nlb.0 } else { nlb.1 <= norm_rhs_val },
920+
Rel::Lt => if invert { norm_rhs_val < lb } else { ub < norm_rhs_val },
921+
Rel::Le => if invert { norm_rhs_val <= lb } else { ub <= norm_rhs_val },
924922
Rel::Eq | Rel::Ne => unreachable!(),
925923
} {
926924
err_upcast_comparison(cx, &span, lhs, true)
927925
} else if match rel {
928-
Rel::Lt => if invert { norm_rhs_val >= nlb.1 } else { nlb.0 >= norm_rhs_val },
929-
Rel::Le => if invert { norm_rhs_val > nlb.1 } else { nlb.0 > norm_rhs_val },
926+
Rel::Lt => if invert { norm_rhs_val >= ub } else { lb >= norm_rhs_val },
927+
Rel::Le => if invert { norm_rhs_val > ub } else { lb > norm_rhs_val },
930928
Rel::Eq | Rel::Ne => unreachable!(),
931929
} {
932930
err_upcast_comparison(cx, &span, lhs, false)

tests/compile-fail/invalid_upcast_comparisons.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ fn main() {
1919

2020
-5 > (zero as i32); //~ERROR because of the numeric bounds on `zero` prior to casting, this expression is always false
2121
-5 >= (u8_max as i32); //~ERROR because of the numeric bounds on `u8_max` prior to casting, this expression is always false
22+
1337 == (u8_max as i32); //~ERROR because of the numeric bounds on `u8_max` prior to casting, this expression is always false
2223

2324
-5 == (zero as i32); //~ERROR because of the numeric bounds on `zero` prior to casting, this expression is always false
2425
-5 != (u8_max as i32); //~ERROR because of the numeric bounds on `u8_max` prior to casting, this expression is always true
26+
27+
// Those are Ok:
28+
42 == (u8_max as i32);
29+
42 != (u8_max as i32);
30+
42 > (u8_max as i32);
31+
(u8_max as i32) == 42;
32+
(u8_max as i32) != 42;
33+
(u8_max as i32) > 42;
34+
(u8_max as i32) < 42;
2535
}

0 commit comments

Comments
 (0)