Skip to content

Commit 6fc9bea

Browse files
Implement SameOperandsAndResultType test.
Signed-off-by: Tomas Fabrizio Orsi <[email protected]> Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
1 parent 2903495 commit 6fc9bea

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

hir/src/derive.rs

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

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

+9-3
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
}

hir/src/ir/traits/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ derive! {
8686
(value.ty().clone(), value.span())
8787
};
8888

89-
let mut results = op.results().iter();
89+
let results = op.results().iter();
9090

9191
for operand in operands {
9292
let operand = operand.borrow();

0 commit comments

Comments
 (0)