Skip to content

Commit ed60426

Browse files
committed
check for wrong number of returned vars in function calls
1 parent be12a66 commit ed60426

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub fn simplify_program(mut program: Program) -> SimpleProgram {
160160
for (name, func) in &program.functions {
161161
let mut array_manager = ArrayManager::default();
162162
let simplified_instructions = simplify_lines(
163+
&program.functions,
163164
&func.body,
164165
&mut counters,
165166
&mut new_functions,
@@ -436,6 +437,7 @@ impl ArrayManager {
436437
}
437438

438439
fn simplify_lines(
440+
functions: &BTreeMap<String, Function>,
439441
lines: &[Line],
440442
counters: &mut Counters,
441443
new_functions: &mut BTreeMap<String, SimpleFunction>,
@@ -455,6 +457,7 @@ fn simplify_lines(
455457
for (i, (pattern, statements)) in arms.iter().enumerate() {
456458
assert_eq!(*pattern, i, "match patterns should be consecutive, starting from 0");
457459
simple_arms.push(simplify_lines(
460+
functions,
458461
statements,
459462
counters,
460463
new_functions,
@@ -606,6 +609,7 @@ fn simplify_lines(
606609

607610
let mut array_manager_then = array_manager.clone();
608611
let then_branch_simplified = simplify_lines(
612+
functions,
609613
then_branch,
610614
counters,
611615
new_functions,
@@ -617,6 +621,7 @@ fn simplify_lines(
617621
array_manager_else.valid = array_manager.valid.clone(); // Crucial: remove the access added in the IF branch
618622

619623
let else_branch_simplified = simplify_lines(
624+
functions,
620625
else_branch,
621626
counters,
622627
new_functions,
@@ -666,6 +671,7 @@ fn simplify_lines(
666671
let mut body_copy = body.clone();
667672
replace_vars_for_unroll(&mut body_copy, iterator, unroll_index, i, &internal_variables);
668673
unrolled_lines.extend(simplify_lines(
674+
functions,
669675
&body_copy,
670676
counters,
671677
new_functions,
@@ -689,6 +695,7 @@ fn simplify_lines(
689695
let valid_aux_vars_in_array_manager_before = array_manager.valid.clone();
690696
array_manager.valid.clear();
691697
let simplified_body = simplify_lines(
698+
functions,
692699
body,
693700
counters,
694701
new_functions,
@@ -763,6 +770,11 @@ fn simplify_lines(
763770
return_data,
764771
line_number,
765772
} => {
773+
let function = functions.get(function_name).expect("Function used but not defined: {function_name}");
774+
if return_data.len() != function.n_returned_vars {
775+
panic!("Expected {} returned vars in call to {function_name}", function.n_returned_vars);
776+
}
777+
766778
let simplified_args = args
767779
.iter()
768780
.map(|arg| simplify_expr(arg, &mut res, counters, array_manager, const_malloc))

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ fn test_duplicate_constant_name() {
3838
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
3939
}
4040

41+
#[test]
42+
#[should_panic]
43+
fn test_wrong_n_returned_vars() {
44+
let program = r#"
45+
fn main() {
46+
a, b = f();
47+
}
48+
49+
fn f() -> 1 {
50+
return 0;
51+
}
52+
"#;
53+
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
54+
}
55+
4156
#[test]
4257
fn test_fibonacci_program() {
4358
// a program to check the value of the 30th Fibonacci number (832040)

0 commit comments

Comments
 (0)