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

cleanup overflow binop code #28

Merged
merged 19 commits into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'tcx> Error for EvalError<'tcx> {
EvalError::Math(..) =>
"mathematical operation failed",
EvalError::InvalidBitShiftRhs(..) =>
"bit shift rhs negative or not an int",
"bit shift rhs not an int",
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't have EvalError variants for things the type checker should catch. If a primitive bit shift RHS isn't an integer, we can just panic.

}
}

Expand Down
27 changes: 19 additions & 8 deletions src/primval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,26 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
match bin_op {
// can have rhs with a different numeric type
Shl | Shr => {
let mask_bits = match left {
Copy link
Member

Choose a reason for hiding this comment

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

@oli-obk Can you add a comment explaining the masking? It's a bit non-obvious.

@eddyb Is there a source in rustc or the docs we can refer to for this?

Copy link
Member

Choose a reason for hiding this comment

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

I8(_) => 3,
I16(_) => 4,
I32(_) => 5,
I64(_) => 6,
U8(_) => 3,
U16(_) => 4,
U32(_) => 5,
U64(_) => 6,
_ => unreachable!(),
};
let r = match right {
I8(i) if i >= 0 => i as u32,
I16(i) if i >= 0 => i as u32,
I32(i) if i >= 0 => i as u32,
I64(i) if i >= 0 && i as i32 as i64 == i => i as u32,
U8(i) => i as u32,
U16(i) => i as u32,
U32(i) => i,
U64(i) if i as u32 as u64 == i => i as u32,
I8(i) => (i & ((1 << mask_bits) - 1)) as u32,
I16(i) => (i & ((1 << mask_bits) - 1)) as u32,
I32(i) => (i & ((1 << mask_bits) - 1)) as u32,
I64(i) => (i & ((1 << mask_bits) - 1)) as u32,
U8(i) => (i & ((1 << mask_bits) - 1)) as u32,
U16(i) => (i & ((1 << mask_bits) - 1)) as u32,
U32(i) => (i & ((1 << mask_bits) - 1)) as u32,
U64(i) => (i & ((1 << mask_bits) - 1)) as u32,
_ => return Err(EvalError::InvalidBitShiftRhs(right)),
};
macro_rules! shift {
Expand Down