Skip to content

Commit 735c018

Browse files
committed
skip double negation in const eval
1 parent 3acee3b commit 735c018

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/librustc_const_eval/eval.rs

+45-38
Original file line numberDiff line numberDiff line change
@@ -562,44 +562,51 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
562562
let result = match e.node {
563563
hir::ExprUnary(hir::UnNeg, ref inner) => {
564564
// unary neg literals already got their sign during creation
565-
if let hir::ExprLit(ref lit) = inner.node {
566-
use syntax::ast::*;
567-
use syntax::ast::LitIntType::*;
568-
const I8_OVERFLOW: u64 = ::std::i8::MAX as u64 + 1;
569-
const I16_OVERFLOW: u64 = ::std::i16::MAX as u64 + 1;
570-
const I32_OVERFLOW: u64 = ::std::i32::MAX as u64 + 1;
571-
const I64_OVERFLOW: u64 = ::std::i64::MAX as u64 + 1;
572-
match (&lit.node, ety.map(|t| &t.sty)) {
573-
(&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
574-
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
575-
return Ok(Integral(I8(::std::i8::MIN)))
576-
},
577-
(&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
578-
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
579-
return Ok(Integral(I16(::std::i16::MIN)))
580-
},
581-
(&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
582-
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
583-
return Ok(Integral(I32(::std::i32::MIN)))
584-
},
585-
(&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
586-
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
587-
return Ok(Integral(I64(::std::i64::MIN)))
588-
},
589-
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
590-
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
591-
match tcx.sess.target.int_type {
592-
IntTy::I32 => if n == I32_OVERFLOW {
593-
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
594-
},
595-
IntTy::I64 => if n == I64_OVERFLOW {
596-
return Ok(Integral(Isize(Is64(::std::i64::MIN))));
597-
},
598-
_ => bug!(),
599-
}
600-
},
601-
_ => {},
602-
}
565+
match inner.node {
566+
hir::ExprLit(ref lit) => {
567+
use syntax::ast::*;
568+
use syntax::ast::LitIntType::*;
569+
const I8_OVERFLOW: u64 = ::std::i8::MAX as u64 + 1;
570+
const I16_OVERFLOW: u64 = ::std::i16::MAX as u64 + 1;
571+
const I32_OVERFLOW: u64 = ::std::i32::MAX as u64 + 1;
572+
const I64_OVERFLOW: u64 = ::std::i64::MAX as u64 + 1;
573+
match (&lit.node, ety.map(|t| &t.sty)) {
574+
(&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
575+
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
576+
return Ok(Integral(I8(::std::i8::MIN)))
577+
},
578+
(&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
579+
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
580+
return Ok(Integral(I16(::std::i16::MIN)))
581+
},
582+
(&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
583+
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
584+
return Ok(Integral(I32(::std::i32::MIN)))
585+
},
586+
(&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
587+
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
588+
return Ok(Integral(I64(::std::i64::MIN)))
589+
},
590+
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
591+
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
592+
match tcx.sess.target.int_type {
593+
IntTy::I32 => if n == I32_OVERFLOW {
594+
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
595+
},
596+
IntTy::I64 => if n == I64_OVERFLOW {
597+
return Ok(Integral(Isize(Is64(::std::i64::MIN))));
598+
},
599+
_ => bug!(),
600+
}
601+
},
602+
_ => {},
603+
}
604+
},
605+
hir::ExprUnary(hir::UnNeg, ref inner) => {
606+
// skip `--$expr`
607+
return eval_const_expr_partial(tcx, inner, ty_hint, fn_args);
608+
},
609+
_ => {},
603610
}
604611
match eval_const_expr_partial(tcx, &inner, ty_hint, fn_args)? {
605612
Float(f) => Float(-f),

src/test/compile-fail/lint-type-overflow2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#[allow(unused_variables)]
1616
fn main() {
1717
let x2: i8 = --128; //~ error: literal out of range for i8
18-
//~^ error: attempted to negate with overflow
1918

2019
let x = -3.40282348e+38_f32; //~ error: literal out of range for f32
2120
let x = 3.40282348e+38_f32; //~ error: literal out of range for f32

0 commit comments

Comments
 (0)