Skip to content

Commit 3bf064b

Browse files
committed
Auto merge of #56462 - Zoxc:query-macro, r=oli-obk
Define queries using a proc macro cc @rust-lang/compiler
2 parents 0f88167 + 198dfce commit 3bf064b

20 files changed

+760
-320
lines changed

src/librustc/dep_graph/dep_node.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl DefId {
423423
}
424424
}
425425

426-
define_dep_nodes!( <'tcx>
426+
rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
427427
// We use this for most things when incr. comp. is turned off.
428428
[] Null,
429429

@@ -492,9 +492,6 @@ define_dep_nodes!( <'tcx>
492492
// table in the tcx (or elsewhere) maps to one of these
493493
// nodes.
494494
[] AssociatedItems(DefId),
495-
[] TypeOfItem(DefId),
496-
[] GenericsOfItem(DefId),
497-
[] PredicatesOfItem(DefId),
498495
[] ExplicitPredicatesOfItem(DefId),
499496
[] PredicatesDefinedOnItem(DefId),
500497
[] InferredOutlivesOf(DefId),
@@ -570,7 +567,6 @@ define_dep_nodes!( <'tcx>
570567
[] FnArgNames(DefId),
571568
[] RenderedConst(DefId),
572569
[] DylibDepFormats(CrateNum),
573-
[] IsPanicRuntime(CrateNum),
574570
[] IsCompilerBuiltins(CrateNum),
575571
[] HasGlobalAllocator(CrateNum),
576572
[] HasPanicHandler(CrateNum),
@@ -588,7 +584,6 @@ define_dep_nodes!( <'tcx>
588584
[] CheckTraitItemWellFormed(DefId),
589585
[] CheckImplItemWellFormed(DefId),
590586
[] ReachableNonGenerics(CrateNum),
591-
[] NativeLibraries(CrateNum),
592587
[] EntryFn(CrateNum),
593588
[] PluginRegistrarFn(CrateNum),
594589
[] ProcMacroDeclsStatic(CrateNum),
@@ -679,7 +674,23 @@ define_dep_nodes!( <'tcx>
679674

680675
[] UpstreamMonomorphizations(CrateNum),
681676
[] UpstreamMonomorphizationsFor(DefId),
682-
);
677+
]);
678+
679+
pub trait RecoverKey<'tcx>: Sized {
680+
fn recover(tcx: TyCtxt<'_, 'tcx, 'tcx>, dep_node: &DepNode) -> Option<Self>;
681+
}
682+
683+
impl RecoverKey<'tcx> for CrateNum {
684+
fn recover(tcx: TyCtxt<'_, 'tcx, 'tcx>, dep_node: &DepNode) -> Option<Self> {
685+
dep_node.extract_def_id(tcx).map(|id| id.krate)
686+
}
687+
}
688+
689+
impl RecoverKey<'tcx> for DefId {
690+
fn recover(tcx: TyCtxt<'_, 'tcx, 'tcx>, dep_node: &DepNode) -> Option<Self> {
691+
dep_node.extract_def_id(tcx)
692+
}
693+
}
683694

684695
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
685696
const CAN_RECONSTRUCT_QUERY_KEY: bool;

src/librustc/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod serialized;
99
pub mod cgu_reuse_tracker;
1010

1111
pub use self::dep_tracking_map::{DepTrackingMap, DepTrackingMapConfig};
12-
pub use self::dep_node::{DepNode, DepKind, DepConstructor, WorkProductId, label_strs};
12+
pub use self::dep_node::{DepNode, DepKind, DepConstructor, WorkProductId, RecoverKey, label_strs};
1313
pub use self::graph::{DepGraph, WorkProduct, DepNodeIndex, DepNodeColor, TaskDeps, hash_result};
1414
pub use self::graph::WorkProductFileKind;
1515
pub use self::prev::PreviousDepGraph;

src/librustc/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#![feature(test)]
6161
#![feature(in_band_lifetimes)]
6262
#![feature(crate_visibility_modifier)]
63+
#![feature(proc_macro_hygiene)]
64+
#![feature(log_syntax)]
6365

6466
#![recursion_limit="512"]
6567

@@ -69,6 +71,7 @@ extern crate getopts;
6971
#[macro_use] extern crate scoped_tls;
7072
#[cfg(windows)]
7173
extern crate libc;
74+
#[macro_use] extern crate rustc_macros;
7275
#[macro_use] extern crate rustc_data_structures;
7376

7477
#[macro_use] extern crate log;
@@ -96,6 +99,9 @@ mod macros;
9699
// registered before they are used.
97100
pub mod diagnostics;
98101

102+
#[macro_use]
103+
pub mod query;
104+
99105
pub mod cfg;
100106
pub mod dep_graph;
101107
pub mod hir;

