Skip to content

Don't remove unreachable statements at HIR lowering #387

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

Merged
merged 1 commit into from
Apr 25, 2021
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
4 changes: 4 additions & 0 deletions gcc/rust/ast/rust-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class EmptyStmt : public Stmt

EmptyStmt (Location locus) : locus (locus) {}

Location get_locus_slow () const final override { return get_locus (); }

Location get_locus () const { return locus; }

void accept_vis (ASTVisitor &vis) override;
Expand Down Expand Up @@ -135,6 +137,8 @@ class LetStmt : public Stmt
LetStmt (LetStmt &&other) = default;
LetStmt &operator= (LetStmt &&other) = default;

Location get_locus_slow () const final override { return get_locus (); }

Location get_locus () const { return locus; }

void accept_vis (ASTVisitor &vis) override;
Expand Down
25 changes: 9 additions & 16 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,16 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
std::vector<std::unique_ptr<HIR::Stmt> > block_stmts;
bool block_did_terminate = false;
expr.iterate_stmts ([&] (AST::Stmt *s) mutable -> bool {
if (block_did_terminate)
rust_warning_at (s->get_locus_slow (), 0, "unreachable statement");

bool terminated = false;
auto translated_stmt = ASTLoweringStmt::translate (s, &terminated);
block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
block_did_terminate = terminated;
return !block_did_terminate;
});

// if there was a return expression everything after that becomes
// unreachable code. This can be detected for any AST NodeIDs that have no
// associated HIR Mappings
expr.iterate_stmts ([&] (AST::Stmt *s) -> bool {
HirId ref;
if (!mappings->lookup_node_to_hir (mappings->get_current_crate (),
s->get_node_id (), &ref))
rust_warning_at (s->get_locus_slow (), 0, "unreachable statement");

block_did_terminate |= terminated;
return true;
});

bool tail_reachable = !block_did_terminate;
if (expr.has_tail_expr () && block_did_terminate)
{
// warning unreachable tail expressions
Expand All @@ -101,10 +91,13 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
HIR::ExprWithoutBlock *tail_expr = nullptr;
if (expr.has_tail_expr ())
{
tail_expr = (HIR::ExprWithoutBlock *) ASTLoweringExpr::translate (
expr.get_tail_expr ().get ());
bool terminated = false;
tail_expr = (HIR::ExprWithoutBlock *)
ASTLoweringExpr::translate (expr.get_tail_expr ().get (), &terminated);
block_did_terminate |= terminated;
}

bool tail_reachable = !block_did_terminate;
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust.test/compile/deadcode2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn foo() -> i32 {
return 1;

let a = -1; // { dg-warning "unreachable statement" }
a // { dg-warning "unreachable expression" }
}

fn main() {
foo();
}
11 changes: 11 additions & 0 deletions gcc/testsuite/rust.test/xfail_compile/deadcode_err1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn foo() -> i32 {
return 1;

let mut a = 1; // { dg-warning "unreachable statement" }
a = 1.1; // { dg-warning "unreachable statement" }
// { dg-error "expected .<integer>. got .<float>." "" { target { *-*-* } } .-1 }
}

fn main() {
foo();
}
16 changes: 16 additions & 0 deletions gcc/testsuite/rust.test/xfail_compile/deadcode_err2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn foo() -> i32 {
return 1;
return 1.5; // { dg-error "expected .i32. got .<float>." }
// { dg-warning "unreachable statement" "" { target *-*-* } .-1 }
}

fn bar() -> i32 {
return 1.5; // { dg-error "expected .i32. got .<float>." }
return 1;
// { dg-warning "unreachable statement" "" { target *-*-* } .-1 }
}

fn main() {
foo();
bar();
}