Skip to content

Commit 9e9d03f

Browse files
committed
Add load_cached query modifier and keep dep node names consistent with query names
1 parent 7d90547 commit 9e9d03f

File tree

17 files changed

+315
-308
lines changed

17 files changed

+315
-308
lines changed

src/librustc/dep_graph/dep_node.rs

-2
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
492492
// table in the tcx (or elsewhere) maps to one of these
493493
// nodes.
494494
[] AssociatedItems(DefId),
495-
[] GenericsOfItem(DefId),
496-
[] PredicatesOfItem(DefId),
497495
[] ExplicitPredicatesOfItem(DefId),
498496
[] PredicatesDefinedOnItem(DefId),
499497
[] InferredOutlivesOf(DefId),

src/librustc/query/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ty::query::QueryDescription;
22
use crate::ty::query::queries;
33
use crate::ty::TyCtxt;
4+
use crate::ty;
45
use crate::hir::def_id::CrateNum;
56
use crate::dep_graph::SerializedDepNodeIndex;
67
use std::borrow::Cow;
@@ -23,6 +24,34 @@ rustc_queries! {
2324
cache { key.is_local() }
2425
}
2526

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+
2655
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
2756
desc { "looking up the native libraries of a linked crate" }
2857
}

src/librustc/ty/query/config.rs

-15
Original file line numberDiff line numberDiff line change
@@ -937,21 +937,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx>
937937
}
938938
}
939939

940-
impl<'tcx> QueryDescription<'tcx> for queries::generics_of<'tcx> {
941-
#[inline]
942-
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
943-
def_id.is_local()
944-
}
945-
946-
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
947-
id: SerializedDepNodeIndex)
948-
-> Option<Self::Value> {
949-
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
950-
.try_load_query_result(tcx, id);
951-
generics.map(|x| tcx.alloc_generics(x))
952-
}
953-
}
954-
955940
impl<'tcx> QueryDescription<'tcx> for queries::program_clauses_for<'tcx> {
956941
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: DefId) -> Cow<'static, str> {
957942
"generating chalk-style clauses".into()

src/librustc/ty/query/mod.rs

