https://gcc.gnu.org/g:447997da72b0d3df4f5a32df95e19231f85161b9

commit r17-4122-g447997da72b0d3df4f5a32df95e19231f85161b9
Author: Owen Avery <[email protected]>
Date:   Sun Aug 16 11:58:46 2026 -0400

    gccrs: Prevent warning on "unreachable" items
    
    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]>

Diff:
---
 gcc/rust/hir/rust-ast-lower.cc            |  2 +-
 gcc/testsuite/rust/compile/unreachable.rs | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 167c5eeac420..413e3f0baaa8 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 000000000000..ddf949b1cd69
--- /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" }
+}

Reply via email to