From 7b960b4d99b4235e4abcd2828158f79650ec8570 Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Tue, 9 Jul 2024 10:30:09 +0400 Subject: [PATCH] Mark associated constants used in equality constraints as used --- compiler/rustc_passes/src/dead.rs | 25 +++++++++++ .../equality-unused-issue-126729.rs | 44 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/ui/associated-consts/equality-unused-issue-126729.rs diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 55514883cb1f8..61f9f5231fd39 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -857,6 +857,31 @@ fn check_item<'tcx>( // global_asm! is always live. worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No)); } + DefKind::OpaqueTy => { + if let hir::ItemKind::OpaqueTy(ty) = tcx.hir().item(id).kind { + for bound in ty.bounds { + if let hir::GenericBound::Trait(polytraitref, _) = bound + && let Some(pathsegment) = polytraitref.trait_ref.path.segments.last() + && let Some(args) = pathsegment.args + { + for constraint in args.constraints { + if let Some(item) = tcx + .associated_items(pathsegment.res.def_id()) + .filter_by_name_unhygienic(constraint.ident.name) + .find(|i| { + i.kind == ty::AssocKind::Const + && i.ident(tcx).normalize_to_macros_2_0() + == constraint.ident + }) + && let Some(local_def_id) = item.def_id.as_local() + { + worklist.push((local_def_id, ComesFromAllowExpect::No)); + } + } + } + } + } + } _ => {} } } diff --git a/tests/ui/associated-consts/equality-unused-issue-126729.rs b/tests/ui/associated-consts/equality-unused-issue-126729.rs new file mode 100644 index 0000000000000..1482b874b9d69 --- /dev/null +++ b/tests/ui/associated-consts/equality-unused-issue-126729.rs @@ -0,0 +1,44 @@ +//@ check-pass + +#![feature(associated_const_equality)] +#![deny(dead_code)] + +trait Tr { + const I: i32; +} + +impl Tr for () { + const I: i32 = 1; +} + +fn foo() -> impl Tr {} + +trait Tr2 { + const J: i32; + const K: i32; +} + +impl Tr2 for () { + const J: i32 = 1; + const K: i32 = 1; +} + +fn foo2() -> impl Tr2 {} + +mod t { + pub trait Tr3 { + const L: i32; + } + + impl Tr3 for () { + const L: i32 = 1; + } +} + +fn foo3() -> impl t::Tr3 {} + +fn main() { + foo(); + foo2(); + foo3(); +}