Skip to content

Commit e2fdb05

Browse files
committed
add '--miri' flag to 'x.py test', and make it work for 'library/core'
This does need some hacks inside libcore though
1 parent 871df0d commit e2fdb05

File tree

13 files changed

+271
-177
lines changed

13 files changed

+271
-177
lines changed

library/alloc/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ compiler-builtins-c = ["compiler_builtins/c"]
3636
compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
3737
compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
3838
compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
39+
# Empty the crate so Miri can run tests
40+
miri-test = ["core/miri-test"]
3941
# Make panics and failed asserts immediately abort without formatting any message
4042
panic_immediate_abort = []

library/core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ panic_immediate_abort = []
3434
# Make `RefCell` store additional debugging information, which is printed out when
3535
# a borrow error occurs
3636
debug_refcell = []
37+
# Empty the crate so Miri can run tests
38+
miri-test = []

library/core/src/lib.rs

Lines changed: 29 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
#![doc(cfg_hide(
7373
not(test),
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_fp_fmt_parse,
7678
target_pointer_width = "16",
7779
target_pointer_width = "32",
@@ -282,160 +284,52 @@
282284
#![feature(wasm_target_feature)]
283285
// tidy-alphabetical-end
284286

287+
// We want to be able to `cargo miri test` this crate, but that's tricky:
288+
// This will build the crate itself again, so now we have two `core` crates, one in the sysroot and
289+
// one built as dependency of the test crate. That leads to duplicate lang item errors. `./x.py test
290+
// library` avoids this by making the sysroot exactly the crates that `cargo build` produces, but
291+
// Miri builds the sysroot itself so that does not work.
292+
// Instead, we use the `miri-test` feature to indicate that we're running `cargo miri test`, and
293+
// with that feature we turn this crate into a re-export of the sysroot crate. We only do this when
294+
// building the crate that will become a dependency, not when doing the actual (doc)test build.
295+
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
296+
extern crate core as realcore;
297+
#[cfg(all(feature = "miri-test", not(any(test, doctest))))]
298+
#[stable(feature = "miri_test", since = "1.0.0")]
299+
pub use realcore::*;
300+
301+
// Otherwise, we build the crate as usual. Everything that follows should have
302+
// `#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]`. To avoid having to repeat the
303+
// same `cfg` so many times, we `include!("lib_.rs")` with the main crate contents, and `cfg` the
304+
// `include!`. However, some macro-related things can't be behind the `include!` so we have to
305+
// repeat the `cfg` for them.
306+
285307
// allow using `core::` in intra-doc links
308+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
286309
#[allow(unused_extern_crates)]
287310
extern crate self as core;
288311

312+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
289313
#[prelude_import]
290314
#[allow(unused)]
291315
use prelude::rust_2021::*;
292316

293317
#[cfg(not(test))] // See #65860
318+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
294319
#[macro_use]
295320
mod macros;
296321

297-
// We don't export this through #[macro_export] for now, to avoid breakage.
298-
// See https://github.com/rust-lang/rust/issues/82913
299-
#[cfg(not(test))]
300-
#[unstable(feature = "assert_matches", issue = "82775")]
301-
/// Unstable module containing the unstable `assert_matches` macro.
302-
pub mod assert_matches {
303-
#[unstable(feature = "assert_matches", issue = "82775")]
304-
pub use crate::macros::{assert_matches, debug_assert_matches};
305-
}
306-
307-
#[unstable(feature = "cfg_match", issue = "115585")]
308-
pub use crate::macros::cfg_match;
309-
310-
#[macro_use]
311-
mod internal_macros;
312-
313-
#[path = "num/shells/int_macros.rs"]
314-
#[macro_use]
315-
mod int_macros;
316-
317-
#[rustc_diagnostic_item = "i128_legacy_mod"]
318-
#[path = "num/shells/i128.rs"]
319-
pub mod i128;
320-
#[rustc_diagnostic_item = "i16_legacy_mod"]
321-
#[path = "num/shells/i16.rs"]
322-
pub mod i16;
323-
#[rustc_diagnostic_item = "i32_legacy_mod"]
324-
#[path = "num/shells/i32.rs"]
325-
pub mod i32;
326-
#[rustc_diagnostic_item = "i64_legacy_mod"]
327-
#[path = "num/shells/i64.rs"]
328-
pub mod i64;
329-
#[rustc_diagnostic_item = "i8_legacy_mod"]
330-
#[path = "num/shells/i8.rs"]
331-
pub mod i8;
332-
#[rustc_diagnostic_item = "isize_legacy_mod"]
333-
#[path = "num/shells/isize.rs"]
334-
pub mod isize;
335-
336-
#[rustc_diagnostic_item = "u128_legacy_mod"]
337-
#[path = "num/shells/u128.rs"]
338-
pub mod u128;
339-
#[rustc_diagnostic_item = "u16_legacy_mod"]
340-
#[path = "num/shells/u16.rs"]
341-
pub mod u16;
342-
#[rustc_diagnostic_item = "u32_legacy_mod"]
343-
#[path = "num/shells/u32.rs"]
344-
pub mod u32;
345-
#[rustc_diagnostic_item = "u64_legacy_mod"]
346-
#[path = "num/shells/u64.rs"]
347-
pub mod u64;
348-
#[rustc_diagnostic_item = "u8_legacy_mod"]
349-
#[path = "num/shells/u8.rs"]
350-
pub mod u8;
351-
#[rustc_diagnostic_item = "usize_legacy_mod"]
352-
#[path = "num/shells/usize.rs"]
353-
pub mod usize;
354-
355-
#[path = "num/f32.rs"]
356-
pub mod f32;
357-
#[path = "num/f64.rs"]
358-
pub mod f64;
359-
360-
#[macro_use]
361-
pub mod num;
362-
363322
/* The core prelude, not as all-encompassing as the std prelude */
364-
323+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
365324
pub mod prelude;
366325

