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]>
---
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 796e47de467..c99c365dd92 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 00000000000..ddd64f95127
--- /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 a783cb567d0..8af990131df 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 d3c3d511264..0f7d44466ca 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 5de6cd11e48..0a083c576c4 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;
--
2.50.1
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.