Skip to content

Commit 11057fe

Browse files
committed
Incorporated first review sugestion from eddyb.
Suggestion was here: #22532 (comment)
1 parent b85b3c9 commit 11057fe

File tree

1 file changed

+34
-100
lines changed

1 file changed

+34
-100
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 34 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -283,106 +283,40 @@ impl ConstEvalErr {
283283
}
284284
}
285285

286-
fn invalid_op_for_bools(e: &Expr, op: ast::BinOp_) -> ConstEvalErr {
287-
ConstEvalErr { span: e.span, kind: ErrKind::InvalidOpForBools(op) }
288-
}
289-
fn invalid_op_for_floats(e: &Expr, op: ast::BinOp_) -> ConstEvalErr {
290-
ConstEvalErr { span: e.span, kind: ErrKind::InvalidOpForFloats(op) }
291-
}
292-
fn invalid_op_for_int_uint(e: &Expr, op: ast::BinOp_) -> ConstEvalErr {
293-
ConstEvalErr { span: e.span, kind: ErrKind::InvalidOpForIntUint(op) }
294-
}
295-
fn invalid_op_for_uint_int(e: &Expr, op: ast::BinOp_) -> ConstEvalErr {
296-
ConstEvalErr { span: e.span, kind: ErrKind::InvalidOpForUintInt(op) }
297-
}
298-
fn negate_on_string(e: &Expr) -> ConstEvalErr {
299-
ConstEvalErr { span: e.span, kind: ErrKind::NegateOnString }
300-
}
301-
fn negate_on_boolean(e: &Expr) -> ConstEvalErr {
302-
ConstEvalErr { span: e.span, kind: ErrKind::NegateOnBoolean }
303-
}
304-
fn not_on_float(e: &Expr) -> ConstEvalErr {
305-
ConstEvalErr { span: e.span, kind: ErrKind::NotOnFloat }
306-
}
307-
fn not_on_string(e: &Expr) -> ConstEvalErr {
308-
ConstEvalErr { span: e.span, kind: ErrKind::NotOnString }
309-
}
310-
311-
fn addi_with_overflow(e: &Expr, a: i64, b: i64) -> ConstEvalErr {
312-
ConstEvalErr { span: e.span, kind: ErrKind::AddiWithOverflow(a, b) }
313-
}
314-
fn subi_with_overflow(e: &Expr, a: i64, b: i64) -> ConstEvalErr {
315-
ConstEvalErr { span: e.span, kind: ErrKind::SubiWithOverflow(a, b) }
316-
}
317-
fn muli_with_overflow(e: &Expr, a: i64, b: i64) -> ConstEvalErr {
318-
ConstEvalErr { span: e.span, kind: ErrKind::MuliWithOverflow(a, b) }
319-
}
320-
fn addu_with_overflow(e: &Expr, a: u64, b: u64) -> ConstEvalErr {
321-
ConstEvalErr { span: e.span, kind: ErrKind::AdduWithOverflow(a, b) }
322-
}
323-
fn subu_with_overflow(e: &Expr, a: u64, b: u64) -> ConstEvalErr {
324-
ConstEvalErr { span: e.span, kind: ErrKind::SubuWithOverflow(a, b) }
325-
}
326-
fn mulu_with_overflow(e: &Expr, a: u64, b: u64) -> ConstEvalErr {
327-
ConstEvalErr { span: e.span, kind: ErrKind::MuluWithOverflow(a, b) }
328-
}
329-
fn divide_by_zero(e: &Expr) -> ConstEvalErr {
330-
ConstEvalErr { span: e.span, kind: ErrKind::DivideByZero }
331-
}
332-
fn divide_with_overflow(e: &Expr) -> ConstEvalErr {
333-
ConstEvalErr { span: e.span, kind: ErrKind::DivideWithOverflow }
334-
}
335-
fn modulo_by_zero(e: &Expr) -> ConstEvalErr {
336-
ConstEvalErr { span: e.span, kind: ErrKind::ModuloByZero }
337-
}
338-
fn modulo_with_overflow(e: &Expr) -> ConstEvalErr {
339-
ConstEvalErr { span: e.span, kind: ErrKind::ModuloWithOverflow }
340-
}
341-
fn missing_struct_field(e: &Expr) -> ConstEvalErr {
342-
ConstEvalErr { span: e.span, kind: ErrKind::MissingStructField }
343-
}
344-
fn non_const_path(e: &Expr) -> ConstEvalErr {
345-
ConstEvalErr { span: e.span, kind: ErrKind::NonConstPath }
346-
}
347-
fn non_const_struct(e: &Expr) -> ConstEvalErr {
348-
ConstEvalErr { span: e.span, kind: ErrKind::NonConstStruct }
349-
}
350-
fn tuple_index_out_of_bounds(e: &Expr) -> ConstEvalErr {
351-
ConstEvalErr { span: e.span, kind: ErrKind::TupleIndexOutOfBounds }
352-
}
286+
macro_rules! signal {
287+
($e:expr, $ctor:ident) => {
288+
return Err(ConstEvalErr { span: $e.span, kind: ErrKind::$ctor })
289+
};
353290

354-
fn misc_binary_op(e: &Expr) -> ConstEvalErr {
355-
ConstEvalErr { span: e.span, kind: ErrKind::MiscBinaryOp }
356-
}
357-
fn misc_catch_all(e: &Expr) -> ConstEvalErr {
358-
ConstEvalErr { span: e.span, kind: ErrKind::MiscCatchAll }
291+
($e:expr, $ctor:ident($($arg:expr),*)) => {
292+
return Err(ConstEvalErr { span: $e.span, kind: ErrKind::$ctor($($arg),*) })
293+
}
359294
}
360295

361-
362296
fn checked_add_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
363297
let (ret, oflo) = a.overflowing_add(b);
364-
if !oflo { Ok(const_int(ret)) } else { Err(addi_with_overflow(e, a, b)) }
298+
if !oflo { Ok(const_int(ret)) } else { signal!(e, AddiWithOverflow(a, b)) }
365299
}
366300
fn checked_sub_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
367301
let (ret, oflo) = a.overflowing_sub(b);
368-
if !oflo { Ok(const_int(ret)) } else { Err(subi_with_overflow(e, a, b)) }
302+
if !oflo { Ok(const_int(ret)) } else { signal!(e, SubiWithOverflow(a, b)) }
369303
}
370304
fn checked_mul_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
371305
let (ret, oflo) = a.overflowing_mul(b);
372-
if !oflo { Ok(const_int(ret)) } else { Err(muli_with_overflow(e, a, b)) }
306+
if !oflo { Ok(const_int(ret)) } else { signal!(e, MuliWithOverflow(a, b)) }
373307
}
374308

