[COMMITTED 35/43] gccrs: Prevent warning on "unreachable" items

arthur.cohen@opensrcsec.com arthur.cohen@opensrcsec.com
Thu Sep 10 08:19:48 GMT 2026


From: Owen Avery <powerboat9.gamer@gmail.com>

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 <powerboat9.gamer@gmail.com>
---
 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



More information about the Gcc-rust mailing list