Skip to content

Commit 4956c80

Browse files
committed
Add a storage query modifier to override the query cache
1 parent 970ff76 commit 4956c80

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ macro_rules! is_eval_always_attr {
9999
}
100100

101101
macro_rules! contains_anon_attr {
102-
($($attr:ident),*) => ({$(is_anon_attr!($attr) | )* false});
102+
($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_anon_attr!($attr) | )* false});
103103
}
104104

105105
macro_rules! contains_eval_always_attr {
106-
($($attr:ident),*) => ({$(is_eval_always_attr!($attr) | )* false});
106+
($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
107107
}
108108

109109
macro_rules! define_dep_nodes {
110110
(<$tcx:tt>
111111
$(
112-
[$($attr:ident),* ]
112+
[$($attrs:tt)*]
113113
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
114114
$({ $($struct_arg_name:ident : $struct_arg_ty:ty),* })*
115115
,)*
@@ -126,7 +126,7 @@ macro_rules! define_dep_nodes {
126126
match *self {
127127
$(
128128
DepKind :: $variant => {
129-
if contains_anon_attr!($($attr),*) {
129+
if contains_anon_attr!($($attrs)*) {
130130
return false;
131131
}
132132

@@ -152,15 +152,15 @@ macro_rules! define_dep_nodes {
152152
pub fn is_anon(&self) -> bool {
153153
match *self {
154154
$(
155-
DepKind :: $variant => { contains_anon_attr!($($attr),*) }
155+
DepKind :: $variant => { contains_anon_attr!($($attrs)*) }
156156
)*
157157
}
158158
}
159159

160160
pub fn is_eval_always(&self) -> bool {
161161
match *self {
162162
$(
163-
DepKind :: $variant => { contains_eval_always_attr!($($attr), *) }
163+
DepKind :: $variant => { contains_eval_always_attr!($($attrs)*) }
164164
)*
165165
}
166166
}

src/librustc/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ rustc_queries! {
5757

5858
/// Records the type of every item.
5959
query type_of(key: DefId) -> Ty<'tcx> {
60+
storage(caches::LocalDenseDefIdCache<Ty<'tcx>>)
6061
cache_on_disk_if { key.is_local() }
6162
}
6263

src/librustc/ty/query/plumbing.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,53 +746,65 @@ macro_rules! handle_cycle_error {
746746
$tcx.report_cycle($error).emit();
747747
Value::from_cycle_error($tcx)
748748
}};
749-
([fatal_cycle$(, $modifiers:ident)*][$tcx:expr, $error:expr]) => {{
749+
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
750750
$tcx.report_cycle($error).emit();
751751
$tcx.sess.abort_if_errors();
752752
unreachable!()
753753
}};
754-
([cycle_delay_bug$(, $modifiers:ident)*][$tcx:expr, $error:expr]) => {{
754+
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
755755
$tcx.report_cycle($error).delay_as_bug();
756756
Value::from_cycle_error($tcx)
757757
}};
758-
([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
759-
handle_cycle_error!([$($modifiers),*][$($args)*])
758+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
759+
handle_cycle_error!([$($($modifiers)*)*][$($args)*])
760760
};
761761
}
762762

763763
macro_rules! is_anon {
764764
([]) => {{
765765
false
766766
}};
767-
([anon$(, $modifiers:ident)*]) => {{
767+
([anon $($rest:tt)*]) => {{
768768
true
769769
}};
770-
([$other:ident$(, $modifiers:ident)*]) => {
771-
is_anon!([$($modifiers),*])
770+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
771+
is_anon!([$($($modifiers)*)*])
772772
};
773773
}
774774

775775
macro_rules! is_eval_always {
776776
([]) => {{
777777
false
778778
}};
779-
([eval_always$(, $modifiers:ident)*]) => {{
779+
([eval_always $($rest:tt)*]) => {{
780780
true
781781
}};
782-
([$other:ident$(, $modifiers:ident)*]) => {
783-
is_eval_always!([$($modifiers),*])
782+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
783+
is_eval_always!([$($($modifiers)*)*])
784+
};
785+
}
786+
787+
macro_rules! query_storage {
788+
([][$K:ty, $V:ty]) => {
789+
<<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache
790+
};
791+
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
792+
$ty
793+
};
794+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
795+
query_storage!([$($($modifiers)*)*][$($args)*])
784796
};
785797
}
786798

787799
macro_rules! hash_result {
788800
([][$hcx:expr, $result:expr]) => {{
789801
dep_graph::hash_result($hcx, &$result)
790802
}};
791-
([no_hash$(, $modifiers:ident)*][$hcx:expr, $result:expr]) => {{
803+
([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
792804
None
793805
}};
794-
([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
795-
hash_result!([$($modifiers),*][$($args)*])
806+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
807+
hash_result!([$($($modifiers)*)*][$($args)*])
796808
};
797809
}
798810

@@ -1049,7 +1061,7 @@ macro_rules! define_queries_inner {
10491061
const ANON: bool = is_anon!([$($modifiers)*]);
10501062
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
10511063

1052-
type Cache = <<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache;
1064+
type Cache = query_storage!([$($modifiers)*][$K, $V]);
10531065

10541066
#[inline(always)]
10551067
fn query(key: Self::Key) -> Query<'tcx> {

src/librustc_macros/src/query.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ enum QueryModifier {
3333
/// The description of the query.
3434
Desc(Option<Ident>, Punctuated<Expr, Token![,]>),
3535

36+
/// Use this type for the in-memory cache.
37+
Storage(Type),
38+
3639
/// Cache the query to disk if the `Expr` returns true.
3740
Cache(Option<(IdentOrWild, IdentOrWild)>, Block),
3841

@@ -106,6 +109,9 @@ impl Parse for QueryModifier {
106109
let id = args.parse()?;
107110
let block = input.parse()?;
108111
Ok(QueryModifier::LoadCached(tcx, id, block))
112+
} else if modifier == "storage" {
113+
let ty = input.parse()?;
114+
Ok(QueryModifier::Storage(ty))
109115
} else if modifier == "fatal_cycle" {
110116
Ok(QueryModifier::FatalCycle)
111117
} else if modifier == "cycle_delay_bug" {
@@ -198,6 +204,9 @@ struct QueryModifiers {
198204
/// The description of the query.
199205
desc: Option<(Option<Ident>, Punctuated<Expr, Token![,]>)>,
200206

207+
/// Use this type for the in-memory cache.
208+
storage: Option<Type>,
209+
201210
/// Cache the query to disk if the `Block` returns true.
202211
cache: Option<(Option<(IdentOrWild, IdentOrWild)>, Block)>,
203212

@@ -226,6 +235,7 @@ struct QueryModifiers {
226235
/// Process query modifiers into a struct, erroring on duplicates
227236
fn process_modifiers(query: &mut Query) -> QueryModifiers {
228237
let mut load_cached = None;
238+
let mut storage = None;
229239
let mut cache = None;
230240
let mut desc = None;
231241
let mut fatal_cycle = false;
@@ -242,6 +252,12 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
242252
}
243253
load_cached = Some((tcx, id, block));
244254
}
255+
QueryModifier::Storage(ty) => {
256+
if storage.is_some() {
257+
panic!("duplicate modifier `storage` for query `{}`", query.name);
258+
}
259+
storage = Some(ty);
260+
}
245261
QueryModifier::Cache(args, expr) => {
246262
if cache.is_some() {
247263
panic!("duplicate modifier `cache` for query `{}`", query.name);
@@ -294,6 +310,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
294310
}
295311
QueryModifiers {
296312
load_cached,
313+
storage,
297314
cache,
298315
desc,
299316
fatal_cycle,
@@ -451,6 +468,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
451468
if modifiers.fatal_cycle {
452469
attributes.push(quote! { fatal_cycle });
453470
};
471+
// Pass on the storage modifier
472+
if let Some(ref ty) = modifiers.storage {
473+
attributes.push(quote! { storage(#ty) });
474+
};
454475
// Pass on the cycle_delay_bug modifier
455476
if modifiers.cycle_delay_bug {
456477
attributes.push(quote! { cycle_delay_bug });

0 commit comments

Comments
 (0)