https://gcc.gnu.org/g:81cd356d43f97ee04fa90300745a07ef188bb7be

commit r17-3122-g81cd356d43f97ee04fa90300745a07ef188bb7be
Author: Utkarsh Bahuguna <[email protected]>
Date:   Fri Jul 31 17:13:10 2026 +0530

    gccrs: Mark types used as generic arguments as live
    
    MarkLive::visit_path_segment marked only the segment's own resolved
    node, never the segment's generic arguments, so a type appearing solely
    as a generic argument was never reached and the dead code lint reported
    it as never constructed.  Resolve each generic type argument in the
    Types namespace and mark the corresponding HIR id live, before the
    existing segment lookup so the arguments are still marked when the
    segment itself does not resolve; an argument that fails to resolve is
    skipped rather than asserted, since unlike a type alias target it need
    not name a resolvable type.
    
    Three existing tests carried dg-warning directives asserting this false
    positive and have had them removed.
    
            Fixes Rust-GCC/gccrs#4585
    
    gcc/rust/ChangeLog:
    
            * checks/lints/rust-lint-marklive.cc (MarkLive::visit_path_segment):
            Mark types in generic arguments as live.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/generic_args_deadcode.rs: New test.
            * rust/compile/torture/traits8.rs: Remove dg-warning.
            * rust/compile/v0-mangle1.rs: Remove dg-warning.
            * rust/execute/torture/impl_desugar.rs: Remove dg-warning.
    
    Signed-off-by: Utkarsh Bahuguna <[email protected]>

Diff:
---
 gcc/rust/checks/lints/rust-lint-marklive.cc         | 15 +++++++++++++++
 gcc/testsuite/rust/compile/generic_args_deadcode.rs | 14 ++++++++++++++
 gcc/testsuite/rust/compile/torture/traits8.rs       |  1 -
 gcc/testsuite/rust/compile/v0-mangle1.rs            |  2 +-
 gcc/testsuite/rust/execute/torture/impl_desugar.rs  |  2 +-
 5 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc 
b/gcc/rust/checks/lints/rust-lint-marklive.cc
index 796e47de467f..c99c365dd92c 100644
--- a/gcc/rust/checks/lints/rust-lint-marklive.cc
+++ b/gcc/rust/checks/lints/rust-lint-marklive.cc
@@ -154,6 +154,21 @@ MarkLive::visit (HIR::MethodCallExpr &expr)
 bool
 MarkLive::visit_path_segment (HIR::PathExprSegment seg)
 {
+  if (seg.has_generic_args ())
+    {
+      for (auto &type : seg.get_generic_args ().get_type_args ())
+       {
+         NodeId node_id = type->get_mappings ().get_nodeid ();
+
+         if (auto resolved
+             = resolver.lookup (node_id, Resolver2_0::Namespace::Types))
+           {
+             if (auto hid = mappings.lookup_node_to_hir (*resolved))
+               mark_hir_id (*hid);
+           }
+       }
+    }
+
   NodeId ast_node_id = seg.get_mappings ().get_nodeid ();
   NodeId ref_node_id = UNKNOWN_NODEID;
 
diff --git a/gcc/testsuite/rust/compile/generic_args_deadcode.rs 
b/gcc/testsuite/rust/compile/generic_args_deadcode.rs
new file mode 100644
index 000000000000..ddd64f951270
--- /dev/null
+++ b/gcc/testsuite/rust/compile/generic_args_deadcode.rs
@@ -0,0 +1,14 @@
+// Test for issue #4585.
+// A type used only as a generic argument must not trigger the
+// "struct is never constructed" warning.
+#![feature(no_core)]
+#![feature(lang_items)]
+#![no_core]
+#[lang = "sized"]
+trait Sized {}
+struct GenericArgType;
+fn anything<T>() {}
+fn main() {
+    anything::<GenericArgType>();
+}
+struct NeverUsed; // { dg-warning "struct is never constructed" }
diff --git a/gcc/testsuite/rust/compile/torture/traits8.rs 
b/gcc/testsuite/rust/compile/torture/traits8.rs
index a783cb567d07..8af990131df7 100644
--- a/gcc/testsuite/rust/compile/torture/traits8.rs
+++ b/gcc/testsuite/rust/compile/torture/traits8.rs
@@ -10,7 +10,6 @@ trait Foo {
 }
 
 struct Bar(i32);
-// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
 
 impl Foo for Bar {
     fn default() -> i32 {
diff --git a/gcc/testsuite/rust/compile/v0-mangle1.rs 
b/gcc/testsuite/rust/compile/v0-mangle1.rs
index d3c3d5112643..0f7d44466ca0 100644
--- a/gcc/testsuite/rust/compile/v0-mangle1.rs
+++ b/gcc/testsuite/rust/compile/v0-mangle1.rs
@@ -16,7 +16,7 @@ pub mod module_a {
     }
 }
 
-struct S; // { dg-warning "struct is never constructed" }
+struct S;
 
 // name starting with underscore.
 pub fn _uc() {}
diff --git a/gcc/testsuite/rust/execute/torture/impl_desugar.rs 
b/gcc/testsuite/rust/execute/torture/impl_desugar.rs
index 5de6cd11e486..0a083c576c4a 100644
--- a/gcc/testsuite/rust/execute/torture/impl_desugar.rs
+++ b/gcc/testsuite/rust/execute/torture/impl_desugar.rs
@@ -12,7 +12,7 @@ pub trait Bar {
     type Baz;
 }
 
-struct MyBaz; // { dg-warning "struct is never constructed" }
+struct MyBaz;
 impl Foo for MyBaz {}
 
 struct MyBar;

Reply via email to