-21
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,6 @@ rustc_query_append! { [define_queries!][ <'tcx>
104104
/// Run analysis passes on the crate
105105
[] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,
106106

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

src/librustc/ty/query/plumbing.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,6 @@ pub fn force_from_dep_node<'tcx>(
12851285
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
12861286
DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
12871287
DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
1288-
DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
1289-
DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
12901288
DepKind::PredicatesDefinedOnItem => { force!(predicates_defined_on, def_id!()); }
12911289
DepKind::ExplicitPredicatesOfItem => { force!(explicit_predicates_of, def_id!()); }
12921290
DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); }
@@ -1501,9 +1499,9 @@ impl_load_from_cache!(
15011499
SymbolName => def_symbol_name,
15021500
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
15031501
CheckMatch => check_match,
1504-
TypeOf => type_of,
1505-
GenericsOfItem => generics_of,
1506-
PredicatesOfItem => predicates_of,
1502+
type_of => type_of,
1503+
generics_of => generics_of,
1504+
predicates_of => predicates_of,
15071505
UsedTraitImports => used_trait_imports,
15081506
CodegenFnAttrs => codegen_fn_attrs,
15091507
SpecializationGraph => specialization_graph_of,

src/librustc_incremental/persist/dirty_clean.rs

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

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

4242
/// DepNodes for functions + methods
4343
const BASE_FN: &[&str] = &[
4444
// Callers will depend on the signature of these items, so we better test
4545
label_strs::FnSignature,
46-
label_strs::GenericsOfItem,
47-
label_strs::PredicatesOfItem,
48-
label_strs::TypeOf,
46+
label_strs::generics_of,
47+
label_strs::predicates_of,
48+
label_strs::type_of,
4949

5050
// And a big part of compilation (that we eventually want to cache) is type inference
5151
// information:
@@ -62,7 +62,7 @@ const BASE_HIR: &[&str] = &[
6262
/// `impl` implementation of struct/trait
6363
const BASE_IMPL: &[&str] = &[
6464
label_strs::AssociatedItemDefIds,
65-
label_strs::GenericsOfItem,
65+
label_strs::generics_of,
6666
label_strs::ImplTraitRef,
6767
];
6868

@@ -78,17 +78,17 @@ const BASE_MIR: &[&str] = &[
7878
/// Note that changing the type of a field does not change the type of the struct or enum, but
7979
/// adding/removing fields or changing a fields name or visibility does.
8080
const BASE_STRUCT: &[&str] = &[
81-
label_strs::GenericsOfItem,
82-
label_strs::PredicatesOfItem,
83-
label_strs::TypeOf,
81+
label_strs::generics_of,
82+
label_strs::predicates_of,
83+
label_strs::type_of,
8484
];
8585

8686
/// Trait definition `DepNode`s.
8787
const BASE_TRAIT_DEF: &[&str] = &[
8888
label_strs::AssociatedItemDefIds,
89-
label_strs::GenericsOfItem,
89+
label_strs::generics_of,
9090
label_strs::ObjectSafety,
91-
label_strs::PredicatesOfItem,
91+
label_strs::predicates_of,
9292
label_strs::SpecializationGraph,
9393
label_strs::TraitDefOfItem,
9494
label_strs::TraitImpls,
@@ -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-
// TypeOf for these.
182+
// type_of for these.
183183

184184
type Labels = FxHashSet<String>;
185185

src/librustc_macros/src/query.rs

+48-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro::TokenStream;
22
use proc_macro2::Span;
33
use syn::{
4-
Token, Ident, Type, Attribute, ReturnType, Expr,
4+
Token, Ident, Type, Attribute, ReturnType, Expr, Block,
55
braced, parenthesized, parse_macro_input,
66
};
77
use syn::parse::{Result, Parse, ParseStream};
@@ -25,6 +25,7 @@ impl Parse for IdentOrWild {
2525
enum QueryAttribute {
2626
Desc(Option<Ident>, Punctuated<Expr, Token![,]>),
2727
Cache(Option<Ident>, Expr),
28+
LoadCached(Ident, Ident, Block),
2829
FatalCycle,
2930
}
3031

@@ -63,6 +64,17 @@ impl Parse for QueryAttribute {
6364
panic!("unexpected tokens in block");
6465
};
6566
Ok(QueryAttribute::Cache(tcx, expr))
67+
} else if attr == "load_cached" {
68+
let args;
69+
parenthesized!(args in input);
70+
let tcx = args.parse()?;
71+
args.parse::<Token![,]>()?;
72+
let id = args.parse()?;
73+
if !args.is_empty() {
74+
panic!("unexpected tokens in arguments");
75+
};
76+
let block = input.parse()?;
77+
Ok(QueryAttribute::LoadCached(tcx, id, block))
6678
} else if attr == "fatal_cycle" {
6779
Ok(QueryAttribute::FatalCycle)
6880
} else {
@@ -153,22 +165,6 @@ impl Parse for Group {
153165
}
154166
}
155167

156-
fn camel_case(string: &str) -> String {
157-
let mut pos = vec![0];
158-
for (i, c) in string.chars().enumerate() {
159-
if c == '_' {
160-
pos.push(i + 1);
161-
}
162-
}
163-
string.chars().enumerate().filter(|c| c.1 != '_').flat_map(|(i, c)| {
164-
if pos.contains(&i) {
165-
c.to_uppercase().collect::<Vec<char>>()
166-
} else {
167-
vec![c]
168-
}
169-
}).collect()
170-
}
171-
172168
pub fn rustc_queries(input: TokenStream) -> TokenStream {
173169
let groups = parse_macro_input!(input as List<Group>);
174170

@@ -181,9 +177,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
181177
let mut group_stream = quote! {};
182178
for query in &group.queries.0 {
183179
let name = &query.name;
184-
let dep_node_name = Ident::new(
185-
&camel_case(&name.to_string()),
186-
name.span());
187180
let arg = &query.arg;
188181
let key = &query.key.0;
189182
let result_full = &query.result;
@@ -192,35 +185,60 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
192185
_ => quote! { #result_full },
193186
};
194187

188+
let load_cached = query.attrs.0.iter().find_map(|attr| match attr {
189+
QueryAttribute::LoadCached(tcx, id, block) => Some((tcx, id, block)),
190+
_ => None,
191+
});
192+
195193
// Find out if we should cache the query on disk
196194
let cache = query.attrs.0.iter().find_map(|attr| match attr {
197195
QueryAttribute::Cache(tcx, expr) => Some((tcx, expr)),
198196
_ => None,
199197
}).map(|(tcx, expr)| {
198+
let try_load_from_disk = if let Some((tcx, id, block)) = load_cached {
199+
quote! {
200+
#[inline]
201+
fn try_load_from_disk(
202+
#tcx: TyCtxt<'_, 'tcx, 'tcx>,
203+
#id: SerializedDepNodeIndex
204+
) -> Option<Self::Value> {
205+
#block
206+
}
207+
}
208+
} else {
209+
quote! {
210+
#[inline]
211+
fn try_load_from_disk(
212+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
213+
id: SerializedDepNodeIndex
214+
) -> Option<Self::Value> {
215+
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
216+
}
217+
}
218+
};
219+
200220
let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
201221
quote! {
202222
#[inline]
203223
fn cache_on_disk(#tcx: TyCtxt<'_, 'tcx, 'tcx>, #key: Self::Key) -> bool {
204224
#expr
205225
}
206226

207-
#[inline]
208-
fn try_load_from_disk(
209-
tcx: TyCtxt<'_, 'tcx, 'tcx>,
210-
id: SerializedDepNodeIndex
211-
) -> Option<Self::Value> {
212-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
213-
}
227+
#try_load_from_disk
214228
}
215229
});
216230

231+
if cache.is_none() && load_cached.is_some() {
232+
panic!("load_cached modifier on query `{}` without a cache modifier", name);
233+
}
234+
217235
let fatal_cycle = query.attrs.0.iter().find_map(|attr| match attr {
218236
QueryAttribute::FatalCycle => Some(()),
219237
_ => None,
220238
}).map(|_| quote! { fatal_cycle }).unwrap_or(quote! {});
221239

222240
group_stream.extend(quote! {
223-
[#fatal_cycle] fn #name: #dep_node_name(#arg) #result,
241+
[#fatal_cycle] fn #name: #name(#arg) #result,
224242
});
225243

226244
let desc = query.attrs.0.iter().find_map(|attr| match attr {
@@ -251,10 +269,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
251269
}
252270

253271
dep_node_def_stream.extend(quote! {
254-
[] #dep_node_name(#arg),
272+
[] #name(#arg),
255273
});
256274
dep_node_force_stream.extend(quote! {
257-
DepKind::#dep_node_name => {
275+
DepKind::#name => {
258276
if let Some(key) = RecoverKey::recover($tcx, $dep_node) {
259277
force_ex!($tcx, #name, key);
260278
} else {

src/test/incremental/hashes/consts.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub const CONST_VISIBILITY: u8 = 0;
2929
const CONST_CHANGE_TYPE_1: i32 = 0;
3030

3131
#[cfg(not(cfail1))]
32-
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
32+
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
3333
#[rustc_clean(cfg="cfail3")]
3434
const CONST_CHANGE_TYPE_1: u32 = 0;
3535

@@ -39,7 +39,7 @@ const CONST_CHANGE_TYPE_1: u32 = 0;
3939
const CONST_CHANGE_TYPE_2: Option<u32> = None;
4040

4141
#[cfg(not(cfail1))]
42-
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
42+
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
4343
#[rustc_clean(cfg="cfail3")]
4444
const CONST_CHANGE_TYPE_2: Option<u64> = None;
4545

@@ -99,11 +99,11 @@ mod const_change_type_indirectly {
9999
#[cfg(not(cfail1))]
100100
use super::ReferencedType2 as Type;
101101

102-
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
102+
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
103103
#[rustc_clean(cfg="cfail3")]
104104
const CONST_CHANGE_TYPE_INDIRECTLY_1: Type = Type;
105105

106-
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
106+
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
107107
#[rustc_clean(cfg="cfail3")]
108108
const CONST_CHANGE_TYPE_INDIRECTLY_2: Option<Type> = None;
109109
}

0 commit comments

Comments
 (0)