Skip to content

Commit faa68ae

Browse files
committed
privacy: port "in public interface" diag
Signed-off-by: David Wood <[email protected]>
1 parent b571813 commit faa68ae

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ privacy-item-is-private = {$kind} `{$descr}` is private
66
.label = private {$kind}
77
privacy-unnamed-item-is-private = {$kind} is private
88
.label = private {$kind}
9+
10+
privacy-in-public-interface = {$vis_descr} {$kind} `{$descr}` in public interface
11+
.label = can't leak {$vis_descr} {$kind}
12+
.visibility-label = `{$descr}` declared as {$vis_descr}

compiler/rustc_privacy/src/errors.rs

+28
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,31 @@ pub struct UnnamedItemIsPrivate {
4545
pub span: Span,
4646
pub kind: &'static str,
4747
}
48+
49+
// Duplicate of `InPublicInterface` but with a different error code, shares the same slug.
50+
#[derive(SessionDiagnostic)]
51+
#[error(code = "E0445", slug = "privacy-in-public-interface")]
52+
pub struct InPublicInterfaceTraits<'a> {
53+
#[primary_span]
54+
#[label]
55+
pub span: Span,
56+
pub vis_descr: &'static str,
57+
pub kind: &'a str,
58+
pub descr: String,
59+
#[label = "visibility-label"]
60+
pub vis_span: Span,
61+
}
62+
63+
// Duplicate of `InPublicInterfaceTraits` but with a different error code, shares the same slug.
64+
#[derive(SessionDiagnostic)]
65+
#[error(code = "E0446", slug = "privacy-in-public-interface")]
66+
pub struct InPublicInterface<'a> {
67+
#[primary_span]
68+
#[label]
69+
pub span: Span,
70+
pub vis_descr: &'static str,
71+
pub kind: &'a str,
72+
pub descr: String,
73+
#[label = "visibility-label"]
74+
pub vis_span: Span,
75+
}

compiler/rustc_privacy/src/lib.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_ast::MacroDef;
1111
use rustc_attr as attr;
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_data_structures::intern::Interned;
14-
use rustc_errors::struct_span_err;
1514
use rustc_hir as hir;
1615
use rustc_hir::def::{DefKind, Res};
1716
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
@@ -36,7 +35,10 @@ use std::marker::PhantomData;
3635
use std::ops::ControlFlow;
3736
use std::{cmp, fmt, mem};
3837

39-
use errors::{FieldIsPrivate, FieldIsPrivateLabel, ItemIsPrivate, UnnamedItemIsPrivate};
38+
use errors::{
39+
FieldIsPrivate, FieldIsPrivateLabel, InPublicInterface, InPublicInterfaceTraits, ItemIsPrivate,
40+
UnnamedItemIsPrivate,
41+
};
4042

4143
////////////////////////////////////////////////////////////////////////////////
4244
/// Generic infrastructure used to implement specific visitors below.
@@ -1748,30 +1750,44 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17481750
}
17491751
}
17501752
};
1751-
let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr);
17521753
let span = self.tcx.def_span(self.item_def_id.to_def_id());
17531754
if self.has_old_errors
17541755
|| self.in_assoc_ty
17551756
|| self.tcx.resolutions(()).has_pub_restricted
17561757
{
1757-
let mut err = if kind == "trait" {
1758-
struct_span_err!(self.tcx.sess, span, E0445, "{}", make_msg())
1759-
} else {
1760-
struct_span_err!(self.tcx.sess, span, E0446, "{}", make_msg())
1761-
};
1758+
let descr = descr.to_string();
17621759
let vis_span =
17631760
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
1764-
err.span_label(span, format!("can't leak {} {}", vis_descr, kind));
1765-
err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr));
1766-
err.emit();
1761+
if kind == "trait" {
1762+
self.tcx.sess.emit_err(InPublicInterfaceTraits {
1763+
span,
1764+
vis_descr,
1765+
kind,
1766+
descr,
1767+
vis_span,
1768+
});
1769+
} else {
1770+
self.tcx.sess.emit_err(InPublicInterface {
1771+
span,
1772+
vis_descr,
1773+
kind,
1774+
descr,
1775+
vis_span,
1776+
});
1777+
}
17671778
} else {
17681779
let err_code = if kind == "trait" { "E0445" } else { "E0446" };
17691780
self.tcx.struct_span_lint_hir(
17701781
lint::builtin::PRIVATE_IN_PUBLIC,
17711782
hir_id,
17721783
span,
17731784
|lint| {
1774-
lint.build(&format!("{} (error {})", make_msg(), err_code)).emit();
1785+
lint.build(&format!(
1786+
"{} (error {})",
1787+
format!("{} {} `{}` in public interface", vis_descr, kind, descr),
1788+
err_code
1789+
))
1790+
.emit();
17751791
},
17761792
);
17771793
}

0 commit comments

Comments
 (0)