Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: never return type in let initializer #264

Merged
merged 1 commit into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions crates/mun_codegen/src/ir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,10 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
Statement::Let {
pat, initializer, ..
} => {
self.gen_let_statement(*pat, *initializer);
// If the let statement never finishes, there is no need to generate more code
if !self.gen_let_statement(*pat, *initializer) {
return None;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a dead_code diagnostic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should add that at some point but thats beyond the scope of this PR

Copy link
Collaborator

@Wodann Wodann Sep 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it though? I'm talking about at least adding the diagnostic

Adding hard opt-outs without giving any user feedback reduces usability. Moving this to a potential future improvement generally results in a large degradation before it's tackled (or even gets forgotten). It seems like this is the best time to also do that additional small change, instead of having inconveniences accumulate over time.

}
}
Statement::Expr(expr) => {
// No need to generate code after a statement that has a `never` return type.
Expand Down Expand Up @@ -510,9 +513,19 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
temp_builder
}

/// Generate IR for a let statement: `let a:int = 3`
fn gen_let_statement(&mut self, pat: PatId, initializer: Option<ExprId>) {
let initializer = initializer.and_then(|expr| self.gen_expr(expr));
/// Generate IR for a let statement: `let a:int = 3`. Returns `false` if the initializer of the
/// statement never returns; `true` otherwise.
fn gen_let_statement(&mut self, pat: PatId, initializer: Option<ExprId>) -> bool {
let initializer = match initializer {
Some(expr) => match self.gen_expr(expr) {
Some(expr) => Some(expr),
None => {
// If the initializer doesnt return a value it never returns
return false;
}
},
None => None,
};

match &self.body[pat] {
Pat::Bind { name } => {
Expand All @@ -534,6 +547,7 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
Pat::Wild => {}
Pat::Missing | Pat::Path(_) => unreachable!(),
}
true
}

/// Generates IR for looking up a certain path expression.
Expand Down
18 changes: 18 additions & 0 deletions crates/mun_codegen/src/snapshots/mun_codegen__test__issue_262.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/mun_codegen/src/test.rs
expression: "fn foo() -> i32 {\n let bar = {\n let b = 3;\n return b + 3;\n };\n\n // This code will never be executed\n let a = 3 + 4;\n a\n}"
---
; == FILE IR =====================================
; ModuleID = 'main.mun'
source_filename = "main.mun"

define i32 @foo() {
body:
ret i32 6
}


; == GROUP IR ====================================
; ModuleID = 'group_name'
source_filename = "group_name"

17 changes: 17 additions & 0 deletions crates/mun_codegen/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ use inkwell::{context::Context, OptimizationLevel};
use mun_target::spec::Target;
use std::{cell::RefCell, sync::Arc};

#[test]
fn issue_262() {
test_snapshot(
r"
fn foo() -> i32 {
let bar = {
let b = 3;
return b + 3;
};

// This code will never be executed
let a = 3 + 4;
a
}",
)
}

#[test]
fn issue_225() {
test_snapshot(
Expand Down