Skip to content

Commit 7d90547

Browse files
committed
Define queries using a proc macro
1 parent 03dafa7 commit 7d90547

File tree

19 files changed

+563
-138
lines changed

19 files changed

+563
-138
lines changed

src/librustc/dep_graph/dep_node.rs

+18-5
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,7 +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),
496495
[] GenericsOfItem(DefId),
497496
[] PredicatesOfItem(DefId),
498497
[] ExplicitPredicatesOfItem(DefId),
@@ -570,7 +569,6 @@ define_dep_nodes!( <'tcx>
570569
[] FnArgNames(DefId),
571570
[] RenderedConst(DefId),
572571
[] DylibDepFormats(CrateNum),
573-
[] IsPanicRuntime(CrateNum),
574572
[] IsCompilerBuiltins(CrateNum),
575573
[] HasGlobalAllocator(CrateNum),
576574
[] HasPanicHandler(CrateNum),
@@ -588,7 +586,6 @@ define_dep_nodes!( <'tcx>
588586
[] CheckTraitItemWellFormed(DefId),
589587
[] CheckImplItemWellFormed(DefId),
590588
[] ReachableNonGenerics(CrateNum),
591-
[] NativeLibraries(CrateNum),
592589
[] EntryFn(CrateNum),
593590
[] PluginRegistrarFn(CrateNum),
594591
[] ProcMacroDeclsStatic(CrateNum),
@@ -679,7 +676,23 @@ define_dep_nodes!( <'tcx>
679676

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

684697
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
685698
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

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::ty::query::QueryDescription;
2+
use crate::ty::query::queries;
3+
use crate::ty::TyCtxt;
4+
use crate::hir::def_id::CrateNum;
5+
use crate::dep_graph::SerializedDepNodeIndex;
6+
use std::borrow::Cow;
7+
8+
// Each of these queries corresponds to a function pointer field in the
9+
// `Providers` struct for requesting a value of that type, and a method
10+
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
11+
// which memoizes and does dep-graph tracking, wrapping around the actual
12+
// `Providers` that the driver creates (using several `rustc_*` crates).
13+
//
14+
// The result type of each query must implement `Clone`, and additionally
15+
// `ty::query::values::Value`, which produces an appropriate placeholder
16+
// (error) value if the query resulted in a query cycle.
17+
// Queries marked with `fatal_cycle` do not need the latter implementation,
18+
// as they will raise an fatal error on query cycles instead.
19+
rustc_queries! {
20+
Other {
21+
/// Records the type of every item.
22+
query type_of(key: DefId) -> Ty<'tcx> {
23+
cache { key.is_local() }
24+
}
25+
26+
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
27+
desc { "looking up the native libraries of a linked crate" }
28+
}
29+
}
30+
31+
Codegen {
32+
query is_panic_runtime(_: CrateNum) -> bool {
33+
fatal_cycle
34+
desc { "checking if the crate is_panic_runtime" }
35+
}
36+
}
37+
}

src/librustc/ty/query/config.rs

+2-15
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()
@@ -1027,7 +1015,6 @@ impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
10271015
impl_disk_cacheable_query!(mir_const_qualif, |_, def_id| def_id.is_local());
10281016
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
10291017
impl_disk_cacheable_query!(def_symbol_name, |_, _| true);
1030-
impl_disk_cacheable_query!(type_of, |_, def_id| def_id.is_local());
10311018
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
10321019
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
10331020
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);

src/librustc/ty/query/mod.rs

+6-10
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,14 +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-
108107
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
109108
/// associated generics.
110109
[] fn generics_of: GenericsOfItem(DefId) -> &'tcx ty::Generics,
@@ -446,7 +445,6 @@ define_queries! { <'tcx>
446445
},
447446

448447
Codegen {
449-
[fatal_cycle] fn is_panic_runtime: IsPanicRuntime(CrateNum) -> bool,
450448
[fatal_cycle] fn is_compiler_builtins: IsCompilerBuiltins(CrateNum) -> bool,
451449
[fatal_cycle] fn has_global_allocator: HasGlobalAllocator(CrateNum) -> bool,
452450
[fatal_cycle] fn has_panic_handler: HasPanicHandler(CrateNum) -> bool,
@@ -504,8 +502,6 @@ define_queries! { <'tcx>
504502
},
505503

506504
Other {
507-
[] fn native_libraries: NativeLibraries(CrateNum) -> Lrc<Vec<NativeLibrary>>,
508-
509505
[] fn foreign_modules: ForeignModules(CrateNum) -> Lrc<Vec<ForeignModule>>,
510506

511507
/// Identifies the entry-point (e.g., the `main` function) for a given
@@ -752,7 +748,7 @@ define_queries! { <'tcx>
752748
[] fn wasm_import_module_map: WasmImportModuleMap(CrateNum)
753749
-> Lrc<FxHashMap<DefId, String>>,
754750
},
755-
}
751+
]}
756752

757753
//////////////////////////////////////////////////////////////////////
758754
// These functions are little shims used to find the dep-node for a

src/librustc/ty/query/plumbing.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1131,10 +1131,12 @@ macro_rules! define_provider_struct {
11311131
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
11321132
/// add it to the "We don't have enough information to reconstruct..." group in
11331133
/// the match below.
1134-
pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
1135-
dep_node: &DepNode)
1136-
-> bool {
1134+
pub fn force_from_dep_node<'tcx>(
1135+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
1136+
dep_node: &DepNode
1137+
) -> bool {
11371138
use crate::hir::def_id::LOCAL_CRATE;
1139+
use crate::dep_graph::RecoverKey;
11381140

11391141
// We must avoid ever having to call force_from_dep_node() for a
11401142
// DepNode::CodegenUnit:
@@ -1171,17 +1173,26 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
11711173
() => { (def_id!()).krate }
11721174
};
11731175