src/librustc/query/mod.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use crate::ty::query::QueryDescription;
2+
use crate::ty::query::queries;
3+
use crate::ty::TyCtxt;
4+
use crate::ty;
5+
use crate::hir::def_id::CrateNum;
6+
use crate::dep_graph::SerializedDepNodeIndex;
7+
use std::borrow::Cow;
8+
9+
// Each of these queries corresponds to a function pointer field in the
10+
// `Providers` struct for requesting a value of that type, and a method
11+
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
12+
// which memoizes and does dep-graph tracking, wrapping around the actual
13+
// `Providers` that the driver creates (using several `rustc_*` crates).
14+
//
15+
// The result type of each query must implement `Clone`, and additionally
16+
// `ty::query::values::Value`, which produces an appropriate placeholder
17+
// (error) value if the query resulted in a query cycle.
18+
// Queries marked with `fatal_cycle` do not need the latter implementation,
19+
// as they will raise an fatal error on query cycles instead.
20+
rustc_queries! {
21+
Other {
22+
/// Records the type of every item.
23+
query type_of(key: DefId) -> Ty<'tcx> {
24+
cache { key.is_local() }
25+
}
26+
27+
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
28+
/// associated generics.
29+
query generics_of(key: DefId) -> &'tcx ty::Generics {
30+
cache { key.is_local() }
31+
load_cached(tcx, id) {
32+
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
33+
.try_load_query_result(tcx, id);
34+
generics.map(|x| tcx.alloc_generics(x))
35+
}
36+
}
37+
38+
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
39+
/// predicates (where-clauses) that must be proven true in order
40+
/// to reference it. This is almost always the "predicates query"
41+
/// that you want.
42+
///
43+
/// `predicates_of` builds on `predicates_defined_on` -- in fact,
44+
/// it is almost always the same as that query, except for the
45+
/// case of traits. For traits, `predicates_of` contains
46+
/// an additional `Self: Trait<...>` predicate that users don't
47+
/// actually write. This reflects the fact that to invoke the
48+
/// trait (e.g., via `Default::default`) you must supply types
49+
/// that actually implement the trait. (However, this extra
50+
/// predicate gets in the way of some checks, which are intended
51+
/// to operate over only the actual where-clauses written by the
52+
/// user.)
53+
query predicates_of(_: DefId) -> Lrc<ty::GenericPredicates<'tcx>> {}
54+
55+
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
56+
desc { "looking up the native libraries of a linked crate" }
57+
}
58+
}
59+
60+
Codegen {
61+
query is_panic_runtime(_: CrateNum) -> bool {
62+
fatal_cycle
63+
desc { "checking if the crate is_panic_runtime" }
64+
}
65+
}
66+
}

src/librustc/ty/query/config.rs

+2-30
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait QueryConfig<'tcx> {
3434
type Value: Clone;
3535
}
3636

37-
pub(super) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
37+
pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
3838
fn query(key: Self::Key) -> Query<'tcx>;
3939

4040
// Don't use this method to access query results, instead use the methods on TyCtxt
@@ -53,7 +53,7 @@ pub(super) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
5353
fn handle_cycle_error(tcx: TyCtxt<'_, 'tcx, '_>, error: CycleError<'tcx>) -> Self::Value;
5454
}
5555

56-
pub(super) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
56+
pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5757
fn describe(tcx: TyCtxt<'_, '_, '_>, key: Self::Key) -> Cow<'static, str>;
5858

5959
#[inline]
@@ -587,12 +587,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::dylib_dependency_formats<'tcx> {
587587
}
588588
}
589589

590-
impl<'tcx> QueryDescription<'tcx> for queries::is_panic_runtime<'tcx> {
591-
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
592-
"checking if the crate is_panic_runtime".into()
593-
}
594-
}
595-
596590
impl<'tcx> QueryDescription<'tcx> for queries::is_compiler_builtins<'tcx> {
597591
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
598592
"checking if the crate is_compiler_builtins".into()
@@ -671,12 +665,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::reachable_non_generics<'tcx> {
671665
}
672666
}
673667

674-
impl<'tcx> QueryDescription<'tcx> for queries::native_libraries<'tcx> {
675-
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
676-
"looking up the native libraries of a linked crate".into()
677-
}
678-
}
679-
680668
impl<'tcx> QueryDescription<'tcx> for queries::foreign_modules<'tcx> {
681669
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
682670
"looking up the foreign modules of a linked crate".into()
@@ -949,21 +937,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx>
949937
}
950938
}
951939

