[gccrs COMMIT] gccrs: Mark types used as generic arguments as live

[email protected]
Newsgroups gmane.comp.gcc.rust,gmane.comp.gcc.patches
Message-ID <[email protected]>
From: Utkarsh Bahuguna <[email protected]>

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]>
---
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/57f5c23a9b9246a2d96679c3939c86ffe896c066

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#4585: https://github.com/Rust-GCC/gccrs/issues/4585

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

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

diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc b/gcc/rust/checks/lints/rust-lint-marklive.cc
index 796e47de4..c99c365dd 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 000000000..ddd64f951
--- /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 a783cb567..8af990131 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 d3c3d5112..0f7d44466 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 5de6cd11e..0a083c576 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;

base-commit: c539964b8cc842e871d77b45150df6a5882335fb
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.