diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
commit | 9918693037dce8aa4bb6f08741b6812923486c18 (patch) | |
tree | 21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /compiler/rustc_privacy/src | |
parent | Releasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff) | |
download | rustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip |
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_privacy/src')
-rw-r--r-- | compiler/rustc_privacy/src/lib.rs | 170 |
1 files changed, 39 insertions, 131 deletions
diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 4bb7e6574..be9c6b725 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1,7 +1,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] -#![cfg_attr(not(bootstrap), doc(rust_logo))] -#![cfg_attr(not(bootstrap), feature(rustdoc_internals))] -#![cfg_attr(not(bootstrap), allow(internal_features))] +#![doc(rust_logo)] +#![feature(rustdoc_internals)] +#![allow(internal_features)] #![feature(associated_type_defaults)] #![feature(rustc_private)] #![feature(try_blocks)] @@ -19,18 +19,15 @@ 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_fluent_macro::fluent_messages; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, CRATE_DEF_ID}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, Node, PatKind}; +use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, PatKind}; use rustc_middle::bug; use rustc_middle::hir::nested_filter; use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level}; use rustc_middle::query::Providers; -use rustc_middle::span_bug; use rustc_middle::ty::GenericArgs; use rustc_middle::ty::{self, Const, GenericParamDefKind}; use rustc_middle::ty::{TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor}; @@ -49,7 +46,7 @@ use errors::{ UnnamedItemIsPrivate, }; -fluent_messages! { "../messages.ftl" } +rustc_fluent_macro::fluent_messages! { "../messages.ftl" } //////////////////////////////////////////////////////////////////////////////// /// Generic infrastructure used to implement specific visitors below. @@ -220,20 +217,21 @@ where return ControlFlow::Continue(()); } - let kind = match kind { - ty::Inherent | ty::Projection => "associated type", - ty::Weak => "type alias", - ty::Opaque => unreachable!(), - }; self.def_id_visitor.visit_def_id( data.def_id, - kind, + match kind { + ty::Inherent | ty::Projection => "associated type", + ty::Weak => "type alias", + ty::Opaque => unreachable!(), + }, &LazyDefPathStr { def_id: data.def_id, tcx }, )?; // This will also visit args if necessary, so we don't need to recurse. return if V::SHALLOW { ControlFlow::Continue(()) + } else if kind == ty::Projection { + self.visit_projection_ty(data) } else { data.args.iter().try_for_each(|subst| subst.visit_with(self)) }; @@ -493,14 +491,14 @@ impl<'tcx> EmbargoVisitor<'tcx> { macro_ev: EffectiveVisibility, ) { // Non-opaque macros cannot make other items more accessible than they already are. - let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id); + let hir_id = self.tcx.local_def_id_to_hir_id(local_def_id); let attrs = self.tcx.hir().attrs(hir_id); if attr::find_transparency(attrs, md.macro_rules).0 != Transparency::Opaque { return; } let macro_module_def_id = self.tcx.local_parent(local_def_id); - if self.tcx.opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) { + if self.tcx.def_kind(macro_module_def_id) != DefKind::Mod { // The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252). return; } @@ -533,44 +531,21 @@ impl<'tcx> EmbargoVisitor<'tcx> { macro_ev: EffectiveVisibility, ) -> bool { if self.macro_reachable.insert((module_def_id, defining_mod)) { - self.update_macro_reachable_mod(module_def_id, defining_mod, macro_ev); + for child in self.tcx.module_children_local(module_def_id.to_local_def_id()) { + if let Res::Def(def_kind, def_id) = child.res + && let Some(def_id) = def_id.as_local() + && child.vis.is_accessible_from(defining_mod, self.tcx) + { + let vis = self.tcx.local_visibility(def_id); + self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod, macro_ev); + } + } true } else { false } } - fn update_macro_reachable_mod( - &mut self, - module_def_id: LocalModDefId, - defining_mod: LocalModDefId, - macro_ev: EffectiveVisibility, - ) { - let module = self.tcx.hir().get_module(module_def_id).0; - for item_id in module.item_ids { - let def_kind = self.tcx.def_kind(item_id.owner_id); - let vis = self.tcx.local_visibility(item_id.owner_id.def_id); - self.update_macro_reachable_def( - item_id.owner_id.def_id, - def_kind, - vis, - defining_mod, - macro_ev, - ); - } - for child in self.tcx.module_children_local(module_def_id.to_local_def_id()) { - // FIXME: Use module children for the logic above too. - if !child.reexport_chain.is_empty() - && child.vis.is_accessible_from(defining_mod, self.tcx) - && let Res::Def(def_kind, def_id) = child.res - && let Some(def_id) = def_id.as_local() - { - let vis = self.tcx.local_visibility(def_id); - self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod, macro_ev); - } - } - } - fn update_macro_reachable_def( &mut self, def_id: LocalDefId, @@ -655,8 +630,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { | DefKind::Field | DefKind::GlobalAsm | DefKind::Impl { .. } - | DefKind::Closure - | DefKind::Coroutine => (), + | DefKind::Closure => (), } } } @@ -664,7 +638,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { if self.impl_trait_pass - && let hir::ItemKind::OpaqueTy(ref opaque) = item.kind + && let hir::ItemKind::OpaqueTy(opaque) = item.kind && !opaque.in_trait { // FIXME: This is some serious pessimization intended to workaround deficiencies @@ -688,7 +662,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { | hir::ItemKind::GlobalAsm(..) => {} // The interface is empty, and all nested items are processed by `visit_item`. hir::ItemKind::Mod(..) | hir::ItemKind::OpaqueTy(..) => {} - hir::ItemKind::Macro(ref macro_def, _) => { + hir::ItemKind::Macro(macro_def, _) => { if let Some(item_ev) = item_ev { self.update_reachability_from_macro(item.owner_id.def_id, macro_def, item_ev); } @@ -727,7 +701,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { self.reach(item.owner_id.def_id, item_ev).generics().predicates(); } } - hir::ItemKind::Impl(ref impl_) => { + hir::ItemKind::Impl(impl_) => { // Type inference is very smart sometimes. It can make an impl reachable even some // components of its type or trait are unreachable. E.g. methods of // `impl ReachableTrait<UnreachableTy> for ReachableTy<UnreachableTy> { ... }` @@ -891,21 +865,6 @@ pub struct TestReachabilityVisitor<'tcx, 'a> { effective_visibilities: &'a EffectiveVisibilities, } -fn vis_to_string<'tcx>(def_id: LocalDefId, vis: ty::Visibility, tcx: TyCtxt<'tcx>) -> String { - match vis { - ty::Visibility::Restricted(restricted_id) => { - if restricted_id.is_top_level_module() { - "pub(crate)".to_string() - } else if restricted_id == tcx.parent_module_from_def_id(def_id).to_local_def_id() { - "pub(self)".to_string() - } else { - format!("pub({})", tcx.item_name(restricted_id.to_def_id())) - } - } - ty::Visibility::Public => "pub".to_string(), - } -} - impl<'tcx, 'a> TestReachabilityVisitor<'tcx, 'a> { fn effective_visibility_diagnostic(&mut self, def_id: LocalDefId) { if self.tcx.has_attr(def_id, sym::rustc_effective_visibility) { @@ -913,7 +872,7 @@ impl<'tcx, 'a> TestReachabilityVisitor<'tcx, 'a> { let span = self.tcx.def_span(def_id.to_def_id()); if let Some(effective_vis) = self.effective_visibilities.effective_vis(def_id) { for level in Level::all_levels() { - let vis_str = vis_to_string(def_id, *effective_vis.at_level(level), self.tcx); + let vis_str = effective_vis.at_level(level).to_string(def_id, self.tcx); if level != Level::Direct { error_msg.push_str(", "); } @@ -1004,7 +963,7 @@ impl<'tcx> NamePrivacyVisitor<'tcx> { // definition of the field let ident = Ident::new(kw::Empty, use_ctxt); - let hir_id = self.tcx.hir().local_def_id_to_hir_id(self.current_item); + let hir_id = self.tcx.local_def_id_to_hir_id(self.current_item); let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1; if !field.vis.is_accessible_from(def_id, self.tcx) { self.tcx.sess.emit_err(FieldIsPrivate { @@ -1271,7 +1230,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { } else { self.tcx .sess - .delay_span_bug(expr.span, "no type-dependent def for method call"); + .span_delayed_bug(expr.span, "no type-dependent def for method call"); } } _ => {} @@ -1442,7 +1401,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { if self.leaks_private_dep(def_id) { 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.local_def_id_to_hir_id(self.item_def_id), self.tcx.def_span(self.item_def_id.to_def_id()), FromPrivateDependencyInPublicInterface { kind, @@ -1499,7 +1458,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { }; self.tcx.emit_spanned_lint( lint, - self.tcx.hir().local_def_id_to_hir_id(self.item_def_id), + self.tcx.local_def_id_to_hir_id(self.item_def_id), span, PrivateInterfacesOrBoundsLint { item_span: span, @@ -1509,11 +1468,11 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { tcx: self.tcx, }) .into(), - item_vis_descr: &vis_to_string(self.item_def_id, reachable_at_vis, self.tcx), + item_vis_descr: &reachable_at_vis.to_string(self.item_def_id, self.tcx), ty_span: vis_span, ty_kind: kind, ty_descr: descr.into(), - ty_vis_descr: &vis_to_string(local_def_id, vis, self.tcx), + ty_vis_descr: &vis.to_string(local_def_id, self.tcx), }, ); } @@ -1582,7 +1541,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> { let reachable_at_vis = effective_vis.at_level(Level::Reachable); if reachable_at_vis.is_public() && reexported_at_vis != reachable_at_vis { - let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); + let hir_id = self.tcx.local_def_id_to_hir_id(def_id); let span = self.tcx.def_span(def_id.to_def_id()); self.tcx.emit_spanned_lint( lint::builtin::UNNAMEABLE_TYPES, @@ -1592,8 +1551,8 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> { span, kind: self.tcx.def_descr(def_id.to_def_id()), descr: (&LazyDefPathStr { def_id: def_id.to_def_id(), tcx: self.tcx }).into(), - reachable_vis: &vis_to_string(def_id, *reachable_at_vis, self.tcx), - reexported_vis: &vis_to_string(def_id, *reexported_at_vis, self.tcx), + reachable_vis: &reachable_at_vis.to_string(def_id, self.tcx), + reexported_vis: &reexported_at_vis.to_string(def_id, self.tcx), }, ); } @@ -1741,7 +1700,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> { // Subitems of trait impls have inherited publicity. DefKind::Impl { .. } => { let item = tcx.hir().item(id); - if let hir::ItemKind::Impl(ref impl_) = item.kind { + if let hir::ItemKind::Impl(impl_) = item.kind { let impl_vis = ty::Visibility::of_impl::<false>( item.owner_id.def_id, tcx, @@ -1756,7 +1715,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> { // fn from(_: Priv) -> Pub {...} // } // - // lints shouldn't be emmited even if `from` effective visibility + // lints shouldn't be emitted even if `from` effective visibility // is larger than `Priv` nominal visibility and if `Priv` can leak // in some scenarios due to type inference. let impl_ev = EffectiveVisibility::of_impl::<false>( @@ -1806,7 +1765,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> { pub fn provide(providers: &mut Providers) { *providers = Providers { - visibility, effective_visibilities, check_private_in_public, check_mod_privacy, @@ -1814,56 +1772,6 @@ pub fn provide(providers: &mut Providers) { }; } -fn visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility<DefId> { - local_visibility(tcx, def_id).to_def_id() -} - -fn local_visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility { - match tcx.resolutions(()).visibilities.get(&def_id) { - Some(vis) => *vis, - None => { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - match tcx.hir().get(hir_id) { - // Unique types created for closures participate in type privacy checking. - // They have visibilities inherited from the module they are defined in. - Node::Expr(hir::Expr { kind: hir::ExprKind::Closure{..}, .. }) - // - AST lowering creates dummy `use` items which don't - // get their entries in the resolver's visibility table. - // - AST lowering also creates opaque type items with inherited visibilities. - // Visibility on them should have no effect, but to avoid the visibility - // query failing on some items, we provide it for opaque types as well. - | Node::Item(hir::Item { - kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) - | hir::ItemKind::OpaqueTy(..), - .. - }) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_local_def_id()), - // Visibilities of trait impl items are inherited from their traits - // and are not filled in resolve. - Node::ImplItem(impl_item) => { - match tcx.hir().get_by_def_id(tcx.hir().get_parent_item(hir_id).def_id) { - Node::Item(hir::Item { - kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }), - .. - }) => tr.path.res.opt_def_id().map_or_else( - || { - tcx.sess.delay_span_bug(tr.path.span, "trait without a def-id"); - ty::Visibility::Public - }, - |def_id| tcx.visibility(def_id).expect_local(), - ), - _ => span_bug!(impl_item.span, "the parent is not a trait impl"), - } - } - _ => span_bug!( - tcx.def_span(def_id), - "visibility table unexpectedly missing a def-id: {:?}", - def_id, - ), - } - } - } -} - fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { // Check privacy of names not checked in previous compilation stages. let mut visitor = NamePrivacyVisitor { |