diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:41 +0000 |
commit | 4f9fe856a25ab29345b90e7725509e9ee38a37be (patch) | |
tree | e4ffd8a9374cae7b21f7cbfb352927e0e074aff6 /compiler/rustc_privacy | |
parent | Adding upstream version 1.68.2+dfsg1. (diff) | |
download | rustc-4f9fe856a25ab29345b90e7725509e9ee38a37be.tar.xz rustc-4f9fe856a25ab29345b90e7725509e9ee38a37be.zip |
Adding upstream version 1.69.0+dfsg1.upstream/1.69.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_privacy')
-rw-r--r-- | compiler/rustc_privacy/Cargo.toml | 1 | ||||
-rw-r--r-- | compiler/rustc_privacy/locales/en-US.ftl | 23 | ||||
-rw-r--r-- | compiler/rustc_privacy/src/errors.rs | 4 | ||||
-rw-r--r-- | compiler/rustc_privacy/src/lib.rs | 76 |
4 files changed, 71 insertions, 33 deletions
diff --git a/compiler/rustc_privacy/Cargo.toml b/compiler/rustc_privacy/Cargo.toml index 832fdc9f0..744cb77dd 100644 --- a/compiler/rustc_privacy/Cargo.toml +++ b/compiler/rustc_privacy/Cargo.toml @@ -13,6 +13,5 @@ rustc_macros = { path = "../rustc_macros" } rustc_middle = { path = "../rustc_middle" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } -rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_hir_analysis = { path = "../rustc_hir_analysis" } tracing = "0.1" diff --git a/compiler/rustc_privacy/locales/en-US.ftl b/compiler/rustc_privacy/locales/en-US.ftl new file mode 100644 index 000000000..a26d1b2b3 --- /dev/null +++ b/compiler/rustc_privacy/locales/en-US.ftl @@ -0,0 +1,23 @@ +privacy_field_is_private = field `{$field_name}` of {$variant_descr} `{$def_path_str}` is private +privacy_field_is_private_is_update_syntax_label = field `{$field_name}` is private +privacy_field_is_private_label = private field + +privacy_item_is_private = {$kind} `{$descr}` is private + .label = private {$kind} +privacy_unnamed_item_is_private = {$kind} is private + .label = private {$kind} + +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_report_effective_visibility = {$descr} + +privacy_from_private_dep_in_public_interface = + {$kind} `{$descr}` from private dependency '{$krate}' in public interface + +privacy_private_in_public_lint = + {$vis_descr} {$kind} `{$descr}` in public interface (error {$kind -> + [trait] E0445 + *[other] E0446 + }) diff --git a/compiler/rustc_privacy/src/errors.rs b/compiler/rustc_privacy/src/errors.rs index a6c95f1a8..72b53eefa 100644 --- a/compiler/rustc_privacy/src/errors.rs +++ b/compiler/rustc_privacy/src/errors.rs @@ -57,7 +57,7 @@ pub struct InPublicInterfaceTraits<'a> { pub vis_descr: &'static str, pub kind: &'a str, pub descr: DiagnosticArgFromDisplay<'a>, - #[label(visibility_label)] + #[label(privacy_visibility_label)] pub vis_span: Span, } @@ -71,7 +71,7 @@ pub struct InPublicInterface<'a> { pub vis_descr: &'static str, pub kind: &'a str, pub descr: DiagnosticArgFromDisplay<'a>, - #[label(visibility_label)] + #[label(privacy_visibility_label)] pub vis_span: Span, } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 9a5d3cceb..50176c802 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -16,11 +16,13 @@ use rustc_ast::MacroDef; use rustc_attr as attr; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::intern::Interned; +use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{AssocItemKind, HirIdSet, ItemId, Node, PatKind}; +use rustc_macros::fluent_messages; use rustc_middle::bug; use rustc_middle::hir::nested_filter; use rustc_middle::middle::privacy::{EffectiveVisibilities, Level}; @@ -44,6 +46,8 @@ use errors::{ UnnamedItemIsPrivate, }; +fluent_messages! { "../locales/en-US.ftl" } + //////////////////////////////////////////////////////////////////////////////// /// Generic infrastructure used to implement specific visitors below. //////////////////////////////////////////////////////////////////////////////// @@ -81,7 +85,10 @@ trait DefIdVisitor<'tcx> { dummy: Default::default(), } } - fn visit(&mut self, ty_fragment: impl TypeVisitable<'tcx>) -> ControlFlow<Self::BreakTy> { + fn visit( + &mut self, + ty_fragment: impl TypeVisitable<TyCtxt<'tcx>>, + ) -> ControlFlow<Self::BreakTy> { ty_fragment.visit_with(&mut self.skeleton()) } fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> ControlFlow<Self::BreakTy> { @@ -159,9 +166,21 @@ where _region, ))) => ty.visit_with(self), ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..)) => ControlFlow::Continue(()), + ty::PredicateKind::Clause(ty::Clause::ConstArgHasType(ct, ty)) => { + ct.visit_with(self)?; + ty.visit_with(self) + } ty::PredicateKind::ConstEvaluatable(ct) => ct.visit_with(self), ty::PredicateKind::WellFormed(arg) => arg.visit_with(self), - _ => bug!("unexpected predicate: {:?}", predicate), + + ty::PredicateKind::ObjectSafe(_) + | ty::PredicateKind::ClosureKind(_, _, _) + | ty::PredicateKind::Subtype(_) + | ty::PredicateKind::Coerce(_) + | ty::PredicateKind::ConstEquate(_, _) + | ty::PredicateKind::TypeWellFormedFromEnv(_) + | ty::PredicateKind::Ambiguous + | ty::PredicateKind::AliasEq(_, _) => bug!("unexpected predicate: {:?}", predicate), } } @@ -174,7 +193,7 @@ where } } -impl<'tcx, V> TypeVisitor<'tcx> for DefIdVisitorSkeleton<'_, 'tcx, V> +impl<'tcx, V> TypeVisitor<TyCtxt<'tcx>> for DefIdVisitorSkeleton<'_, 'tcx, V> where V: DefIdVisitor<'tcx> + ?Sized, { @@ -198,7 +217,8 @@ where // Something like `fn() -> Priv {my_func}` is considered a private type even if // `my_func` is public, so we need to visit signatures. if let ty::FnDef(..) = ty.kind() { - tcx.fn_sig(def_id).visit_with(self)?; + // FIXME: this should probably use `substs` from `FnDef` + tcx.fn_sig(def_id).subst_identity().visit_with(self)?; } // Inherent static methods don't have self type in substs. // Something like `fn() {my_method}` type of the method @@ -206,7 +226,7 @@ where // so we need to visit the self type additionally. if let Some(assoc_item) = tcx.opt_associated_item(def_id) { if let Some(impl_def_id) = assoc_item.impl_container(tcx) { - tcx.type_of(impl_def_id).visit_with(self)?; + tcx.type_of(impl_def_id).subst_identity().visit_with(self)?; } } } @@ -269,9 +289,11 @@ where | ty::Ref(..) | ty::FnPtr(..) | ty::Param(..) + | ty::Bound(..) | ty::Error(_) - | ty::GeneratorWitness(..) => {} - ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) => { + | ty::GeneratorWitness(..) + | ty::GeneratorWitnessMIR(..) => {} + ty::Placeholder(..) | ty::Infer(..) => { bug!("unexpected type: {:?}", ty) } } @@ -339,7 +361,7 @@ trait VisibilityLike: Sized { effective_visibilities: &EffectiveVisibilities, ) -> Self { let mut find = FindMin { tcx, effective_visibilities, min: Self::MAX }; - find.visit(tcx.type_of(def_id)); + find.visit(tcx.type_of(def_id).subst_identity()); if let Some(trait_ref) = tcx.impl_trait_ref(def_id) { find.visit_trait(trait_ref.subst_identity()); } @@ -591,7 +613,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { | DefKind::InlineConst | DefKind::Field | DefKind::GlobalAsm - | DefKind::Impl + | DefKind::Impl { .. } | DefKind::Closure | DefKind::Generator => (), } @@ -835,11 +857,11 @@ impl ReachEverythingInTheInterfaceVisitor<'_, '_> { GenericParamDefKind::Lifetime => {} GenericParamDefKind::Type { has_default, .. } => { if has_default { - self.visit(self.ev.tcx.type_of(param.def_id)); + self.visit(self.ev.tcx.type_of(param.def_id).subst_identity()); } } GenericParamDefKind::Const { has_default } => { - self.visit(self.ev.tcx.type_of(param.def_id)); + self.visit(self.ev.tcx.type_of(param.def_id).subst_identity()); if has_default { self.visit(self.ev.tcx.const_param_default(param.def_id).subst_identity()); } @@ -855,7 +877,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, '_> { } fn ty(&mut self) -> &mut Self { - self.visit(self.ev.tcx.type_of(self.item_def_id)); + self.visit(self.ev.tcx.type_of(self.item_def_id).subst_identity()); self } @@ -1266,7 +1288,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { // Method calls have to be checked specially. self.span = segment.ident.span; if let Some(def_id) = self.typeck_results().type_dependent_def_id(expr.hir_id) { - if self.visit(self.tcx.type_of(def_id)).is_break() { + if self.visit(self.tcx.type_of(def_id).subst_identity()).is_break() { return; } } else { @@ -1314,7 +1336,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { hir::QPath::Resolved(_, path) => Some(self.tcx.def_path_str(path.res.def_id())), hir::QPath::TypeRelative(_, segment) => Some(segment.ident.to_string()), }; - let kind = kind.descr(def_id); + let kind = self.tcx.def_descr(def_id); let sess = self.tcx.sess; let _ = match name { Some(name) => { @@ -1740,12 +1762,12 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { GenericParamDefKind::Lifetime => {} GenericParamDefKind::Type { has_default, .. } => { if has_default { - self.visit(self.tcx.type_of(param.def_id)); + self.visit(self.tcx.type_of(param.def_id).subst_identity()); } } // FIXME(generic_const_exprs): May want to look inside const here GenericParamDefKind::Const { .. } => { - self.visit(self.tcx.type_of(param.def_id)); + self.visit(self.tcx.type_of(param.def_id).subst_identity()); } } } @@ -1772,7 +1794,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { } fn ty(&mut self) -> &mut Self { - self.visit(self.tcx.type_of(self.item_def_id)); + self.visit(self.tcx.type_of(self.item_def_id).subst_identity()); self } @@ -1877,7 +1899,7 @@ impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> { struct PrivateItemsInPublicInterfacesChecker<'tcx> { tcx: TyCtxt<'tcx>, - old_error_set_ancestry: LocalDefIdSet, + old_error_set_ancestry: HirIdSet, } impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { @@ -1890,7 +1912,9 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { tcx: self.tcx, item_def_id: def_id, required_visibility, - has_old_errors: self.old_error_set_ancestry.contains(&def_id), + has_old_errors: self + .old_error_set_ancestry + .contains(&self.tcx.hir().local_def_id_to_hir_id(def_id)), in_assoc_ty: false, } } @@ -1993,7 +2017,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { // Subitems of inherent impls have their own publicity. // A trait impl is public when both its type and its trait are public // Subitems of trait impls have inherited publicity. - DefKind::Impl => { + DefKind::Impl { .. } => { let item = tcx.hir().item(id); if let hir::ItemKind::Impl(ref impl_) = item.kind { let impl_vis = @@ -2156,15 +2180,7 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { } // Check for private types and traits in public interfaces. - let mut checker = PrivateItemsInPublicInterfacesChecker { - tcx, - // Only definition IDs are ever searched in `old_error_set_ancestry`, - // so we can filter away all non-definition IDs at this point. - old_error_set_ancestry: old_error_set_ancestry - .into_iter() - .filter_map(|hir_id| tcx.hir().opt_local_def_id(hir_id)) - .collect(), - }; + let mut checker = PrivateItemsInPublicInterfacesChecker { tcx, old_error_set_ancestry }; for id in tcx.hir().items() { checker.check_item(id); |