Skip to content

Commit 11e1280

Browse files
authored
Rollup merge of #107831 - nnethercote:query-refactoring, r=oli-obk
Query refactoring Just some cleanups I found when learning about the query system. Best reviewed one commit at a time. r? `@oli-obk`
2 parents 8fc9ed5 + 243944c commit 11e1280

File tree

12 files changed

+49
-55
lines changed

12 files changed

+49
-55
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
5454

5555
trace!(
5656
"eval_body_using_ecx: pushing stack frame for global: {}{}",
57-
with_no_trimmed_paths!(ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id()))),
57+
with_no_trimmed_paths!(ecx.tcx.def_path_str(cid.instance.def_id())),
5858
cid.promoted.map_or_else(String::new, |p| format!("::promoted[{:?}]", p))
5959
);
6060

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,10 @@ impl CanonicalizeMode for CanonicalizeQueryResponse {
203203
// rust-lang/rust#57464: `impl Trait` can leak local
204204
// scopes (in manner violating typeck). Therefore, use
205205
// `delay_span_bug` to allow type error over an ICE.
206-
ty::tls::with(|tcx| {
207-
tcx.sess.delay_span_bug(
208-
rustc_span::DUMMY_SP,
209-
&format!("unexpected region in query response: `{:?}`", r),
210-
);
211-
});
206+
canonicalizer.tcx.sess.delay_span_bug(
207+
rustc_span::DUMMY_SP,
208+
&format!("unexpected region in query response: `{:?}`", r),
209+
);
212210
r
213211
}
214212
}

compiler/rustc_interface/src/callbacks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn track_diagnostic(diagnostic: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnost
3838

3939
// Diagnostics are tracked, we can ignore the dependency.
4040
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
41-
return tls::enter_context(&icx, move |_| (*f)(diagnostic));
41+
return tls::enter_context(&icx, move || (*f)(diagnostic));
4242
}
4343

4444
// In any other case, invoke diagnostics anyway.

compiler/rustc_interface/src/passes.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -738,30 +738,16 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock<ExternProviders> = LazyLock:
738738
extern_providers
739739
});
740740

741-
pub struct QueryContext<'tcx> {
742-
gcx: &'tcx GlobalCtxt<'tcx>,
743-
}
744-
745-
impl<'tcx> QueryContext<'tcx> {
746-
pub fn enter<F, R>(&mut self, f: F) -> R
747-
where
748-
F: FnOnce(TyCtxt<'tcx>) -> R,
749-
{
750-
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
751-
ty::tls::enter_context(&icx, |_| f(icx.tcx))
752-
}
753-
}
754-
755741
pub fn create_global_ctxt<'tcx>(
756742
compiler: &'tcx Compiler,
757743
lint_store: Lrc<LintStore>,
758744
dep_graph: DepGraph,
759745
untracked: Untracked,
760746
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
761-
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
747+
gcx_cell: &'tcx OnceCell<GlobalCtxt<'tcx>>,
762748
arena: &'tcx WorkerLocal<Arena<'tcx>>,
763749
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
764-
) -> QueryContext<'tcx> {
750+
) -> &'tcx GlobalCtxt<'tcx> {
765751
// We're constructing the HIR here; we don't care what we will
766752
// read, since we haven't even constructed the *input* to
767753
// incr. comp. yet.
@@ -785,8 +771,8 @@ pub fn create_global_ctxt<'tcx>(
785771
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
786772
});
787773

788-
let gcx = sess.time("setup_global_ctxt", || {
789-
global_ctxt.get_or_init(move || {
774+
sess.time("setup_global_ctxt", || {
775+
gcx_cell.get_or_init(move || {
790776
TyCtxt::create_global_ctxt(
791777
sess,
792778
lint_store,
@@ -799,9 +785,7 @@ pub fn create_global_ctxt<'tcx>(
799785
rustc_query_impl::query_callbacks(arena),
800786
)
801787
})
802-
});
803-
804-
QueryContext { gcx }
788+
})
805789
}
806790

807791
/// Runs the resolution, type-checking, region checking and other

compiler/rustc_interface/src/queries.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
22
use crate::interface::{Compiler, Result};
3-
use crate::passes::{self, BoxedResolver, QueryContext};
3+
use crate::passes::{self, BoxedResolver};
44

55
use rustc_ast as ast;
66
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -64,7 +64,7 @@ impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
6464
}
6565
}
6666

