Skip to content
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
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R)
icx.tcx.dep_graph.record_diagnostic(icx.tcx, &diagnostic);

// Diagnostics are tracked, we can ignore the dependency.
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..*icx };
tls::enter_context(&icx, move || (*f)(diagnostic))
} else {
// In any other case, invoke diagnostics anyway.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
OP: FnOnce() -> R,
{
ty::tls::with_context(|icx| {
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
let icx = ty::tls::ImplicitCtxt { task_deps, ..*icx };
ty::tls::enter_context(&icx, op)
})
}
Expand Down
26 changes: 0 additions & 26 deletions compiler/rustc_middle/src/ty/context/tls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{mem, ptr};

use rustc_data_structures::sync;

use super::{GlobalCtxt, TyCtxt};
Expand All @@ -11,7 +9,6 @@ use crate::query::QueryJobId;
/// executing a new query. Whenever there's a `TyCtxt` value available
/// you should also have access to an `ImplicitCtxt` through the functions
/// in this module.
#[derive(Clone)]
pub struct ImplicitCtxt<'a, 'tcx> {
/// The current `TyCtxt`.
pub tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -89,29 +86,6 @@ where
with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls")))
}

/// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument
/// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime
/// as the `TyCtxt` passed in.
/// This will panic if you pass it a `TyCtxt` which is different from the current
/// `ImplicitCtxt`'s `tcx` field.
#[inline]
pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R
where
F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R,
{
with_context(|context| {
// The two gcx have different invariant lifetimes, so we need to erase them for the comparison.
assert!(ptr::eq(
context.tcx.gcx as *const _ as *const (),
tcx.gcx as *const _ as *const ()
));

let context: &ImplicitCtxt<'_, '_> = unsafe { mem::transmute(context) };

f(context)
})
}

/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
/// Panics if there is no `ImplicitCtxt` available.
#[inline]
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_query_impl/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn cycle_error<'tcx, C: QueryCache>(
.ok()
.expect("failed to collect active queries");

let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(tcx), span);
let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(), span);
(mk_cycle(query, tcx, error.lift()), None)
}

Expand Down Expand Up @@ -305,7 +305,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
}
}

let current_job_id = current_query_job(tcx);
let current_job_id = current_query_job();

match state_lock.entry(key_hash, equivalent_key(&key), |(k, _)| sharded::make_hash(k)) {
Entry::Vacant(entry) => {
Expand Down Expand Up @@ -422,8 +422,7 @@ fn execute_job_non_incr<'tcx, C: QueryCache>(

let prof_timer = tcx.prof.query_provider();
// Call the query provider.
let value =
start_query(tcx, job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
let value = start_query(job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
prof_timer.finish_with_query_invocation_id(dep_node_index.into());

Expand Down Expand Up @@ -457,7 +456,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(

// The diagnostics for this query will be promoted to the current session during
// `try_mark_green()`, so we can ignore them here.
if let Some(ret) = start_query(tcx, job_id, false, || try {
if let Some(ret) = start_query(job_id, false, || try {
let (prev_index, dep_node_index) = dep_graph_data.try_mark_green(tcx, dep_node)?;
let value = load_from_disk_or_invoke_provider_green(
tcx,
Expand All @@ -476,7 +475,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(

let prof_timer = tcx.prof.query_provider();

let (result, dep_node_index) = start_query(tcx, job_id, query.depth_limit, || {
let (result, dep_node_index) = start_query(job_id, query.depth_limit, || {
if query.anon {
// Call the query provider inside an anon task.
return dep_graph_data.with_anon_task_inner(tcx, query.dep_kind, || {
Expand Down
32 changes: 13 additions & 19 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,31 @@ pub(crate) fn next_job_id<'tcx>(tcx: TyCtxt<'tcx>) -> QueryJobId {
}

#[inline]
pub(crate) fn current_query_job<'tcx>(tcx: TyCtxt<'tcx>) -> Option<QueryJobId> {
tls::with_related_context(tcx, |icx| icx.query)
pub(crate) fn current_query_job() -> Option<QueryJobId> {
tls::with_context(|icx| icx.query)
}

/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes.
/// Executes a job by changing the `ImplicitCtxt` to point to the new query job while it executes.
#[inline(always)]
pub(crate) fn start_query<'tcx, R>(
tcx: TyCtxt<'tcx>,
token: QueryJobId,
pub(crate) fn start_query<R>(
Comment on lines -69 to +70
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remark: I've been meaning to rename this function at some point, because I find start_query pretty unhelpful for indicating what it actually does and doesn't do.

job_id: QueryJobId,
depth_limit: bool,
compute: impl FnOnce() -> R,
) -> R {
// The `TyCtxt` stored in TLS has the same global interner lifetime
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
// when accessing the `ImplicitCtxt`.
tls::with_related_context(tcx, move |current_icx| {
if depth_limit && !tcx.recursion_limit().value_within_limit(current_icx.query_depth) {
depth_limit_error(tcx, token);
tls::with_context(move |icx| {
if depth_limit && !icx.tcx.recursion_limit().value_within_limit(icx.query_depth) {
depth_limit_error(icx.tcx, job_id);
}

// Update the `ImplicitCtxt` to point to our new query job.
let new_icx = ImplicitCtxt {
tcx,
query: Some(token),
query_depth: current_icx.query_depth + depth_limit as usize,
task_deps: current_icx.task_deps,
let icx = ImplicitCtxt {
query: Some(job_id),
query_depth: icx.query_depth + if depth_limit { 1 } else { 0 },
..*icx
};

// Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&new_icx, compute)
tls::enter_context(&icx, compute)
})
}

Expand Down
Loading