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

Generalize binary operators #23673

Merged
merged 5 commits into from
Mar 30, 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
6 changes: 3 additions & 3 deletions src/libcollectionstest/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ macro_rules! map_insert_rand_bench {
let mut rng = rand::weak_rng();

for _ in 0..n {
let i = rng.gen() % n;
let i = rng.gen::<usize>() % n;
map.insert(i, i);
}

// measure
b.iter(|| {
let k = rng.gen() % n;
let k = rng.gen::<usize>() % n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely not be usize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I presume because that would alter what is being tested when run on 32-bit and 64-bit systems? Makes sense. I can change to u32.

map.insert(k, k);
map.remove(&k);
});
Expand Down Expand Up @@ -77,7 +77,7 @@ macro_rules! map_find_rand_bench {

// setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen() % n).collect();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen::<usize>() % n).collect();

for &k in &keys {
map.insert(k, k);
Expand Down
24 changes: 2 additions & 22 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ pub trait Neg {
macro_rules! neg_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(unsigned_negation)]
impl Neg for $t {
#[stable(feature = "rust1", since = "1.0.0")]
type Output = $t;
Expand All @@ -498,28 +499,7 @@ macro_rules! neg_impl {
)*)
}

macro_rules! neg_uint_impl {
($t:ty, $t_signed:ty) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Neg for $t {
type Output = $t;

#[inline]
fn neg(self) -> $t { -(self as $t_signed) as $t }
}

forward_ref_unop! { impl Neg, neg for $t }
}
}

neg_impl! { isize i8 i16 i32 i64 f32 f64 }

neg_uint_impl! { usize, isize }
neg_uint_impl! { u8, i8 }
neg_uint_impl! { u16, i16 }
neg_uint_impl! { u32, i32 }
neg_uint_impl! { u64, i64 }

neg_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }

/// The `Not` trait is used to specify the functionality of unary `!`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/librand/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn ziggurat<R: Rng, P, Z>(
return zero_case(rng, u);
}
// algebraically equivalent to f1 + DRanU()*(f0 - f1) < 1
if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.gen() < pdf(x) {
if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.gen::<f64>() < pdf(x) {
return x;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librand/distributions/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ macro_rules! float_impl {
}
}
fn sample_range<R: Rng>(r: &Range<$ty>, rng: &mut R) -> $ty {
r.low + r.range * rng.gen()
r.low + r.range * rng.gen::<$ty>()
}
}
}
Expand Down
84 changes: 10 additions & 74 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,10 @@ pub fn mk_nil<'tcx>(cx: &ctxt<'tcx>) -> Ty<'tcx> {
mk_tup(cx, Vec::new())
}

pub fn mk_bool<'tcx>(cx: &ctxt<'tcx>) -> Ty<'tcx> {
mk_t(cx, ty_bool)
}

pub fn mk_bare_fn<'tcx>(cx: &ctxt<'tcx>,
opt_def_id: Option<ast::DefId>,
fty: &'tcx BareFnTy<'tcx>) -> Ty<'tcx> {
Expand Down Expand Up @@ -3406,8 +3410,12 @@ pub fn type_is_scalar(ty: Ty) -> bool {
/// Returns true if this type is a floating point type and false otherwise.
pub fn type_is_floating_point(ty: Ty) -> bool {
match ty.sty {
ty_float(_) => true,
_ => false,
ty_float(_) |
ty_infer(FloatVar(_)) =>
true,

_ =>
false,
}
}

Expand Down Expand Up @@ -5805,78 +5813,6 @@ pub fn closure_upvars<'tcx>(typer: &mc::Typer<'tcx>,
}
}

pub fn is_binopable<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>, op: ast::BinOp) -> bool {
#![allow(non_upper_case_globals)]
const tycat_other: isize = 0;
const tycat_bool: isize = 1;
const tycat_char: isize = 2;
const tycat_int: isize = 3;
const tycat_float: isize = 4;
const tycat_raw_ptr: isize = 6;

const opcat_add: isize = 0;
const opcat_sub: isize = 1;
const opcat_mult: isize = 2;
const opcat_shift: isize = 3;
const opcat_rel: isize = 4;
const opcat_eq: isize = 5;
const opcat_bit: isize = 6;
const opcat_logic: isize = 7;
const opcat_mod: isize = 8;

fn opcat(op: ast::BinOp) -> isize {
match op.node {
ast::BiAdd => opcat_add,
ast::BiSub => opcat_sub,
ast::BiMul => opcat_mult,
ast::BiDiv => opcat_mult,
ast::BiRem => opcat_mod,
ast::BiAnd => opcat_logic,
ast::BiOr => opcat_logic,
ast::BiBitXor => opcat_bit,
ast::BiBitAnd => opcat_bit,
ast::BiBitOr => opcat_bit,
ast::BiShl => opcat_shift,
ast::BiShr => opcat_shift,
ast::BiEq => opcat_eq,
ast::BiNe => opcat_eq,
ast::BiLt => opcat_rel,
ast::BiLe => opcat_rel,
ast::BiGe => opcat_rel,
ast::BiGt => opcat_rel
}
}

fn tycat<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> isize {
if type_is_simd(cx, ty) {
return tycat(cx, simd_type(cx, ty))
}
match ty.sty {
ty_char => tycat_char,
ty_bool => tycat_bool,
ty_int(_) | ty_uint(_) | ty_infer(IntVar(_)) => tycat_int,
ty_float(_) | ty_infer(FloatVar(_)) => tycat_float,
ty_ptr(_) => tycat_raw_ptr,
_ => tycat_other
}
}

const t: bool = true;
const f: bool = false;

let tbl = [
// +, -, *, shift, rel, ==, bit, logic, mod
/*other*/ [f, f, f, f, f, f, f, f, f],
/*bool*/ [f, f, f, f, t, t, t, t, f],
/*char*/ [f, f, f, f, t, t, f, f, f],
/*isize*/ [t, t, t, t, t, t, t, f, t],
/*float*/ [t, t, t, f, t, t, f, f, f],
/*bot*/ [t, t, t, t, t, t, t, t, t],
/*raw ptr*/ [f, f, f, f, t, t, f, f, f]];

return tbl[tycat(cx, ty) as usize ][opcat(op) as usize];
}

