From a63036074c5cf4beddac868a3a67229fe577bb48 Mon Sep 17 00:00:00 2001 From: mu001999 Date: Fri, 12 Jun 2026 17:49:42 +0800 Subject: [PATCH] Query the trait solver in slow path --- compiler/rustc_lint/src/builtin.rs | 62 +++++++++++++------ .../impl-debug-for-alias-type.rs | 22 +++++++ .../impl-debug-for-project.rs | 20 ++++++ .../missing-debug-implementations-lint.rs | 0 .../missing-debug-implementations-lint.stderr | 0 5 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-alias-type.rs create mode 100644 tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-project.rs rename tests/ui/lint/{ => missing-debug-implementations-lint}/missing-debug-implementations-lint.rs (100%) rename tests/ui/lint/{ => missing-debug-implementations-lint}/missing-debug-implementations-lint.stderr (100%) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 2c7ccfb25ae9b..c822651b47e00 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -685,37 +685,61 @@ impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]); impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations { fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) { - if !cx.effective_visibilities.is_reachable(item.owner_id.def_id) { + let def_id = item.owner_id.def_id; + if !cx.effective_visibilities.is_reachable(def_id) { return; } - match item.kind { - hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) | hir::ItemKind::Enum(..) => {} + let is_generic = match item.kind { + hir::ItemKind::Struct(_, generics, _) + | hir::ItemKind::Union(_, generics, _) + | hir::ItemKind::Enum(_, generics, _) => !generics.params.is_empty(), _ => return, - } + }; + + let tcx = cx.tcx; // Avoid listing trait impls if the trait is allowed. - if cx.tcx.lint_level_spec_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id()).is_allow() { + if tcx.lint_level_spec_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id()).is_allow() { return; } - let Some(debug) = cx.tcx.get_diagnostic_item(sym::Debug) else { return }; + let Some(debug) = tcx.get_diagnostic_item(sym::Debug) else { return }; - let has_impl = cx - .tcx - .non_blanket_impls_for_ty( - debug, - cx.tcx.type_of(item.owner_id).instantiate_identity().skip_norm_wip(), - ) + let ty = tcx.type_of(item.owner_id); + if tcx + .non_blanket_impls_for_ty(debug, ty.instantiate_identity().skip_norm_wip()) .next() - .is_some(); - if !has_impl { - cx.emit_span_lint( - MISSING_DEBUG_IMPLEMENTATIONS, - item.span, - BuiltinMissingDebugImpl { tcx: cx.tcx, def_id: debug }, - ); + .is_some() + { + return; } + + let infcx = tcx.infer_ctxt().build(cx.typing_mode()); + if is_generic { + let args = infcx.fresh_args_for_item(item.span, def_id.to_def_id()); + if infcx + .type_implements_trait_shallow( + debug, + ty.instantiate(tcx, args).skip_norm_wip(), + cx.param_env, + ) + .is_some() + { + return; + } + } else if infcx + .type_implements_trait(debug, [ty.instantiate_identity().skip_norm_wip()], cx.param_env) + .must_apply_modulo_regions() + { + return; + } + + cx.emit_span_lint( + MISSING_DEBUG_IMPLEMENTATIONS, + item.span, + BuiltinMissingDebugImpl { tcx: cx.tcx, def_id: debug }, + ); } } diff --git a/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-alias-type.rs b/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-alias-type.rs new file mode 100644 index 0000000000000..5222df24b61a6 --- /dev/null +++ b/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-alias-type.rs @@ -0,0 +1,22 @@ +//@ check-pass + +#![feature(lazy_type_alias)] +#![deny(missing_debug_implementations)] + +pub struct Local; + +pub type Alias = Local; + +impl std::fmt::Debug for Alias { + fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) } +} + +pub struct Generic(T); + +pub type GenericAlias = Generic; + +impl std::fmt::Debug for GenericAlias { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) } +} + +fn main() {} diff --git a/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-project.rs b/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-project.rs new file mode 100644 index 0000000000000..ecdca8c211382 --- /dev/null +++ b/tests/ui/lint/missing-debug-implementations-lint/impl-debug-for-project.rs @@ -0,0 +1,20 @@ +//@ check-pass + +#![deny(missing_debug_implementations)] + +pub struct Local; + +impl std::fmt::Debug for ::Output { + fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) } +} + +pub trait Identity { type Output; } +impl Identity for T { type Output = T; } + +pub struct Generic(T); + +impl std::fmt::Debug for as Identity>::Output { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) } +} + +fn main() {} diff --git a/tests/ui/lint/missing-debug-implementations-lint.rs b/tests/ui/lint/missing-debug-implementations-lint/missing-debug-implementations-lint.rs similarity index 100% rename from tests/ui/lint/missing-debug-implementations-lint.rs rename to tests/ui/lint/missing-debug-implementations-lint/missing-debug-implementations-lint.rs diff --git a/tests/ui/lint/missing-debug-implementations-lint.stderr b/tests/ui/lint/missing-debug-implementations-lint/missing-debug-implementations-lint.stderr similarity index 100% rename from tests/ui/lint/missing-debug-implementations-lint.stderr rename to tests/ui/lint/missing-debug-implementations-lint/missing-debug-implementations-lint.stderr