Skip to content

Commit 42963f4

Browse files
committed
Query modifier
1 parent 8cd168f commit 42963f4

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

compiler/rustc_macros/src/query.rs

+20
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum QueryModifier {
5858

5959
/// Use a separate query provider for local and extern crates
6060
SeparateProvideExtern(Ident),
61+
62+
/// Always remap the ParamEnv's constness before hashing and passing to the query provider
63+
RemapEnvConstness(Ident),
6164
}
6265

6366
impl Parse for QueryModifier {
@@ -123,6 +126,8 @@ impl Parse for QueryModifier {
123126
Ok(QueryModifier::EvalAlways(modifier))
124127
} else if modifier == "separate_provide_extern" {
125128
Ok(QueryModifier::SeparateProvideExtern(modifier))
129+
} else if modifier == "remap_env_constness" {
130+
Ok(QueryModifier::RemapEnvConstness(modifier))
126131
} else {
127132
Err(Error::new(modifier.span(), "unknown query modifier"))
128133
}
@@ -222,6 +227,9 @@ struct QueryModifiers {
222227

223228
/// Use a separate query provider for local and extern crates
224229
separate_provide_extern: Option<Ident>,
230+
231+
/// Always remap the ParamEnv's constness before hashing.
232+
remap_env_constness: Option<Ident>,
225233
}
226234

227235
/// Process query modifiers into a struct, erroring on duplicates
@@ -236,6 +244,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
236244
let mut anon = None;
237245
let mut eval_always = None;
238246
let mut separate_provide_extern = None;
247+
let mut remap_env_constness = None;
239248
for modifier in query.modifiers.0.drain(..) {
240249
match modifier {
241250
QueryModifier::LoadCached(tcx, id, block) => {
@@ -335,6 +344,12 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
335344
}
336345
separate_provide_extern = Some(ident);
337346
}
347+
QueryModifier::RemapEnvConstness(ident) => {
348+
if remap_env_constness.is_some() {
349+
panic!("duplicate modifier `remap_env_constness` for query `{}`", query.name);
350+
}
351+
remap_env_constness = Some(ident)
352+
}
338353
}
339354
}
340355
let desc = desc.unwrap_or_else(|| {
@@ -351,6 +366,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
351366
anon,
352367
eval_always,
353368
separate_provide_extern,
369+
remap_env_constness,
354370
}
355371
}
356372

@@ -485,6 +501,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
485501
if let Some(separate_provide_extern) = &modifiers.separate_provide_extern {
486502
attributes.push(quote! { (#separate_provide_extern) });
487503
}
504+
// Pass on the remap_env_constness modifier
505+
if let Some(remap_env_constness) = &modifiers.remap_env_constness {
506+
attributes.push(quote! { (#remap_env_constness) });
507+
}
488508

489509
// This uses the span of the query definition for the commas,
490510
// which can be important if we later encounter any ambiguity

compiler/rustc_middle/src/query/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ rustc_queries! {
880880
key: ty::ParamEnvAnd<'tcx, ConstAlloc<'tcx>>
881881
) -> Option<ty::ValTree<'tcx>> {
882882
desc { "destructure constant" }
883+
remap_env_constness
883884
}
884885

885886
/// Destructure a constant ADT or array into its variant index and its
@@ -888,6 +889,7 @@ rustc_queries! {
888889
key: ty::ParamEnvAnd<'tcx, &'tcx ty::Const<'tcx>>
889890
) -> mir::DestructuredConst<'tcx> {
890891
desc { "destructure constant" }
892+
remap_env_constness
891893
}
892894

893895
/// Dereference a constant reference or raw pointer and turn the result into a constant
@@ -896,6 +898,7 @@ rustc_queries! {
896898
key: ty::ParamEnvAnd<'tcx, &'tcx ty::Const<'tcx>>
897899
) -> &'tcx ty::Const<'tcx> {
898900
desc { "deref constant" }
901+
remap_env_constness
899902
}
900903

901904
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
@@ -1100,26 +1103,32 @@ rustc_queries! {
11001103
/// `ty.is_copy()`, etc, since that will prune the environment where possible.
11011104
query is_copy_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11021105
desc { "computing whether `{}` is `Copy`", env.value }
1106+
remap_env_constness
11031107
}
11041108
/// Query backing `TyS::is_sized`.
11051109
query is_sized_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11061110
desc { "computing whether `{}` is `Sized`", env.value }
1111+
remap_env_constness
11071112
}
11081113
/// Query backing `TyS::is_freeze`.
11091114
query is_freeze_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11101115
desc { "computing whether `{}` is freeze", env.value }
1116+
remap_env_constness
11111117
}
11121118
/// Query backing `TyS::is_unpin`.
11131119
query is_unpin_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11141120
desc { "computing whether `{}` is `Unpin`", env.value }
1121+
remap_env_constness
11151122
}
11161123
/// Query backing `TyS::needs_drop`.
11171124
query needs_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11181125
desc { "computing whether `{}` needs drop", env.value }
1126+
remap_env_constness
11191127
}
11201128
/// Query backing `TyS::has_significant_drop_raw`.
11211129
query has_significant_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
11221130
desc { "computing whether `{}` has a significant drop", env.value }
1131+
remap_env_constness
11231132
}
11241133

