Skip to content

Commit

Permalink
add function call type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed May 7, 2024
1 parent bd93e07 commit fde335e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
19 changes: 19 additions & 0 deletions crates/concrete_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,24 @@ pub fn lowering_error_to_report(
.with_message(msg)
.finish()
}
LoweringError::CallParamCountMismatch {
span,
found,
needs,
program_id,
} => {
let path = file_paths[program_id].to_str().unwrap().to_string();
Report::build(ReportKind::Error, path.clone(), span.from)
.with_code("CallParamCountMismatch")
.with_label(
Label::new((path, span.into()))
.with_message(format!(
"function call parameter count mismatch: found {}, needs {}.",
found, needs
))
.with_color(colors.next()),
)
.finish()
}
}
}
33 changes: 33 additions & 0 deletions crates/concrete_driver/tests/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,36 @@ fn undeclared_var() {
error
);
}

#[test]
fn call_count_mismatch() {
let (source, name) = (
include_str!("invalid_programs/call_count_mismatch.con"),
"call_count_mismatch",
);
let error = check_invalid_program(source, name);

assert!(
matches!(
&error,
LoweringError::CallParamCountMismatch { found, needs, .. } if *found == 2 && *needs == 1
),
"{:#?}",
error
);
}

#[test]
fn invalid_call_params() {
let (source, name) = (
include_str!("invalid_programs/invalid_call_params.con"),
"invalid_call_params",
);
let error = check_invalid_program(source, name);

assert!(
matches!(&error, LoweringError::UnexpectedType { .. }),
"{:#?}",
error
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod Test {
fn main() -> i32 {
return hello(1, 2);
}

fn hello(a: i32) -> i32 {
return a * 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod Test {
fn main() -> i32 {
let x: u32 = 1;
return hello(x);
}

fn hello(a: i32) -> i32 {
return a * 2;
}
}
23 changes: 21 additions & 2 deletions crates/concrete_ir/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,11 +1179,30 @@ fn lower_fn_call(
}
};

if args_ty.len() != info.args.len() {
return Err(LoweringError::CallParamCountMismatch {
span: info.span,
found: info.args.len(),
needs: args_ty.len(),
program_id: builder.get_module_body().id.program_id,
});
}

let mut args = Vec::new();

for (arg, arg_ty) in info.args.iter().zip(args_ty) {
let rvalue = lower_expression(builder, arg, Some(arg_ty.clone()))?;
args.push(rvalue.0);
let (rvalue, rvalue_ty, arg_span) = lower_expression(builder, arg, Some(arg_ty.clone()))?;

if rvalue_ty.kind != arg_ty.kind {
return Err(LoweringError::UnexpectedType {
span: arg_span,
found: rvalue_ty,
expected: arg_ty,
program_id: builder.get_module_body().id.program_id,
});
}

args.push(rvalue);
}

let dest_local = builder.add_local(Local::temp(ret_ty.clone()));
Expand Down
7 changes: 7 additions & 0 deletions crates/concrete_ir/src/lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ pub enum LoweringError {
},
#[error("internal error: {0}")]
InternalError(String, usize),
#[error("function call parameter count mismatch, found {found}, needs {needs}")]
CallParamCountMismatch {
span: Span,
found: usize,
needs: usize,
program_id: usize,
},
}

0 comments on commit fde335e

Please sign in to comment.