67-
impl<'a, 'tcx> QueryResult<'a, QueryContext<'tcx>> {
67+
impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
6868
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
6969
(*self.0).get_mut().enter(f)
7070
}
@@ -78,7 +78,7 @@ impl<T> Default for Query<T> {
7878

7979
pub struct Queries<'tcx> {
8080
compiler: &'tcx Compiler,
81-
gcx: OnceCell<GlobalCtxt<'tcx>>,
81+
gcx_cell: OnceCell<GlobalCtxt<'tcx>>,
8282
queries: OnceCell<TcxQueries<'tcx>>,
8383

8484
arena: WorkerLocal<Arena<'tcx>>,
@@ -90,15 +90,16 @@ pub struct Queries<'tcx> {
9090
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
9191
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
9292
dep_graph: Query<DepGraph>,
93-
global_ctxt: Query<QueryContext<'tcx>>,
93+
// This just points to what's in `gcx_cell`.
94+
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
9495
ongoing_codegen: Query<Box<dyn Any>>,
9596
}
9697

9798
impl<'tcx> Queries<'tcx> {
9899
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
99100
Queries {
100101
compiler,
101-
gcx: OnceCell::new(),
102+
gcx_cell: OnceCell::new(),
102103
queries: OnceCell::new(),
103104
arena: WorkerLocal::new(|_| Arena::default()),
104105
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
@@ -108,7 +109,7 @@ impl<'tcx> Queries<'tcx> {
108109
register_plugins: Default::default(),
109110
expansion: Default::default(),
110111
dep_graph: Default::default(),
111-
global_ctxt: Default::default(),
112+
gcx: Default::default(),
112113
ongoing_codegen: Default::default(),
113114
}
114115
}
@@ -207,8 +208,8 @@ impl<'tcx> Queries<'tcx> {
207208
})
208209
}
209210

