Skip to content

Commit 7569e17

Browse files
committed
Don't remove unreachable statements at HIR lowering
1 parent 61ce12f commit 7569e17

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

gcc/rust/hir/rust-ast-lower-stmt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ASTLoweringStmt : public ASTLoweringBase
4040
ASTLoweringStmt resolver;
4141
stmt->accept_vis (resolver);
4242
rust_assert (resolver.translated != nullptr);
43-
*terminated = resolver.terminated;
43+
*terminated |= resolver.terminated;
4444
return resolver.translated;
4545
}
4646

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,11 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
7171
std::vector<std::unique_ptr<HIR::Stmt> > block_stmts;
7272
bool block_did_terminate = false;
7373
expr.iterate_stmts ([&] (AST::Stmt *s) mutable -> bool {
74-
auto translated_stmt = ASTLoweringStmt::translate (s, &block_did_terminate);
75-
block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
76-
return !block_did_terminate;
77-
});
78-
79-
// if there was a return expression everything after that becomes
80-
// unreachable code. This can be detected for any AST NodeIDs that have no
81-
// associated HIR Mappings
82-
expr.iterate_stmts ([&] (AST::Stmt *s) -> bool {
83-
HirId ref;
84-
if (!mappings->lookup_node_to_hir (mappings->get_current_crate (),
85-
s->get_node_id (), &ref))
74+
if (block_did_terminate)
8675
rust_warning_at (s->get_locus_slow (), 0, "unreachable statement");
8776

77+
auto translated_stmt = ASTLoweringStmt::translate (s, &block_did_terminate);
78+
block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
8879
return true;
8980
});
9081

@@ -96,7 +87,7 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
9687
}
9788

9889
HIR::ExprWithoutBlock *tail_expr = nullptr;
99-
if (expr.has_tail_expr () && !block_did_terminate)
90+
if (expr.has_tail_expr ())
10091
{
10192
tail_expr = (HIR::ExprWithoutBlock *) ASTLoweringExpr::translate (
10293
expr.get_tail_expr ().get (), &block_did_terminate);

gcc/testsuite/rust.test/xfail_compile/implicit_returns_err2.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
fn test(x: i32) -> i32 {
2-
return true; // { dg-error "expected .i32. got .bool." }
3-
1 // { dg-warning "unreachable expression" }
2+
// { dg-error "expected .i32. got .bool." "" { target *-*-*} .-1 }
3+
return 1;
4+
// { dg-warning "unreachable expression" "" { target *-*-* } .+1 }
5+
true
46
}
57

68
fn main() {

0 commit comments

Comments
 (0)