From a42afa0444118ae5549f89e8ea9a15b73702fbcf Mon Sep 17 00:00:00 2001 From: Rejyr Date: Mon, 5 Sep 2022 11:52:08 -0400 Subject: [PATCH] migrate: `traits.rs` --- compiler/rustc_lint/src/lints.rs | 30 +++++++++++++++++++++++++++++- compiler/rustc_lint/src/traits.rs | 27 ++++++++++++--------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index c68bfc98f93..691cc8667e3 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1,11 +1,39 @@ use rustc_errors::{fluent, AddSubdiagnostic, Applicability, DecorateLint, EmissionGuarantee}; use rustc_hir::def_id::DefId; use rustc_macros::{LintDiagnostic, SessionSubdiagnostic}; -use rustc_middle::ty::Ty; +use rustc_middle::ty::{Predicate, Ty, TyCtxt}; use rustc_span::{Span, Symbol}; use crate::LateContext; +pub struct DropTraitConstraintsDiag<'a> { + pub predicate: Predicate<'a>, + pub tcx: TyCtxt<'a>, + pub def_id: DefId, +} + +impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropTraitConstraintsDiag<'a> { + fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) { + let mut diag = diag.build(fluent::lint_drop_trait_constraints); + diag.set_arg("predicate", self.predicate); + diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id)); + diag.emit(); + } +} + +pub struct DropGlue<'a> { + pub tcx: TyCtxt<'a>, + pub def_id: DefId, +} + +impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropGlue<'a> { + fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) { + let mut diag = diag.build(fluent::lint_drop_glue); + diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id)); + diag.emit(); + } +} + #[derive(LintDiagnostic)] #[diag(lint_range_endpoint_out_of_range)] pub struct RangeEndpointOutOfRange<'a> { diff --git a/compiler/rustc_lint/src/traits.rs b/compiler/rustc_lint/src/traits.rs index 1b21c2dac37..2e778cf0d0f 100644 --- a/compiler/rustc_lint/src/traits.rs +++ b/compiler/rustc_lint/src/traits.rs @@ -1,7 +1,9 @@ +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] +use crate::lints::{DropGlue, DropTraitConstraintsDiag}; use crate::LateContext; use crate::LateLintPass; use crate::LintContext; -use rustc_errors::fluent; use rustc_hir as hir; use rustc_span::symbol::sym; @@ -101,17 +103,13 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints { if trait_predicate.trait_ref.self_ty().is_impl_trait() { continue; } - let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { - continue; + let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { + return }; - cx.struct_span_lint( + cx.emit_spanned_lint( DROP_BOUNDS, span, - fluent::lint_drop_trait_constraints, - |lint| { - lint.set_arg("predicate", predicate) - .set_arg("needs_drop", cx.tcx.def_path_str(needs_drop)) - }, + DropTraitConstraintsDiag { predicate, tcx: cx.tcx, def_id }, ); } } @@ -123,12 +121,11 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints { }; for bound in &bounds[..] { let def_id = bound.trait_ref.trait_def_id(); - if cx.tcx.lang_items().drop_trait() == def_id - && let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) - { - cx.struct_span_lint(DYN_DROP, bound.span, fluent::lint_drop_glue, |lint| { - lint.set_arg("needs_drop", cx.tcx.def_path_str(needs_drop)) - }); + if cx.tcx.lang_items().drop_trait() == def_id { + let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { + return + }; + cx.emit_spanned_lint(DYN_DROP, bound.span, DropGlue { tcx: cx.tcx, def_id }); } } }