]> git.lizzy.rs Git - rust.git/commitdiff
Fix for bitshift errors lint on cross compilation #18587
authorFalco Hirschenberger <falco.hirschenberger@gmail.com>
Mon, 3 Nov 2014 23:48:03 +0000 (00:48 +0100)
committerFalco Hirschenberger <falco.hirschenberger@gmail.com>
Mon, 3 Nov 2014 23:48:03 +0000 (00:48 +0100)
src/librustc/lint/builtin.rs
src/test/compile-fail/lint-exceeding-bitshifts.rs

index 76ef6206d64164cbd76efb183d28f6621f506ff8..1f39cb0132c89d57be1f5bbd4d3594b7c42ad9b2 100644 (file)
@@ -39,7 +39,7 @@
 use std::collections::HashMap;
 use std::collections::hashmap::{Occupied, Vacant};
 use std::slice;
-use std::{int, i8, i16, i32, i64, uint, u8, u16, u32, u64, f32, f64};
+use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
 use syntax::abi;
 use syntax::ast_map;
 use syntax::ast_util::is_shift_binop;
@@ -180,8 +180,8 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
 
                 if is_shift_binop(binop) {
                     let opt_ty_bits = match ty::get(ty::expr_ty(cx.tcx, &**l)).sty {
-                        ty::ty_int(t) => Some(int_ty_bits(t)),
-                        ty::ty_uint(t) => Some(uint_ty_bits(t)),
+                        ty::ty_int(t) => Some(int_ty_bits(t, cx.sess().targ_cfg.int_type)),
+                        ty::ty_uint(t) => Some(uint_ty_bits(t, cx.sess().targ_cfg.uint_type)),
                         _ => None
                     };
 
@@ -312,9 +312,9 @@ fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
             }
         }
 
-        fn int_ty_bits(int_ty: ast::IntTy) -> u64 {
+        fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
             match int_ty {
-                ast::TyI =>    int::BITS as u64,
+                ast::TyI =>    int_ty_bits(target_int_ty, target_int_ty),
                 ast::TyI8 =>   i8::BITS  as u64,
                 ast::TyI16 =>  i16::BITS as u64,
                 ast::TyI32 =>  i32::BITS as u64,
@@ -322,9 +322,9 @@ fn int_ty_bits(int_ty: ast::IntTy) -> u64 {
             }
         }
 
-        fn uint_ty_bits(uint_ty: ast::UintTy) -> u64 {
+        fn uint_ty_bits(uint_ty: ast::UintTy, target_uint_ty: ast::UintTy) -> u64 {
             match uint_ty {
-                ast::TyU =>    uint::BITS as u64,
+                ast::TyU =>    uint_ty_bits(target_uint_ty, target_uint_ty),
                 ast::TyU8 =>   u8::BITS  as u64,
                 ast::TyU16 =>  u16::BITS as u64,
                 ast::TyU32 =>  u32::BITS as u64,
index f270994bd38ef12230c018edf152d98b60dc6d16..6de2ad8a0f3dc4db72d5865e6ece218101b8d245 100644 (file)
 
 #![deny(exceeding_bitshifts)]
 #![allow(unused_variables)]
+#![allow(dead_code)]
 
 fn main() {
-      let n = 1u8 << 8;
-      let n = 1u8 << 9;   //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u16 << 16;
-      let n = 1u16 << 17; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u32 << 32;
-      let n = 1u32 << 33; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u64 << 64;
-      let n = 1u64 << 65; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i8 << 8;
-      let n = 1i8 << 9;   //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i16 << 16;
-      let n = 1i16 << 17; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i32 << 32;
-      let n = 1i32 << 33; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i64 << 64;
-      let n = 1i64 << 65; //~ ERROR: bitshift exceeds the type's number of bits
-
-      let n = 1u8 >> 8;
-      let n = 1u8 >> 9;   //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u16 >> 16;
-      let n = 1u16 >> 17; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u32 >> 32;
-      let n = 1u32 >> 33; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1u64 >> 64;
-      let n = 1u64 >> 65; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i8 >> 8;
-      let n = 1i8 >> 9;   //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i16 >> 16;
-      let n = 1i16 >> 17; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i32 >> 32;
-      let n = 1i32 >> 33; //~ ERROR: bitshift exceeds the type's number of bits
-      let n = 1i64 >> 64;
-      let n = 1i64 >> 65; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u8 << 7;
+      let n = 1u8 << 8;   //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u16 << 15;
+      let n = 1u16 << 16; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u32 << 31;
+      let n = 1u32 << 32; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u64 << 63;
+      let n = 1u64 << 64; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i8 << 7;
+      let n = 1i8 << 8;   //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i16 << 15;
+      let n = 1i16 << 16; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i32 << 31;
+      let n = 1i32 << 32; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i64 << 63;
+      let n = 1i64 << 64; //~ ERROR: bitshift exceeds the type's number of bits
+
+      let n = 1u8 >> 7;
+      let n = 1u8 >> 8;   //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u16 >> 15;
+      let n = 1u16 >> 16; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u32 >> 31;
+      let n = 1u32 >> 32; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1u64 >> 63;
+      let n = 1u64 >> 64; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i8 >> 7;
+      let n = 1i8 >> 8;   //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i16 >> 15;
+      let n = 1i16 >> 16; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i32 >> 31;
+      let n = 1i32 >> 32; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = 1i64 >> 63;
+      let n = 1i64 >> 64; //~ ERROR: bitshift exceeds the type's number of bits
 
       let n = 1u8;
-      let n = n << 8;
-      let n = n << 9; //~ ERROR: bitshift exceeds the type's number of bits
+      let n = n << 7;
+      let n = n << 8; //~ ERROR: bitshift exceeds the type's number of bits
+
+      let n = 1u8 << -8; //~ ERROR: bitshift exceeds the type's number of bits
+
+      let n = 1u8 << (4+3);
+      let n = 1u8 << (4+4); //~ ERROR: bitshift exceeds the type's number of bits
 
-      let n = 1u8 << -9; //~ ERROR: bitshift exceeds the type's number of bits
+      #[cfg(target_word_size = "32")]
+      fn dead_but_still_linted() {
+        let n = 1i << 32; //~ ERROR: bitshift exceeds the type's number of bits
+        let n = 1u << 32; //~ ERROR: bitshift exceeds the type's number of bits
+      }
 
-      let n = 1u8 << (4+4);
-      let n = 1u8 << (4+5); //~ ERROR: bitshift exceeds the type's number of bits
+      #[cfg(target_word_size = "64")]
+      fn dead_but_still_still_linted() {
+        let n = 1i << 64; //~ ERROR: bitshift exceeds the type's number of bits
+        let n = 1u << 64; //~ ERROR: bitshift exceeds the type's number of bits
+      }
 }