Skip to content

Commit 8fca829

Browse files
committed
Auto merge of rust-lang#119136 - petrochenkov:feedvis3, r=WaffleLapkin
resolve: Eagerly feed closure visibilities Also factor out all tcx-dependent operations performed for every created definition into `TyCtxt::create_def`. Addresses rust-lang#118657 (comment)
2 parents f9d52dc + 5e5d82e commit 8fca829

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
6060
use rustc_hir::{ConstArg, GenericArg, ItemLocalMap, ParamName, TraitCandidate};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
6262
use rustc_middle::span_bug;
63-
use rustc_middle::ty::{ResolverAstLowering, TyCtxt, Visibility};
63+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6464
use rustc_session::parse::{add_feature_diagnostics, feature_err};
6565
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6666
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
@@ -1651,10 +1651,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16511651
);
16521652
debug!(?opaque_ty_def_id);
16531653

1654-
// Meaningless, but provided so that all items have visibilities.
1655-
let parent_mod = self.tcx.parent_module_from_def_id(opaque_ty_def_id).to_def_id();
1656-
self.tcx.feed_local_def_id(opaque_ty_def_id).visibility(Visibility::Restricted(parent_mod));
1657-
16581654
// Map from captured (old) lifetime to synthetic (new) lifetime.
16591655
// Used to resolve lifetimes in the bounds of the opaque.
16601656
let mut captured_to_synthesized_mapping = LocalDefIdMap::default();

compiler/rustc_middle/src/ty/context.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1039,17 +1039,34 @@ impl<'tcx> TyCtxtAt<'tcx> {
10391039
// This is fine because:
10401040
// - those queries are `eval_always` so we won't miss their result changing;
10411041
// - this write will have happened before these queries are called.
1042-
let data = def_kind.def_path_data(name);
1043-
let key = self.untracked.definitions.write().create_def(parent, data);
1042+
let def_id = self.tcx.create_def(parent, name, def_kind);
10441043

1045-
let feed = TyCtxtFeed { tcx: self.tcx, key };
1046-
feed.def_kind(def_kind);
1044+
let feed = self.tcx.feed_local_def_id(def_id);
10471045
feed.def_span(self.span);
10481046
feed
10491047
}
10501048
}
10511049

10521050
impl<'tcx> TyCtxt<'tcx> {
1051+
/// `tcx`-dependent operations performed for every created definition.
1052+
pub fn create_def(self, parent: LocalDefId, name: Symbol, def_kind: DefKind) -> LocalDefId {
1053+
let data = def_kind.def_path_data(name);
1054+
let def_id = self.untracked.definitions.write().create_def(parent, data);
1055+
1056+
let feed = self.feed_local_def_id(def_id);
1057+
feed.def_kind(def_kind);
1058+
// Unique types created for closures participate in type privacy checking.
1059+
// They have visibilities inherited from the module they are defined in.
1060+
// Visibilities for opaque types are meaningless, but still provided
1061+
// so that all items have visibilities.
1062+
if matches!(def_kind, DefKind::Closure | DefKind::OpaqueTy) {
1063+
let parent_mod = self.parent_module_from_def_id(def_id).to_def_id();
1064+
feed.visibility(ty::Visibility::Restricted(parent_mod));
1065+
}
1066+
1067+
def_id
1068+
}
1069+
10531070
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
10541071
// Create a dependency to the red node to be sure we re-execute this when the amount of
10551072
// definitions change.

compiler/rustc_privacy/src/lib.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1765,15 +1765,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> {
17651765

17661766
pub fn provide(providers: &mut Providers) {
17671767
*providers = Providers {
1768-
visibility: |tcx, def_id| {
1769-
// Unique types created for closures participate in type privacy checking.
1770-
// They have visibilities inherited from the module they are defined in.
1771-
// FIXME: Consider evaluating visibilities for closures eagerly, like for all
1772-
// other nodes. However, unlike for others, for closures it may cause a perf
1773-
// regression, because closure visibilities are not commonly queried.
1774-
assert_eq!(tcx.def_kind(def_id), DefKind::Closure);
1775-
ty::Visibility::Restricted(tcx.parent_module_from_def_id(def_id).to_def_id())
1776-
},
17771768
effective_visibilities,
17781769
check_private_in_public,
17791770
check_mod_privacy,

compiler/rustc_resolve/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1225,10 +1225,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
12251225
);
12261226

12271227
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
1228-
let def_id = self.tcx.untracked().definitions.write().create_def(parent, data);
1229-
1230-
let feed = self.tcx.feed_local_def_id(def_id);
1231-
feed.def_kind(def_kind);
1228+
let def_id = self.tcx.create_def(parent, name, def_kind);
12321229

12331230
// Create the definition.
12341231
if expn_id != ExpnId::root() {

0 commit comments

Comments
 (0)