Skip to content

Commit 8488eeb

Browse files
committed
remove bool -> int conversion in const evaluator
the const evaluator has a bool constant value, no need to use integers
1 parent 3bd622f commit 8488eeb

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ pub struct ConstEvalErr {
390390
pub enum ErrKind {
391391
CannotCast,
392392
CannotCastTo(&'static str),
393+
InvalidOpForInts(hir::BinOp_),
394+
InvalidOpForUInts(hir::BinOp_),
393395
InvalidOpForBools(hir::BinOp_),
394396
InvalidOpForFloats(hir::BinOp_),
395397
InvalidOpForIntUint(hir::BinOp_),
@@ -428,6 +430,8 @@ impl ConstEvalErr {
428430
match self.kind {
429431
CannotCast => "can't cast this type".into_cow(),
430432
CannotCastTo(s) => format!("can't cast this type to {}", s).into_cow(),
433+
InvalidOpForInts(_) => "can't do this op on signed integrals".into_cow(),
434+
InvalidOpForUInts(_) => "can't do this op on unsigned integrals".into_cow(),
431435
InvalidOpForBools(_) => "can't do this op on bools".into_cow(),
432436
InvalidOpForFloats(_) => "can't do this op on floats".into_cow(),
433437
InvalidOpForIntUint(..) => "can't do this op on an isize and usize".into_cow(),
@@ -764,8 +768,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
764768
e: &Expr,
765769
ty_hint: EvalHint<'tcx>,
766770
fn_args: FnArgMap) -> EvalResult {
767-
fn fromb(b: bool) -> ConstVal { Int(b as i64) }
768-
769771
// Try to compute the type of the expression based on the EvalHint.
770772
// (See also the definition of EvalHint, and the FIXME above EvalHint.)
771773
let ety = match ty_hint {
@@ -837,13 +839,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
837839
hir::BiMul => Float(a * b),
838840
hir::BiDiv => Float(a / b),
839841
hir::BiRem => Float(a % b),
840-
hir::BiEq => fromb(a == b),
841-
hir::BiLt => fromb(a < b),
842-
hir::BiLe => fromb(a <= b),
843-
hir::BiNe => fromb(a != b),
844-
hir::BiGe => fromb(a >= b),
845-
hir::BiGt => fromb(a > b),
846-
_ => signal!(e, InvalidOpForFloats(op.node))
842+
hir::BiEq => Bool(a == b),
843+
hir::BiLt => Bool(a < b),
844+
hir::BiLe => Bool(a <= b),
845+
hir::BiNe => Bool(a != b),
846+
hir::BiGe => Bool(a >= b),
847+
hir::BiGt => Bool(a > b),
848+
_ => signal!(e, InvalidOpForFloats(op.node)),
847849
}
848850
}
849851
(Int(a), Int(b)) => {
@@ -853,17 +855,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
853855
hir::BiMul => try!(const_int_checked_mul(a,b,e,expr_int_type)),
854856
hir::BiDiv => try!(const_int_checked_div(a,b,e,expr_int_type)),
855857
hir::BiRem => try!(const_int_checked_rem(a,b,e,expr_int_type)),
856-
hir::BiAnd | hir::BiBitAnd => Int(a & b),
857-
hir::BiOr | hir::BiBitOr => Int(a | b),
858+
hir::BiBitAnd => Int(a & b),
859+
hir::BiBitOr => Int(a | b),
858860
hir::BiBitXor => Int(a ^ b),
859861
hir::BiShl => try!(const_int_checked_shl(a,b,e,expr_int_type)),
860862
hir::BiShr => try!(const_int_checked_shr(a,b,e,expr_int_type)),
861-
hir::BiEq => fromb(a == b),
862-
hir::BiLt => fromb(a < b),
863-
hir::BiLe => fromb(a <= b),
864-
hir::BiNe => fromb(a != b),
865-
hir::BiGe => fromb(a >= b),
866-
hir::BiGt => fromb(a > b)
863+
hir::BiEq => Bool(a == b),
864+
hir::BiLt => Bool(a < b),
865+
hir::BiLe => Bool(a <= b),
866+
hir::BiNe => Bool(a != b),
867+
hir::BiGe => Bool(a >= b),
868+
hir::BiGt => Bool(a > b),
869+
_ => signal!(e, InvalidOpForInts(op.node)),
867870
}
868871
}
869872
(Uint(a), Uint(b)) => {
@@ -873,17 +876,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
873876
hir::BiMul => try!(const_uint_checked_mul(a,b,e,expr_uint_type)),
874877
hir::BiDiv => try!(const_uint_checked_div(a,b,e,expr_uint_type)),
875878
hir::BiRem => try!(const_uint_checked_rem(a,b,e,expr_uint_type)),
876-
hir::BiAnd | hir::BiBitAnd => Uint(a & b),
877-
hir::BiOr | hir::BiBitOr => Uint(a | b),
879+
hir::BiBitAnd => Uint(a & b),
880+
hir::BiBitOr => Uint(a | b),
878881
hir::BiBitXor => Uint(a ^ b),
879882
hir::BiShl => try!(const_uint_checked_shl(a,b,e,expr_uint_type)),
880883
hir::BiShr => try!(const_uint_checked_shr(a,b,e,expr_uint_type)),
881-
hir::BiEq => fromb(a == b),
882-
hir::BiLt => fromb(a < b),
883-
hir::BiLe => fromb(a <= b),
884-
hir::BiNe => fromb(a != b),
885-
hir::BiGe => fromb(a >= b),
886-
hir::BiGt => fromb(a > b),
884+
hir::BiEq => Bool(a == b),
885+
hir::BiLt => Bool(a < b),
886+
hir::BiLe => Bool(a <= b),
887+
hir::BiNe => Bool(a != b),
888+
hir::BiGe => Bool(a >= b),
889+
hir::BiGt => Bool(a > b),
890+
_ => signal!(e, InvalidOpForUInts(op.node)),
887891
}
888892
}
889893
// shifts can have any integral type as their rhs

0 commit comments

Comments
 (0)