-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/test/ui/consts/const-eval/auxiliary/post_monomorphization_error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Auxiliary crate used for testing post-monomorphization errors cross-crate. | ||
// It duplicates the setup used in `stdarch` to validate its intrinsics' const arguments. | ||
|
||
struct ValidateConstImm<const IMM: i32, const MIN: i32, const MAX: i32>; | ||
impl<const IMM: i32, const MIN: i32, const MAX: i32> ValidateConstImm<IMM, MIN, MAX> { | ||
pub(crate) const VALID: () = { | ||
let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize); | ||
}; | ||
} | ||
|
||
macro_rules! static_assert_imm1 { | ||
($imm:ident) => { | ||
let _ = $crate::ValidateConstImm::<$imm, 0, { (1 << 1) - 1 }>::VALID; | ||
}; | ||
} | ||
|
||
// This function triggers an error whenever the const argument does not fit in 1-bit. | ||
pub fn stdarch_intrinsic<const IMM1: i32>() { | ||
static_assert_imm1!(IMM1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This is a test with a setup similar to issue 85155, which triggers a const eval error: a const | ||
// argument value is outside the range expected by the `stdarch` intrinsic. | ||
// | ||
// It's not the exact code mentioned in that issue because it depends both on `stdarch` intrinsics | ||
// only available on x64, and internal implementation details of `stdarch`. But mostly because these | ||
// are not important to trigger the diagnostics issue: it's specifically about the lack of context | ||
// in the diagnostics of post-monomorphization errors (PMEs) for consts, happening in a dependency. | ||
// Therefore, its setup is reproduced with an aux crate, which will similarly trigger a PME | ||
// depending on the const argument value, like the `stdarch` intrinsics would. | ||
// | ||
// aux-build: post_monomorphization_error.rs | ||
// build-fail: this is a post-monomorphization error, it passes check runs and requires building | ||
// to actually fail. | ||
|
||
extern crate post_monomorphization_error; | ||
|
||
fn main() { | ||
// This function triggers a PME whenever the const argument does not fit in 1-bit. | ||
post_monomorphization_error::stdarch_intrinsic::<2>(); | ||
//~^ NOTE the above error was encountered while instantiating | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/auxiliary/post_monomorphization_error.rs:7:17 | ||
| | ||
LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero | ||
|
||
note: the above error was encountered while instantiating `fn stdarch_intrinsic::<2_i32>` | ||
--> $DIR/issue-85155.rs:19:5 | ||
| | ||
LL | post_monomorphization_error::stdarch_intrinsic::<2>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |