From 6bb0f18de2961595cd50990d2624dd18f2f32564 Mon Sep 17 00:00:00 2001 From: mu001999 Date: Sat, 20 Jul 2024 23:12:03 +0800 Subject: [PATCH] mark the self ty live when check impl item --- compiler/rustc_passes/src/dead.rs | 6 ++++++ .../alias-type-used-as-generic-arg-in-impl.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/ui/lint/dead-code/alias-type-used-as-generic-arg-in-impl.rs diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 55514883cb1f8..5915bb6b8e991 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -525,6 +525,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { _ => {} } } + + // the self_ty may contain alias type, mark it live + if let hir::ItemKind::Impl(imp) = self.tcx.hir().expect_item(item).kind { + intravisit::walk_ty(self, imp.self_ty); + } + intravisit::walk_impl_item(self, impl_item); } Node::ForeignItem(foreign_item) => { diff --git a/tests/ui/lint/dead-code/alias-type-used-as-generic-arg-in-impl.rs b/tests/ui/lint/dead-code/alias-type-used-as-generic-arg-in-impl.rs new file mode 100644 index 0000000000000..4857ef6a9b8bf --- /dev/null +++ b/tests/ui/lint/dead-code/alias-type-used-as-generic-arg-in-impl.rs @@ -0,0 +1,19 @@ +//@ check-pass + +#![deny(dead_code)] + +struct T(X); + +type A = T; + +trait Tr { + fn foo(); +} + +impl Tr for T> { + fn foo() {} +} + +fn main() { + T::>::foo(); +}