Skip to content

Commit 2760f87

Browse files
committed
const-stabilize const_int_ops + reverse_bits
1 parent 50152d2 commit 2760f87

File tree

11 files changed

+163
-143
lines changed

11 files changed

+163
-143
lines changed

src/libcore/intrinsics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ extern "rust-intrinsic" {
13431343
/// use std::intrinsics::ctlz;
13441344
///
13451345
/// let x = 0b0001_1100_u8;
1346-
/// let num_leading = unsafe { ctlz(x) };
1346+
/// let num_leading = ctlz(x);
13471347
/// assert_eq!(num_leading, 3);
13481348
/// ```
13491349
///
@@ -1355,7 +1355,7 @@ extern "rust-intrinsic" {
13551355
/// use std::intrinsics::ctlz;
13561356
///
13571357
/// let x = 0u16;
1358-
/// let num_leading = unsafe { ctlz(x) };
1358+
/// let num_leading = ctlz(x);
13591359
/// assert_eq!(num_leading, 16);
13601360
/// ```
13611361
pub fn ctlz<T>(x: T) -> T;
@@ -1386,7 +1386,7 @@ extern "rust-intrinsic" {
13861386
/// use std::intrinsics::cttz;
13871387
///
13881388
/// let x = 0b0011_1000_u8;
1389-
/// let num_trailing = unsafe { cttz(x) };
1389+
/// let num_trailing = cttz(x);
13901390
/// assert_eq!(num_trailing, 3);
13911391
/// ```
13921392
///
@@ -1398,7 +1398,7 @@ extern "rust-intrinsic" {
13981398
/// use std::intrinsics::cttz;
13991399
///
14001400
/// let x = 0u16;
1401-
/// let num_trailing = unsafe { cttz(x) };
1401+
/// let num_trailing = cttz(x);
14021402
/// assert_eq!(num_trailing, 16);
14031403
/// ```
14041404
pub fn cttz<T>(x: T) -> T;

src/libcore/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
#![feature(cfg_target_has_atomic)]
7272
#![feature(concat_idents)]
7373
#![feature(const_fn)]
74-
#![feature(const_int_ops)]
74+
#![cfg_attr(stage0, feature(const_int_ops))]
7575
#![feature(const_fn_union)]
7676
#![feature(custom_attribute)]
7777
#![feature(doc_cfg)]

src/libcore/num/mod.rs

+36-21
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ $EndFeature, "
275275
```
276276
"),
277277
#[stable(feature = "rust1", since = "1.0.0")]
278-
#[rustc_const_unstable(feature = "const_int_ops")]
278+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
279279
#[inline]
280280
pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
281281
}
@@ -291,7 +291,7 @@ Basic usage:
291291
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value().count_zeros(), 1);", $EndFeature, "
292292
```"),
293293
#[stable(feature = "rust1", since = "1.0.0")]
294-
#[rustc_const_unstable(feature = "const_int_ops")]
294+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
295295
#[inline]
296296
pub const fn count_zeros(self) -> u32 {
297297
(!self).count_ones()
@@ -312,7 +312,7 @@ assert_eq!(n.leading_zeros(), 0);",
312312
$EndFeature, "
313313
```"),
314314
#[stable(feature = "rust1", since = "1.0.0")]
315-
#[rustc_const_unstable(feature = "const_int_ops")]
315+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
316316
#[inline]
317317
pub const fn leading_zeros(self) -> u32 {
318318
(self as $UnsignedT).leading_zeros()
@@ -333,7 +333,7 @@ assert_eq!(n.trailing_zeros(), 2);",
333333
$EndFeature, "
334334
```"),
335335
#[stable(feature = "rust1", since = "1.0.0")]
336-
#[rustc_const_unstable(feature = "const_int_ops")]
336+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
337337
#[inline]
338338
pub const fn trailing_zeros(self) -> u32 {
339339
(self as $UnsignedT).trailing_zeros()
@@ -404,7 +404,7 @@ let m = n.swap_bytes();
404404
assert_eq!(m, ", $swapped, ");
405405
```"),
406406
#[stable(feature = "rust1", since = "1.0.0")]
407-
#[rustc_const_unstable(feature = "const_int_ops")]
407+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
408408
#[inline]
409409
pub const fn swap_bytes(self) -> Self {
410410
(self as $UnsignedT).swap_bytes() as Self
@@ -454,7 +454,7 @@ if cfg!(target_endian = \"big\") {
454454
$EndFeature, "
455455
```"),
456456
#[stable(feature = "rust1", since = "1.0.0")]
457-
#[rustc_const_unstable(feature = "const_int_ops")]
457+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
458458
#[inline]
459459
pub const fn from_be(x: Self) -> Self {
460460
#[cfg(target_endian = "big")]
@@ -488,7 +488,7 @@ if cfg!(target_endian = \"little\") {
488488
$EndFeature, "
489489
```"),
490490
#[stable(feature = "rust1", since = "1.0.0")]
491-
#[rustc_const_unstable(feature = "const_int_ops")]
491+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
492492
#[inline]
493493
pub const fn from_le(x: Self) -> Self {
494494
#[cfg(target_endian = "little")]
@@ -522,7 +522,7 @@ if cfg!(target_endian = \"big\") {
522522
$EndFeature, "
523523
```"),
524524
#[stable(feature = "rust1", since = "1.0.0")]
525-
#[rustc_const_unstable(feature = "const_int_ops")]
525+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
526526
#[inline]
527527
pub const fn to_be(self) -> Self { // or not to be?
528528
#[cfg(target_endian = "big")]
@@ -556,7 +556,7 @@ if cfg!(target_endian = \"little\") {
556556
$EndFeature, "
557557
```"),
558558
#[stable(feature = "rust1", since = "1.0.0")]
559-
#[rustc_const_unstable(feature = "const_int_ops")]
559+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
560560
#[inline]
561561
pub const fn to_le(self) -> Self {
562562
#[cfg(target_endian = "little")]
@@ -2234,10 +2234,13 @@ Basic usage:
22342234
assert_eq!(n.count_ones(), 3);", $EndFeature, "
22352235
```"),
22362236
#[stable(feature = "rust1", since = "1.0.0")]
2237-
#[rustc_const_unstable(feature = "const_int_ops")]
2237+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
22382238
#[inline]
22392239
pub const fn count_ones(self) -> u32 {
2240+
#[cfg(stage0)]
22402241
unsafe { intrinsics::ctpop(self as $ActualT) as u32 }
2242+
#[cfg(not(stage0))]
2243+
{ intrinsics::ctpop(self as $ActualT) as u32 }
22412244
}
22422245
}
22432246

@@ -2252,7 +2255,7 @@ Basic usage:
22522255
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value().count_zeros(), 0);", $EndFeature, "
22532256
```"),
22542257
#[stable(feature = "rust1", since = "1.0.0")]
2255-
#[rustc_const_unstable(feature = "const_int_ops")]
2258+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
22562259
#[inline]
22572260
pub const fn count_zeros(self) -> u32 {
22582261
(!self).count_ones()
@@ -2272,10 +2275,13 @@ Basic usage:
22722275
assert_eq!(n.leading_zeros(), 2);", $EndFeature, "
22732276
```"),
22742277
#[stable(feature = "rust1", since = "1.0.0")]
2275-
#[rustc_const_unstable(feature = "const_int_ops")]
2278+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
22762279
#[inline]
22772280
pub const fn leading_zeros(self) -> u32 {
2281+
#[cfg(stage0)]
22782282
unsafe { intrinsics::ctlz(self as $ActualT) as u32 }
2283+
#[cfg(not(stage0))]
2284+
{ intrinsics::ctlz(self as $ActualT) as u32 }
22792285
}
22802286
}
22812287

@@ -2293,10 +2299,13 @@ Basic usage:
22932299
assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
22942300
```"),
22952301
#[stable(feature = "rust1", since = "1.0.0")]
2296-
#[rustc_const_unstable(feature = "const_int_ops")]
2302+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
22972303
#[inline]
22982304
pub const fn trailing_zeros(self) -> u32 {
2305+
#[cfg(stage0)]
22992306
unsafe { intrinsics::cttz(self) as u32 }
2307+
#[cfg(not(stage0))]
2308+
{ intrinsics::cttz(self) as u32 }
23002309
}
23012310
}
23022311

@@ -2370,10 +2379,13 @@ let m = n.swap_bytes();
23702379
assert_eq!(m, ", $swapped, ");
23712380
```"),
23722381
#[stable(feature = "rust1", since = "1.0.0")]
2373-
#[rustc_const_unstable(feature = "const_int_ops")]
2382+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
23742383
#[inline]
23752384
pub const fn swap_bytes(self) -> Self {
2385+
#[cfg(stage0)]
23762386
unsafe { intrinsics::bswap(self as $ActualT) as Self }
2387+
#[cfg(not(stage0))]
2388+
{ intrinsics::bswap(self as $ActualT) as Self }
23772389
}
23782390
}
23792391

@@ -2393,10 +2405,13 @@ let m = n.reverse_bits();
23932405
assert_eq!(m, ", $reversed, ");
23942406
```"),
23952407
#[unstable(feature = "reverse_bits", issue = "48763")]
2396-
#[rustc_const_unstable(feature = "const_int_conversion")]
2408+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_conversion"))]
23972409
#[inline]
23982410
pub const fn reverse_bits(self) -> Self {
2411+
#[cfg(stage0)]
23992412
unsafe { intrinsics::bitreverse(self as $ActualT) as Self }
2413+
#[cfg(not(stage0))]
2414+
{ intrinsics::bitreverse(self as $ActualT) as Self }
24002415
}
24012416
}
24022417

@@ -2420,7 +2435,7 @@ if cfg!(target_endian = \"big\") {
24202435
}", $EndFeature, "
24212436
```"),
24222437
#[stable(feature = "rust1", since = "1.0.0")]
2423-
#[rustc_const_unstable(feature = "const_int_ops")]
2438+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
24242439
#[inline]
24252440
pub const fn from_be(x: Self) -> Self {
24262441
#[cfg(target_endian = "big")]
@@ -2454,7 +2469,7 @@ if cfg!(target_endian = \"little\") {
24542469
}", $EndFeature, "
24552470
```"),
24562471
#[stable(feature = "rust1", since = "1.0.0")]
2457-
#[rustc_const_unstable(feature = "const_int_ops")]
2472+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
24582473
#[inline]
24592474
pub const fn from_le(x: Self) -> Self {
24602475
#[cfg(target_endian = "little")]
@@ -2488,7 +2503,7 @@ if cfg!(target_endian = \"big\") {
24882503
}", $EndFeature, "
24892504
```"),
24902505
#[stable(feature = "rust1", since = "1.0.0")]
2491-
#[rustc_const_unstable(feature = "const_int_ops")]
2506+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
24922507
#[inline]
24932508
pub const fn to_be(self) -> Self { // or not to be?
24942509
#[cfg(target_endian = "big")]
@@ -2522,7 +2537,7 @@ if cfg!(target_endian = \"little\") {
25222537
}", $EndFeature, "
25232538
```"),
25242539
#[stable(feature = "rust1", since = "1.0.0")]
2525-
#[rustc_const_unstable(feature = "const_int_ops")]
2540+
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
25262541
#[inline]
25272542
pub const fn to_le(self) -> Self {
25282543
#[cfg(target_endian = "little")]
@@ -2957,8 +2972,8 @@ $EndFeature, "
29572972
unsafe {
29582973
intrinsics::overflowing_mul(self, rhs)
29592974
}
2960-
#[cfg(not(stage0))]
2961-
intrinsics::overflowing_mul(self, rhs)
2975+
#[cfg(not(stage0))]
2976+
intrinsics::overflowing_mul(self, rhs)
29622977
}
29632978

29642979
doc_comment! {

src/librustc_mir/transform/qualify_min_const_fn.rs

+5
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ fn is_intrinsic_whitelisted(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool
404404
| "unchecked_shr" // ~> .wrapping_shr
405405
| "rotate_left" // ~> .rotate_left
406406
| "rotate_right" // ~> .rotate_right
407+
| "ctpop" // ~> .count_ones
408+
| "ctlz" // ~> .leading_zeros
409+
| "cttz" // ~> .trailing_zeros
410+
| "bswap" // ~> .swap_bytes
411+
| "bitreverse" // ~> .reverse_bits
407412
=> true,
408413
_ => false,
409414
}

src/librustc_typeck/check/intrinsic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
7171
match intrinsic {
7272
"size_of" | "min_align_of" | "needs_drop" |
7373
"overflowing_add" | "overflowing_sub" | "overflowing_mul" |
74-
"rotate_left" | "rotate_right"
74+
"rotate_left" | "rotate_right" |
75+
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse"
7576
=> hir::Unsafety::Normal,
7677
_ => hir::Unsafety::Unsafe,
7778
}

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
#![feature(char_error_internals)]
243243
#![feature(compiler_builtins_lib)]
244244
#![feature(concat_idents)]
245-
#![feature(const_int_ops)]
245+
#![cfg_attr(stage0, feature(const_int_ops))]
246246
#![feature(const_ip)]
247247
#![feature(const_raw_ptr_deref)]
248248
#![feature(const_cstr_unchecked)]

src/test/run-pass/const-int-conversion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(const_int_conversion, const_int_ops, reverse_bits)]
1+
#![feature(const_int_conversion, reverse_bits)]
22

33
const REVERSE: u32 = 0x12345678_u32.reverse_bits();
44
const FROM_BE_BYTES: i32 = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
@@ -21,4 +21,3 @@ fn main() {
2121
assert_eq!(TO_LE_BYTES, ident([0x78, 0x56, 0x34, 0x12]));
2222
assert_eq!(TO_NE_BYTES, ident([0x80, 0, 0, 0]));
2323
}
24-

src/test/run-pass/consts/const-endianess.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// run-pass
2-
#![feature(const_int_ops)]
32
#![feature(test)]
43

54
extern crate test;
@@ -8,7 +7,6 @@ use test::black_box as b;
87
const BE_U32: u32 = 55u32.to_be();
98
const LE_U32: u32 = 55u32.to_le();
109

11-
1210
fn main() {
1311
assert_eq!(BE_U32, b(55u32).to_be());
1412
assert_eq!(LE_U32, b(55u32).to_le());

src/test/run-pass/ctfe/bswap-const.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use std::intrinsics;
66

7-
const SWAPPED_U8: u8 = unsafe { intrinsics::bswap(0x12_u8) };
8-
const SWAPPED_U16: u16 = unsafe { intrinsics::bswap(0x12_34_u16) };
9-
const SWAPPED_I32: i32 = unsafe { intrinsics::bswap(0x12_34_56_78_i32) };
7+
const SWAPPED_U8: u8 = intrinsics::bswap(0x12_u8);
8+
const SWAPPED_U16: u16 = intrinsics::bswap(0x12_34_u16);
9+
const SWAPPED_I32: i32 = intrinsics::bswap(0x12_34_56_78_i32);
1010

1111
fn main() {
1212
assert_eq!(SWAPPED_U8, 0x12);

0 commit comments

Comments
 (0)