1174-
macro_rules! force {
1175-
($query:ident, $key:expr) => {
1176+
macro_rules! force_ex {
1177+
($tcx:expr, $query:ident, $key:expr) => {
11761178
{
1177-
tcx.force_query::<crate::ty::query::queries::$query<'_>>($key, DUMMY_SP, *dep_node);
1179+
$tcx.force_query::<crate::ty::query::queries::$query<'_>>(
1180+
$key,
1181+
DUMMY_SP,
1182+
*dep_node
1183+
);
11781184
}
11791185
}
11801186
};
11811187

1188+
macro_rules! force {
1189+
($query:ident, $key:expr) => { force_ex!(tcx, $query, $key) }
1190+
};
1191+
11821192
// FIXME(#45015): We should try move this boilerplate code into a macro
11831193
// somehow.
1184-
match dep_node.kind {
1194+
1195+
rustc_dep_node_force!([dep_node, tcx]
11851196
// These are inputs that are expected to be pre-allocated and that
11861197
// should therefore always be red or green already
11871198
DepKind::AllLocalTraitImpls |
@@ -1274,7 +1285,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12741285
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
12751286
DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
12761287
DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
1277-
DepKind::TypeOfItem => { force!(type_of, def_id!()); }
12781288
DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
12791289
DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
12801290
DepKind::PredicatesDefinedOnItem => { force!(predicates_defined_on, def_id!()); }
@@ -1332,7 +1342,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
13321342
DepKind::FnArgNames => { force!(fn_arg_names, def_id!()); }
13331343
DepKind::RenderedConst => { force!(rendered_const, def_id!()); }
13341344
DepKind::DylibDepFormats => { force!(dylib_dependency_formats, krate!()); }
1335-
DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }
13361345
DepKind::IsCompilerBuiltins => { force!(is_compiler_builtins, krate!()); }
13371346
DepKind::HasGlobalAllocator => { force!(has_global_allocator, krate!()); }
13381347
DepKind::HasPanicHandler => { force!(has_panic_handler, krate!()); }
@@ -1349,7 +1358,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
13491358
DepKind::CheckTraitItemWellFormed => { force!(check_trait_item_well_formed, def_id!()); }
13501359
DepKind::CheckImplItemWellFormed => { force!(check_impl_item_well_formed, def_id!()); }
13511360
DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
1352-
DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
13531361
DepKind::EntryFn => { force!(entry_fn, krate!()); }
13541362
DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
13551363
DepKind::ProcMacroDeclsStatic => { force!(proc_macro_decls_static, krate!()); }
@@ -1432,7 +1440,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
14321440
DepKind::BackendOptimizationLevel => {
14331441
force!(backend_optimization_level, krate!());
14341442
}
1435-
}
1443+
);
14361444

14371445
true
14381446
}
@@ -1493,7 +1501,7 @@ impl_load_from_cache!(
14931501
SymbolName => def_symbol_name,
14941502
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
14951503
CheckMatch => check_match,
1496-
TypeOfItem => type_of,
1504+
TypeOf => type_of,
14971505
GenericsOfItem => generics_of,
14981506
PredicatesOfItem => predicates_of,
14991507
UsedTraitImports => used_trait_imports,

src/librustc_incremental/persist/dirty_clean.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const CFG: &str = "cfg";
3636

3737
/// For typedef, constants, and statics
3838
const BASE_CONST: &[&str] = &[
39-
label_strs::TypeOfItem,
39+
label_strs::TypeOf,
4040
];
4141

4242
/// DepNodes for functions + methods
@@ -45,7 +45,7 @@ const BASE_FN: &[&str] = &[
4545
label_strs::FnSignature,
4646
label_strs::GenericsOfItem,
4747
label_strs::PredicatesOfItem,
48-
label_strs::TypeOfItem,
48+
label_strs::TypeOf,
4949

5050
// And a big part of compilation (that we eventually want to cache) is type inference
5151
// information:
@@ -80,7 +80,7 @@ const BASE_MIR: &[&str] = &[
8080
const BASE_STRUCT: &[&str] = &[
8181
label_strs::GenericsOfItem,
8282
label_strs::PredicatesOfItem,
83-
label_strs::TypeOfItem,
83+
label_strs::TypeOf,
8484
];
8585

8686
/// Trait definition `DepNode`s.
@@ -179,7 +179,7 @@ const LABELS_TRAIT: &[&[&str]] = &[
179179
// Fields are kind of separate from their containers, as they can change independently from
180180
// them. We should at least check
181181
//
182-
// TypeOfItem for these.
182+
// TypeOf for these.
183183

184184
type Labels = FxHashSet<String>;
185185

src/librustc_macros/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#![feature(proc_macro_hygiene)]
22
#![deny(rust_2018_idioms)]
33

4+
extern crate proc_macro;
5+
46
use synstructure::decl_derive;
57

8+
use proc_macro::TokenStream;
9+
610
mod hash_stable;
11+
mod query;
12+
mod tt;
13+
14+
#[proc_macro]
15+
pub fn rustc_queries(input: TokenStream) -> TokenStream {
16+
query::rustc_queries(input)
17+
}
718

819
decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);

0 commit comments

Comments
 (0)