Skip to content

Commit 0cc99f9

Browse files
committed
Auto merge of #25527 - inrustwetrust:const-not-overflow, r=alexcrichton
Fixes #23968. Since the values are stored in a u64 internally, we need to be mask away the high bits after applying the ! operator. Otherwise, these bits will be set to one, causing overflow.
2 parents e4d56b7 + d1605de commit 0cc99f9

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/librustc/middle/const_eval.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -34,7 +34,7 @@ use std::borrow::{Cow, IntoCow};
3434
use std::num::wrapping::OverflowingOps;
3535
use std::cmp::Ordering;
3636
use std::collections::hash_map::Entry::Vacant;
37-
use std::{i8, i16, i32, i64};
37+
use std::{i8, i16, i32, i64, u8, u16, u32, u64};
3838
use std::rc::Rc;
3939

4040
fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
@@ -461,6 +461,16 @@ pub fn const_uint_checked_neg<'a>(
461461
Ok(const_uint((!a).wrapping_add(1)))
462462
}
463463

464+
fn const_uint_not(a: u64, opt_ety: Option<UintTy>) -> const_val {
465+
let mask = match opt_ety {
466+
Some(UintTy::U8) => u8::MAX as u64,
467+
Some(UintTy::U16) => u16::MAX as u64,
468+
Some(UintTy::U32) => u32::MAX as u64,
469+
None | Some(UintTy::U64) => u64::MAX,
470+
};
471+
const_uint(!a & mask)
472+
}
473+
464474
macro_rules! overflow_checking_body {
465475
($a:ident, $b:ident, $ety:ident, $overflowing_op:ident,
466476
lhs: $to_8_lhs:ident $to_16_lhs:ident $to_32_lhs:ident,
@@ -677,7 +687,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
677687
ast::ExprUnary(ast::UnNot, ref inner) => {
678688
match try!(eval_const_expr_partial(tcx, &**inner, ety)) {
679689
const_int(i) => const_int(!i),
680-
const_uint(i) => const_uint(!i),
690+
const_uint(i) => const_uint_not(i, expr_uint_type),
681691
const_bool(b) => const_bool(!b),
682692
const_str(_) => signal!(e, NotOnString),
683693
const_float(_) => signal!(e, NotOnFloat),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const U8_MAX_HALF: u8 = !0u8 / 2;
12+
const U16_MAX_HALF: u16 = !0u16 / 2;
13+
const U32_MAX_HALF: u32 = !0u32 / 2;
14+
const U64_MAX_HALF: u64 = !0u64 / 2;
15+
16+
fn main() {
17+
assert_eq!(U8_MAX_HALF, 0x7f);
18+
assert_eq!(U16_MAX_HALF, 0x7fff);
19+
assert_eq!(U32_MAX_HALF, 0x7fff_ffff);
20+
assert_eq!(U64_MAX_HALF, 0x7fff_ffff_ffff_ffff);
21+
}

0 commit comments

Comments
 (0)