@@ -1465,6 +1465,22 @@ pub const unsafe fn assume(b: bool) {
1465
1465
}
1466
1466
}
1467
1467
1468
+ /// Hints to the compiler that current code path is cold.
1469
+ ///
1470
+ /// Note that, unlike most intrinsics, this is safe to call;
1471
+ /// it does not require an `unsafe` block.
1472
+ /// Therefore, implementations must not require the user to uphold
1473
+ /// any safety invariants.
1474
+ ///
1475
+ /// This intrinsic does not have a stable counterpart.
1476
+ #[ unstable( feature = "core_intrinsics" , issue = "none" ) ]
1477
+ #[ cfg_attr( not( bootstrap) , rustc_intrinsic) ]
1478
+ #[ cfg( not( bootstrap) ) ]
1479
+ #[ rustc_nounwind]
1480
+ #[ miri:: intrinsic_fallback_is_spec]
1481
+ #[ cold]
1482
+ pub const fn cold_path ( ) { }
1483
+
1468
1484
/// Hints to the compiler that branch condition is likely to be true.
1469
1485
/// Returns the value passed to it.
1470
1486
///
@@ -1480,13 +1496,21 @@ pub const unsafe fn assume(b: bool) {
1480
1496
bootstrap,
1481
1497
rustc_const_stable( feature = "const_likely" , since = "CURRENT_RUSTC_VERSION" )
1482
1498
) ]
1483
- #[ cfg_attr( not( bootstrap) , rustc_const_stable_intrinsic) ]
1484
1499
#[ unstable( feature = "core_intrinsics" , issue = "none" ) ]
1485
- #[ rustc_intrinsic]
1486
1500
#[ rustc_nounwind]
1487
- #[ miri :: intrinsic_fallback_is_spec ]
1501
+ #[ inline ( always ) ]
1488
1502
pub const fn likely ( b : bool ) -> bool {
1489
- b
1503
+ #[ cfg( bootstrap) ]
1504
+ {
1505
+ b
1506
+ }
1507
+ #[ cfg( not( bootstrap) ) ]
1508
+ if b {
1509
+ true
1510
+ } else {
1511
+ cold_path ( ) ;
1512
+ false
1513
+ }
1490
1514
}
1491
1515
1492
1516
/// Hints to the compiler that branch condition is likely to be false.
@@ -1504,13 +1528,21 @@ pub const fn likely(b: bool) -> bool {
1504
1528
bootstrap,
1505
1529
rustc_const_stable( feature = "const_likely" , since = "CURRENT_RUSTC_VERSION" )
1506
1530
) ]
1507
- #[ cfg_attr( not( bootstrap) , rustc_const_stable_intrinsic) ]
1508
1531
#[ unstable( feature = "core_intrinsics" , issue = "none" ) ]
1509
- #[ rustc_intrinsic]
1510
1532
#[ rustc_nounwind]
1511
- #[ miri :: intrinsic_fallback_is_spec ]
1533
+ #[ inline ( always ) ]
1512
1534
pub const fn unlikely ( b : bool ) -> bool {
1513
- b
1535
+ #[ cfg( bootstrap) ]
1536
+ {
1537
+ b
1538
+ }
1539
+ #[ cfg( not( bootstrap) ) ]
1540
+ if b {
1541
+ cold_path ( ) ;
1542
+ true
1543
+ } else {
1544
+ false
1545
+ }
1514
1546
}
1515
1547
1516
1548
/// Returns either `true_val` or `false_val` depending on condition `b` with a
0 commit comments