11251134
/// Query backing `TyS::is_structural_eq_shallow`.
@@ -1158,6 +1167,7 @@ rustc_queries! {
11581167
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>
11591168
) -> Result<ty::layout::TyAndLayout<'tcx>, ty::layout::LayoutError<'tcx>> {
11601169
desc { "computing layout of `{}`", key.value }
1170+
remap_env_constness
11611171
}
11621172

11631173
/// Compute a `FnAbi` suitable for indirect calls, i.e. to `fn` pointers.
@@ -1168,6 +1178,7 @@ rustc_queries! {
11681178
key: ty::ParamEnvAnd<'tcx, (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>)>
11691179
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, ty::layout::FnAbiError<'tcx>> {
11701180
desc { "computing call ABI of `{}` function pointers", key.value.0 }
1181+
remap_env_constness
11711182
}
11721183

11731184
/// Compute a `FnAbi` suitable for declaring/defining an `fn` instance, and for
@@ -1179,6 +1190,7 @@ rustc_queries! {
11791190
key: ty::ParamEnvAnd<'tcx, (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>)>
11801191
) -> Result<&'tcx abi::call::FnAbi<'tcx, Ty<'tcx>>, ty::layout::FnAbiError<'tcx>> {
11811192
desc { "computing call ABI of `{}`", key.value.0 }
1193+
remap_env_constness
11821194
}
11831195

11841196
query dylib_dependency_formats(_: CrateNum)
@@ -1463,6 +1475,7 @@ rustc_queries! {
14631475
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>
14641476
) -> ty::inhabitedness::DefIdForest {
14651477
desc { "computing the inhabitedness of `{:?}`", key }
1478+
remap_env_constness
14661479
}
14671480

14681481
query dep_kind(_: CrateNum) -> CrateDepKind {
@@ -1654,27 +1667,31 @@ rustc_queries! {
16541667
goal: ParamEnvAnd<'tcx, GenericArg<'tcx>>
16551668
) -> GenericArg<'tcx> {
16561669
desc { "normalizing `{}`", goal.value }
1670+
remap_env_constness
16571671
}
16581672

16591673
/// Do not call this query directly: invoke `normalize_erasing_regions` instead.
16601674
query normalize_mir_const_after_erasing_regions(
16611675
goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
16621676
) -> mir::ConstantKind<'tcx> {
16631677
desc { "normalizing `{}`", goal.value }
1678+
remap_env_constness
16641679
}
16651680

16661681
/// Do not call this query directly: invoke `try_normalize_erasing_regions` instead.
16671682
query try_normalize_generic_arg_after_erasing_regions(
16681683
goal: ParamEnvAnd<'tcx, GenericArg<'tcx>>
16691684
) -> Result<GenericArg<'tcx>, NoSolution> {
16701685
desc { "normalizing `{}`", goal.value }
1686+
remap_env_constness
16711687
}
16721688

16731689
/// Do not call this query directly: invoke `try_normalize_erasing_regions` instead.
16741690
query try_normalize_mir_const_after_erasing_regions(
16751691
goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
16761692
) -> Result<mir::ConstantKind<'tcx>, NoSolution> {
16771693
desc { "normalizing `{}`", goal.value }
1694+
remap_env_constness
16781695
}
16791696

