Skip to content

Commit 8002918

Browse files
committed
test: simplification
1 parent 6897865 commit 8002918

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

libs/@local/hashql/mir/src/body/operand.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ impl<'heap> Operand<'heap> {
5959
}
6060
}
6161

62+
impl From<!> for Operand<'_> {
63+
fn from(value: !) -> Self {
64+
value
65+
}
66+
}
67+
6268
impl From<Local> for Operand<'_> {
6369
fn from(local: Local) -> Self {
6470
Operand::Place(Place::local(local))

libs/@local/hashql/mir/src/builder/body.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ macro_rules! body {
311311
(@type $types:ident; Int) => {
312312
$types.integer()
313313
};
314-
(@type $types:ident; ($($sub:tt),*)) => {
314+
(@type $types:ident; ()) => {
315+
$types.tuple([] as [hashql_core::r#type::TypeId; 0])
316+
};
317+
(@type $types:ident; ($($sub:tt),+)) => {
315318
$types.tuple([$($crate::builder::body!(@type $types; $sub)),*])
316319
};
317320
(@type $types:ident; ($($name:ident: $sub:tt),*)) => {

libs/@local/hashql/mir/src/builder/rvalue.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ macro_rules! rvalue {
224224
rv.closure($def, $env)
225225
}; $payload; $($rest)*)
226226
};
227+
($resume:path; $payload:tt; tuple; $($rest:tt)*) => {
228+
$resume!(@rvalue |rv| {
229+
rv.tuple([] as [!; 0])
230+
}; $payload; $($rest)*)
231+
};
227232
($resume:path; $payload:tt; tuple $($members:tt),+; $($rest:tt)*) => {
228233
$resume!(@rvalue |rv| {
229234
let members = [$($crate::builder::_private::operand!(rv; $members)),*];

libs/@local/hashql/mir/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
// Library Features
1515
allocator_api,
1616
assert_matches,
17+
binary_heap_drain_sorted,
1718
const_type_name,
1819
iter_array_chunks,
1920
iter_collect_into,
2021
iter_intersperse,
22+
maybe_uninit_fill,
23+
step_trait,
2124
string_from_utf8_lossy_owned,
2225
try_trait_v2,
23-
step_trait,
24-
maybe_uninit_fill,
25-
binary_heap_into_iter_sorted,
26-
binary_heap_drain_sorted,
2726
)]
2827
#![expect(clippy::indexing_slicing)]
2928
extern crate alloc;

libs/@local/hashql/mir/src/pass/transform/inst_simplify/tests.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![expect(clippy::min_ident_chars, reason = "tests")]
2-
use std::{io::Write as _, path::PathBuf};
2+
use std::{assert_matches::assert_matches, io::Write as _, path::PathBuf};
33

44
use bstr::ByteVec as _;
55
use hashql_core::{
@@ -12,8 +12,20 @@ use insta::{Settings, assert_snapshot};
1212

1313
use super::InstSimplify;
1414
use crate::{
15-
body::Body, builder::body, context::MirContext, def::DefIdSlice, intern::Interner,
16-
pass::TransformPass as _, pretty::TextFormat,
15+
body::{
16+
Body,
17+
basic_block::BasicBlockId,
18+
constant::Constant,
19+
operand::Operand,
20+
rvalue::RValue,
21+
statement::{Assign, StatementKind},
22+
},
23+
builder::body,
24+
context::MirContext,
25+
def::DefIdSlice,
26+
intern::Interner,
27+
pass::{Changed, TransformPass as _},
28+
pretty::TextFormat,
1729
};
1830

1931
#[track_caller]
@@ -394,3 +406,38 @@ fn idempotent_to_const_forwarding() {
394406
},
395407
);
396408
}
409+
410+
/// Tests that an empty tuple aggregate is simplified to a unit constant.
411+
#[test]
412+
fn empty_tuple_to_unit() {
413+
let heap = Heap::new();
414+
let interner = Interner::new(&heap);
415+
let env = Environment::new(&heap);
416+
417+
let mut body = body!(interner, env; fn@0/0 -> () {
418+
decl result: ();
419+
420+
bb0() {
421+
result = tuple;
422+
return result;
423+
}
424+
});
425+
426+
let changed = InstSimplify::new().run(
427+
&mut MirContext {
428+
heap: &heap,
429+
env: &env,
430+
interner: &interner,
431+
diagnostics: DiagnosticIssues::new(),
432+
},
433+
&mut body,
434+
);
435+
assert_eq!(changed, Changed::Yes);
436+
assert_matches!(
437+
body.basic_blocks[BasicBlockId::START].statements[0].kind,
438+
StatementKind::Assign(Assign {
439+
lhs: _,
440+
rhs: RValue::Load(Operand::Constant(Constant::Unit))
441+
})
442+
);
443+
}

0 commit comments

Comments
 (0)