Skip to content

Commit b8f9c88

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

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

src/librustc_const_eval/eval.rs

+41-48
Original file line numberDiff line numberDiff line change
@@ -543,54 +543,47 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
543543
let result = match e.node {
544544
hir::ExprUnary(hir::UnNeg, ref inner) => {
545545
// unary neg literals already got their sign during creation
546-
match inner.node {
547-
hir::ExprLit(ref lit) => {
548-
use syntax::ast::*;
549-
use syntax::ast::LitIntType::*;
550-
const I8_OVERFLOW: u64 = ::std::i8::MAX as u64 + 1;
551-
const I16_OVERFLOW: u64 = ::std::i16::MAX as u64 + 1;
552-
const I32_OVERFLOW: u64 = ::std::i32::MAX as u64 + 1;
553-
const I64_OVERFLOW: u64 = ::std::i64::MAX as u64 + 1;
554-
match (&lit.node, ety.map(|t| &t.sty)) {
555-
(&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
556-
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
557-
return Ok(Integral(I8(::std::i8::MIN)))
558-
},
559-
(&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
560-
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
561-
return Ok(Integral(I16(::std::i16::MIN)))
562-
},
563-
(&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
564-
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
565-
return Ok(Integral(I32(::std::i32::MIN)))
566-
},
567-
(&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
568-
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
569-
return Ok(Integral(I64(::std::i64::MIN)))
570-
},
571-
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
572-
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
573-
match tcx.sess.target.int_type {
574-
IntTy::I16 => if n == I16_OVERFLOW {
575-
return Ok(Integral(Isize(Is16(::std::i16::MIN))));
576-
},
577-
IntTy::I32 => if n == I32_OVERFLOW {
578-
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
579-
},
580-
IntTy::I64 => if n == I64_OVERFLOW {
581-
return Ok(Integral(Isize(Is64(::std::i64::MIN))));
582-
},
583-
_ => bug!(),
584-
}
585-
},
586-
_ => {},
587-
}
588-
},
589-
hir::ExprUnary(hir::UnNeg, ref inner) => {
590-
// skip `--$expr`
591-
return eval_const_expr_partial(tcx, inner, ty_hint, fn_args);
592-
},
593-
_ => {},
546+
if let hir::ExprLit(ref lit) = inner.node {
547+
use syntax::ast::*;
548+
use syntax::ast::LitIntType::*;
549+
const I8_OVERFLOW: u64 = ::std::i8::MAX as u64 + 1;
550+
const I16_OVERFLOW: u64 = ::std::i16::MAX as u64 + 1;
551+
const I32_OVERFLOW: u64 = ::std::i32::MAX as u64 + 1;
552+
const I64_OVERFLOW: u64 = ::std::i64::MAX as u64 + 1;
553+
match (&lit.node, ety.map(|t| &t.sty)) {
554+
(&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
555+
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
556+
return Ok(Integral(I8(::std::i8::MIN)))
557+
},
558+
(&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
559+
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
560+
return Ok(Integral(I16(::std::i16::MIN)))
561+
},
562+
(&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
563+
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
564+
return Ok(Integral(I32(::std::i32::MIN)))
565+
},
566+
(&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
567+
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
568+
return Ok(Integral(I64(::std::i64::MIN)))
569+
},
570+
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
571+
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
572+
match tcx.sess.target.int_type {
573+
IntTy::I16 => if n == I16_OVERFLOW {
574+
return Ok(Integral(Isize(Is16(::std::i16::MIN))));
575+
},
576+
IntTy::I32 => if n == I32_OVERFLOW {
577+
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
578+
},
579+
IntTy::I64 => if n == I64_OVERFLOW {
580+
return Ok(Integral(Isize(Is64(::std::i64::MIN))));
581+
},
582+
_ => bug!(),
583+
}
584+
},
585+
_ => {},
586+
}
594587
}
595588
match eval_const_expr_partial(tcx, &inner, ty_hint, fn_args)? {
596589
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)