Skip to content

Commit

Permalink
fix cf bug
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Apr 13, 2024
1 parent 050377d commit c568cd4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 60 deletions.
75 changes: 41 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/edlang_driver/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mod common;
#[test_case(TEST_IF_BOTH, "test_if_both", false, 2, &["a"] ; "test_if_both_args")]
#[test_case(TEST_IF_NO_ELSE, "test_if_no_else", false, 1, &[] ; "test_if_no_else")]
#[test_case(TEST_IF_NO_ELSE, "test_if_no_else", false, 2, &["a"] ; "test_if_no_else_args")]
#[test_case(include_str!("programs/while_if_false.ed"), "while_if_false", false, 7, &[] ; "while_if_false")]
#[test_case(include_str!("programs/if_if_false.ed"), "if_if_false", false, 7, &[] ; "if_if_false")]
fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, args: &[&str]) {
let program = compile_program(source, name, is_library).unwrap();

Expand Down
14 changes: 14 additions & 0 deletions lib/edlang_driver/tests/programs/if_if_false.ed
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

fn main() -> i32 {
let foo: i32 = 7;

if true {
if false {
return 1;
}
} else {
foo = 14;
}

return foo;
}
12 changes: 12 additions & 0 deletions lib/edlang_driver/tests/programs/while_if_false.ed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() -> i32 {
let mut result: i32 = 7;

while false {
if false {
return 0;
}
result = 14;
}

return result;
}
35 changes: 9 additions & 26 deletions lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,12 @@ fn lower_function(
lower_statement(&mut builder, stmt, &ret_ty)?;
}

if !builder.statements.is_empty() {
let statements = std::mem::take(&mut builder.statements);
builder.body.blocks.push(BasicBlock {
statements: statements.into(),
terminator: Terminator::Return,
terminator_span: None,
});
}
let statements = std::mem::take(&mut builder.statements);
builder.body.blocks.push(BasicBlock {
statements: statements.into(),
terminator: Terminator::Return,
terminator_span: None,
});
}
let (mut ctx, body) = (builder.ctx, builder.body);
ctx.unresolved_function_signatures.remove(&body.def_id);
Expand Down Expand Up @@ -366,10 +364,7 @@ fn lower_while(
}

// keet idx to change terminator if there is no return
let last_then_block_idx = if !matches!(
builder.body.blocks.last().unwrap().terminator,
Terminator::Return
) {
let last_then_block_idx = {
builder.body.blocks.len();
let statements = std::mem::take(&mut builder.statements);
let idx = builder.body.blocks.len();
Expand All @@ -379,8 +374,6 @@ fn lower_while(
terminator_span: Some(Span::new(info.block.span.hi, info.block.span.hi)),
});
Some(idx)
} else {
None
};

let otherwise_block_idx = builder.body.blocks.len();
Expand Down Expand Up @@ -447,10 +440,7 @@ fn lower_if_stmt(
}

// keet idx to change terminator
let last_then_block_idx = if !matches!(
builder.body.blocks.last().unwrap().terminator,
Terminator::Return
) {
let last_then_block_idx = {
let idx = builder.body.blocks.len();
let statements = std::mem::take(&mut builder.statements);
builder.body.blocks.push(BasicBlock {
Expand All @@ -459,8 +449,6 @@ fn lower_if_stmt(
terminator_span: Some(Span::new(info.then_block.span.hi, info.then_block.span.hi)),
});
Some(idx)
} else {
None
};

let first_else_block_idx = builder.body.blocks.len();
Expand All @@ -471,10 +459,7 @@ fn lower_if_stmt(
}
}

let last_else_block_idx = if !matches!(
builder.body.blocks.last().unwrap().terminator,
Terminator::Return
) {
let last_else_block_idx = {
let idx = builder.body.blocks.len();
let statements = std::mem::take(&mut builder.statements);
builder.body.blocks.push(BasicBlock {
Expand All @@ -486,8 +471,6 @@ fn lower_if_stmt(
.map(|x| Span::new(x.span.hi, x.span.hi)),
});
Some(idx)
} else {
None
};

let targets = SwitchTarget {
Expand Down
Binary file added program
Binary file not shown.

0 comments on commit c568cd4

Please sign in to comment.