about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgnzlbg <gonzalobg88@gmail.com>2019-03-21 09:15:52 +0100
committergnzlbg <gonzalobg88@gmail.com>2019-03-25 11:44:06 +0100
commitf5d6b3af6966470f7f3beac16ec2bd77b04d8905 (patch)
treee7a50a041fbe0cb83458095ace43a78289f28944
parent60eca54a7ce931c590801048f54ac0b2d2cd5067 (diff)
downloadrust-f5d6b3af6966470f7f3beac16ec2bd77b04d8905.tar.gz
rust-f5d6b3af6966470f7f3beac16ec2bd77b04d8905.zip
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.

I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).

For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
-rw-r--r--src/libcore/hint.rs19
-rw-r--r--src/libtest/lib.rs18
2 files changed, 20 insertions, 17 deletions
diff --git a/src/libcore/hint.rs b/src/libcore/hint.rs
index b2f82ef0d17..d6ddab0d8f5 100644
--- a/src/libcore/hint.rs
+++ b/src/libcore/hint.rs
@@ -91,3 +91,22 @@ pub fn spin_loop() {
         }
     }
 }
+
+/// A function that is opaque to the optimizer, to allow benchmarks to
+/// pretend to use outputs to assist in avoiding dead-code
+/// elimination.
+///
+/// This function is a no-op, and does not even read from `dummy`.
+#[cfg_attr(any(target_arch = "asmjs", target_arch = "wasm32"), inline(never))]
+#[unstable(feature = "test", issue = "27812")]
+pub fn black_box<T>(dummy: T) -> T {
+    #[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] {
+        // we need to "use" the argument in some way LLVM can't
+        // introspect.
+        unsafe { asm!("" : : "r"(&dummy)) }
+        dummy
+    }
+    #[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))] {
+        dummy
+    }
+}
diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs
index cb0ce480e42..5c91c0ec43b 100644
--- a/src/libtest/lib.rs
+++ b/src/libtest/lib.rs
@@ -27,23 +27,7 @@ pub use libtest::{
     TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk, stats::Summary
 };
 
-/// A function that is opaque to the optimizer, to allow benchmarks to
-/// pretend to use outputs to assist in avoiding dead-code
-/// elimination.
-///
-/// This function is a no-op, and does not even read from `dummy`.
-#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))]
-pub fn black_box<T>(dummy: T) -> T {
-    // we need to "use" the argument in some way LLVM can't
-    // introspect.
-    unsafe { asm!("" : : "r"(&dummy)) }
-    dummy
-}
-#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))]
-#[inline(never)]
-pub fn black_box<T>(dummy: T) -> T {
-    dummy
-}
+pub use std::hint::black_box;
 
 #[cfg(test)]
 mod tests {