Skip to content

Commit

Permalink
improve implicit returns from functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 15, 2024
1 parent 425322c commit 780e32c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ fn main() {
return n;
}
return fib(n - 1) + fib(n - 2);
fib(n - 1) + fib(n - 2)
}
fn main() -> int {
return fib(19);
fib(19)
}"#;

let result = compile_and_run(source, true);
Expand Down
7 changes: 6 additions & 1 deletion src/stage/lower_ir/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ fn lower_function(ctx: &mut impl IRCtx, function: ast::Function) {
});

// Perform the lowering
lower_block(ctx, &mut builder, &function.body);
let value = lower_block(ctx, &mut builder, &function.body);

// If implicit return, add in a return statement
if !matches!(value, Value::Unit) {
builder.set_terminator(Terminator::Return(value));
}

// Consume the builder
builder.build(ctx);
Expand Down

0 comments on commit 780e32c

Please sign in to comment.