Skip to content

Commit a916ff6

Browse files
authored
Rollup merge of rust-lang#62104 - Zoxc:query-info, r=eddyb
Inform the query system about properties of queries at compile time
2 parents 11b4093 + 11221d1 commit a916ff6

File tree

6 files changed

+61
-32
lines changed

6 files changed

+61
-32
lines changed

src/librustc/dep_graph/dep_node.rs

-4
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ macro_rules! define_dep_nodes {
142142
}
143143
}
144144

145-
// FIXME: Make `is_anon`, `is_eval_always` and `has_params` properties
146-
// of queries
147-
#[inline(always)]
148145
pub fn is_anon(&self) -> bool {
149146
match *self {
150147
$(
@@ -163,7 +160,6 @@ macro_rules! define_dep_nodes {
163160
}
164161

165162
#[allow(unreachable_code)]
166-
#[inline(always)]
167163
pub fn has_params(&self) -> bool {
168164
match *self {
169165
$(

src/librustc/ty/query/config.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::dep_graph::SerializedDepNodeIndex;
2-
use crate::dep_graph::DepNode;
2+
use crate::dep_graph::{DepKind, DepNode};
33
use crate::hir::def_id::{CrateNum, DefId};
44
use crate::ty::TyCtxt;
55
use crate::ty::query::queries;
@@ -28,13 +28,18 @@ pub trait QueryConfig<'tcx> {
2828
}
2929

3030
pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
31+
const ANON: bool;
32+
const EVAL_ALWAYS: bool;
33+
3134
fn query(key: Self::Key) -> Query<'tcx>;
3235

3336
// Don't use this method to access query results, instead use the methods on TyCtxt
3437
fn query_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Lock<QueryCache<'tcx, Self>>;
3538

3639
fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
3740

41+
fn dep_kind() -> DepKind;
42+
3843
// Don't use this method to compute query results, instead use the methods on TyCtxt
3944
fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
4045

src/librustc/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ pub use self::on_disk_cache::OnDiskCache;
101101
rustc_query_append! { [define_queries!][ <'tcx>
102102
Other {
103103
/// Runs analysis passes on the crate.
104-
[] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,
104+
[eval_always] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,
105105
},
106106
]}

src/librustc/ty/query/plumbing.rs

+45-12
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,13 @@ impl<'tcx> TyCtxt<'tcx> {
376376
return self.force_query_with_job::<Q>(key, job, null_dep_node).0;
377377
}
378378

379-
let dep_node = Q::to_dep_node(self, &key);
380-
381-
if dep_node.kind.is_anon() {
379+
if Q::ANON {
382380
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
383381
self.sess.profiler(|p| p.start_query(Q::NAME));
384382

385383
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
386384
self.start_query(job.job.clone(), diagnostics, |tcx| {
387-
tcx.dep_graph.with_anon_task(dep_node.kind, || {
385+
tcx.dep_graph.with_anon_task(Q::dep_kind(), || {
388386
Q::compute(tcx.global_tcx(), key)
389387
})
390388
})
@@ -405,7 +403,9 @@ impl<'tcx> TyCtxt<'tcx> {
405403
return result;
406404
}
407405

408-
if !dep_node.kind.is_eval_always() {
406+
let dep_node = Q::to_dep_node(self, &key);
407+
408+
if !Q::EVAL_ALWAYS {
409409
// The diagnostics for this query will be
410410
// promoted to the current session during
411411
// try_mark_green(), so we can ignore them here.
@@ -546,7 +546,7 @@ impl<'tcx> TyCtxt<'tcx> {
546546

547547
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
548548
self.start_query(job.job.clone(), diagnostics, |tcx| {
549-
if dep_node.kind.is_eval_always() {
549+
if Q::EVAL_ALWAYS {
550550
tcx.dep_graph.with_eval_always_task(dep_node,
551551
tcx,
552552
key,
@@ -569,8 +569,8 @@ impl<'tcx> TyCtxt<'tcx> {
569569
self.dep_graph.mark_loaded_from_cache(dep_node_index, false);
570570
}
571571

572-
if dep_node.kind != crate::dep_graph::DepKind::Null {
573-
if unlikely!(!diagnostics.is_empty()) {
572+
if unlikely!(!diagnostics.is_empty()) {
573+
if dep_node.kind != crate::dep_graph::DepKind::Null {
574574
self.queries.on_disk_cache
575575
.store_diagnostics(dep_node_index, diagnostics);
576576
}
@@ -589,15 +589,16 @@ impl<'tcx> TyCtxt<'tcx> {
589589
///
590590
/// Note: The optimization is only available during incr. comp.
591591
pub(super) fn ensure_query<Q: QueryDescription<'tcx>>(self, key: Q::Key) -> () {
592-
let dep_node = Q::to_dep_node(self, &key);
593-
594-
if dep_node.kind.is_eval_always() {
592+
if Q::EVAL_ALWAYS {
595593
let _ = self.get_query::<Q>(DUMMY_SP, key);
596594
return;
597595
}
598596

599597
// Ensuring an anonymous query makes no sense
600-
assert!(!dep_node.kind.is_anon());
598+
assert!(!Q::ANON);
599+
600+
let dep_node = Q::to_dep_node(self, &key);
601+
601602
if self.dep_graph.try_mark_green_and_read(self, &dep_node).is_none() {
602603
// A None return from `try_mark_green_and_read` means that this is either
603604
// a new dep node or that the dep node has already been marked red.
@@ -653,6 +654,30 @@ macro_rules! handle_cycle_error {
653654
};
654655
}
655656

657+
macro_rules! is_anon {
658+
([]) => {{
659+
false
660+
}};
661+
([anon$(, $modifiers:ident)*]) => {{
662+
true
663+
}};
664+
([$other:ident$(, $modifiers:ident)*]) => {
665+
is_anon!([$($modifiers),*])
666+
};
667+
}
668+
669+
macro_rules! is_eval_always {
670+
([]) => {{
671+
false
672+
}};
673+
([eval_always$(, $modifiers:ident)*]) => {{
674+
true
675+
}};
676+
([$other:ident$(, $modifiers:ident)*]) => {
677+
is_eval_always!([$($modifiers),*])
678+
};
679+
}
680+
656681
macro_rules! hash_result {
657682
([][$hcx:expr, $result:expr]) => {{
658683
dep_graph::hash_result($hcx, &$result)
@@ -933,6 +958,9 @@ macro_rules! define_queries_inner {
933958
}
934959

935960
impl<$tcx> QueryAccessors<$tcx> for queries::$name<$tcx> {
961+
const ANON: bool = is_anon!([$($modifiers)*]);
962+
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
963+
936964
#[inline(always)]
937965
fn query(key: Self::Key) -> Query<'tcx> {
938966
Query::$name(key)
@@ -951,6 +979,11 @@ macro_rules! define_queries_inner {
951979
DepNode::new(tcx, $node(*key))
952980
}
953981

982+
#[inline(always)]
983+
fn dep_kind() -> dep_graph::DepKind {
984+
dep_graph::DepKind::$node
985+
}
986+
954987
#[inline]
955988
fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
956989
__query_compute::$name(move || {

src/librustc_macros/src/query.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -423,20 +423,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
423423
if modifiers.no_hash {
424424
attributes.push(quote! { no_hash });
425425
};
426-
427-
let mut attribute_stream = quote! {};
428-
429-
for e in attributes.into_iter().intersperse(quote! {,}) {
430-
attribute_stream.extend(e);
431-
}
432-
433-
// Add the query to the group
434-
group_stream.extend(quote! {
435-
[#attribute_stream] fn #name: #name(#arg) #result,
436-
});
437-
438-
let mut attributes = Vec::new();
439-
440426
// Pass on the anon modifier
441427
if modifiers.anon {
442428
attributes.push(quote! { anon });
@@ -450,6 +436,12 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
450436
for e in attributes.into_iter().intersperse(quote! {,}) {
451437
attribute_stream.extend(e);
452438
}
439+
440+
// Add the query to the group
441+
group_stream.extend(quote! {
442+
[#attribute_stream] fn #name: #name(#arg) #result,
443+
});
444+
453445
// Create a dep node for the query
454446
dep_node_def_stream.extend(quote! {
455447
[#attribute_stream] #name(#arg),

src/libsyntax_pos/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ impl LocalInternedString {
11321132
}
11331133
}
11341134

1135+
#[inline]
11351136
pub fn get(&self) -> &str {
11361137
// This returns a valid string since we ensure that `self` outlives the interner
11371138
// by creating the interner on a thread which outlives threads which can access it.
@@ -1145,6 +1146,7 @@ impl<U: ?Sized> std::convert::AsRef<U> for LocalInternedString
11451146
where
11461147
str: std::convert::AsRef<U>
11471148
{
1149+
#[inline]
11481150
fn as_ref(&self) -> &U {
11491151
self.string.as_ref()
11501152
}
@@ -1185,6 +1187,7 @@ impl !Sync for LocalInternedString {}
11851187

11861188
impl std::ops::Deref for LocalInternedString {
11871189
type Target = str;
1190+
#[inline]
11881191
fn deref(&self) -> &str { self.string }
11891192
}
11901193

0 commit comments

Comments
 (0)