Skip to content

Commit ac061f1

Browse files
Implement SameOperandsAndResultType test.
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
1 parent 45d8b2e commit ac061f1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

hir/src/derive.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,28 @@ mod tests {
212212
let op = op_builder(lhs, invalid_rhs, Overflow::Wrapping);
213213
let _op = op.unwrap();
214214
}
215+
216+
/// Fails if [`InvalidOpsWithReturn`] is created successfully. [`InvalidOpsWithReturn`] is a
217+
/// struct that has differing types in its result and arguments, despite implementing the
218+
/// [`SameOperandsAndResultType`] trait.
219+
#[test]
220+
#[should_panic = "expected 'u32', got 'i32'"]
221+
fn same_operands_and_result_type_verifier_test() {
222+
use crate::{SourceSpan, Type};
223+
224+
let context = Rc::new(Context::default());
225+
let block = context.create_block_with_params([Type::I32, Type::I32]);
226+
let (lhs, rhs) = {
227+
let block = block.borrow();
228+
let lhs = block.get_argument(0).upcast::<dyn crate::Value>();
229+
let rhs = block.get_argument(1).upcast::<dyn crate::Value>();
230+
(lhs, rhs)
231+
};
232+
let mut builder = context.builder();
233+
builder.set_insertion_point_to_end(block);
234+
// Try to create instance of AddOp with mismatched operand types
235+
let op_builder = builder.create::<InvalidOpsWithReturn, _>(SourceSpan::default());
236+
let op = op_builder(lhs, rhs);
237+
let _op = op.unwrap();
238+
}
215239
}

hir/src/dialects/test/ops/binary.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ impl InferTypeOpInterface for Shl {
7575
}
7676
}
7777

78-
/// Invalid operation that breaks the SameOperandsAndResultType trait
78+
/// Invalid operation that breaks the SameOperandsAndResultType trait (used for testing).
7979
#[operation(
8080
dialect = TestDialect,
8181
traits(BinaryOp, SameTypeOperands, SameOperandsAndResultType),
82+
implements(InferTypeOpInterface)
8283
)]
8384
pub struct InvalidOpsWithReturn {
8485
#[operand]
@@ -87,6 +88,11 @@ pub struct InvalidOpsWithReturn {
8788
rhs: AnyInteger,
8889
#[result]
8990
result: AnyUnsignedInteger,
90-
#[attr]
91-
overflow: Overflow,
91+
}
92+
93+
impl InferTypeOpInterface for InvalidOpsWithReturn {
94+
fn infer_return_types(&mut self, context: &Context) -> Result<(), Report> {
95+
self.result_mut().set_type(Type::U64);
96+
Ok(())
97+
}
9298
}

0 commit comments

Comments
 (0)