Skip to content

Commit e7f3109

Browse files
Fix for bitshift errors lint on cross compilation #18587
1 parent 2790505 commit e7f3109

File tree

2 files changed

+58
-45
lines changed

2 files changed

+58
-45
lines changed

src/librustc/lint/builtin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::cmp;
3939
use std::collections::HashMap;
4040
use std::collections::hashmap::{Occupied, Vacant};
4141
use std::slice;
42-
use std::{int, i8, i16, i32, i64, uint, u8, u16, u32, u64, f32, f64};
42+
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
4343
use syntax::abi;
4444
use syntax::ast_map;
4545
use syntax::ast_util::is_shift_binop;
@@ -180,8 +180,8 @@ impl LintPass for TypeLimits {
180180

181181
if is_shift_binop(binop) {
182182
let opt_ty_bits = match ty::get(ty::expr_ty(cx.tcx, &**l)).sty {
183-
ty::ty_int(t) => Some(int_ty_bits(t)),
184-
ty::ty_uint(t) => Some(uint_ty_bits(t)),
183+
ty::ty_int(t) => Some(int_ty_bits(t, cx.sess().targ_cfg.int_type)),
184+
ty::ty_uint(t) => Some(uint_ty_bits(t, cx.sess().targ_cfg.uint_type)),
185185
_ => None
186186
};
187187

@@ -312,19 +312,19 @@ impl LintPass for TypeLimits {
312312
}
313313
}
314314

315-
fn int_ty_bits(int_ty: ast::IntTy) -> u64 {
315+
fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
316316
match int_ty {
317-
ast::TyI => int::BITS as u64,
317+
ast::TyI => int_ty_bits(target_int_ty, target_int_ty),
318318
ast::TyI8 => i8::BITS as u64,
319319
ast::TyI16 => i16::BITS as u64,
320320
ast::TyI32 => i32::BITS as u64,
321321
ast::TyI64 => i64::BITS as u64
322322
}
323323
}
324324

325-
fn uint_ty_bits(uint_ty: ast::UintTy) -> u64 {
325+
fn uint_ty_bits(uint_ty: ast::UintTy, target_uint_ty: ast::UintTy) -> u64 {
326326
match uint_ty {
327-
ast::TyU => uint::BITS as u64,
327+
ast::TyU => uint_ty_bits(target_uint_ty, target_uint_ty),
328328
ast::TyU8 => u8::BITS as u64,
329329
ast::TyU16 => u16::BITS as u64,
330330
ast::TyU32 => u32::BITS as u64,

src/test/compile-fail/lint-exceeding-bitshifts.rs

+51-38
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,62 @@
1010

1111
#![deny(exceeding_bitshifts)]
1212
#![allow(unused_variables)]
13+
#![allow(dead_code)]
1314

1415
fn main() {
15-
let n = 1u8 << 8;
16-
let n = 1u8 << 9; //~ ERROR: bitshift exceeds the type's number of bits
17-
let n = 1u16 << 16;
18-
let n = 1u16 << 17; //~ ERROR: bitshift exceeds the type's number of bits
19-
let n = 1u32 << 32;
20-
let n = 1u32 << 33; //~ ERROR: bitshift exceeds the type's number of bits
21-
let n = 1u64 << 64;
22-
let n = 1u64 << 65; //~ ERROR: bitshift exceeds the type's number of bits
23-
let n = 1i8 << 8;
24-
let n = 1i8 << 9; //~ ERROR: bitshift exceeds the type's number of bits
25-
let n = 1i16 << 16;
26-
let n = 1i16 << 17; //~ ERROR: bitshift exceeds the type's number of bits
27-
let n = 1i32 << 32;
28-
let n = 1i32 << 33; //~ ERROR: bitshift exceeds the type's number of bits
29-
let n = 1i64 << 64;
30-
let n = 1i64 << 65; //~ ERROR: bitshift exceeds the type's number of bits
31-
32-
let n = 1u8 >> 8;
33-
let n = 1u8 >> 9; //~ ERROR: bitshift exceeds the type's number of bits
34-
let n = 1u16 >> 16;
35-
let n = 1u16 >> 17; //~ ERROR: bitshift exceeds the type's number of bits
36-
let n = 1u32 >> 32;
37-
let n = 1u32 >> 33; //~ ERROR: bitshift exceeds the type's number of bits
38-
let n = 1u64 >> 64;
39-
let n = 1u64 >> 65; //~ ERROR: bitshift exceeds the type's number of bits
40-
let n = 1i8 >> 8;
41-
let n = 1i8 >> 9; //~ ERROR: bitshift exceeds the type's number of bits
42-
let n = 1i16 >> 16;
43-
let n = 1i16 >> 17; //~ ERROR: bitshift exceeds the type's number of bits
44-
let n = 1i32 >> 32;
45-
let n = 1i32 >> 33; //~ ERROR: bitshift exceeds the type's number of bits
46-
let n = 1i64 >> 64;
47-
let n = 1i64 >> 65; //~ ERROR: bitshift exceeds the type's number of bits
16+
let n = 1u8 << 7;
17+
let n = 1u8 << 8; //~ ERROR: bitshift exceeds the type's number of bits
18+
let n = 1u16 << 15;
19+
let n = 1u16 << 16; //~ ERROR: bitshift exceeds the type's number of bits
20+
let n = 1u32 << 31;
21+
let n = 1u32 << 32; //~ ERROR: bitshift exceeds the type's number of bits
22+
let n = 1u64 << 63;
23+
let n = 1u64 << 64; //~ ERROR: bitshift exceeds the type's number of bits
24+
let n = 1i8 << 7;
25+
let n = 1i8 << 8; //~ ERROR: bitshift exceeds the type's number of bits
26+
let n = 1i16 << 15;
27+
let n = 1i16 << 16; //~ ERROR: bitshift exceeds the type's number of bits
28+
let n = 1i32 << 31;
29+
let n = 1i32 << 32; //~ ERROR: bitshift exceeds the type's number of bits
30+
let n = 1i64 << 63;
31+
let n = 1i64 << 64; //~ ERROR: bitshift exceeds the type's number of bits
32+
33+
let n = 1u8 >> 7;
34+
let n = 1u8 >> 8; //~ ERROR: bitshift exceeds the type's number of bits
35+
let n = 1u16 >> 15;
36+
let n = 1u16 >> 16; //~ ERROR: bitshift exceeds the type's number of bits
37+
let n = 1u32 >> 31;
38+
let n = 1u32 >> 32; //~ ERROR: bitshift exceeds the type's number of bits
39+
let n = 1u64 >> 63;
40+
let n = 1u64 >> 64; //~ ERROR: bitshift exceeds the type's number of bits
41+
let n = 1i8 >> 7;
42+
let n = 1i8 >> 8; //~ ERROR: bitshift exceeds the type's number of bits
43+
let n = 1i16 >> 15;
44+
let n = 1i16 >> 16; //~ ERROR: bitshift exceeds the type's number of bits
45+
let n = 1i32 >> 31;
46+
let n = 1i32 >> 32; //~ ERROR: bitshift exceeds the type's number of bits
47+
let n = 1i64 >> 63;
48+
let n = 1i64 >> 64; //~ ERROR: bitshift exceeds the type's number of bits
4849

4950
let n = 1u8;
50-
let n = n << 8;
51-
let n = n << 9; //~ ERROR: bitshift exceeds the type's number of bits
51+
let n = n << 7;
52+
let n = n << 8; //~ ERROR: bitshift exceeds the type's number of bits
53+
54+
let n = 1u8 << -8; //~ ERROR: bitshift exceeds the type's number of bits
55+
56+
let n = 1u8 << (4+3);
57+
let n = 1u8 << (4+4); //~ ERROR: bitshift exceeds the type's number of bits
5258

53-
let n = 1u8 << -9; //~ ERROR: bitshift exceeds the type's number of bits
59+
#[cfg(target_word_size = "32")]
60+
fn dead_but_still_linted() {
61+
let n = 1i << 32; //~ ERROR: bitshift exceeds the type's number of bits
62+
let n = 1u << 32; //~ ERROR: bitshift exceeds the type's number of bits
63+
}
5464

55-
let n = 1u8 << (4+4);
56-
let n = 1u8 << (4+5); //~ ERROR: bitshift exceeds the type's number of bits
65+
#[cfg(target_word_size = "64")]
66+
fn dead_but_still_still_linted() {
67+
let n = 1i << 64; //~ ERROR: bitshift exceeds the type's number of bits
68+
let n = 1u << 64; //~ ERROR: bitshift exceeds the type's number of bits
69+
}
5770
}
5871

0 commit comments

Comments
 (0)