Skip to content

Commit

Permalink
Rollup merge of rust-lang#131707 - clarfonthey:constify-core-tests, r…
Browse files Browse the repository at this point in the history
…=thomcc

Run most `core::num` tests in const context too

This adds some infrastructure for something I was going to use in rust-lang#131566, but it felt worthwhile enough on its own to merge/discuss separately.

Essentially, right now we tend to rely on UI tests to ensure that things work in const context, rather than just using library tests. This uses a few simple macro tricks to make it *relatively* painless to execute tests in both runtime and compile-time context. And this only applies to the numeric tests, and not anything else.

Recommended to review without whitespace in the diff.

cc `@RalfJung`
  • Loading branch information
matthiaskrgr authored Oct 23, 2024
2 parents b0fc9c7 + acaf7e2 commit 05b4955
Show file tree
Hide file tree
Showing 3 changed files with 539 additions and 525 deletions.
35 changes: 35 additions & 0 deletions core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
#![feature(clone_to_uninit)]
#![feature(const_align_of_val_raw)]
#![feature(const_align_offset)]
#![feature(const_bigint_helper_methods)]
#![feature(const_black_box)]
#![feature(const_eval_select)]
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_likely)]
#![feature(const_nonnull_new)]
#![feature(const_num_midpoint)]
#![feature(const_option_ext)]
#![feature(const_pin_2)]
#![feature(const_pointer_is_aligned)]
Expand All @@ -46,6 +49,7 @@
#![feature(get_many_mut)]
#![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)]
#![feature(inline_const_pat)]
#![feature(int_roundings)]
#![feature(ip)]
#![feature(ip_from)]
Expand Down Expand Up @@ -104,6 +108,37 @@
#![deny(fuzzy_provenance_casts)]
#![deny(unsafe_op_in_unsafe_fn)]

/// Version of `assert_matches` that ignores fancy runtime printing in const context and uses structural equality.
macro_rules! assert_eq_const_safe {
($left:expr, $right:expr$(, $($arg:tt)+)?) => {
{
fn runtime() {
assert_eq!($left, $right, $($arg)*);
}
const fn compiletime() {
assert!(matches!($left, const { $right }));
}
core::intrinsics::const_eval_select((), compiletime, runtime)
}
};
}

/// Creates a test for runtime and a test for constant-time.
macro_rules! test_runtime_and_compiletime {
($(
$(#[$attr:meta])*
fn $test:ident() $block:block
)*) => {
$(
$(#[$attr])*
#[test]
fn $test() $block
$(#[$attr])*
const _: () = $block;
)*
}
}

mod alloc;
mod any;
mod array;
Expand Down
Loading

0 comments on commit 05b4955

Please # to comment.