Skip to content

Commit 7ea3f8b

Browse files
committed
check for wrong number of returned vars in return statements
1 parent d6d8db2 commit 7ea3f8b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub fn simplify_program(mut program: Program) -> SimpleProgram {
161161
let mut array_manager = ArrayManager::default();
162162
let simplified_instructions = simplify_lines(
163163
&program.functions,
164+
func.n_returned_vars,
164165
&func.body,
165166
&mut counters,
166167
&mut new_functions,
@@ -436,8 +437,10 @@ impl ArrayManager {
436437
}
437438
}
438439

440+
#[allow(clippy::too_many_arguments)]
439441
fn simplify_lines(
440442
functions: &BTreeMap<String, Function>,
443+
n_returned_vars: usize,
441444
lines: &[Line],
442445
counters: &mut Counters,
443446
new_functions: &mut BTreeMap<String, SimpleFunction>,
@@ -458,6 +461,7 @@ fn simplify_lines(
458461
assert_eq!(*pattern, i, "match patterns should be consecutive, starting from 0");
459462
simple_arms.push(simplify_lines(
460463
functions,
464+
0,
461465
statements,
462466
counters,
463467
new_functions,
@@ -610,6 +614,7 @@ fn simplify_lines(
610614
let mut array_manager_then = array_manager.clone();
611615
let then_branch_simplified = simplify_lines(
612616
functions,
617+
0,
613618
then_branch,
614619
counters,
615620
new_functions,
@@ -622,6 +627,7 @@ fn simplify_lines(
622627

623628
let else_branch_simplified = simplify_lines(
624629
functions,
630+
0,
625631
else_branch,
626632
counters,
627633
new_functions,
@@ -672,6 +678,7 @@ fn simplify_lines(
672678
replace_vars_for_unroll(&mut body_copy, iterator, unroll_index, i, &internal_variables);
673679
unrolled_lines.extend(simplify_lines(
674680
functions,
681+
0,
675682
&body_copy,
676683
counters,
677684
new_functions,
@@ -696,6 +703,7 @@ fn simplify_lines(
696703
array_manager.valid.clear();
697704
let simplified_body = simplify_lines(
698705
functions,
706+
0,
699707
body,
700708
counters,
701709
new_functions,
@@ -793,6 +801,10 @@ fn simplify_lines(
793801
}
794802
Line::FunctionRet { return_data } => {
795803
assert!(!in_a_loop, "Function return inside a loop is not currently supported");
804+
assert!(
805+
return_data.len() == n_returned_vars,
806+
"Wrong number of return values in return statement"
807+
);
796808
let simplified_return_data = return_data
797809
.iter()
798810
.map(|ret| simplify_expr(ret, &mut res, counters, array_manager, const_malloc))

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn test_duplicate_constant_name() {
4040

4141
#[test]
4242
#[should_panic]
43-
fn test_wrong_n_returned_vars() {
43+
fn test_wrong_n_returned_vars_1() {
4444
let program = r#"
4545
fn main() {
4646
a, b = f();
@@ -53,6 +53,21 @@ fn test_wrong_n_returned_vars() {
5353
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
5454
}
5555

56+
#[test]
57+
#[should_panic]
58+
fn test_wrong_n_returned_vars_2() {
59+
let program = r#"
60+
fn main() {
61+
a = f();
62+
}
63+
64+
fn f() -> 1 {
65+
return 0, 1;
66+
}
67+
"#;
68+
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
69+
}
70+
5671
#[test]
5772
fn test_fibonacci_program() {
5873
// a program to check the value of the 30th Fibonacci number (832040)

0 commit comments

Comments
 (0)