Auto merge of #38482 - est31:i128, r=eddyb

i128 and u128 support

Brings i128 and u128 support to nightly rust, behind a feature flag. The goal of this PR is to do the bulk of the work for 128 bit integer support. Smaller but just as tricky features needed for stabilisation like 128 bit enum discriminants are left for future PRs.

Rebased version of  #37900, which in turn was a rebase + improvement of #35954 . Sadly I couldn't reopen #37900 due to github. There goes my premium position in the homu queue...

[plugin-breaking-change]

cc #35118 (tracking issue)
This commit is contained in:
bors 2016-12-31 18:54:31 +00:00
commit 38bd207626
121 changed files with 2233 additions and 486 deletions

View file

@ -10,7 +10,7 @@
// Test spans of errors
const TUP: (usize,) = 5 << 64;
const TUP: (usize,) = 5usize << 64;
//~^ ERROR E0080
//~| attempt to shift left with overflow
const ARR: [i32; TUP.0] = [];

View file

@ -0,0 +1,29 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn test1() -> i128 { //~ ERROR 128-bit type is unstable
0
}
fn test1_2() -> u128 { //~ ERROR 128-bit type is unstable
0
}
fn test3() {
let x: i128 = 0; //~ ERROR 128-bit type is unstable
}
fn test3_2() {
let x: u128 = 0; //~ ERROR 128-bit type is unstable
}
#[repr(u128)]
enum A { //~ ERROR 128-bit type is unstable
A(u64)
}

View file

@ -0,0 +1,17 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn test2() {
0i128; //~ ERROR 128-bit integers are not stable
}
fn test2_2() {
0u128; //~ ERROR 128-bit integers are not stable
}

View file

@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -8,6 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
println!("{}", 18446744073709551616u64); //~ error: int literal is too large
extern "unadjusted" fn foo() {
//~^ ERROR: unadjusted ABI is an implementation detail and perma-unstable
}
fn main() {
foo();
}

View file

@ -13,7 +13,7 @@
// issue #17123
fn main() {
100000000000000000000000000000000 //~ ERROR int literal is too large
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
//~^ ERROR int literal is too large
; // the span shouldn't point to this.
}

View file

@ -11,6 +11,6 @@
// compile-flags: -Z parse-only
fn main() {
let __isize = 18446744073709551616; // 2^64
let __isize = 340282366920938463463374607431768211456; // 2^128
//~^ ERROR int literal is too large
}

View file

@ -11,6 +11,6 @@
// compile-flags: -Z parse-only
fn main() {
let __isize = 0xff_ffff_ffff_ffff_ffff;
let __isize = 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ff;
//~^ ERROR int literal is too large
}

View file

