Skip to content

Commit be9ebfd

Browse files
committed
privacy: port "field is private" diag
Signed-off-by: David Wood <[email protected]>
1 parent ae61224 commit be9ebfd

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4280,6 +4280,7 @@ dependencies = [
42804280
"rustc_data_structures",
42814281
"rustc_errors",
42824282
"rustc_hir",
4283+
"rustc_macros",
42834284
"rustc_middle",
42844285
"rustc_session",
42854286
"rustc_span",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
privacy-field-is-private = field `{$field_name}` of {$variant_descr} `{$def_path_str}` is private
2+
privacy-field-is-private-is-update-syntax-label = field `{$field_name}` is private
3+
privacy-field-is-private-label = private field

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use unic_langid::{langid, LanguageIdentifier};
3232
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
3333
fluent_messages! {
3434
parser => "../locales/en-US/parser.ftl",
35+
privacy => "../locales/en-US/privacy.ftl",
3536
typeck => "../locales/en-US/typeck.ftl",
3637
builtin_macros => "../locales/en-US/builtin_macros.ftl",
3738
}

compiler/rustc_privacy/Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
rustc_middle = { path = "../rustc_middle" }
87
rustc_ast = { path = "../rustc_ast" }
98
rustc_attr = { path = "../rustc_attr" }
9+
rustc_data_structures = { path = "../rustc_data_structures" }
1010
rustc_errors = { path = "../rustc_errors" }
1111
rustc_hir = { path = "../rustc_hir" }
12-
rustc_typeck = { path = "../rustc_typeck" }
12+
rustc_macros = { path = "../rustc_macros" }
13+
rustc_middle = { path = "../rustc_middle" }
1314
rustc_session = { path = "../rustc_session" }
1415
rustc_span = { path = "../rustc_span" }
15-
rustc_data_structures = { path = "../rustc_data_structures" }
1616
rustc_trait_selection = { path = "../rustc_trait_selection" }
17+
rustc_typeck = { path = "../rustc_typeck" }
1718
tracing = "0.1"

compiler/rustc_privacy/src/errors.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
2+
use rustc_span::{Span, Symbol};
3+
4+
#[derive(SessionDiagnostic)]
5+
#[error(privacy::field_is_private, code = "E0451")]
6+
pub struct FieldIsPrivate {
7+
#[primary_span]
8+
pub span: Span,
9+
pub field_name: Symbol,
10+
pub variant_descr: &'static str,
11+
pub def_path_str: String,
12+
#[subdiagnostic]
13+
pub label: FieldIsPrivateLabel,
14+
}
15+
16+
#[derive(SessionSubdiagnostic)]
17+
pub enum FieldIsPrivateLabel {
18+
#[label(privacy::field_is_private_is_update_syntax_label)]
19+
IsUpdateSyntax {
20+
#[primary_span]
21+
span: Span,
22+
field_name: Symbol,
23+
},
24+
#[label(privacy::field_is_private_label)]
25+
Other {
26+
#[primary_span]
27+
span: Span,
28+
},
29+
}

compiler/rustc_privacy/src/lib.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#![recursion_limit = "256"]
66
#![allow(rustc::potential_query_instability)]
77

8+
mod errors;
9+
810
use rustc_ast::MacroDef;
911
use rustc_attr as attr;
1012
use rustc_data_structures::fx::FxHashSet;
@@ -34,6 +36,8 @@ use std::marker::PhantomData;
3436
use std::ops::ControlFlow;
3537
use std::{cmp, fmt, mem};
3638

39+
use errors::{FieldIsPrivate, FieldIsPrivateLabel};
40+
3741
////////////////////////////////////////////////////////////////////////////////
3842
/// Generic infrastructure used to implement specific visitors below.
3943
////////////////////////////////////////////////////////////////////////////////
@@ -935,23 +939,17 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
935939
let hir_id = self.tcx.hir().local_def_id_to_hir_id(self.current_item);
936940
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1;
937941
if !field.vis.is_accessible_from(def_id, self.tcx) {
938-
let label = if in_update_syntax {
939-
format!("field `{}` is private", field.name)
940-
} else {
941-
"private field".to_string()
942-
};
943-
944-
struct_span_err!(
945-
self.tcx.sess,
942+
self.tcx.sess.emit_err(FieldIsPrivate {
946943
span,
947-
E0451,
948-
"field `{}` of {} `{}` is private",
949-
field.name,
950-
def.variant_descr(),
951-
self.tcx.def_path_str(def.did())
952-
)
953-
.span_label(span, label)
954-
.emit();
944+
field_name: field.name,
945+
variant_descr: def.variant_descr(),
946+
def_path_str: self.tcx.def_path_str(def.did()),
947+
label: if in_update_syntax {
948+
FieldIsPrivateLabel::IsUpdateSyntax { span, field_name: field.name }
949+
} else {
950+
FieldIsPrivateLabel::Other { span }
951+
},
952+
});
955953
}
956954
}
957955
}

0 commit comments

Comments
 (0)