Skip to content

Commit acf3ccd

Browse files
committed
Revert "skip double negation in const eval"
This reverts commit 735c018.
1 parent fe7ccf0 commit acf3ccd

File tree

2 files changed

+39
-45
lines changed

2 files changed

+39
-45
lines changed

src/librustc_const_eval/eval.rs

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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
1819

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

0 commit comments

Comments
 (0)