210-
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, QueryContext<'tcx>>> {
211-
self.global_ctxt.compute(|| {
211+
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
212+
self.gcx.compute(|| {
212213
let crate_name = *self.crate_name()?.borrow();
213214
let (krate, resolver, lint_store) = self.expansion()?.steal();
214215

@@ -218,18 +219,18 @@ impl<'tcx> Queries<'tcx> {
218219
ast_lowering: untracked_resolver_for_lowering,
219220
} = BoxedResolver::to_resolver_outputs(resolver);
220221

221-
let mut qcx = passes::create_global_ctxt(
222+
let gcx = passes::create_global_ctxt(
222223
self.compiler,
223224
lint_store,
224225
self.dep_graph()?.steal(),
225226
untracked,
226227
&self.queries,
227-
&self.gcx,
228+
&self.gcx_cell,
228229
&self.arena,
229230
&self.hir_arena,
230231
);
231232

232-
qcx.enter(|tcx| {
233+
gcx.enter(|tcx| {
233234
let feed = tcx.feed_unit_query();
234235
feed.resolver_for_lowering(
235236
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
@@ -239,7 +240,7 @@ impl<'tcx> Queries<'tcx> {
239240
let feed = tcx.feed_local_crate();
240241
feed.crate_name(crate_name);
241242
});
242-
Ok(qcx)
243+
Ok(gcx)
243244
})
244245
}
245246

@@ -387,7 +388,7 @@ impl Compiler {
387388

388389
// NOTE: intentionally does not compute the global context if it hasn't been built yet,
389390
// since that likely means there was a parse error.
390-
if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() {
391+
if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
391392
let gcx = gcx.get_mut();
392393
// We assume that no queries are run past here. If there are new queries
393394
// after this point, they'll show up as "<unknown>" in self-profiling data.

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
5555
ty::tls::with_context(|icx| {
5656
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
5757

58-
ty::tls::enter_context(&icx, |_| op())
58+
ty::tls::enter_context(&icx, op)
5959
})
6060
}
6161

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(get_mut_unchecked)]
3535
#![feature(if_let_guard)]
3636
#![feature(iter_from_generator)]
37+
#![feature(local_key_cell_methods)]
3738
#![feature(negative_impls)]
3839
#![feature(never_type)]
3940
#![feature(extern_types)]

compiler/rustc_middle/src/ty/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ pub struct GlobalCtxt<'tcx> {
468468
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
469469
}
470470

471+
impl<'tcx> GlobalCtxt<'tcx> {
472+
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
473+
/// `f`.
474+
pub fn enter<'a: 'tcx, F, R>(&'a self, f: F) -> R
475+
where
476+
F: FnOnce(TyCtxt<'tcx>) -> R,
477+
{
478+
let icx = tls::ImplicitCtxt::new(self);
479+
tls::enter_context(&icx, || f(icx.tcx))
480+
}
481+
}
482+
471483
impl<'tcx> TyCtxt<'tcx> {
472484
/// Expects a body and returns its codegen attributes.
473485
///

compiler/rustc_middle/src/ty/context/tls.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ mod tlv {
8989
/// This is used to set the pointer to the new `ImplicitCtxt`.
9090
#[inline]
9191
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
92-
let old = get_tlv();
93-
let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old)));
94-
TLV.with(|tlv| tlv.set(value));
92+
let old = TLV.replace(value);
93+
let _reset = rustc_data_structures::OnDrop(move || TLV.set(old));
9594
f()
9695
}
9796
}
@@ -110,9 +109,9 @@ unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> {
110109
#[inline]
111110
pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R
112111
where
113-
F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
112+
F: FnOnce() -> R,
114113
{
115-
tlv::with_tlv(erase(context), || f(&context))
114+
tlv::with_tlv(erase(context), f)
116115
}
117116

118117
/// Allows access to the current `ImplicitCtxt` in a closure if one is available.

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl QueryContext for QueryCtxt<'_> {
124124
};
125125

126126
// Use the `ImplicitCtxt` while we execute the query.
127-
tls::enter_context(&new_icx, |_| {
127+
tls::enter_context(&new_icx, || {
128128
rustc_data_structures::stack::ensure_sufficient_stack(compute)
129129
})
130130
})

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl<'tcx, 'a> GeneratorData<'tcx, 'a> {
9898
// obligation
9999
fn get_from_await_ty<F>(
100100
&self,
101+
tcx: TyCtxt<'tcx>,
101102
visitor: AwaitsVisitor,
102103
hir: map::Map<'tcx>,
103104
ty_matches: F,
@@ -134,9 +135,7 @@ impl<'tcx, 'a> GeneratorData<'tcx, 'a> {
134135
.unwrap_or_else(|| {
135136
bug!(
136137
"node_type: no type for node {}",
137-
ty::tls::with(|tcx| tcx
138-
.hir()
139-
.node_to_string(await_expr.hir_id))
138+
tcx.hir().node_to_string(await_expr.hir_id)
140139
)
141140
})
142141
},
@@ -2351,7 +2350,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23512350

23522351
let mut interior_or_upvar_span = None;
23532352

2354-
let from_awaited_ty = generator_data.get_from_await_ty(visitor, hir, ty_matches);
2353+
let from_awaited_ty = generator_data.get_from_await_ty(self.tcx, visitor, hir, ty_matches);
23552354
debug!(?from_awaited_ty);
23562355

23572356
// The generator interior types share the same binders

src/librustdoc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,9 @@ fn main_args(at_args: &[String]) -> MainResult {
814814
sess.fatal("Compilation failed, aborting rustdoc");
815815
}
816816

817-
let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess);
817+
let mut gcx = abort_on_err(queries.global_ctxt(), sess);
818818

819-
global_ctxt.enter(|tcx| {
819+
gcx.enter(|tcx| {
820820
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
821821
core::run_global_ctxt(
822822
tcx,

0 commit comments

Comments
 (0)