Skip to content

Commit 0144d6a

Browse files
committed
Do not hold query key in Query.
1 parent f96e960 commit 0144d6a

File tree

4 files changed

+69
-58
lines changed

4 files changed

+69
-58
lines changed

compiler/rustc_query_impl/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ use rustc_middle::dep_graph;
2626
use rustc_middle::ich::StableHashingContext;
2727
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
2828
use rustc_middle::ty::query::{Providers, QueryEngine};
29-
use rustc_middle::ty::TyCtxt;
29+
use rustc_middle::ty::{self, TyCtxt};
3030
use rustc_serialize::opaque;
3131
use rustc_span::{Span, DUMMY_SP};
32-
use std::mem;
3332

3433
#[macro_use]
3534
mod plumbing;

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl HasDepContext for QueryCtxt<'tcx> {
4545
}
4646

4747
impl QueryContext for QueryCtxt<'tcx> {
48-
type Query = Query<'tcx>;
48+
type Query = Query;
4949

5050
fn def_path_str(&self, def_id: DefId) -> String {
5151
self.tcx.def_path_str(def_id)
@@ -59,7 +59,7 @@ impl QueryContext for QueryCtxt<'tcx> {
5959
&self,
6060
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
6161
{
62-
self.queries.try_collect_active_jobs()
62+
self.queries.try_collect_active_jobs(**self)
6363
}
6464

6565
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
@@ -185,12 +185,12 @@ impl<'tcx> QueryCtxt<'tcx> {
185185
#[cold]
186186
pub(super) fn report_cycle(
187187
self,
188-
CycleError { usage, cycle: stack }: CycleError<Query<'tcx>>,
188+
CycleError { usage, cycle: stack }: CycleError<Query>,
189189
) -> DiagnosticBuilder<'tcx> {
190190
assert!(!stack.is_empty());
191191

192-
let fix_span = |span: Span, query: &Query<'tcx>| {
193-
self.sess.source_map().guess_head_span(query.default_span(*self, span))
192+
let fix_span = |span: Span, query: &Query| {
193+
self.sess.source_map().guess_head_span(query.default_span(span))
194194
};
195195

196196
// Disable naming impls with types in this path, since that
@@ -204,24 +204,24 @@ impl<'tcx> QueryCtxt<'tcx> {
204204
span,
205205
E0391,
206206
"cycle detected when {}",
207-
stack[0].query.describe(self)
207+
stack[0].query.description
208208
);
209209

210210
for i in 1..stack.len() {
211211
let query = &stack[i].query;
212212
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
213-
err.span_note(span, &format!("...which requires {}...", query.describe(self)));
213+
err.span_note(span, &format!("...which requires {}...", query.description));
214214
}
215215

216216
err.note(&format!(
217217
"...which again requires {}, completing the cycle",
218-
stack[0].query.describe(self)
218+
stack[0].query.description
219219
));
220220

221221
if let Some((span, query)) = usage {
222222
err.span_note(
223223
fix_span(span, &query),
224-
&format!("cycle used when {}", query.describe(self)),
224+
&format!("cycle used when {}", query.description),
225225
);
226226
}
227227

@@ -371,54 +371,58 @@ macro_rules! define_queries {
371371
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
372372
}
373373

374-
#[allow(nonstandard_style)]
375374
#[derive(Clone, Debug)]
376-
pub enum Query<$tcx> {
377-
$($(#[$attr])* $name(query_keys::$name<$tcx>)),*
375+
pub struct Query {
376+
pub name: &'static str,
377+
hash: Fingerprint,
378+
description: String,
379+
span: Option<Span>,
378380
}
379381

380-
impl<$tcx> Query<$tcx> {
381-
pub fn name(&self) -> &'static str {
382-
match *self {
383-
$(Query::$name(_) => stringify!($name),)*
384-
}
385-
}
386-
387-
pub(crate) fn describe(&self, tcx: QueryCtxt<$tcx>) -> String {
388-
let (r, name) = match *self {
389-
$(Query::$name(key) => {
390-
(queries::$name::describe(tcx, key), stringify!($name))
391-
})*
382+
impl Query {
383+
$(#[allow(nonstandard_style)] $(#[$attr])*
384+
pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> Self {
385+
let kind = dep_graph::DepKind::$name;
386+
let name = stringify!($name);
387+
let description = ty::print::with_forced_impl_filename_line(
388+
// Force filename-line mode to avoid invoking `type_of` query.
389+
|| queries::$name::describe(tcx, key)
390+
);
391+
let description = if tcx.sess.verbose() {
392+
format!("{} [{}]", description, name)
393+
} else {
394+
description
392395
};
393-
if tcx.sess.verbose() {
394-
format!("{} [{}]", r, name)
396+
let span = if kind == dep_graph::DepKind::def_span {
397+
// The `def_span` query is used to calculate `default_span`,
398+
// so exit to avoid infinite recursion.
399+
None
395400
} else {
396-
r
397-
}
398-
}
401+
Some(key.default_span(*tcx))
402+
};
403+
let hash = {
404+
let mut hcx = tcx.create_stable_hashing_context();
405+
let mut hasher = StableHasher::new();
406+
std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
407+
key.hash_stable(&mut hcx, &mut hasher);
408+
hasher.finish()
409+
};
410+
411+
Self { name, description, span, hash }
412+
})*
399413

400414
// FIXME(eddyb) Get more valid `Span`s on queries.
401-
pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span {
415+
pub fn default_span(&self, span: Span) -> Span {
402416
if !span.is_dummy() {
403417
return span;
404418
}
405-
// The `def_span` query is used to calculate `default_span`,
406-
// so exit to avoid infinite recursion.
407-
if let Query::def_span(..) = *self {
408-
return span
409-
}
410-
match *self {
411-
$(Query::$name(key) => key.default_span(tcx),)*
412-
}
419+
self.span.unwrap_or(span)
413420
}
414421
}
415422

416-
impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
423+
impl<'a> HashStable<StableHashingContext<'a>> for Query {
417424
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
418-
mem::discriminant(self).hash_stable(hcx, hasher);
419-
match *self {
420-
$(Query::$name(key) => key.hash_stable(hcx, hasher),)*
421-
}
425+
self.hash.hash_stable(hcx, hasher)
422426
}
423427
}
424428

@@ -446,7 +450,9 @@ macro_rules! define_queries {
446450
type Cache = query_storage::$name<$tcx>;
447451

448452
#[inline(always)]
449-
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> {
453+
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query, Self::Key>
454+
where QueryCtxt<$tcx>: 'a
455+
{
450456
&tcx.queries.$name
451457
}
452458

@@ -478,7 +484,7 @@ macro_rules! define_queries {
478484

479485
fn handle_cycle_error(
480486
tcx: QueryCtxt<'tcx>,
481-
error: CycleError<Query<'tcx>>
487+
error: CycleError<Query>
482488
) -> Self::Value {
483489
handle_cycle_error!([$($modifiers)*][tcx, error])
484490
}
@@ -581,7 +587,7 @@ macro_rules! define_queries_struct {
581587

582588
$($(#[$attr])* $name: QueryState<
583589
crate::dep_graph::DepKind,
584-
Query<$tcx>,
590+
Query,
585591
query_keys::$name<$tcx>,
586592
>,)*
587593
}
@@ -599,13 +605,16 @@ macro_rules! define_queries_struct {
599605
}
600606

601607
pub(crate) fn try_collect_active_jobs(
602-
&self
603-
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, Query<$tcx>>>> {
608+
&$tcx self,
609+
tcx: TyCtxt<$tcx>,
610+
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, Query>>> {
611+
let tcx = QueryCtxt { tcx, queries: self };
604612
let mut jobs = FxHashMap::default();
605613

606614
$(
607615
self.$name.try_collect_active_jobs(
608-
<queries::$name<'tcx> as QueryAccessors<QueryCtxt<'tcx>>>::DEP_KIND,
616+
tcx,
617+
dep_graph::DepKind::$name,
609618
Query::$name,
610619
&mut jobs,
611620
)?;
@@ -651,7 +660,7 @@ macro_rules! define_queries_struct {
651660
handler: &Handler,
652661
num_frames: Option<usize>,
653662
) -> usize {
654-
let query_map = self.try_collect_active_jobs();
663+
let query_map = self.try_collect_active_jobs(tcx);
655664

656665
let mut current_query = query;
657666
let mut i = 0;
@@ -671,8 +680,8 @@ macro_rules! define_queries_struct {
671680
&format!(
672681
"#{} [{}] {}",
673682
i,
674-
query_info.info.query.name(),
675-
query_info.info.query.describe(QueryCtxt { tcx, queries: self })
683+
query_info.info.query.name,
684+
query_info.info.query.description,
676685
),
677686
);
678687
diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into();

compiler/rustc_query_system/src/query/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
7373
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
7474

7575
// Don't use this method to access query results, instead use the methods on TyCtxt
76-
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, CTX::Query, Self::Key>;
76+
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, CTX::Query, Self::Key>
77+
where
78+
CTX: 'a;
7779

7880
// Don't use this method to access query results, instead use the methods on TyCtxt
7981
fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ where
119119
shards.iter().all(|shard| shard.active.is_empty())
120120
}
121121

122-
pub fn try_collect_active_jobs(
122+
pub fn try_collect_active_jobs<CTX: Copy>(
123123
&self,
124+
tcx: CTX,
124125
kind: D,
125-
make_query: fn(K) -> Q,
126+
make_query: fn(CTX, K) -> Q,
126127
jobs: &mut QueryMap<D, Q>,
127128
) -> Option<()> {
128129
// We use try_lock_shards here since we are called from the
@@ -133,7 +134,7 @@ where
133134
shard.active.iter().filter_map(move |(k, v)| {
134135
if let QueryResult::Started(ref job) = *v {
135136
let id = QueryJobId::new(job.id, shard_id, kind);
136-
let info = QueryInfo { span: job.span, query: make_query(k.clone()) };
137+
let info = QueryInfo { span: job.span, query: make_query(tcx, k.clone()) };
137138
Some((id, QueryJobInfo { info, job: job.clone() }))
138139
} else {
139140
None

0 commit comments

Comments
 (0)