Skip to content

Commit 4cb5ca9

Browse files
committed
explore a different approach for cfg handling in core/lib.rs
1 parent b9aa4c3 commit 4cb5ca9

File tree

2 files changed

+165
-161
lines changed

2 files changed

+165
-161
lines changed

library/core/src/lib.rs

+165-5
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,7 @@ extern crate core as realcore;
299299
pub use realcore::*;
300300

301301
// 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.
302+
// `#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]`.
306303

307304
// allow using `core::` in intra-doc links
308305
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
@@ -343,5 +340,168 @@ pub mod prelude;
343340
#[allow(rustdoc::bare_urls)]
344341
mod core_arch;
345342

343+
// To avoid repeating the `cfg` over and over, we use an identity macro and `cfg` the macro
344+
// invocation. We can't use this for *everything* as having macro-related imports inside a macro
345+
// does not work.
346346
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
347-
include!("lib_.rs");
347+
macro_rules! identity { ($($x:tt)*) => { $($x)* } }
348+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
349+
identity! {
350+
// We don't export this through #[macro_export] for now, to avoid breakage.
351+
// See https://github.com/rust-lang/rust/issues/82913
352+
#[cfg(not(test))]
353+
#[unstable(feature = "assert_matches", issue = "82775")]
354+
/// Unstable module containing the unstable `assert_matches` macro.
355+
pub mod assert_matches {
356+
#[unstable(feature = "assert_matches", issue = "82775")]
357+
pub use crate::macros::{assert_matches, debug_assert_matches};
358+
}
359+
360+
#[unstable(feature = "cfg_match", issue = "115585")]
361+
#[cfg(not(all(feature = "miri-test", not(any(test, doctest)))))]
362+
pub use crate::macros::cfg_match;
363+
364+
#[macro_use]
365+
mod internal_macros;
366+
367+
#[path = "num/shells/int_macros.rs"]
368+
#[macro_use]
369+
mod int_macros;
370+
371+
#[rustc_diagnostic_item = "i128_legacy_mod"]
372+
#[path = "num/shells/i128.rs"]
373+
pub mod i128;
374+
#[rustc_diagnostic_item = "i16_legacy_mod"]
375+
#[path = "num/shells/i16.rs"]
376+
pub mod i16;
377+
#[rustc_diagnostic_item = "i32_legacy_mod"]
378+
#[path = "num/shells/i32.rs"]
379+
pub mod i32;
380+
#[rustc_diagnostic_item = "i64_legacy_mod"]
381+
#[path = "num/shells/i64.rs"]
382+
pub mod i64;
383+
#[rustc_diagnostic_item = "i8_legacy_mod"]
384+
#[path = "num/shells/i8.rs"]
385+
pub mod i8;
386+
#[rustc_diagnostic_item = "isize_legacy_mod"]
387+
#[path = "num/shells/isize.rs"]
388+
pub mod isize;
389+
390+
#[rustc_diagnostic_item = "u128_legacy_mod"]
391+
#[path = "num/shells/u128.rs"]
392+
pub mod u128;
393+
#[rustc_diagnostic_item = "u16_legacy_mod"]
394+
#[path = "num/shells/u16.rs"]
395+
pub mod u16;
396+
#[rustc_diagnostic_item = "u32_legacy_mod"]
397+
#[path = "num/shells/u32.rs"]
398+
pub mod u32;
399+
#[rustc_diagnostic_item = "u64_legacy_mod"]
400+
#[path = "num/shells/u64.rs"]
401+
pub mod u64;
402+
#[rustc_diagnostic_item = "u8_legacy_mod"]
403+
#[path = "num/shells/u8.rs"]
404+
pub mod u8;
405+
#[rustc_diagnostic_item = "usize_legacy_mod"]
406+
#[path = "num/shells/usize.rs"]
407+
pub mod usize;
408+
409+
#[path = "num/f32.rs"]
410+
pub mod f32;
411+
#[path = "num/f64.rs"]
412+
pub mod f64;
413+
414+
#[macro_use]
415+
pub mod num;
416+
417+
/* Core modules for ownership management */
418+
419+
pub mod hint;
420+
pub mod intrinsics;
421+
pub mod mem;
422+
pub mod ptr;
423+
mod ub_checks;
424+
425+
/* Core language traits */
426+
427+
pub mod borrow;
428+
pub mod clone;
429+
pub mod cmp;
430+
pub mod convert;
431+
pub mod default;
432+
pub mod error;
433+
pub mod marker;
434+
pub mod ops;
435+
436+
/* Core types and methods on primitives */
437+
438+
pub mod any;
439+
pub mod array;
440+
pub mod ascii;
441+
pub mod asserting;
442+
#[unstable(feature = "async_iterator", issue = "79024")]
443+
pub mod async_iter;
444+
pub mod cell;
445+
pub mod char;
446+
pub mod ffi;
447+
#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
448+
pub mod io;
449+
pub mod iter;
450+
pub mod net;
451+
pub mod option;
452+
pub mod panic;
453+
pub mod panicking;
454+
pub mod pin;
455+
pub mod result;
456+
pub mod sync;
457+
458+
pub mod fmt;
459+
pub mod hash;
460+
pub mod slice;
461+
pub mod str;
462+
pub mod time;
463+
464+
pub mod unicode;
465+
466+
/* Async */
467+
pub mod future;
468+
pub mod task;
469+
470+
/* Heap memory allocator trait */
471+
#[allow(missing_docs)]
472+
pub mod alloc;
473+
474+
// note: does not need to be public
475+
mod bool;
476+
mod escape;
477+
mod tuple;
478+
mod unit;
479+
480+
#[stable(feature = "core_primitive", since = "1.43.0")]
481+
pub mod primitive;
482+
483+
#[stable(feature = "simd_arch", since = "1.27.0")]
484+
pub mod arch;
485+
486+
// Pull in the `core_simd` crate directly into core. The contents of
487+
// `core_simd` are in a different repository: rust-lang/portable-simd.
488+
//
489+
// `core_simd` depends on core, but the contents of this module are
490+
// set up in such a way that directly pulling it here works such that the
491+
// crate uses this crate as its core.
492+
#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
493+
#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
494+
#[allow(rustdoc::bare_urls)]
495+
#[unstable(feature = "portable_simd", issue = "86656")]
496+
mod core_simd;
497+
498+
#[unstable(feature = "portable_simd", issue = "86656")]
499+
pub mod simd {
500+
#![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
501+
502+
#[unstable(feature = "portable_simd", issue = "86656")]
503+
pub use crate::core_simd::simd::*;
504+
}
505+
506+
include!("primitive_docs.rs");
507+
}

library/core/src/lib_.rs

-156
This file was deleted.

0 commit comments

Comments
 (0)