Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove bool -> int conversion in const evaluator #29797

Merged
merged 2 commits into from
Nov 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ pub struct ConstEvalErr {
pub enum ErrKind {
CannotCast,
CannotCastTo(&'static str),
InvalidOpForInts(hir::BinOp_),
InvalidOpForUInts(hir::BinOp_),
InvalidOpForBools(hir::BinOp_),
InvalidOpForFloats(hir::BinOp_),
InvalidOpForIntUint(hir::BinOp_),
Expand Down Expand Up @@ -428,6 +430,8 @@ impl ConstEvalErr {
match self.kind {
CannotCast => "can't cast this type".into_cow(),
CannotCastTo(s) => format!("can't cast this type to {}", s).into_cow(),
InvalidOpForInts(_) => "can't do this op on signed integrals".into_cow(),
InvalidOpForUInts(_) => "can't do this op on unsigned integrals".into_cow(),
InvalidOpForBools(_) => "can't do this op on bools".into_cow(),
InvalidOpForFloats(_) => "can't do this op on floats".into_cow(),
InvalidOpForIntUint(..) => "can't do this op on an isize and usize".into_cow(),
Expand Down Expand Up @@ -764,8 +768,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
e: &Expr,
ty_hint: EvalHint<'tcx>,
fn_args: FnArgMap) -> EvalResult {
fn fromb(b: bool) -> ConstVal { Int(b as i64) }

// Try to compute the type of the expression based on the EvalHint.
// (See also the definition of EvalHint, and the FIXME above EvalHint.)
let ety = match ty_hint {
Expand Down Expand Up @@ -837,13 +839,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
hir::BiMul => Float(a * b),
hir::BiDiv => Float(a / b),
hir::BiRem => Float(a % b),
hir::BiEq => fromb(a == b),
hir::BiLt => fromb(a < b),
hir::BiLe => fromb(a <= b),
hir::BiNe => fromb(a != b),
hir::BiGe => fromb(a >= b),
hir::BiGt => fromb(a > b),
_ => signal!(e, InvalidOpForFloats(op.node))
hir::BiEq => Bool(a == b),
hir::BiLt => Bool(a < b),
hir::BiLe => Bool(a <= b),
hir::BiNe => Bool(a != b),
hir::BiGe => Bool(a >= b),
hir::BiGt => Bool(a > b),
_ => signal!(e, InvalidOpForFloats(op.node)),
}
}
(Int(a), Int(b)) => {
Expand All @@ -853,17 +855,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
hir::BiMul => try!(const_int_checked_mul(a,b,e,expr_int_type)),
hir::BiDiv => try!(const_int_checked_div(a,b,e,expr_int_type)),
hir::BiRem => try!(const_int_checked_rem(a,b,e,expr_int_type)),
hir::BiAnd | hir::BiBitAnd => Int(a & b),
hir::BiOr | hir::BiBitOr => Int(a | b),
hir::BiBitAnd => Int(a & b),
hir::BiBitOr => Int(a | b),
hir::BiBitXor => Int(a ^ b),
hir::BiShl => try!(const_int_checked_shl(a,b,e,expr_int_type)),
hir::BiShr => try!(const_int_checked_shr(a,b,e,expr_int_type)),
hir::BiEq => fromb(a == b),
hir::BiLt => fromb(a < b),
hir::BiLe => fromb(a <= b),
hir::BiNe => fromb(a != b),
hir::BiGe => fromb(a >= b),
hir::BiGt => fromb(a > b)
hir::BiEq => Bool(a == b),
hir::BiLt => Bool(a < b),
hir::BiLe => Bool(a <= b),
hir::BiNe => Bool(a != b),
hir::BiGe => Bool(a >= b),
hir::BiGt => Bool(a > b),
_ => signal!(e, InvalidOpForInts(op.node)),
}
}
(Uint(a), Uint(b)) => {
Expand All @@ -873,17 +876,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
hir::BiMul => try!(const_uint_checked_mul(a,b,e,expr_uint_type)),
hir::BiDiv => try!(const_uint_checked_div(a,b,e,expr_uint_type)),
hir::BiRem => try!(const_uint_checked_rem(a,b,e,expr_uint_type)),
hir::BiAnd | hir::BiBitAnd => Uint(a & b),
hir::BiOr | hir::BiBitOr => Uint(a | b),
hir::BiBitAnd => Uint(a & b),
hir::BiBitOr => Uint(a | b),
hir::BiBitXor => Uint(a ^ b),
hir::BiShl => try!(const_uint_checked_shl(a,b,e,expr_uint_type)),
hir::BiShr => try!(const_uint_checked_shr(a,b,e,expr_uint_type)),
hir::BiEq => fromb(a == b),
hir::BiLt => fromb(a < b),
hir::BiLe => fromb(a <= b),
hir::BiNe => fromb(a != b),
hir::BiGe => fromb(a >= b),
hir::BiGt => fromb(a > b),
hir::BiEq => Bool(a == b),
hir::BiLt => Bool(a < b),
hir::BiLe => Bool(a <= b),
hir::BiNe => Bool(a != b),
hir::BiGe => Bool(a >= b),
hir::BiGt => Bool(a > b),
_ => signal!(e, InvalidOpForUInts(op.node)),
}
}
// shifts can have any integral type as their rhs
Expand Down
46 changes: 46 additions & 0 deletions src/test/compile-fail/const-integer-bool-ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

const X: usize = 42 && 39; //~ ERROR: can't do this op on unsigned integrals
const ARR: [i32; X] = [99; 34]; //~ NOTE: for array length here

const X1: usize = 42 || 39; //~ ERROR: can't do this op on unsigned integrals
const ARR1: [i32; X1] = [99; 47]; //~ NOTE: for array length here

// FIXME: the error should be `on signed integrals`
const X2: usize = -42 || -39; //~ ERROR: can't do this op on unsigned integrals
const ARR2: [i32; X2] = [99; 18446744073709551607]; //~ NOTE: for array length here

// FIXME: the error should be `on signed integrals`
const X3: usize = -42 && -39; //~ ERROR: can't do this op on unsigned integrals
const ARR3: [i32; X3] = [99; 6]; //~ NOTE: for array length here

const Y: usize = 42.0 == 42.0;
const ARRR: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
const Y1: usize = 42.0 >= 42.0;
const ARRR1: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
const Y2: usize = 42.0 <= 42.0;
const ARRR2: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length
const Y3: usize = 42.0 > 42.0;
const ARRR3: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length
const Y4: usize = 42.0 < 42.0;
const ARRR4: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length
const Y5: usize = 42.0 != 42.0;
const ARRR5: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length

fn main() {
let _ = ARR;
let _ = ARRR;
let _ = ARRR1;
let _ = ARRR2;
let _ = ARRR3;
let _ = ARRR4;
let _ = ARRR5;
}