From 86decc912b031b98bb822df2fdd5f9f66ba7349c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 8 May 2024 17:24:22 -0300 Subject: [PATCH 1/2] Add type check --- crates/concrete_ir/src/lowering.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 6cf620e..d70842f 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -738,7 +738,16 @@ fn lower_assign(builder: &mut FnBodyBuilder, info: &AssignStmt) -> Result<(), Lo place.projection.push(PlaceElem::Deref); } - let (rvalue, _rvalue_ty, _exp_span) = lower_expression(builder, &info.value, Some(ty.clone()))?; + let (rvalue, rvalue_ty, _exp_span) = lower_expression(builder, &info.value, Some(ty.clone()))?; + + if ty.kind != rvalue_ty.kind { + return Err(LoweringError::UnexpectedType { + span: info.span, + found: rvalue_ty, + expected: ty.clone(), + program_id: builder.local_module.program_id, + }); + } builder.statements.push(Statement { span: Some(info.target.first.span), From da05e4e2e726f14de3cac8a9c0e4c95153f095ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 8 May 2024 17:24:32 -0300 Subject: [PATCH 2/2] Add test for unexpected type in assign --- crates/concrete_driver/tests/checks.rs | 15 +++++++++++++++ .../invalid_programs/assign_unexpected_type.con | 8 ++++++++ 2 files changed, 23 insertions(+) create mode 100644 crates/concrete_driver/tests/invalid_programs/assign_unexpected_type.con diff --git a/crates/concrete_driver/tests/checks.rs b/crates/concrete_driver/tests/checks.rs index 2a5e670..d392759 100644 --- a/crates/concrete_driver/tests/checks.rs +++ b/crates/concrete_driver/tests/checks.rs @@ -18,6 +18,21 @@ fn module_not_found() { ); } +#[test] +fn assign_unexpected_type() { + let (source, name) = ( + include_str!("invalid_programs/assign_unexpected_type.con"), + "assign_unexpected_type", + ); + let error = check_invalid_program(source, name); + + assert!( + matches!(&error, LoweringError::UnexpectedType { .. }), + "{:#?}", + error + ); +} + #[test] fn import_not_found() { let (source, name) = (include_str!("invalid_programs/import2.con"), "import2"); diff --git a/crates/concrete_driver/tests/invalid_programs/assign_unexpected_type.con b/crates/concrete_driver/tests/invalid_programs/assign_unexpected_type.con new file mode 100644 index 0000000..7a5c684 --- /dev/null +++ b/crates/concrete_driver/tests/invalid_programs/assign_unexpected_type.con @@ -0,0 +1,8 @@ +mod Simple { + fn main() -> i32 { + let var: i32 = 5; + var = foo(); + } + + fn foo() {} +}