Skip to content

Commit db6bc0f

Browse files
committed
Auto merge of #151674 - Zalathar:rollup-pNhrXnP, r=Zalathar
Rollup of 2 pull requests Successful merges: - #151612 (Update documentation for `cold_path`, `likely`, and `unlikely`) - #151670 (compiletest: Parse aux `proc-macro` directive into struct)
2 parents 0462e8f + 43b955a commit db6bc0f

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

library/core/src/hint.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ pub const fn must_use<T>(value: T) -> T {
658658
/// }
659659
/// }
660660
/// ```
661-
#[unstable(feature = "likely_unlikely", issue = "136873")]
661+
#[unstable(feature = "likely_unlikely", issue = "151619")]
662662
#[inline(always)]
663663
pub const fn likely(b: bool) -> bool {
664664
crate::intrinsics::likely(b)
@@ -708,7 +708,7 @@ pub const fn likely(b: bool) -> bool {
708708
/// }
709709
/// }
710710
/// ```
711-
#[unstable(feature = "likely_unlikely", issue = "136873")]
711+
#[unstable(feature = "likely_unlikely", issue = "151619")]
712712
#[inline(always)]
713713
pub const fn unlikely(b: bool) -> bool {
714714
crate::intrinsics::unlikely(b)
@@ -717,6 +717,10 @@ pub const fn unlikely(b: bool) -> bool {
717717
/// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
718718
/// choose to optimize paths that are not cold at the expense of paths that are cold.
719719
///
720+
/// Note that like all hints, the exact effect to codegen is not guaranteed. Using `cold_path`
721+
/// can actually *decrease* performance if the branch is called more than expected. It is advisable
722+
/// to perform benchmarks to tell if this function is useful.
723+
///
720724
/// # Examples
721725
///
722726
/// ```
@@ -741,6 +745,38 @@ pub const fn unlikely(b: bool) -> bool {
741745
/// }
742746
/// }
743747
/// ```
748+
///
749+
/// This can also be used to implement `likely` and `unlikely` helpers to hint the condition rather
750+
/// than the branch:
751+
///
752+
/// ```
753+
/// #![feature(cold_path)]
754+
/// use core::hint::cold_path;
755+
///
756+
/// #[inline(always)]
757+
/// pub const fn likely(b: bool) -> bool {
758+
/// if !b {
759+
/// cold_path();
760+
/// }
761+
/// b
762+
/// }
763+
///
764+
/// #[inline(always)]
765+
/// pub const fn unlikely(b: bool) -> bool {
766+
/// if b {
767+
/// cold_path();
768+
/// }
769+
/// b
770+
/// }
771+
///
772+
/// fn foo(x: i32) {
773+
/// if likely(x > 0) {
774+
/// println!("this branch is likely to be taken");
775+
/// } else {
776+
/// println!("this branch is unlikely to be taken");
777+
/// }
778+
/// }
779+
/// ```
744780
#[unstable(feature = "cold_path", issue = "136873")]
745781
#[inline(always)]
746782
pub const fn cold_path() {

src/tools/compiletest/src/directives/auxiliary.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ pub struct AuxCrate {
2525
pub path: String,
2626
}
2727

28+
/// The value of a `proc-macro` directive.
29+
#[derive(Clone, Debug, Default)]
30+
pub(crate) struct ProcMacro {
31+
/// With `proc-macro: bar.rs` this will be `bar.rs`.
32+
pub path: String,
33+
}
34+
2835
/// Properties parsed from `aux-*` test directives.
2936
#[derive(Clone, Debug, Default)]
3037
pub(crate) struct AuxProps {
@@ -37,7 +44,7 @@ pub(crate) struct AuxProps {
3744
/// to build and pass with the `--extern` flag.
3845
pub(crate) crates: Vec<AuxCrate>,
3946
/// Same as `builds`, but for proc-macros.
40-
pub(crate) proc_macros: Vec<String>,
47+
pub(crate) proc_macros: Vec<ProcMacro>,
4148
/// Similar to `builds`, but also uses the resulting dylib as a
4249
/// `-Zcodegen-backend` when compiling the test file.
4350
pub(crate) codegen_backend: Option<String>,
@@ -53,7 +60,7 @@ impl AuxProps {
5360
.chain(builds.iter().map(String::as_str))
5461
.chain(bins.iter().map(String::as_str))
5562
.chain(crates.iter().map(|c| c.path.as_str()))
56-
.chain(proc_macros.iter().map(String::as_str))
63+
.chain(proc_macros.iter().map(|p| p.path.as_str()))
5764
.chain(codegen_backend.iter().map(String::as_str))
5865
}
5966
}
@@ -74,8 +81,8 @@ pub(super) fn parse_and_update_aux(
7481
config.push_name_value_directive(ln, AUX_BUILD, &mut aux.builds, |r| r.trim().to_string());
7582
config.push_name_value_directive(ln, AUX_BIN, &mut aux.bins, |r| r.trim().to_string());
7683
config.push_name_value_directive(ln, AUX_CRATE, &mut aux.crates, parse_aux_crate);
77-
config
78-
.push_name_value_directive(ln, PROC_MACRO, &mut aux.proc_macros, |r| r.trim().to_string());
84+
config.push_name_value_directive(ln, PROC_MACRO, &mut aux.proc_macros, parse_proc_macro);
85+
7986
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
8087
aux.codegen_backend = Some(r.trim().to_owned());
8188
}
@@ -99,3 +106,7 @@ fn parse_aux_crate(r: String) -> AuxCrate {
99106

100107
AuxCrate { extern_modifiers: modifiers, name, path }
101108
}
109+
110+
fn parse_proc_macro(r: String) -> ProcMacro {
111+
ProcMacro { path: r.trim().to_string() }
112+
}

src/tools/compiletest/src/runtest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,13 +1298,13 @@ impl<'test> TestCx<'test> {
12981298
}
12991299

13001300
for proc_macro in &self.props.aux.proc_macros {
1301-
self.build_auxiliary(proc_macro, &aux_dir, Some(AuxType::ProcMacro));
1302-
let crate_name = path_to_crate_name(proc_macro);
1301+
self.build_auxiliary(&proc_macro.path, &aux_dir, Some(AuxType::ProcMacro));
1302+
let crate_name = path_to_crate_name(&proc_macro.path);
13031303
add_extern(
13041304
rustc,
13051305
None, // `extern_modifiers`
13061306
&crate_name,
1307-
proc_macro,
1307+
&proc_macro.path,
13081308
AuxType::ProcMacro,
13091309
);
13101310
}

0 commit comments

Comments
 (0)