952-
impl<'tcx> QueryDescription<'tcx> for queries::generics_of<'tcx> {
953-
#[inline]
954-
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
955-
def_id.is_local()
956-
}
957-
958-
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
959-
id: SerializedDepNodeIndex)
960-
-> Option<Self::Value> {
961-
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
962-
.try_load_query_result(tcx, id);
963-
generics.map(|x| tcx.alloc_generics(x))
964-
}
965-
}
966-
967940
impl<'tcx> QueryDescription<'tcx> for queries::program_clauses_for<'tcx> {
968941
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: DefId) -> Cow<'static, str> {
969942
"generating chalk-style clauses".into()
@@ -1027,7 +1000,6 @@ impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
10271000
impl_disk_cacheable_query!(mir_const_qualif, |_, def_id| def_id.is_local());
10281001
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
10291002
impl_disk_cacheable_query!(def_symbol_name, |_, _| true);
1030-
impl_disk_cacheable_query!(type_of, |_, def_id| def_id.is_local());
10311003
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
10321004
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
10331005
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);

src/librustc/ty/query/mod.rs

+6-31
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ mod values;
8080
use self::values::Value;
8181

8282
mod config;
83+
pub(crate) use self::config::QueryDescription;
8384
pub use self::config::QueryConfig;
84-
use self::config::{QueryAccessors, QueryDescription};
85+
use self::config::QueryAccessors;
8586

8687
mod on_disk_cache;
8788
pub use self::on_disk_cache::OnDiskCache;
8889

89-
// Each of these quries corresponds to a function pointer field in the
90+
// Each of these queries corresponds to a function pointer field in the
9091
// `Providers` struct for requesting a value of that type, and a method
9192
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
9293
// which memoizes and does dep-graph tracking, wrapping around the actual
@@ -97,35 +98,12 @@ pub use self::on_disk_cache::OnDiskCache;
9798
// (error) value if the query resulted in a query cycle.
9899
// Queries marked with `fatal_cycle` do not need the latter implementation,
99100
// as they will raise an fatal error on query cycles instead.
100-
define_queries! { <'tcx>
101+
102+
rustc_query_append! { [define_queries!][ <'tcx>
101103
Other {
102104
/// Run analysis passes on the crate
103105
[] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,
104106

105-
/// Records the type of every item.
106-
[] fn type_of: TypeOfItem(DefId) -> Ty<'tcx>,
107-
108-
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
109-
/// associated generics.
110-
[] fn generics_of: GenericsOfItem(DefId) -> &'tcx ty::Generics,
111-
112-
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
113-
/// predicates (where-clauses) that must be proven true in order
114-
/// to reference it. This is almost always the "predicates query"
115-
/// that you want.
116-
///
117-
/// `predicates_of` builds on `predicates_defined_on` -- in fact,
118-
/// it is almost always the same as that query, except for the
119-
/// case of traits. For traits, `predicates_of` contains
120-
/// an additional `Self: Trait<...>` predicate that users don't
121-
/// actually write. This reflects the fact that to invoke the
122-
/// trait (e.g., via `Default::default`) you must supply types
123-
/// that actually implement the trait. (However, this extra
124-
/// predicate gets in the way of some checks, which are intended
125-
/// to operate over only the actual where-clauses written by the
126-
/// user.)
127-
[] fn predicates_of: PredicatesOfItem(DefId) -> Lrc<ty::GenericPredicates<'tcx>>,
128-
129107
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
130108
/// predicates (where-clauses) directly defined on it. This is
131109
/// equal to the `explicit_predicates_of` predicates plus the
@@ -446,7 +424,6 @@ define_queries! { <'tcx>
446424
},
447425

448426
Codegen {
449-
[fatal_cycle] fn is_panic_runtime: IsPanicRuntime(CrateNum) -> bool,
450427
[fatal_cycle] fn is_compiler_builtins: IsCompilerBuiltins(CrateNum) -> bool,
451428
[fatal_cycle] fn has_global_allocator: HasGlobalAllocator(CrateNum) -> bool,
452429
[fatal_cycle] fn has_panic_handler: HasPanicHandler(CrateNum) -> bool,
@@ -504,8 +481,6 @@ define_queries! { <'tcx>
504481
},
505482

506483
Other {
507-
[] fn native_libraries: NativeLibraries(CrateNum) -> Lrc<Vec<NativeLibrary>>,
508-
509484
[] fn foreign_modules: ForeignModules(CrateNum) -> Lrc<Vec<ForeignModule>>,
510485

511486
/// Identifies the entry-point (e.g., the `main` function) for a given
@@ -752,7 +727,7 @@ define_queries! { <'tcx>
752727
[] fn wasm_import_module_map: WasmImportModuleMap(CrateNum)
753728
-> Lrc<FxHashMap<DefId, String>>,
754729
},
755-
}
730+
]}
756731

757732
//////////////////////////////////////////////////////////////////////
758733
// These functions are little shims used to find the dep-node for a

0 commit comments

Comments
 (0)