Skip to content

Commit b23a803

Browse files
committed
fix direct return
1 parent f83c6b2 commit b23a803

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,11 @@ fn make_non_exhaustive_exhaustive(lines: &mut Vec<Line>) {
11141114
else_branch,
11151115
} = &lines[i]
11161116
{
1117-
else_branch.is_empty() && has_return(then_branch)
1117+
// Only restructure if:
1118+
// 1. The else branch is empty AND
1119+
// 2. The then branch has a direct return (not nested) AND
1120+
// 3. There are subsequent statements
1121+
else_branch.is_empty() && has_direct_return(then_branch) && i + 1 < lines.len()
11181122
} else {
11191123
false
11201124
};
@@ -1128,7 +1132,7 @@ fn make_non_exhaustive_exhaustive(lines: &mut Vec<Line>) {
11281132
else_branch,
11291133
} = &mut lines[i]
11301134
{
1131-
// Recursively process the then branch
1135+
// Recursively process the then branch first
11321136
make_non_exhaustive_exhaustive(then_branch);
11331137

11341138
// Move subsequent statements to else branch
@@ -1154,6 +1158,49 @@ fn make_non_exhaustive_exhaustive(lines: &mut Vec<Line>) {
11541158
}
11551159
}
11561160

1161+
// Only look for direct returns, not nested ones
1162+
fn has_direct_return(lines: &[Line]) -> bool {
1163+
for line in lines {
1164+
match line {
1165+
Line::FunctionRet { .. } => return true,
1166+
_ => {} // Don't recurse into nested structures
1167+
}
1168+
}
1169+
false
1170+
}
1171+
1172+
// More comprehensive return detection that looks for returns anywhere in nested structures
1173+
fn has_return_anywhere(lines: &[Line]) -> bool {
1174+
for line in lines {
1175+
match line {
1176+
Line::FunctionRet { .. } => return true,
1177+
Line::IfCondition {
1178+
then_branch,
1179+
else_branch,
1180+
..
1181+
} => {
1182+
if has_return_anywhere(then_branch) || has_return_anywhere(else_branch) {
1183+
return true;
1184+
}
1185+
}
1186+
Line::Match { arms, .. } => {
1187+
for (_, arm_lines) in arms {
1188+
if has_return_anywhere(arm_lines) {
1189+
return true;
1190+
}
1191+
}
1192+
}
1193+
Line::ForLoop { body, .. } => {
1194+
if has_return_anywhere(body) {
1195+
return true;
1196+
}
1197+
}
1198+
_ => {}
1199+
}
1200+
}
1201+
false
1202+
}
1203+
11571204
fn has_return(lines: &[Line]) -> bool {
11581205
for line in lines {
11591206
match line {

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ fn test_inline_non_exhaustive_conditions() {
578578
fn main() {
579579
result1 = works(0);
580580
result2 = works(1);
581-
result3 = doesnt_work(0);
582-
result4 = doesnt_work(1);
581+
result3 = should_work(0);
582+
result4 = should_work(1);
583583
print(result1);
584584
print(result2);
585585
print(result3);
@@ -595,7 +595,7 @@ fn test_inline_non_exhaustive_conditions() {
595595
}
596596
}
597597
598-
fn doesnt_work(x) inline -> 1 {
598+
fn should_work(x) inline -> 1 {
599599
if x == 0 {
600600
return 0;
601601
}
@@ -605,3 +605,25 @@ fn test_inline_non_exhaustive_conditions() {
605605

606606
compile_and_run(program, &[], &[], false);
607607
}
608+
609+
#[test]
610+
fn test_inline_of_todo() {
611+
let program = r#"
612+
fn main() {
613+
b = should_work(1);
614+
print(b);
615+
return;
616+
}
617+
618+
fn should_work(x) inline -> 1 {
619+
if x == 1 {
620+
if x == 0 {
621+
return 100;
622+
}
623+
}
624+
return 200;
625+
}
626+
"#;
627+
628+
compile_and_run(program, &[], &[], false);
629+
}

0 commit comments

Comments
 (0)