From f1f1c9b25b1046baecd271963739a8fef7c358e1 Mon Sep 17 00:00:00 2001 From: Aris Merchant <22333129+inquisitivecrystal@users.noreply.github.com> Date: Sun, 13 Jun 2021 01:36:53 -0700 Subject: Improve errors for missing Debug and Display impls --- compiler/rustc_span/src/symbol.rs | 1 + .../src/traits/error_reporting/mod.rs | 24 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) (limited to 'compiler') diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index fb37c5e9c1e..45c35959380 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -479,6 +479,7 @@ symbols! { discriminant_type, discriminant_value, dispatch_from_dyn, + display_trait, div, div_assign, doc, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 19c3385dd4c..e276d92bf5a 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -514,6 +514,30 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { } } + // Return early if the trait is Debug or Display and the invocation + // originates within a standard library macro, because the output + // is otherwise overwhelming and unhelpful (see #85844 for an + // example). + + let trait_is_debug = + self.tcx.is_diagnostic_item(sym::debug_trait, trait_ref.def_id()); + let trait_is_display = + self.tcx.is_diagnostic_item(sym::display_trait, trait_ref.def_id()); + + let in_std_macro = + match obligation.cause.span.ctxt().outer_expn_data().macro_def_id { + Some(macro_def_id) => { + let crate_name = tcx.crate_name(macro_def_id.krate); + crate_name == sym::std || crate_name == sym::core + } + None => false, + }; + + if in_std_macro && (trait_is_debug || trait_is_display) { + err.emit(); + return; + } + err } -- cgit 1.4.1-3-g733a5