Add e2e mir test for checked arithmetic.

This commit is contained in:
Camille GILLOT 2023-04-30 14:53:24 +00:00
parent 2c6f137d9a
commit 434fa8c26d
4 changed files with 285 additions and 0 deletions

View file

@ -0,0 +1,160 @@
// MIR for `checked_shl` after PreCodegen
fn checked_shl(_1: u32, _2: u32) -> Option<u32> {
debug x => _1; // in scope 0 at $DIR/checked_ops.rs:+0:20: +0:21
debug rhs => _2; // in scope 0 at $DIR/checked_ops.rs:+0:28: +0:31
let mut _0: std::option::Option<u32>; // return place in scope 0 at $DIR/checked_ops.rs:+0:41: +0:52
scope 1 (inlined core::num::<impl u32>::checked_shl) { // at $DIR/checked_ops.rs:14:7: 14:23
debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug rhs => _2; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _13: (u32, bool); // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let _14: u32; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let _15: bool; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _16: bool; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL
scope 2 {
debug a => _14; // in scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug b => _15; // in scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
scope 3 (inlined core::num::<impl u32>::overflowing_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug self => _1; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug rhs => _2; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _11: u32; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _12: bool; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
scope 4 (inlined core::num::<impl u32>::wrapping_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug self => _1; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug rhs => _2; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _3: u32; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _4: u32; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
scope 5 {
scope 6 (inlined core::num::<impl u32>::unchecked_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug self => _1; // in scope 6 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug rhs => _4; // in scope 6 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _5: (u32,); // in scope 6 at $SRC_DIR/core/src/num/mod.rs:LL:COL
let mut _6: u32; // in scope 6 at $SRC_DIR/core/src/num/mod.rs:LL:COL
let mut _10: u32; // in scope 6 at $SRC_DIR/core/src/num/mod.rs:LL:COL
scope 7 {
scope 8 (inlined core::num::<impl u32>::unchecked_shl::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL
debug x => _6; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL
let mut _7: std::result::Result<u32, std::convert::Infallible>; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL
let mut _9: std::option::Option<u32>; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL
scope 9 {
scope 10 (inlined <u32 as TryInto<u32>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL
debug self => _6; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
scope 11 (inlined <u32 as TryFrom<u32>>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL
debug value => _6; // in scope 11 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
scope 21 (inlined <u32 as Into<u32>>::into) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL
debug self => _6; // in scope 21 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
scope 22 (inlined <u32 as From<u32>>::from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL
debug t => _6; // in scope 22 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
}
}
}
}
scope 12 (inlined Result::<u32, Infallible>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL
debug self => _7; // in scope 12 at $SRC_DIR/core/src/result.rs:LL:COL
let _8: u32; // in scope 12 at $SRC_DIR/core/src/result.rs:LL:COL
scope 13 {
debug x => _8; // in scope 13 at $SRC_DIR/core/src/result.rs:LL:COL
}
}
scope 14 (inlined #[track_caller] Option::<u32>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL
debug self => _9; // in scope 14 at $SRC_DIR/core/src/option.rs:LL:COL
let mut _17: &std::option::Option<u32>; // in scope 14 at $SRC_DIR/core/src/option.rs:LL:COL
scope 15 {
debug val => _10; // in scope 15 at $SRC_DIR/core/src/option.rs:LL:COL
}
scope 16 {
scope 18 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL
scope 19 {
scope 20 (inlined unreachable_unchecked::runtime) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}
}
}
scope 17 (inlined Option::<u32>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL
debug self => _17; // in scope 17 at $SRC_DIR/core/src/option.rs:LL:COL
}
}
}
}
}
}
}
}
}
}
bb0: {
StorageLive(_14); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23
StorageLive(_15); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23
StorageLive(_13); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_11); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_6); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_4); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_3 = const 31_u32; // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_4 = BitAnd(_2, move _3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_10); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageLive(_5); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_5 = (_4,); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_6 = move (_5.0: u32); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageLive(_9); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageLive(_7); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_7 = Result::<u32, Infallible>::Ok(_6); // scope 11 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
StorageLive(_8); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_8 = move ((_7 as Ok).0: u32); // scope 12 at $SRC_DIR/core/src/result.rs:LL:COL
_9 = Option::<u32>::Some(move _8); // scope 13 at $SRC_DIR/core/src/result.rs:LL:COL
StorageDead(_8); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageDead(_7); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageLive(_17); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_10 = move ((_9 as Some).0: u32); // scope 14 at $SRC_DIR/core/src/option.rs:LL:COL
StorageDead(_17); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageDead(_9); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL
StorageDead(_5); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_11 = unchecked_shl::<u32>(_1, move _10) -> [return: bb1, unwind unreachable]; // scope 7 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// + literal: Const { ty: unsafe extern "rust-intrinsic" fn(u32, u32) -> u32 {unchecked_shl::<u32>}, val: Value(<ZST>) }
}
bb1: {
StorageDead(_10); // scope 7 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_4); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_6); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_12); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_12 = Ge(_2, const _); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_13 = (move _11, move _12); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_12); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_11); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_14 = (_13.0: u32); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_15 = (_13.1: bool); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_13); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageLive(_16); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL
_16 = unlikely(_15) -> [return: bb2, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/num/mod.rs:LL:COL
// + literal: Const { ty: extern "rust-intrinsic" fn(bool) -> bool {unlikely}, val: Value(<ZST>) }
}
bb2: {
switchInt(move _16) -> [0: bb3, otherwise: bb4]; // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL
}
bb3: {
_0 = Option::<u32>::Some(_14); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
goto -> bb5; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
bb4: {
_0 = Option::<u32>::None; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
goto -> bb5; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
bb5: {
StorageDead(_16); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_15); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23
StorageDead(_14); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23
return; // scope 0 at $DIR/checked_ops.rs:+2:2: +2:2
}
}

View file

@ -0,0 +1,41 @@
// MIR for `ilog2` after PreCodegen
fn ilog2(_1: u32) -> u32 {
debug x => _1; // in scope 0 at $DIR/checked_ops.rs:+0:14: +0:15
let mut _0: u32; // return place in scope 0 at $DIR/checked_ops.rs:+0:25: +0:28
scope 1 (inlined #[track_caller] core::num::<impl u32>::ilog2) { // at $DIR/checked_ops.rs:19:7: 19:14
debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _2: std::option::Option<u32>; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _3: isize; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
let mut _4: !; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
scope 2 {
debug log => _0; // in scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
}
bb0: {
StorageLive(_2); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
_2 = core::num::<impl u32>::checked_ilog2(_1) -> bb1; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// + literal: Const { ty: fn(u32) -> Option<u32> {core::num::<impl u32>::checked_ilog2}, val: Value(<ZST>) }
}
bb1: {
_3 = discriminant(_2); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
switchInt(move _3) -> [1: bb2, otherwise: bb3]; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
bb2: {
_0 = ((_2 as Some).0: u32); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_2); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
return; // scope 0 at $DIR/checked_ops.rs:+2:2: +2:2
}
bb3: {
_4 = core::num::int_log10::panic_for_nonpositive_argument(); // scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
// + literal: Const { ty: fn() -> ! {core::num::int_log10::panic_for_nonpositive_argument}, val: Value(<ZST>) }
}
}

View file

@ -0,0 +1,20 @@
// compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=2
// ignore-debug
#![crate_type = "lib"]
#![feature(step_trait)]
// EMIT_MIR checked_ops.step_forward.PreCodegen.after.mir
pub fn step_forward(x: u32, n: usize) -> u32 {
std::iter::Step::forward(x, n)
}
// EMIT_MIR checked_ops.checked_shl.PreCodegen.after.mir
pub fn checked_shl(x: u32, rhs: u32) -> Option<u32> {
x.checked_shl(rhs)
}
// EMIT_MIR checked_ops.ilog2.PreCodegen.after.mir
pub fn ilog2(x: u32) -> u32 {
x.ilog2()
}

View file

@ -0,0 +1,64 @@
// MIR for `step_forward` after PreCodegen
fn step_forward(_1: u32, _2: usize) -> u32 {
debug x => _1; // in scope 0 at $DIR/checked_ops.rs:+0:21: +0:22
debug n => _2; // in scope 0 at $DIR/checked_ops.rs:+0:29: +0:30
let mut _0: u32; // return place in scope 0 at $DIR/checked_ops.rs:+0:42: +0:45
scope 1 (inlined <u32 as Step>::forward) { // at $DIR/checked_ops.rs:9:5: 9:35
debug start => _1; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
debug n => _2; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
let _3: std::option::Option<u32>; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
let mut _4: &std::option::Option<u32>; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
let mut _7: bool; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
let mut _8: u32; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
scope 2 {
}
scope 3 (inlined Option::<u32>::is_none) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL
debug self => _4; // in scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
let mut _6: bool; // in scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
scope 4 (inlined Option::<u32>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL
debug self => _4; // in scope 4 at $SRC_DIR/core/src/option.rs:LL:COL
let mut _5: isize; // in scope 4 at $SRC_DIR/core/src/option.rs:LL:COL
}
}
scope 5 (inlined core::num::<impl u32>::wrapping_add) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL
debug self => _1; // in scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
debug rhs => _8; // in scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
}
}
bb0: {
StorageLive(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
StorageLive(_4); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
StorageLive(_3); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
_3 = <u32 as Step>::forward_checked(_1, _2) -> bb1; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/iter/range.rs:LL:COL
// + literal: Const { ty: fn(u32, usize) -> Option<u32> {<u32 as Step>::forward_checked}, val: Value(<ZST>) }
}
bb1: {
_4 = &_3; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
StorageLive(_6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
_5 = discriminant((*_4)); // scope 4 at $SRC_DIR/core/src/option.rs:LL:COL
_6 = Eq(_5, const 1_isize); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
_7 = Not(move _6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
StorageDead(_6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
StorageDead(_3); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
StorageDead(_4); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
switchInt(move _7) -> [0: bb3, otherwise: bb2]; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
}
bb2: {
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const _, const 1_u32) -> bb3; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
}
bb3: {
StorageDead(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
StorageLive(_8); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
_8 = _2 as u32 (IntToInt); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
_0 = Add(_1, _8); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
StorageDead(_8); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL
return; // scope 0 at $DIR/checked_ops.rs:+2:2: +2:2
}
}