Auto merge of #53697 - Cyres:const-fn-int-ops, r=oli-obk

Add more const int ops

r? @oli-obk

Tracking Issue: #53718

list of `const fn`s in this PR:

- `feature = const_int_rotate`
  - `rotate_left`
  - `rotate_right`
- `feature = const_int_wrapping`
  - `wrapping_add`
  - `wrapping_sub`
  - `wrapping_mul`
  - `wrapping_shl`
  - `wrapping_shr`
- `feature = const_int_overflowing`
  - `overflowing_add`
  - `overflowing_sub`
  - `overflowing_mul`
  - `overflowing_shl`
  - `overflowing_shr`
- `feature = const_int_sign`
  - `is_positive`
  - `is_negative`
- `feature = const_int_conversion`
  - `reverse_bits`
  - `to_le_bytes`
  - `to_ne_bytes`
  - `from_be_bytes`
  - `from_le_bytes`
  - `from_ne_bytes`
  - `reverse_bits`
This commit is contained in:
bors 2018-09-03 16:31:34 +00:00
commit cd5c26f0eb
20 changed files with 1033 additions and 58 deletions

View file

@ -0,0 +1,34 @@
// Copyright 2018 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.
#![feature(const_int_conversion, const_int_ops, reverse_bits, int_to_from_bytes)]
const REVERSE: u32 = 0x12345678_u32.reverse_bits();
const FROM_BE_BYTES: i32 = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
const FROM_LE_BYTES: i32 = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);
const FROM_NE_BYTES: i32 = i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0]));
const TO_BE_BYTES: [u8; 4] = 0x12_34_56_78_i32.to_be_bytes();
const TO_LE_BYTES: [u8; 4] = 0x12_34_56_78_i32.to_le_bytes();
const TO_NE_BYTES: [u8; 4] = i32::min_value().to_be().to_ne_bytes();
fn ident<T>(ident: T) -> T {
ident
}
fn main() {
assert_eq!(REVERSE, ident(0x1e6a2c48));
assert_eq!(FROM_BE_BYTES, ident(0x12_34_56_78));
assert_eq!(FROM_LE_BYTES, ident(0x78_56_34_12));
assert_eq!(FROM_NE_BYTES, ident(i32::min_value()));
assert_eq!(TO_BE_BYTES, ident([0x12, 0x34, 0x56, 0x78]));
assert_eq!(TO_LE_BYTES, ident([0x78, 0x56, 0x34, 0x12]));
assert_eq!(TO_NE_BYTES, ident([0x80, 0, 0, 0]));
}

View file

@ -0,0 +1,47 @@
// Copyright 2018 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.
#![feature(const_int_overflowing)]
const ADD_A: (u32, bool) = 5u32.overflowing_add(2);
const ADD_B: (u32, bool) = u32::max_value().overflowing_add(1);
const SUB_A: (u32, bool) = 5u32.overflowing_sub(2);
const SUB_B: (u32, bool) = 0u32.overflowing_sub(1);
const MUL_A: (u32, bool) = 5u32.overflowing_mul(2);
const MUL_B: (u32, bool) = 1_000_000_000u32.overflowing_mul(10);
const SHL_A: (u32, bool) = 0x1u32.overflowing_shl(4);
const SHL_B: (u32, bool) = 0x1u32.overflowing_shl(132);
const SHR_A: (u32, bool) = 0x10u32.overflowing_shr(4);
const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132);
fn ident<T>(ident: T) -> T {
ident
}
fn main() {
assert_eq!(ADD_A, ident((7, false)));
assert_eq!(ADD_B, ident((0, true)));
assert_eq!(SUB_A, ident((3, false)));
assert_eq!(SUB_B, ident((u32::max_value(), true)));
assert_eq!(MUL_A, ident((10, false)));
assert_eq!(MUL_B, ident((1410065408, true)));
assert_eq!(SHL_A, ident((0x10, false)));
assert_eq!(SHL_B, ident((0x10, true)));
assert_eq!(SHR_A, ident((0x1, false)));
assert_eq!(SHR_B, ident((0x1, true)));
}

View file

@ -0,0 +1,23 @@
// Copyright 2018 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.
#![feature(const_int_rotate)]
const LEFT: u32 = 0x10000b3u32.rotate_left(8);
const RIGHT: u32 = 0xb301u32.rotate_right(8);
fn ident<T>(ident: T) -> T {
ident
}
fn main() {
assert_eq!(LEFT, ident(0xb301));
assert_eq!(RIGHT, ident(0x10000b3));
}

View file

@ -0,0 +1,23 @@
// Copyright 2018 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.
#![feature(const_int_sign)]
const NEGATIVE_A: bool = (-10i32).is_negative();
const NEGATIVE_B: bool = 10i32.is_negative();
const POSITIVE_A: bool= (-10i32).is_positive();
const POSITIVE_B: bool= 10i32.is_positive();
fn main() {
assert!(NEGATIVE_A);
assert!(!NEGATIVE_B);
assert!(!POSITIVE_A);
assert!(POSITIVE_B);
}

View file

@ -0,0 +1,47 @@
// Copyright 2018 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.
#![feature(const_int_wrapping)]
const ADD_A: u32 = 200u32.wrapping_add(55);
const ADD_B: u32 = 200u32.wrapping_add(u32::max_value());
const SUB_A: u32 = 100u32.wrapping_sub(100);
const SUB_B: u32 = 100u32.wrapping_sub(u32::max_value());
const MUL_A: u8 = 10u8.wrapping_mul(12);
const MUL_B: u8 = 25u8.wrapping_mul(12);
const SHL_A: u32 = 1u32.wrapping_shl(7);
const SHL_B: u32 = 1u32.wrapping_shl(128);
const SHR_A: u32 = 128u32.wrapping_shr(7);
const SHR_B: u32 = 128u32.wrapping_shr(128);
fn ident<T>(ident: T) -> T {
ident
}
fn main() {
assert_eq!(ADD_A, ident(255));
assert_eq!(ADD_B, ident(199));
assert_eq!(SUB_A, ident(0));
assert_eq!(SUB_B, ident(101));
assert_eq!(MUL_A, ident(120));
assert_eq!(MUL_B, ident(44));
assert_eq!(SHL_A, ident(128));
assert_eq!(SHL_B, ident(1));
assert_eq!(SHR_A, ident(1));
assert_eq!(SHR_B, ident(128));
}

View file

@ -0,0 +1,28 @@
// Copyright 2018 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.
#![feature(reverse_bits, int_to_from_bytes)]
fn main() {
let x: &'static i32 = &(5_i32.reverse_bits());
//~^ ERROR does not live long enough
let y: &'static i32 = &(i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]));
//~^ ERROR does not live long enough
let z: &'static i32 = &(i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]));
//~^ ERROR does not live long enough
let a: &'static i32 = &(i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0])));
//~^ ERROR does not live long enough
let b: &'static [u8] = &(0x12_34_56_78_i32.to_be_bytes());
//~^ ERROR does not live long enough
let c: &'static [u8] = &(0x12_34_56_78_i32.to_le_bytes());
//~^ ERROR does not live long enough
let d: &'static [u8] = &(i32::min_value().to_be().to_ne_bytes());
//~^ ERROR does not live long enough
}

View file

@ -0,0 +1,80 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:14:28
|
LL | let x: &'static i32 = &(5_i32.reverse_bits());
| ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:16:28
|
LL | let y: &'static i32 = &(i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:18:28
|
LL | let z: &'static i32 = &(i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:20:28
|
LL | let a: &'static i32 = &(i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0])));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:22:29
|
LL | let b: &'static [u8] = &(0x12_34_56_78_i32.to_be_bytes());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:24:29
|
LL | let c: &'static [u8] = &(0x12_34_56_78_i32.to_le_bytes());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-conversion.rs:26:29
|
LL | let d: &'static [u8] = &(i32::min_value().to_be().to_ne_bytes());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | //~^ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,15 @@
// Copyright 2018 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 main() {
let x: &'static (i32, bool) = &(5_i32.overflowing_add(3)); //~ ERROR does not live long enough
let y: &'static (i32, bool) = &(5_i32.overflowing_sub(3)); //~ ERROR does not live long enough
let z: &'static (i32, bool) = &(5_i32.overflowing_mul(3)); //~ ERROR does not live long enough
}

View file

@ -0,0 +1,35 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-overflowing.rs:12:36
|
LL | let x: &'static (i32, bool) = &(5_i32.overflowing_add(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-overflowing.rs:13:36
|
LL | let y: &'static (i32, bool) = &(5_i32.overflowing_sub(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | let z: &'static (i32, bool) = &(5_i32.overflowing_mul(3)); //~ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-overflowing.rs:14:36
|
LL | let z: &'static (i32, bool) = &(5_i32.overflowing_mul(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,14 @@
// Copyright 2018 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 main() {
let x: &'static i32 = &(5_i32.rotate_left(3)); //~ ERROR does not live long enough
let y: &'static i32 = &(5_i32.rotate_right(3)); //~ ERROR does not live long enough
}

View file

@ -0,0 +1,24 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-rotate.rs:12:28
|
LL | let x: &'static i32 = &(5_i32.rotate_left(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | let y: &'static i32 = &(5_i32.rotate_right(3)); //~ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-rotate.rs:13:28
|
LL | let y: &'static i32 = &(5_i32.rotate_right(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,14 @@
// Copyright 2018 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 main() {
let x: &'static bool = &(5_i32.is_negative()); //~ ERROR does not live long enough
let y: &'static bool = &(5_i32.is_positive()); //~ ERROR does not live long enough
}

View file

@ -0,0 +1,24 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-sign.rs:12:29
|
LL | let x: &'static bool = &(5_i32.is_negative()); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | let y: &'static bool = &(5_i32.is_positive()); //~ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-sign.rs:13:29
|
LL | let y: &'static bool = &(5_i32.is_positive()); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,17 @@
// Copyright 2018 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 main() {
let x: &'static i32 = &(5_i32.wrapping_add(3)); //~ ERROR does not live long enough
let y: &'static i32 = &(5_i32.wrapping_sub(3)); //~ ERROR does not live long enough
let z: &'static i32 = &(5_i32.wrapping_mul(3)); //~ ERROR does not live long enough
let a: &'static i32 = &(5_i32.wrapping_shl(3)); //~ ERROR does not live long enough
let b: &'static i32 = &(5_i32.wrapping_shr(3)); //~ ERROR does not live long enough
}

View file

@ -0,0 +1,57 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-wrapping.rs:12:28
|
LL | let x: &'static i32 = &(5_i32.wrapping_add(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-wrapping.rs:13:28
|
LL | let y: &'static i32 = &(5_i32.wrapping_sub(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-wrapping.rs:14:28
|
LL | let z: &'static i32 = &(5_i32.wrapping_mul(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-wrapping.rs:15:28
|
LL | let a: &'static i32 = &(5_i32.wrapping_shl(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | let b: &'static i32 = &(5_i32.wrapping_shr(3)); //~ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: borrowed value does not live long enough
--> $DIR/const-int-wrapping.rs:16:28
|
LL | let b: &'static i32 = &(5_i32.wrapping_shr(3)); //~ ERROR does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0597`.