367-
/* Core modules for ownership management */
368-
369-
pub mod hint;
370-
pub mod intrinsics;
371-
pub mod mem;
372-
pub mod ptr;
373-
mod ub_checks;
374-
375-
/* Core language traits */
376-
377-
pub mod borrow;
378-
pub mod clone;
379-
pub mod cmp;
380-
pub mod convert;
381-
pub mod default;
382-
pub mod error;
383-
pub mod marker;
384-
pub mod ops;
385-
386-
/* Core types and methods on primitives */
387-
388-
pub mod any;
389-
pub mod array;
390-
pub mod ascii;
391-
pub mod asserting;
392-
#[unstable(feature = "async_iterator", issue = "79024")]
393-
pub mod async_iter;
394-
pub mod cell;
395-
pub mod char;
396-
pub mod ffi;
397-
#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
398-
pub mod io;
399-
pub mod iter;
400-
pub mod net;
401-
pub mod option;
402-
pub mod panic;
403-
pub mod panicking;
404-
pub mod pin;
405-
pub mod result;
406-
pub mod sync;
407-
408-
pub mod fmt;
409-
pub mod hash;
410-
pub mod slice;
411-
pub mod str;
412-
pub mod time;
413-
414-
pub mod unicode;
415-
416-
/* Async */
417-
pub mod future;
418-
pub mod task;
419-
420-
/* Heap memory allocator trait */
421-
#[allow(missing_docs)]
422-
pub mod alloc;
423-
424-
// note: does not need to be public
425-
mod bool;
426-
mod escape;
427-
mod tuple;
428-
mod unit;
429-
430-
#[stable(feature = "core_primitive", since = "1.43.0")]
431-
pub mod primitive;
432-
433326
// Pull in the `core_arch` crate directly into core. The contents of
434327
// `core_arch` are in a different repository: rust-lang/stdarch.
435328
//
436329
// `core_arch` depends on core, but the contents of this module are
437330
// set up in such a way that directly pulling it here works such that the
438331
// crate uses the this crate as its core.
332+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
439333
#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
440334
#[allow(
441335
missing_docs,
@@ -449,27 +343,5 @@ pub mod primitive;
449343
#[allow(rustdoc::bare_urls)]
450344
mod core_arch;
451345

452-
#[stable(feature = "simd_arch", since = "1.27.0")]
453-
pub mod arch;
454-
455-
// Pull in the `core_simd` crate directly into core. The contents of
456-
// `core_simd` are in a different repository: rust-lang/portable-simd.
457-
//
458-
// `core_simd` depends on core, but the contents of this module are
459-
// set up in such a way that directly pulling it here works such that the
460-
// crate uses this crate as its core.
461-
#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
462-
#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
463-
#[allow(rustdoc::bare_urls)]
464-
#[unstable(feature = "portable_simd", issue = "86656")]
465-
mod core_simd;
466-
467-
#[unstable(feature = "portable_simd", issue = "86656")]
468-
pub mod simd {
469-
#![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
470-
471-
#[unstable(feature = "portable_simd", issue = "86656")]
472-
pub use crate::core_simd::simd::*;
473-
}
474-
475-
include!("primitive_docs.rs");
346+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
347+
include!("lib_.rs");

