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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/e9dac80b7d28daaffe20a73900d3804ba6e034a1

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4785

 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 167c5eeac..413e3f0ba 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 000000000..ddf949b1c
--- /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" }
+}

base-commit: 38fc8929b297f4f4758dda6987c59f561fd17078
-- 
2.54.0

Reply via email to