Skip to content

Commit 66e68eb

Browse files
committed
---
yaml --- r: 188303 b: refs/heads/tmp c: f1ea2b3 h: refs/heads/master i: 188301: d2b4e33 188299: 0eca1fd 188295: f8b2dbb 188287: 787ea4e v: v3
1 parent 8ed3bfb commit 66e68eb

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
3434
refs/heads/beta: 522d09dfecbeca1595f25ac58c6d0178bbd21d7d
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
37-
refs/heads/tmp: faf3bcd72c85774805ae0e84d0458aa3e67b20e4
37+
refs/heads/tmp: f1ea2b3094b1c28e64af30e187e31aa82f5ff004
3838
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/tmp/src/librustc/middle/const_eval.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use syntax::parse::token::InternedString;
2525
use syntax::ptr::P;
2626
use syntax::{ast_map, ast_util, codemap};
2727

28+
use std::num::wrapping::OverflowingOps;
2829
use std::cmp::Ordering;
2930
use std::collections::hash_map::Entry::Vacant;
3031
use std::{i8, i16, i32, i64};
@@ -206,6 +207,33 @@ pub fn eval_const_expr(tcx: &ty::ctxt, e: &Expr) -> const_val {
206207
}
207208
}
208209

210+
fn checked_add_int(a: i64, b: i64) -> Result<const_val, String> {
211+
let (ret, oflo) = a.overflowing_add(b);
212+
if !oflo { Ok(const_int(ret)) } else { Err(format!("constant arithmetic overflow")) }
213+
}
214+
fn checked_sub_int(a: i64, b: i64) -> Result<const_val, String> {
215+
let (ret, oflo) = a.overflowing_sub(b);
216+
if !oflo { Ok(const_int(ret)) } else { Err(format!("constant arithmetic overflow")) }
217+
}
218+
fn checked_mul_int(a: i64, b: i64) -> Result<const_val, String> {
219+
let (ret, oflo) = a.overflowing_mul(b);
220+
if !oflo { Ok(const_int(ret)) } else { Err(format!("constant arithmetic overflow")) }
221+
}
222+
223+
fn checked_add_uint(a: u64, b: u64) -> Result<const_val, String> {
224+
let (ret, oflo) = a.overflowing_add(b);
225+
if !oflo { Ok(const_uint(ret)) } else { Err(format!("constant arithmetic overflow")) }
226+
}
227+
fn checked_sub_uint(a: u64, b: u64) -> Result<const_val, String> {
228+
let (ret, oflo) = a.overflowing_sub(b);
229+
if !oflo { Ok(const_uint(ret)) } else { Err(format!("constant arithmetic overflow")) }
230+
}
231+
fn checked_mul_uint(a: u64, b: u64) -> Result<const_val, String> {
232+
let (ret, oflo) = a.overflowing_mul(b);
233+
if !oflo { Ok(const_uint(ret)) } else { Err(format!("constant arithmetic overflow")) }
234+
}
235+
236+
209237
pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
210238
e: &Expr,
211239
ty_hint: Option<Ty<'tcx>>)
@@ -276,9 +304,9 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
276304
}
277305
};
278306
match op.node {
279-
ast::BiAdd => Ok(const_int(a + b)),
280-
ast::BiSub => Ok(const_int(a - b)),
281-
ast::BiMul => Ok(const_int(a * b)),
307+
ast::BiAdd => checked_add_int(a, b),
308+
ast::BiSub => checked_sub_int(a, b),
309+
ast::BiMul => checked_mul_int(a, b),
282310
ast::BiDiv => {
283311
if b == 0 {
284312
Err("attempted to divide by zero".to_string())
@@ -312,9 +340,9 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
312340
}
313341
(Ok(const_uint(a)), Ok(const_uint(b))) => {
314342
match op.node {
315-
ast::BiAdd => Ok(const_uint(a + b)),
316-
ast::BiSub => Ok(const_uint(a - b)),
317-
ast::BiMul => Ok(const_uint(a * b)),
343+
ast::BiAdd => checked_add_uint(a, b),
344+
ast::BiSub => checked_sub_uint(a, b),
345+
ast::BiMul => checked_mul_uint(a, b),
318346
ast::BiDiv if b == 0 => {
319347
Err("attempted to divide by zero".to_string())
320348
}

0 commit comments

Comments
 (0)