Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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>(T);

pub type GenericAlias<T> = Generic<T>;

impl<T: std::fmt::Debug> std::fmt::Debug for GenericAlias<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ check-pass

#![deny(missing_debug_implementations)]

pub struct Local;

impl std::fmt::Debug for <Local as Identity>::Output {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
}

pub trait Identity { type Output; }
impl<T> Identity for T { type Output = T; }

pub struct Generic<T>(T);

impl std::fmt::Debug for <Generic<i32> as Identity>::Output {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
}

fn main() {}
Loading