@ -23,8 +23,8 @@ fn main() {
0o; //~ ERROR: no valid digits
1e+; //~ ERROR: expected at least one digit in exponent
0x539.0; //~ ERROR: hexadecimal float literal is not supported
99999999999999999999999999999999; //~ ERROR: int literal is too large
99999999999999999999999999999999; //~ ERROR: int literal is too large
9900000000000000000000000000999999999999999999999999999999; //~ ERROR: int literal is too large
9900000000000000000000000000999999999999999999999999999999; //~ ERROR: int literal is too large
0x; //~ ERROR: no valid digits
0xu32; //~ ERROR: no valid digits
0ou32; //~ ERROR: no valid digits

99
src/test/run-pass/i128.rs Normal file
View file

@ -0,0 +1,99 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-stage0
// ignore-stage1
#![feature(i128_type, test)]
extern crate test;
use test::black_box as b;
fn main() {
let x: i128 = -1;
assert_eq!(0, !x);
let y: i128 = -2;
assert_eq!(!1, y);
let z: i128 = 0xABCD_EF;
assert_eq!(z * z, 0x734C_C2F2_A521);
assert_eq!(z * z * z * z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
assert_eq!(-z * -z, 0x734C_C2F2_A521);
assert_eq!(-z * -z * -z * -z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
assert_eq!(-z + -z + -z + -z, -0x2AF3_7BC);
let k: i128 = -0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210;
assert_eq!(k + k, -0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420);
assert_eq!(0, k - k);
assert_eq!(-0x1234_5678_9ABC_DEFF_EDCB_A987_5A86_421, k + z);
assert_eq!(-0x1000_0000_0000_0000_0000_0000_0000_000,
k + 0x234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!(-0x6EF5_DE4C_D3BC_2AAA_3BB4_CC5D_D6EE_8, k / 42);
assert_eq!(-k, k / -1);
assert_eq!(-0x91A2_B3C4_D5E6_F8, k >> 65);
assert_eq!(-0xFDB9_7530_ECA8_6420_0000_0000_0000_0000, k << 65);
assert!(k < z);
assert!(y > k);
assert!(y < x);
assert_eq!(x as i64, -1);
assert_eq!(z as i64, 0xABCD_EF);
assert_eq!(k as i64, -0xFEDC_BA98_7654_3210);
assert_eq!(k as u128, 0xFEDC_BA98_7654_3210_0123_4567_89AB_CDF0);
assert_eq!(-k as u128, 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!((-z as f64) as i128, -z);
assert_eq!((-z as f32) as i128, -z);
assert_eq!((-z as f64 * 16.0) as i128, -z * 16);
assert_eq!((-z as f32 * 16.0) as i128, -z * 16);
// Same stuff as above, but blackboxed, to force use of intrinsics
let x: i128 = b(-1);
assert_eq!(0, !x);
let y: i128 = b(-2);
assert_eq!(!1, y);
let z: i128 = b(0xABCD_EF);
assert_eq!(z * z, 0x734C_C2F2_A521);
assert_eq!(z * z * z * z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
assert_eq!(-z * -z, 0x734C_C2F2_A521);
assert_eq!(-z * -z * -z * -z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
assert_eq!(-z + -z + -z + -z, -0x2AF3_7BC);
let k: i128 = b(-0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!(k + k, -0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420);
assert_eq!(0, k - k);
assert_eq!(-0x1234_5678_9ABC_DEFF_EDCB_A987_5A86_421, k + z);
assert_eq!(-0x1000_0000_0000_0000_0000_0000_0000_000,
k + 0x234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!(-0x6EF5_DE4C_D3BC_2AAA_3BB4_CC5D_D6EE_8, k / 42);
assert_eq!(-k, k / -1);
assert_eq!(-0x91A2_B3C4_D5E6_F8, k >> 65);
assert_eq!(-0xFDB9_7530_ECA8_6420_0000_0000_0000_0000, k << 65);
assert!(k < z);
assert!(y > k);
assert!(y < x);
assert_eq!(x as i64, -1);
assert_eq!(z as i64, 0xABCD_EF);
assert_eq!(k as i64, -0xFEDC_BA98_7654_3210);
assert_eq!(k as u128, 0xFEDC_BA98_7654_3210_0123_4567_89AB_CDF0);
assert_eq!(-k as u128, 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!((-z as f64) as i128, -z);
assert_eq!((-z as f32) as i128, -z);
assert_eq!((-z as f64 * 16.0) as i128, -z * 16);
assert_eq!((-z as f32 * 16.0) as i128, -z * 16);
// formatting
let j: i128 = -(1 << 67);
assert_eq!("-147573952589676412928", format!("{}", j));
assert_eq!("fffffffffffffff80000000000000000", format!("{:x}", j));
assert_eq!("3777777777777777777760000000000000000000000", format!("{:o}", j));
assert_eq!("1111111111111111111111111111111111111111111111111111111111111\
0000000000000000000000000000000000000000000000000000000000000000000",
format!("{:b}", j));
assert_eq!("-147573952589676412928", format!("{:?}", j));
// common traits
assert_eq!(x, b(x.clone()));
// overflow checks
assert_eq!((-z).checked_mul(-z), Some(0x734C_C2F2_A521));
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
assert_eq!((k).checked_mul(k), None);
}

67
src/test/run-pass/u128.rs Normal file
View file

@ -0,0 +1,67 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-stage0
// ignore-stage1
#![feature(i128_type)]
fn main() {
let x: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFF;
assert_eq!(0, !x);
assert_eq!(0, !x);
let y: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFE;
assert_eq!(!1, y);
assert_eq!(x, y | 1);
assert_eq!(0xFAFF_0000_FF8F_0000__FFFF_0000_FFFF_FFFE,
y &
0xFAFF_0000_FF8F_0000__FFFF_0000_FFFF_FFFF);
let z: u128 = 0xABCD_EF;
assert_eq!(z * z, 0x734C_C2F2_A521);
assert_eq!(z * z * z * z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
assert_eq!(z + z + z + z, 0x2AF3_7BC);
let k: u128 = 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210;
assert_eq!(k + k, 0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420);
assert_eq!(0, k - k);
assert_eq!(0x1234_5678_9ABC_DEFF_EDCB_A987_5A86_421, k - z);
assert_eq!(0x1000_0000_0000_0000_0000_0000_0000_000,
k - 0x234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!(0x6EF5_DE4C_D3BC_2AAA_3BB4_CC5D_D6EE_8, k / 42);
assert_eq!(0, k % 42);
assert_eq!(15, z % 42);
assert_eq!(0x169D_A8020_CEC18, k % 0x3ACB_FE49_FF24_AC);
assert_eq!(0x91A2_B3C4_D5E6_F7, k >> 65);
assert_eq!(0xFDB9_7530_ECA8_6420_0000_0000_0000_0000, k << 65);
assert!(k > z);
assert!(y > k);
assert!(y < x);
assert_eq!(x as u64, !0);
assert_eq!(z as u64, 0xABCD_EF);
assert_eq!(k as u64, 0xFEDC_BA98_7654_3210);
assert_eq!(k as i128, 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210);
assert_eq!((z as f64) as u128, z);
assert_eq!((z as f32) as u128, z);
assert_eq!((z as f64 * 16.0) as u128, z * 16);
assert_eq!((z as f32 * 16.0) as u128, z * 16);
let l :u128 = 432 << 100;
assert_eq!((l as f32) as u128, l);
assert_eq!((l as f64) as u128, l);
// formatting
let j: u128 = 1 << 67;
assert_eq!("147573952589676412928", format!("{}", j));
assert_eq!("80000000000000000", format!("{:x}", j));
assert_eq!("20000000000000000000000", format!("{:o}", j));
assert_eq!("10000000000000000000000000000000000000000000000000000000000000000000",
format!("{:b}", j));
assert_eq!("340282366920938463463374607431768211455",
format!("{}", u128::max_value()));
assert_eq!("147573952589676412928", format!("{:?}", j));
// common traits
x.clone();
}

View file

@ -1,4 +1,4 @@
error: invalid ABI: expected one of [cdecl, stdcall, fastcall, vectorcall, aapcs, win64, sysv64, ptx-kernel, Rust, C, system, rust-intrinsic, rust-call, platform-intrinsic], found `路濫狼á́́`
error: invalid ABI: expected one of [cdecl, stdcall, fastcall, vectorcall, aapcs, win64, sysv64, ptx-kernel, Rust, C, system, rust-intrinsic, rust-call, platform-intrinsic, unadjusted], found `路濫狼á́́`
--> $DIR/unicode.rs:11:8
|
11 | extern "路濫狼á́́" fn foo() {}