16801697
query implied_outlives_bounds(
@@ -1836,6 +1853,7 @@ rustc_queries! {
18361853
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>
18371854
) -> Result<Option<ty::Instance<'tcx>>, ErrorReported> {
18381855
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }
1856+
remap_env_constness
18391857
}
18401858

18411859
query resolve_instance_of_const_arg(
@@ -1845,6 +1863,7 @@ rustc_queries! {
18451863
"resolving instance of the const argument `{}`",
18461864
ty::Instance::new(key.value.0.to_def_id(), key.value.2),
18471865
}
1866+
remap_env_constness
18481867
}
18491868

18501869
query normalize_opaque_types(key: &'tcx ty::List<ty::Predicate<'tcx>>) -> &'tcx ty::List<ty::Predicate<'tcx>> {
@@ -1859,6 +1878,7 @@ rustc_queries! {
18591878
/// size, to account for partial initialisation. See #49298 for details.)
18601879
query conservative_is_privately_uninhabited(key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
18611880
desc { "conservatively checking if {:?} is privately uninhabited", key }
1881+
remap_env_constness
18621882
}
18631883

18641884
query limits(key: ()) -> Limits {

compiler/rustc_middle/src/ty/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,12 @@ impl<'tcx, T> ParamEnvAnd<'tcx, T> {
14351435
pub fn into_parts(self) -> (ParamEnv<'tcx>, T) {
14361436
(self.param_env, self.value)
14371437
}
1438+
1439+
#[inline]
1440+
pub fn without_const(mut self) -> Self {
1441+
self.param_env = self.param_env.without_const();
1442+
self
1443+
}
14381444
}
14391445

14401446
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for ParamEnvAnd<'tcx, T>

compiler/rustc_middle/src/ty/query.rs

+14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ macro_rules! separate_provide_extern_default {
156156
};
157157
}
158158

159+
macro_rules! opt_remap_env_constness {
160+
([][$name:ident]) => {};
161+
([(remap_env_constness) $($rest:tt)*][$name:ident]) => {
162+
let $name = $name.without_const();
163+
};
164+
([$other:tt $($modifiers:tt)*][$name:ident]) => {
165+
opt_remap_env_constness!([$($modifiers)*][$name])
166+
};
167+
}
168+
159169
macro_rules! define_callbacks {
160170
(<$tcx:tt>
161171
$($(#[$attr:meta])*
@@ -202,6 +212,8 @@ macro_rules! define_callbacks {
202212
#[inline(always)]
203213
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
204214
let key = key.into_query_param();
215+
opt_remap_env_constness!([$($modifiers)*][key]);
216+
205217
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop);
206218

207219
let lookup = match cached {
@@ -229,6 +241,8 @@ macro_rules! define_callbacks {
229241
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
230242
{
231243
let key = key.into_query_param();
244+
opt_remap_env_constness!([$($modifiers)*][key]);
245+
232246
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, Clone::clone);
233247

234248
let lookup = match cached {

compiler/rustc_query_impl/src/plumbing.rs

+12
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ macro_rules! get_provider {
231231
};
232232
}
233233

234+
macro_rules! opt_remap_env_constness {
235+
([][$name:ident]) => {};
236+
([(remap_env_constness) $($rest:tt)*][$name:ident]) => {
237+
let $name = $name.without_const();
238+
};
239+
([$other:tt $($modifiers:tt)*][$name:ident]) => {
240+
opt_remap_env_constness!([$($modifiers)*][$name])
241+
};
242+
}
243+
234244
macro_rules! define_queries {
235245
(<$tcx:tt>
236246
$($(#[$attr:meta])*
@@ -247,6 +257,7 @@ macro_rules! define_queries {
247257
// Create an eponymous constructor for each query.
248258
$(#[allow(nonstandard_style)] $(#[$attr])*
249259
pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
260+
opt_remap_env_constness!([$($modifiers)*][key]);
250261
let kind = dep_graph::DepKind::$name;
251262
let name = stringify!($name);
252263
// Disable visible paths printing for performance reasons.
@@ -521,6 +532,7 @@ macro_rules! define_queries_struct {
521532
lookup: QueryLookup,
522533
mode: QueryMode,
523534
) -> Option<query_stored::$name<$tcx>> {
535+
opt_remap_env_constness!([$($modifiers)*][key]);
524536
let qcx = QueryCtxt { tcx, queries: self };
525537
get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
526538
})*

0 commit comments

Comments
 (0)