errors: lint on LintDiagnosticBuilder::build

Apply the `#[rustc_lint_diagnostics]` attribute to
`LintDiagnosticBuilder::build` so that diagnostic migration lints will
trigger for it.

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-07-11 16:29:24 +01:00
parent 06f480661f
commit c3fdf74885
4 changed files with 39 additions and 24 deletions

View File

@ -10,3 +10,12 @@ privacy-unnamed-item-is-private = {$kind} is private
privacy-in-public-interface = {$vis_descr} {$kind} `{$descr}` in public interface
.label = can't leak {$vis_descr} {$kind}
.visibility-label = `{$descr}` declared as {$vis_descr}
privacy-from-private-dep-in-public-interface =
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
private-in-public-lint =
{$vis_descr} {$kind} `{$descr}` in public interface (error {$kind ->
[trait] E0445
*[other] E0446
})

View File

@ -595,6 +595,7 @@ macro_rules! error_code {
pub struct LintDiagnosticBuilder<'a, G: EmissionGuarantee>(DiagnosticBuilder<'a, G>);
impl<'a, G: EmissionGuarantee> LintDiagnosticBuilder<'a, G> {
#[rustc_lint_diagnostics]
/// Return the inner `DiagnosticBuilder`, first setting the primary message to `msg`.
pub fn build(mut self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'a, G> {
self.0.set_primary_message(msg);

View File

@ -1,4 +1,4 @@
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::{Span, Symbol};
#[derive(SessionDiagnostic)]
@ -73,3 +73,19 @@ pub struct InPublicInterface<'a> {
#[label(privacy::visibility_label)]
pub vis_span: Span,
}
#[derive(LintDiagnostic)]
#[lint(privacy::from_private_dep_in_public_interface)]
pub struct FromPrivateDependencyInPublicInterface<'a> {
pub kind: &'a str,
pub descr: String,
pub krate: Symbol,
}
#[derive(LintDiagnostic)]
#[lint(privacy::private_in_public_lint)]
pub struct PrivateInPublicLint<'a> {
pub vis_descr: &'static str,
pub kind: &'a str,
pub descr: String,
}

View File

@ -38,8 +38,8 @@ use std::ops::ControlFlow;
use std::{cmp, fmt, mem};
use errors::{
FieldIsPrivate, FieldIsPrivateLabel, InPublicInterface, InPublicInterfaceTraits, ItemIsPrivate,
UnnamedItemIsPrivate,
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint, UnnamedItemIsPrivate,
};
////////////////////////////////////////////////////////////////////////////////
@ -1716,19 +1716,14 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
if self.leaks_private_dep(def_id) {
self.tcx.struct_span_lint_hir(
self.tcx.emit_spanned_lint(
lint::builtin::EXPORTED_PRIVATE_DEPENDENCIES,
self.tcx.hir().local_def_id_to_hir_id(self.item_def_id),
self.tcx.def_span(self.item_def_id.to_def_id()),
|lint| {
lint.build(&format!(
"{} `{}` from private dependency '{}' in public \
interface",
kind,
descr,
self.tcx.crate_name(def_id.krate)
))
.emit();
FromPrivateDependencyInPublicInterface {
kind,
descr: descr.to_string(),
krate: self.tcx.crate_name(def_id.krate),
},
);
}
@ -1754,12 +1749,14 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
}
};
let span = self.tcx.def_span(self.item_def_id.to_def_id());
let descr = descr.to_string();
if self.has_old_errors
|| self.in_assoc_ty
|| self.tcx.resolutions(()).has_pub_restricted
{
let descr = descr.to_string();
let vis_span = self.tcx.def_span(def_id);
let vis_span =
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
if kind == "trait" {
self.tcx.sess.emit_err(InPublicInterfaceTraits {
span,
@ -1778,19 +1775,11 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
});
}
} else {
let err_code = if kind == "trait" { "E0445" } else { "E0446" };
self.tcx.struct_span_lint_hir(
self.tcx.emit_spanned_lint(
lint::builtin::PRIVATE_IN_PUBLIC,
hir_id,
span,
|lint| {
lint.build(&format!(
"{} (error {})",
format!("{} {} `{}` in public interface", vis_descr, kind, descr),
err_code
))
.emit();
},
PrivateInPublicLint { vis_descr, kind, descr },
);
}
}