Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Eagerly feed closure visibilities #119136

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{ConstArg, GenericArg, ItemLocalMap, ParamName, TraitCandidate};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::span_bug;
use rustc_middle::ty::{ResolverAstLowering, TyCtxt, Visibility};
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use rustc_session::parse::{add_feature_diagnostics, feature_err};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
Expand Down Expand Up @@ -1651,10 +1651,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
);
debug!(?opaque_ty_def_id);

// Meaningless, but provided so that all items have visibilities.
let parent_mod = self.tcx.parent_module_from_def_id(opaque_ty_def_id).to_def_id();
self.tcx.feed_local_def_id(opaque_ty_def_id).visibility(Visibility::Restricted(parent_mod));

// Map from captured (old) lifetime to synthetic (new) lifetime.
// Used to resolve lifetimes in the bounds of the opaque.
let mut captured_to_synthesized_mapping = LocalDefIdMap::default();
Expand Down
25 changes: 21 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,17 +1039,34 @@ impl<'tcx> TyCtxtAt<'tcx> {
// This is fine because:
// - those queries are `eval_always` so we won't miss their result changing;
// - this write will have happened before these queries are called.
let data = def_kind.def_path_data(name);
let key = self.untracked.definitions.write().create_def(parent, data);
let def_id = self.tcx.create_def(parent, name, def_kind);

let feed = TyCtxtFeed { tcx: self.tcx, key };
feed.def_kind(def_kind);
let feed = self.tcx.feed_local_def_id(def_id);
feed.def_span(self.span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this span not simply passed to create_def?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This span is not available in resolver right now, it will be passed when the FIXME (#119136 (comment)) is addressed.

feed
}
}

impl<'tcx> TyCtxt<'tcx> {
/// `tcx`-dependent operations performed for every created definition.
pub fn create_def(self, parent: LocalDefId, name: Symbol, def_kind: DefKind) -> LocalDefId {
let data = def_kind.def_path_data(name);
let def_id = self.untracked.definitions.write().create_def(parent, data);

let feed = self.feed_local_def_id(def_id);
feed.def_kind(def_kind);
// Unique types created for closures participate in type privacy checking.
// They have visibilities inherited from the module they are defined in.
// Visibilities for opaque types are meaningless, but still provided
// so that all items have visibilities.
if matches!(def_kind, DefKind::Closure | DefKind::OpaqueTy) {
let parent_mod = self.parent_module_from_def_id(def_id).to_def_id();
feed.visibility(ty::Visibility::Restricted(parent_mod));
}

def_id
}

pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
// Create a dependency to the red node to be sure we re-execute this when the amount of
// definitions change.
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,15 +1765,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> {

pub fn provide(providers: &mut Providers) {
*providers = Providers {
visibility: |tcx, def_id| {
// Unique types created for closures participate in type privacy checking.
// They have visibilities inherited from the module they are defined in.
// FIXME: Consider evaluating visibilities for closures eagerly, like for all
// other nodes. However, unlike for others, for closures it may cause a perf
// regression, because closure visibilities are not commonly queried.
assert_eq!(tcx.def_kind(def_id), DefKind::Closure);
ty::Visibility::Restricted(tcx.parent_module_from_def_id(def_id).to_def_id())
},
effective_visibilities,
check_private_in_public,
check_mod_privacy,
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
);

// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fixme up to date?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, #118633 is trying to address it, but there are some complications.

let def_id = self.tcx.untracked().definitions.write().create_def(parent, data);

let feed = self.tcx.feed_local_def_id(def_id);
feed.def_kind(def_kind);
let def_id = self.tcx.create_def(parent, name, def_kind);

// Create the definition.
if expn_id != ExpnId::root() {
Expand Down
Loading