rust/tests/mir-opt/building/custom/terminators.rs
Tomasz Miąsko 78da577650 Custom MIR: Support cleanup blocks
Cleanup blocks are declared with `bb (cleanup) = { ... }`.

`Call` and `Drop` terminators take an additional argument describing the
unwind action, which is one of the following:

* `UnwindContinue()`
* `UnwindUnreachable()`
* `UnwindTerminate(reason)`, where reason is `ReasonAbi` or `ReasonInCleanup`
* `UnwindCleanup(block)`

Also support unwind resume and unwind terminate terminators:

* `UnwindResume()`
* `UnwindTerminate(reason)`
2023-11-14 08:23:58 +01:00

111 lines
2.1 KiB
Rust

// skip-filecheck
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;
fn ident<T>(t: T) -> T {
t
}
// EMIT_MIR terminators.direct_call.built.after.mir
#[custom_mir(dialect = "built")]
fn direct_call(x: i32) -> i32 {
mir!(
{
Call(RET = ident(x), retblock, UnwindContinue())
}
retblock = {
Return()
}
)
}
// EMIT_MIR terminators.indirect_call.built.after.mir
#[custom_mir(dialect = "built")]
fn indirect_call(x: i32, f: fn(i32) -> i32) -> i32 {
mir!(
{
Call(RET = f(x), retblock, UnwindContinue())
}
retblock = {
Return()
}
)
}
struct WriteOnDrop<'a>(&'a mut i32, i32);
impl<'a> Drop for WriteOnDrop<'a> {
fn drop(&mut self) {
*self.0 = self.1;
}
}
// EMIT_MIR terminators.drop_first.built.after.mir
#[custom_mir(dialect = "built")]
fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
mir!(
{
Drop(a, retblock, UnwindContinue())
}
retblock = {
a = Move(b);
Return()
}
)
}
// EMIT_MIR terminators.drop_second.built.after.mir
#[custom_mir(dialect = "built")]
fn drop_second<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
mir!(
{
Drop(b, retblock, UnwindContinue())
}
retblock = {
Return()
}
)
}
// EMIT_MIR terminators.assert_nonzero.built.after.mir
#[custom_mir(dialect = "built")]
fn assert_nonzero(a: i32) {
mir!(
{
match a {
0 => unreachable,
_ => retblock
}
}
unreachable = {
Unreachable()
}
retblock = {
Return()
}
)
}
fn main() {
assert_eq!(direct_call(5), 5);
assert_eq!(indirect_call(5, ident), 5);
let mut a = 0;
let mut b = 0;
drop_first(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1));
assert_eq!((a, b), (1, 0));
let mut a = 0;
let mut b = 0;
drop_second(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1));
assert_eq!((a, b), (0, 1));
}