// Returns the repeat count for a repeating vector expression.
pub fn eval_repeat_count(tcx: &ctxt, count_expr: &ast::Expr) -> usize {
match const_eval::eval_const_expr_partial(tcx, count_expr, Some(tcx.types.usize)) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ pub fn compare_scalar_types<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
_ => bcx.sess().bug("compare_scalar_types: must be a comparison operator")
}
}
ty::ty_bool | ty::ty_uint(_) | ty::ty_char => {
ty::ty_bare_fn(..) | ty::ty_bool | ty::ty_uint(_) | ty::ty_char => {
ICmp(bcx, bin_op_to_icmp_predicate(bcx.ccx(), op, false), lhs, rhs, debug_loc)
}
ty::ty_ptr(mt) if common::type_is_sized(bcx.tcx(), mt.ty) => {
Expand Down
12 changes: 11 additions & 1 deletion src/librustc_trans/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,14 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
e: &ast::Expr,
ety: Ty<'tcx>,
param_substs: &'tcx Substs<'tcx>) -> ValueRef {
param_substs: &'tcx Substs<'tcx>)
-> ValueRef
{
debug!("const_expr_unadjusted(e={}, ety={}, param_substs={})",
e.repr(cx.tcx()),
ety.repr(cx.tcx()),
param_substs.repr(cx.tcx()));

let map_list = |exprs: &[P<ast::Expr>]| {
exprs.iter().map(|e| const_expr(cx, &**e, param_substs).0)
.fold(Vec::new(), |mut l, val| { l.push(val); l })
Expand All @@ -366,6 +373,9 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
/* Neither type is bottom, and we expect them to be unified
* already, so the following is safe. */
let (te1, ty) = const_expr(cx, &**e1, param_substs);
debug!("const_expr_unadjusted: te1={}, ty={}",
cx.tn().val_to_string(te1),
ty.repr(cx.tcx()));
let is_simd = ty::type_is_simd(cx.tcx(), ty);
let intype = if is_simd {
ty::simd_type(cx.tcx(), ty)
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,7 @@ fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}
}

#[derive(Debug)]
enum OverflowOp {
Add,
Sub,
Expand Down Expand Up @@ -2413,6 +2414,7 @@ enum OverflowCodegen {

enum OverflowOpViaInputCheck { Shl, Shr, }

#[derive(Debug)]
enum OverflowOpViaIntrinsic { Add, Sub, Mul, }

impl OverflowOpViaIntrinsic {
Expand All @@ -2437,7 +2439,8 @@ impl OverflowOpViaIntrinsic {
_ => panic!("unsupported target word size")
},
ref t @ ty_uint(_) | ref t @ ty_int(_) => t.clone(),
_ => panic!("tried to get overflow intrinsic for non-int type")
_ => panic!("tried to get overflow intrinsic for {:?} applied to non-int type",
*self)
};

match *self {
Expand Down
4 changes: 0 additions & 4 deletions src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use super::autoderef;
use super::AutorefArgs;
use super::check_argument_types;
use super::check_expr;
use super::check_method_argument_types;
Expand Down Expand Up @@ -258,7 +257,6 @@ fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
&fn_sig.inputs,
&expected_arg_tys[..],
arg_exprs,
AutorefArgs::No,
fn_sig.variadic,
TupleArgumentsFlag::DontTupleArguments);

Expand Down Expand Up @@ -288,7 +286,6 @@ fn confirm_deferred_closure_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
&*fn_sig.inputs,
&*expected_arg_tys,
arg_exprs,
AutorefArgs::No,
fn_sig.variadic,
TupleArgumentsFlag::TupleArguments);

Expand All @@ -308,7 +305,6 @@ fn confirm_overloaded_call<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
method_callee.ty,
callee_expr,
arg_exprs,
AutorefArgs::No,
TupleArgumentsFlag::TupleArguments,
expected);
write_call(fcx, call_expr, output_type);
Expand Down
Loading