Skip to content

Commit 841e36e

Browse files
committed
Auto merge of #29797 - oli-obk:remove-fromb, r=nikomatsakis
the const evaluator has a bool constant value, no need to use integers the `fromb` function is very old. It took me a while of git-blame until i found where it was created. I think it was just a hack. All tests still pass. I also forbade `&&` and `||` on integral types
2 parents b2f5393 + 96cfac6 commit 841e36e

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-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
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const X: usize = 42 && 39; //~ ERROR: can't do this op on unsigned integrals
12+
const ARR: [i32; X] = [99; 34]; //~ NOTE: for array length here
13+
14+
const X1: usize = 42 || 39; //~ ERROR: can't do this op on unsigned integrals
15+
const ARR1: [i32; X1] = [99; 47]; //~ NOTE: for array length here
16+
17+
// FIXME: the error should be `on signed integrals`
18+
const X2: usize = -42 || -39; //~ ERROR: can't do this op on unsigned integrals
19+
const ARR2: [i32; X2] = [99; 18446744073709551607]; //~ NOTE: for array length here
20+
21+
// FIXME: the error should be `on signed integrals`
22+
const X3: usize = -42 && -39; //~ ERROR: can't do this op on unsigned integrals
23+
const ARR3: [i32; X3] = [99; 6]; //~ NOTE: for array length here
24+
25+
const Y: usize = 42.0 == 42.0;
26+
const ARRR: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
27+
const Y1: usize = 42.0 >= 42.0;
28+
const ARRR1: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
29+
const Y2: usize = 42.0 <= 42.0;
30+
const ARRR2: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
31+
const Y3: usize = 42.0 > 42.0;
32+
const ARRR3: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length
33+
const Y4: usize = 42.0 < 42.0;
34+
const ARRR4: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length
35+
const Y5: usize = 42.0 != 42.0;
36+
const ARRR5: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length
37+
38+
fn main() {
39+
let _ = ARR;
40+
let _ = ARRR;
41+
let _ = ARRR1;
42+
let _ = ARRR2;
43+
let _ = ARRR3;
44+
let _ = ARRR4;
45+
let _ = ARRR5;
46+
}

0 commit comments

Comments
 (0)