375309
fn checked_add_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
376310
let (ret, oflo) = a.overflowing_add(b);
377-
if !oflo { Ok(const_uint(ret)) } else { Err(addu_with_overflow(e, a, b)) }
311+
if !oflo { Ok(const_uint(ret)) } else { signal!(e, AdduWithOverflow(a, b)) }
378312
}
379313
fn checked_sub_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
380314
let (ret, oflo) = a.overflowing_sub(b);
381-
if !oflo { Ok(const_uint(ret)) } else { Err(subu_with_overflow(e, a, b)) }
315+
if !oflo { Ok(const_uint(ret)) } else { signal!(e, SubuWithOverflow(a, b)) }
382316
}
383317
fn checked_mul_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
384318
let (ret, oflo) = a.overflowing_mul(b);
385-
if !oflo { Ok(const_uint(ret)) } else { Err(mulu_with_overflow(e, a, b)) }
319+
if !oflo { Ok(const_uint(ret)) } else { signal!(e, MuluWithOverflow(a, b)) }
386320
}
387321

388322

@@ -400,8 +334,8 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
400334
Ok(const_float(f)) => Ok(const_float(-f)),
401335
Ok(const_int(i)) => Ok(const_int(-i)),
402336
Ok(const_uint(i)) => Ok(const_uint(-i)),
403-
Ok(const_str(_)) => Err(negate_on_string(e)),
404-
Ok(const_bool(_)) => Err(negate_on_boolean(e)),
337+
Ok(const_str(_)) => signal!(e, NegateOnString),
338+
Ok(const_bool(_)) => signal!(e, NegateOnBoolean),
405339
err => err
406340
}
407341
}
@@ -410,8 +344,8 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
410344
Ok(const_int(i)) => Ok(const_int(!i)),
411345
Ok(const_uint(i)) => Ok(const_uint(!i)),
412346
Ok(const_bool(b)) => Ok(const_bool(!b)),
413-
Ok(const_str(_)) => Err(not_on_string(e)),
414-
Ok(const_float(_)) => Err(not_on_float(e)),
347+
Ok(const_str(_)) => signal!(e, NotOnString),
348+
Ok(const_float(_)) => signal!(e, NotOnFloat),
415349
err => err
416350
}
417351
}
@@ -435,7 +369,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
435369
ast::BiNe => fromb(a != b),
436370
ast::BiGe => fromb(a >= b),
437371
ast::BiGt => fromb(a > b),
438-
_ => Err(invalid_op_for_floats(e, op.node)),
372+
_ => signal!(e, InvalidOpForFloats(op.node))
439373
}
440374
}
441375
(Ok(const_int(a)), Ok(const_int(b))) => {
@@ -463,18 +397,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
463397
ast::BiMul => checked_mul_int(e, a, b),
464398
ast::BiDiv => {
465399
if b == 0 {
466-
Err(divide_by_zero(e))
400+
signal!(e, DivideByZero);
467401
} else if b == -1 && is_a_min_value() {
468-
Err(divide_with_overflow(e))
402+
signal!(e, DivideWithOverflow);
469403
} else {
470404
Ok(const_int(a / b))
471405
}
472406
}
473407
ast::BiRem => {
474408
if b == 0 {
475-
Err(modulo_by_zero(e))
409+
signal!(e, ModuloByZero)
476410
} else if b == -1 && is_a_min_value() {
477-
Err(modulo_with_overflow(e))
411+
signal!(e, ModuloWithOverflow)
478412
} else {
479413
Ok(const_int(a % b))
480414
}
@@ -497,9 +431,9 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
497431
ast::BiAdd => checked_add_uint(e, a, b),
498432
ast::BiSub => checked_sub_uint(e, a, b),
499433
ast::BiMul => checked_mul_uint(e, a, b),
500-
ast::BiDiv if b == 0 => Err(divide_by_zero(e)),
434+
ast::BiDiv if b == 0 => signal!(e, DivideByZero),
501435
ast::BiDiv => Ok(const_uint(a / b)),
502-
ast::BiRem if b == 0 => Err(modulo_by_zero(e)),
436+
ast::BiRem if b == 0 => signal!(e, ModuloByZero),
503437
ast::BiRem => Ok(const_uint(a % b)),
504438
ast::BiAnd | ast::BiBitAnd => Ok(const_uint(a & b)),
505439
ast::BiOr | ast::BiBitOr => Ok(const_uint(a | b)),
@@ -519,14 +453,14 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
519453
match op.node {
520454
ast::BiShl => Ok(const_int(a << b as uint)),
521455
ast::BiShr => Ok(const_int(a >> b as uint)),
522-
_ => Err(invalid_op_for_int_uint(e, op.node)),
456+
_ => signal!(e, InvalidOpForIntUint(op.node)),
523457
}
524458
}
525459
(Ok(const_uint(a)), Ok(const_int(b))) => {
526460
match op.node {
527461
ast::BiShl => Ok(const_uint(a << b as uint)),
528462
ast::BiShr => Ok(const_uint(a >> b as uint)),
529-
_ => Err(invalid_op_for_uint_int(e, op.node)),
463+
_ => signal!(e, InvalidOpForUintInt(op.node)),
530464
}
531465
}
532466
(Ok(const_bool(a)), Ok(const_bool(b))) => {
@@ -538,13 +472,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
538472
ast::BiBitOr => a | b,
539473
ast::BiEq => a == b,
540474
ast::BiNe => a != b,
541-
_ => return Err(invalid_op_for_bools(e, op.node)),
475+
_ => signal!(e, InvalidOpForBools(op.node)),
542476
}))
543477
}
544478
(err @ Err(..), _) |
545479
(_, err @ Err(..)) => err,
546480

