Skip to content

Commit 6942e33

Browse files
committed
also get library/alloc to test in miri
1 parent 18d049a commit 6942e33

File tree

4 files changed

+94
-62
lines changed

4 files changed

+94
-62
lines changed

library/alloc/benches/vec_deque_append.rs

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const WARMUP_N: usize = 100;
55
const BENCH_N: usize = 1000;
66

77
fn main() {
8+
if cfg!(miri) {
9+
// Don't benchmark Miri...
10+
// (Due to cargo quirks, this gets picked up by `x.py test library/alloc --no-doc`.)
11+
return;
12+
}
813
let a: VecDeque<i32> = (0..VECDEQUE_LEN).collect();
914
let b: VecDeque<i32> = (0..VECDEQUE_LEN).collect();
1015

library/alloc/src/lib.rs

+26-61
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
not(test),
7373
not(any(test, bootstrap)),
7474
any(not(feature = "miri-test-libstd"), test, doctest),
75+
all(feature = "miri-test", not(any(test, doctest))),
76+
not(all(feature = "miri-test", not(any(test, doctest)))),
7577
no_global_oom_handling,
7678
not(no_global_oom_handling),
7779
not(no_rc),
@@ -217,6 +219,28 @@
217219
// from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
218220
#![feature(intra_doc_pointers)]
219221

222+
// We want to be able to `cargo miri test` this crate, but that's tricky.
223+
// We use the `miri-test` feature to indicate that we're running `cargo miri test`, and
224+
// with that feature we turn this crate into a re-export of the sysroot crate. We only do this when
225+
// building the crate that will become a dependency, not when doing the actual (doc)test build.
226+
// See `core/src/lib.rs` for more information.
227+
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
228+
extern crate alloc as realalloc;
229+
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
230+
#[stable(feature = "miri_test", since = "1.0.0")]
231+
pub use realalloc::*;
232+
233+
// Otherwise, we build the crate as usual. Everything that follows should have
234+
// `#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]`. To avoid having to repeat the
235+
// same `cfg` so many times, we `include!("lib_.rs")` with the main crate contents, and `cfg` the
236+
// `include!`. However, some macro-related things can't be behind the `include!` so we have to
237+
// repeat the `cfg` for them.
238+
239+
// Module with internal macros used by other modules (needs to be included before other modules).
240+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
241+
#[macro_use]
242+
mod macros;
243+
220244
// Allow testing this library
221245
#[cfg(test)]
222246
#[macro_use]
@@ -226,64 +250,5 @@ extern crate test;
226250
#[cfg(test)]
227251
mod testing;
228252

229-
// Module with internal macros used by other modules (needs to be included before other modules).
230-
#[macro_use]
231-
mod macros;
232-
233-
mod raw_vec;
234-
235-
// Heaps provided for low-level allocation strategies
236-
237-
pub mod alloc;
238-
239-
// Primitive types using the heaps above
240-
241-
// Need to conditionally define the mod from `boxed.rs` to avoid
242-
// duplicating the lang-items when building in test cfg; but also need
243-
// to allow code to have `use boxed::Box;` declarations.
244-
#[cfg(not(test))]
245-
pub mod boxed;
246-
#[cfg(test)]
247-
mod boxed {
248-
pub use std::boxed::Box;
249-
}
250-
pub mod borrow;
251-
pub mod collections;
252-
#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
253-
pub mod ffi;
254-
pub mod fmt;
255-
#[cfg(not(no_rc))]
256-
pub mod rc;
257-
pub mod slice;
258-
pub mod str;
259-
pub mod string;
260-
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
261-
pub mod sync;
262-
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
263-
pub mod task;
264-
#[cfg(test)]
265-
mod tests;
266-
pub mod vec;
267-
268-
#[doc(hidden)]
269-
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
270-
pub mod __export {
271-
pub use core::format_args;
272-
}
273-
274-
#[cfg(test)]
275-
#[allow(dead_code)] // Not used in all configurations
276-
pub(crate) mod test_helpers {
277-
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
278-
/// seed not being the same for every RNG invocation too.
279-
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
280-
use std::hash::{BuildHasher, Hash, Hasher};
281-
let mut hasher = std::hash::RandomState::new().build_hasher();
282-
std::panic::Location::caller().hash(&mut hasher);
283-
let hc64 = hasher.finish();
284-
let seed_vec =
285-
hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
286-
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
287-
rand::SeedableRng::from_seed(seed)
288-
}
289-
}
253+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
254+
include!("lib_.rs");

library/alloc/src/lib_.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Vec and VecDeque shared core
2+
3+
mod raw_vec;
4+
5+
// Heaps provided for low-level allocation strategies
6+
7+
pub mod alloc;
8+
9+
// Primitive types using the heaps above
10+
11+
// Need to conditionally define the mod from `boxed.rs` to avoid
12+
// duplicating the lang-items when building in test cfg; but also need
13+
// to allow code to have `use boxed::Box;` declarations.
14+
#[cfg(not(test))]
15+
pub mod boxed;
16+
#[cfg(test)]
17+
mod boxed {
18+
pub use std::boxed::Box;
19+
}
20+
pub mod borrow;
21+
pub mod collections;
22+
#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
23+
pub mod ffi;
24+
pub mod fmt;
25+
#[cfg(not(no_rc))]
26+
pub mod rc;
27+
pub mod slice;
28+
pub mod str;
29+
pub mod string;
30+
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
31+
pub mod sync;
32+
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
33+
pub mod task;
34+
#[cfg(test)]
35+
mod tests;
36+
pub mod vec;
37+
38+
#[doc(hidden)]
39+
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
40+
pub mod __export {
41+
pub use core::format_args;
42+
}
43+
44+
#[cfg(test)]
45+
#[allow(dead_code)] // Not used in all configurations
46+
pub(crate) mod test_helpers {
47+
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
48+
/// seed not being the same for every RNG invocation too.
49+
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
50+
use std::hash::{BuildHasher, Hash, Hasher};
51+
let mut hasher = std::hash::RandomState::new().build_hasher();
52+
std::panic::Location::caller().hash(&mut hasher);
53+
let hc64 = hasher.finish();
54+
let seed_vec =
55+
hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
56+
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
57+
rand::SeedableRng::from_seed(seed)
58+
}
59+
}

src/bootstrap/src/core/build_steps/test.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2618,7 +2618,9 @@ impl Step for Crate {
26182618
std::process::exit(1);
26192619
}
26202620

2621-
// Prepare sysroot
2621+
// Prepare sysroots
2622+
builder.ensure(compile::Std::new(compiler, compiler.host));
2623+
let sysroot = builder.sysroot(compiler);
26222624
let miri_sysroot = Miri::build_miri_sysroot(builder, compiler, target);
26232625

26242626
// Build `cargo miri test` command
@@ -2631,6 +2633,7 @@ impl Step for Crate {
26312633
"miri-test",
26322634
);
26332635
cargo.env("MIRI_SYSROOT", &miri_sysroot);
2636+
cargo.env("MIRI_HOST_SYSROOT", &sysroot);
26342637
// See `Miri` tests for why
26352638
if builder.doc_tests != DocTests::No {
26362639
cargo.env("RUSTDOC", builder.rustdoc(compiler));

0 commit comments

Comments
 (0)