From: Owen Avery <[email protected]>

Items declared in a block expression can't be unreachable.

gcc/rust/ChangeLog:

        * hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Check if a
        statement is an item before warning.

gcc/testsuite/ChangeLog:

        * rust/compile/unreachable.rs: New test.

Signed-off-by: Owen Avery <[email protected]>
---
 gcc/rust/hir/rust-ast-lower.cc            |  2 +-
 gcc/testsuite/rust/compile/unreachable.rs | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/unreachable.rs

diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 167c5eeac42..413e3f0baaa 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -117,7 +117,7 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
          "this point in "
          "the pipeline, they should all have been expanded");
 
-      if (block_did_terminate)
+      if (s->get_stmt_kind () != AST::Stmt::Kind::Item && block_did_terminate)
        rust_warning_at (s->get_locus (), 0, "unreachable statement");
 
       bool terminated = false;
diff --git a/gcc/testsuite/rust/compile/unreachable.rs 
b/gcc/testsuite/rust/compile/unreachable.rs
new file mode 100644
index 00000000000..ddf949b1cd6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/unreachable.rs
@@ -0,0 +1,21 @@
+#![feature(no_core)]
+#![no_core]
+
+pub fn f1() {
+    return f11();
+
+    // no warning
+    fn f11() {}
+
+    f11() // { dg-warning "unreachable expression" }
+}
+
+pub fn f2() {
+    return;
+    f1(); // { dg-warning "unreachable statement" }
+}
+
+pub fn f3() {
+    return;
+    f1() // { dg-warning "unreachable expression" }
+}
-- 
2.50.1

Reply via email to