Skip to content

Commit c3fdf74

Browse files
committed
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 <[email protected]>
1 parent 06f4806 commit c3fdf74

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

compiler/rustc_error_messages/locales/en-US/privacy.ftl

+9
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ privacy-unnamed-item-is-private = {$kind} is private
1010
privacy-in-public-interface = {$vis_descr} {$kind} `{$descr}` in public interface
1111
.label = can't leak {$vis_descr} {$kind}
1212
.visibility-label = `{$descr}` declared as {$vis_descr}
13+
14+
privacy-from-private-dep-in-public-interface =
15+
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
16+
17+
private-in-public-lint =
18+
{$vis_descr} {$kind} `{$descr}` in public interface (error {$kind ->
19+
[trait] E0445
20+
*[other] E0446
21+
})

compiler/rustc_errors/src/diagnostic_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ macro_rules! error_code {
595595
pub struct LintDiagnosticBuilder<'a, G: EmissionGuarantee>(DiagnosticBuilder<'a, G>);
596596

597597
impl<'a, G: EmissionGuarantee> LintDiagnosticBuilder<'a, G> {
598+
#[rustc_lint_diagnostics]
598599
/// Return the inner `DiagnosticBuilder`, first setting the primary message to `msg`.
599600
pub fn build(mut self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'a, G> {
600601
self.0.set_primary_message(msg);

compiler/rustc_privacy/src/errors.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
1+
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
22
use rustc_span::{Span, Symbol};
33

44
#[derive(SessionDiagnostic)]
@@ -73,3 +73,19 @@ pub struct InPublicInterface<'a> {
7373
#[label(privacy::visibility_label)]
7474
pub vis_span: Span,
7575
}
76+
77+
#[derive(LintDiagnostic)]
78+
#[lint(privacy::from_private_dep_in_public_interface)]
79+
pub struct FromPrivateDependencyInPublicInterface<'a> {
80+
pub kind: &'a str,
81+
pub descr: String,
82+
pub krate: Symbol,
83+
}
84+
85+
#[derive(LintDiagnostic)]
86+
#[lint(privacy::private_in_public_lint)]
87+
pub struct PrivateInPublicLint<'a> {
88+
pub vis_descr: &'static str,
89+
pub kind: &'a str,
90+
pub descr: String,
91+
}

compiler/rustc_privacy/src/lib.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use std::ops::ControlFlow;
3838
use std::{cmp, fmt, mem};
3939

4040
use errors::{
41-
FieldIsPrivate, FieldIsPrivateLabel, InPublicInterface, InPublicInterfaceTraits, ItemIsPrivate,
42-
UnnamedItemIsPrivate,
41+
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
42+
InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint, UnnamedItemIsPrivate,
4343
};
4444

4545
////////////////////////////////////////////////////////////////////////////////
@@ -1716,19 +1716,14 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17161716

17171717
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
17181718
if self.leaks_private_dep(def_id) {
1719-
self.tcx.struct_span_lint_hir(
1719+
self.tcx.emit_spanned_lint(
17201720
lint::builtin::EXPORTED_PRIVATE_DEPENDENCIES,
17211721
self.tcx.hir().local_def_id_to_hir_id(self.item_def_id),
17221722
self.tcx.def_span(self.item_def_id.to_def_id()),
1723-
|lint| {
1724-
lint.build(&format!(
1725-
"{} `{}` from private dependency '{}' in public \
1726-
interface",
1727-
kind,
1728-
descr,
1729-
self.tcx.crate_name(def_id.krate)
1730-
))
1731-
.emit();
1723+
FromPrivateDependencyInPublicInterface {
1724+
kind,
1725+
descr: descr.to_string(),
1726+
krate: self.tcx.crate_name(def_id.krate),
17321727
},
17331728
);
17341729
}
@@ -1754,12 +1749,14 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17541749
}
17551750
};
17561751
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1752+
let descr = descr.to_string();
17571753
if self.has_old_errors
17581754
|| self.in_assoc_ty
17591755
|| self.tcx.resolutions(()).has_pub_restricted
17601756
{
17611757
let descr = descr.to_string();
1762-
let vis_span = self.tcx.def_span(def_id);
1758+
let vis_span =
1759+
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
17631760
if kind == "trait" {
17641761
self.tcx.sess.emit_err(InPublicInterfaceTraits {
17651762
span,
@@ -1778,19 +1775,11 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17781775
});
17791776
}
17801777
} else {
1781-
let err_code = if kind == "trait" { "E0445" } else { "E0446" };
1782-
self.tcx.struct_span_lint_hir(
1778+
self.tcx.emit_spanned_lint(
17831779
lint::builtin::PRIVATE_IN_PUBLIC,
17841780
hir_id,
17851781
span,
1786-
|lint| {
1787-
lint.build(&format!(
1788-
"{} (error {})",
1789-
format!("{} {} `{}` in public interface", vis_descr, kind, descr),
1790-
err_code
1791-
))
1792-
.emit();
1793-
},
1782+
PrivateInPublicLint { vis_descr, kind, descr },
17941783
);
17951784
}
17961785
}

0 commit comments

Comments
 (0)