Skip to content

Commit 6f61456

Browse files
committed
add test for issue 85155 and similar
This test reproduces post-monomorphization errors one can encounter when using incorrect immediate arguments to some of the stdarch intrinsics using const generics.
1 parent d14dd9f commit 6f61456

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Auxiliary crate used for testing post-monomorphization errors cross-crate.
2+
// It duplicates the setup used in `stdarch` to validate its intrinsics' const arguments.
3+
4+
struct ValidateConstImm<const IMM: i32, const MIN: i32, const MAX: i32>;
5+
impl<const IMM: i32, const MIN: i32, const MAX: i32> ValidateConstImm<IMM, MIN, MAX> {
6+
pub(crate) const VALID: () = {
7+
let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
8+
};
9+
}
10+
11+
macro_rules! static_assert_imm1 {
12+
($imm:ident) => {
13+
let _ = $crate::ValidateConstImm::<$imm, 0, { (1 << 1) - 1 }>::VALID;
14+
};
15+
}
16+
17+
// This function triggers an error whenever the const argument does not fit in 1-bit.
18+
pub fn stdarch_intrinsic<const IMM1: i32>() {
19+
static_assert_imm1!(IMM1);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This is a test with a setup similar to issue 85155, which triggers a const eval error: a const
2+
// argument value is outside the range expected by the `stdarch` intrinsic.
3+
//
4+
// It's not the exact code mentioned in that issue because it depends both on `stdarch` intrinsics
5+
// only available on x64, and internal implementation details of `stdarch`. But mostly because these
6+
// are not important to trigger the diagnostics issue: it's specifically about the lack of context
7+
// in the diagnostics of post-monomorphization errors (PMEs) for consts, happening in a dependency.
8+
// Therefore, its setup is reproduced with an aux crate, which will similarly trigger a PME
9+
// depending on the const argument value, like the `stdarch` intrinsics would.
10+
//
11+
// aux-build: post_monomorphization_error.rs
12+
// build-fail: this is a post-monomorphization error, it passes check runs and requires building
13+
// to actually fail.
14+
15+
extern crate post_monomorphization_error;
16+
17+
fn main() {
18+
// This function triggers a PME whenever the const argument does not fit in 1-bit.
19+
post_monomorphization_error::stdarch_intrinsic::<2>();
20+
//~^ NOTE the above error was encountered while instantiating
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/auxiliary/post_monomorphization_error.rs:7:17
3+
|
4+
LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero
6+
7+
note: the above error was encountered while instantiating `fn stdarch_intrinsic::<2_i32>`
8+
--> $DIR/issue-85155.rs:19:5
9+
|
10+
LL | post_monomorphization_error::stdarch_intrinsic::<2>();
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)