Skip to content

Commit 954cd79

Browse files
committed
Move GlobalCtxt::finish to TyCtxt
This allows us to call GlobalCtxt::finish exactly once.
1 parent 8780253 commit 954cd79

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

compiler/rustc_interface/src/passes.rs

+41-41
Original file line numberDiff line numberDiff line change
@@ -718,19 +718,17 @@ pub fn create_and_enter_global_ctxt<T>(
718718
let arena = WorkerLocal::new(|_| Arena::default());
719719
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
720720

721-
let gcx = create_global_ctxt(compiler, krate, &gcx_cell, &arena, &hir_arena);
722-
let ret = gcx.enter(f);
723-
gcx.finish();
724-
ret
721+
create_and_enter_global_ctxt_inner(compiler, krate, &gcx_cell, &arena, &hir_arena, f)
725722
}
726723

727-
fn create_global_ctxt<'tcx>(
724+
fn create_and_enter_global_ctxt_inner<'tcx, T>(
728725
compiler: &'tcx Compiler,
729726
mut krate: rustc_ast::Crate,
730727
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
731728
arena: &'tcx WorkerLocal<Arena<'tcx>>,
732729
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
733-
) -> &'tcx GlobalCtxt<'tcx> {
730+
f: impl FnOnce(TyCtxt<'tcx>) -> T,
731+
) -> T {
734732
let sess = &compiler.sess;
735733

736734
rustc_builtin_macros::cmdline_attrs::inject(
@@ -778,43 +776,45 @@ fn create_global_ctxt<'tcx>(
778776

779777
let incremental = dep_graph.is_fully_enabled();
780778

781-
sess.time("setup_global_ctxt", || {
782-
let qcx = gcx_cell.get_or_init(move || {
783-
TyCtxt::create_global_ctxt(
784-
sess,
785-
crate_types,
786-
stable_crate_id,
787-
arena,
788-
hir_arena,
789-
untracked,
790-
dep_graph,
791-
rustc_query_impl::query_callbacks(arena),
792-
rustc_query_impl::query_system(
793-
providers.queries,
794-
providers.extern_queries,
795-
query_result_on_disk_cache,
796-
incremental,
797-
),
798-
providers.hooks,
799-
compiler.current_gcx.clone(),
800-
)
801-
});
779+
let qcx = gcx_cell.get_or_init(move || {
780+
TyCtxt::create_global_ctxt(
781+
sess,
782+
crate_types,
783+
stable_crate_id,
784+
arena,
785+
hir_arena,
786+
untracked,
787+
dep_graph,
788+
rustc_query_impl::query_callbacks(arena),
789+
rustc_query_impl::query_system(
790+
providers.queries,
791+
providers.extern_queries,
792+
query_result_on_disk_cache,
793+
incremental,
794+
),
795+
providers.hooks,
796+
compiler.current_gcx.clone(),
797+
)
798+
});
802799

803-
qcx.enter(|tcx| {
804-
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
805-
assert_eq!(feed.key(), LOCAL_CRATE);
806-
feed.crate_name(crate_name);
800+
qcx.enter(|tcx| {
801+
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
802+
assert_eq!(feed.key(), LOCAL_CRATE);
803+
feed.crate_name(crate_name);
807804

808-
let feed = tcx.feed_unit_query();
809-
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
810-
sess,
811-
&pre_configured_attrs,
812-
crate_name,
813-
)));
814-
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
815-
feed.output_filenames(Arc::new(outputs));
816-
});
817-
qcx
805+
let feed = tcx.feed_unit_query();
806+
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
807+
sess,
808+
&pre_configured_attrs,
809+
crate_name,
810+
)));
811+
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
812+
feed.output_filenames(Arc::new(outputs));
813+
814+
let res = f(tcx);
815+
// FIXME maybe run finish even when a fatal error occured? or at least tcx.alloc_self_profile_query_strings()?
816+
tcx.finish();
817+
res
818818
})
819819
}
820820

compiler/rustc_middle/src/ty/context.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1374,19 +1374,6 @@ impl<'tcx> GlobalCtxt<'tcx> {
13741374

13751375
tls::enter_context(&icx, || f(icx.tcx))
13761376
}
1377-
1378-
pub fn finish(&'tcx self) {
1379-
// We assume that no queries are run past here. If there are new queries
1380-
// after this point, they'll show up as "<unknown>" in self-profiling data.
1381-
self.enter(|tcx| tcx.alloc_self_profile_query_strings());
1382-
1383-
self.enter(|tcx| tcx.save_dep_graph());
1384-
self.enter(|tcx| tcx.query_key_hash_verify_all());
1385-
1386-
if let Err((path, error)) = self.dep_graph.finish_encoding() {
1387-
self.sess.dcx().emit_fatal(crate::error::FailedWritingFile { path: &path, error });
1388-
}
1389-
}
13901377
}
13911378

13921379
/// This is used to get a reference to a `GlobalCtxt` if one is available.
@@ -2120,6 +2107,19 @@ impl<'tcx> TyCtxt<'tcx> {
21202107
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
21212108
self.hir().expect_opaque_ty(def_id).origin
21222109
}
2110+
2111+
pub fn finish(self) {
2112+
// We assume that no queries are run past here. If there are new queries
2113+
// after this point, they'll show up as "<unknown>" in self-profiling data.
2114+
self.alloc_self_profile_query_strings();
2115+
2116+
self.save_dep_graph();
2117+
self.query_key_hash_verify_all();
2118+
2119+
if let Err((path, error)) = self.dep_graph.finish_encoding() {
2120+
self.sess.dcx().emit_fatal(crate::error::FailedWritingFile { path: &path, error });
2121+
}
2122+
}
21232123
}
21242124

21252125
macro_rules! nop_lift {

0 commit comments

Comments
 (0)