547-
_ => Err(misc_binary_op(e)),
481+
_ => signal!(e, MiscBinaryOp),
548482
}
549483
}
550484
ast::ExprCast(ref base, ref target_ty) => {
@@ -589,7 +523,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
589523
};
590524
let const_expr = match const_expr {
591525
Some(actual_e) => actual_e,
592-
None => return Err(non_const_path(e)),
526+
None => signal!(e, NonConstPath)
593527
};
594528
let ety = ety.or_else(|| const_ty.and_then(|ty| ast_ty_to_prim_ty(tcx, ty)));
595529
eval_const_expr_partial(tcx, const_expr, ety)
@@ -611,11 +545,11 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
611545
if fields.len() > index.node {
612546
return eval_const_expr_partial(tcx, &*fields[index.node], None)
613547
} else {
614-
return Err(tuple_index_out_of_bounds(e))
548+
signal!(e, TupleIndexOutOfBounds);
615549
}
616550
}
617551

618-
Err(non_const_struct(e))
552+
signal!(e, NonConstStruct);
619553
}
620554
ast::ExprField(ref base, field_name) => {
621555
// Get the base expression if it is a struct and it is constant
@@ -626,13 +560,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
626560
f.ident.node.as_str() == field_name.node.as_str()) {
627561
return eval_const_expr_partial(tcx, &*f.expr, None)
628562
} else {
629-
return Err(missing_struct_field(e));
563+
signal!(e, MissingStructField);
630564
}
631565
}
632566

633-
Err(non_const_struct(e))
567+
signal!(e, NonConstStruct);
634568
}
635-
_ => Err(misc_catch_all(e))
569+
_ => signal!(e, MiscCatchAll)
636570
}
637571
}
638572

0 commit comments

Comments
 (0)