library/core/src/lib_.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// We don't export this through #[macro_export] for now, to avoid breakage.
2+
// See https://github.com/rust-lang/rust/issues/82913
3+
#[cfg(not(test))]
4+
#[unstable(feature = "assert_matches", issue = "82775")]
5+
/// Unstable module containing the unstable `assert_matches` macro.
6+
pub mod assert_matches {
7+
#[unstable(feature = "assert_matches", issue = "82775")]
8+
pub use crate::macros::{assert_matches, debug_assert_matches};
9+
}
10+
11+
#[unstable(feature = "cfg_match", issue = "115585")]
12+
pub use crate::macros::cfg_match;
13+
14+
#[macro_use]
15+
mod internal_macros;
16+
17+
#[path = "num/shells/int_macros.rs"]
18+
#[macro_use]
19+
mod int_macros;
20+
21+
#[rustc_diagnostic_item = "i128_legacy_mod"]
22+
#[path = "num/shells/i128.rs"]
23+
pub mod i128;
24+
#[rustc_diagnostic_item = "i16_legacy_mod"]
25+
#[path = "num/shells/i16.rs"]
26+
pub mod i16;
27+
#[rustc_diagnostic_item = "i32_legacy_mod"]
28+
#[path = "num/shells/i32.rs"]
29+
pub mod i32;
30+
#[rustc_diagnostic_item = "i64_legacy_mod"]
31+
#[path = "num/shells/i64.rs"]
32+
pub mod i64;
33+
#[rustc_diagnostic_item = "i8_legacy_mod"]
34+
#[path = "num/shells/i8.rs"]
35+
pub mod i8;
36+
#[rustc_diagnostic_item = "isize_legacy_mod"]
37+
#[path = "num/shells/isize.rs"]
38+
pub mod isize;
39+
40+
#[rustc_diagnostic_item = "u128_legacy_mod"]
41+
#[path = "num/shells/u128.rs"]
42+
pub mod u128;
43+
#[rustc_diagnostic_item = "u16_legacy_mod"]
44+
#[path = "num/shells/u16.rs"]
45+
pub mod u16;
46+
#[rustc_diagnostic_item = "u32_legacy_mod"]
47+
#[path = "num/shells/u32.rs"]
48+
pub mod u32;
49+
#[rustc_diagnostic_item = "u64_legacy_mod"]
50+
#[path = "num/shells/u64.rs"]
51+
pub mod u64;
52+
#[rustc_diagnostic_item = "u8_legacy_mod"]
53+
#[path = "num/shells/u8.rs"]
54+
pub mod u8;
55+
#[rustc_diagnostic_item = "usize_legacy_mod"]
56+
#[path = "num/shells/usize.rs"]
57+
pub mod usize;
58+
59+
#[path = "num/f32.rs"]
60+
pub mod f32;
61+
#[path = "num/f64.rs"]
62+
pub mod f64;
63+
64+
#[macro_use]
65+
pub mod num;
66+
67+
/* Core modules for ownership management */
68+
69+
pub mod hint;
70+
pub mod intrinsics;
71+
pub mod mem;
72+
pub mod ptr;
73+
mod ub_checks;
74+
75+
/* Core language traits */
76+
77+
pub mod borrow;
78+
pub mod clone;
79+
pub mod cmp;
80+
pub mod convert;
81+
pub mod default;
82+
pub mod error;
83+
pub mod marker;
84+
pub mod ops;
85+
86+
/* Core types and methods on primitives */
87+
88+
pub mod any;
89+
pub mod array;
90+
pub mod ascii;
91+
pub mod asserting;
92+
#[unstable(feature = "async_iterator", issue = "79024")]
93+
pub mod async_iter;
94+
pub mod cell;
95+
pub mod char;
96+
pub mod ffi;
97+
#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
98+
pub mod io;
99+
pub mod iter;
100+
pub mod net;
101+
pub mod option;
102+
pub mod panic;
103+
pub mod panicking;
104+
pub mod pin;
105+
pub mod result;
106+
pub mod sync;
107+
108+
pub mod fmt;
109+
pub mod hash;
110+
pub mod slice;
111+
pub mod str;
112+
pub mod time;
113+
114+
pub mod unicode;
115+
116+
/* Async */
117+
pub mod future;
118+
pub mod task;
119+
120+
/* Heap memory allocator trait */
121+
#[allow(missing_docs)]
122+
pub mod alloc;
123+
124+
// note: does not need to be public
125+
mod bool;
126+
mod escape;
127+
mod tuple;
128+
mod unit;
129+
130+
#[stable(feature = "core_primitive", since = "1.43.0")]
131+
pub mod primitive;
132+
133+
#[stable(feature = "simd_arch", since = "1.27.0")]
134+
pub mod arch;
135+
136+
// Pull in the `core_simd` crate directly into core. The contents of
137+
// `core_simd` are in a different repository: rust-lang/portable-simd.
138+
//
139+
// `core_simd` depends on core, but the contents of this module are
140+
// set up in such a way that directly pulling it here works such that the
141+
// crate uses this crate as its core.
142+
#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
143+
#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
144+
#[allow(rustdoc::bare_urls)]
145+
#[unstable(feature = "portable_simd", issue = "86656")]
146+
mod core_simd;
147+
148+
#[unstable(feature = "portable_simd", issue = "86656")]
149+
pub mod simd {
150+
#![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
151+
152+
#[unstable(feature = "portable_simd", issue = "86656")]
153+
pub use crate::core_simd::simd::*;
154+
}
155+
156+
include!("primitive_docs.rs");

0 commit comments

Comments
 (0)