Skip to content

Commit 64e18fe

Browse files
committed
Allow ensure queries to return Result<(), ErrorGuaranteed>
1 parent 57178e1 commit 64e18fe

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1899,10 +1899,10 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
18991899

19001900
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
19011901
let items = tcx.hir_module_items(module);
1902-
let mut res = items.par_items(|item| tcx.check_well_formed(item.owner_id));
1903-
res = res.and(items.par_impl_items(|item| tcx.check_well_formed(item.owner_id)));
1904-
res = res.and(items.par_trait_items(|item| tcx.check_well_formed(item.owner_id)));
1905-
res.and(items.par_foreign_items(|item| tcx.check_well_formed(item.owner_id)))
1902+
let mut res = items.par_items(|item| tcx.ensure().check_well_formed(item.owner_id));
1903+
res = res.and(items.par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1904+
res = res.and(items.par_trait_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1905+
res.and(items.par_foreign_items(|item| tcx.ensure().check_well_formed(item.owner_id)))
19061906
}
19071907

19081908
fn error_392(

Diff for: compiler/rustc_hir_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
206206
}
207207

208208
tcx.sess.time("wf_checking", || {
209-
tcx.hir().try_par_for_each_module(|module| tcx.check_mod_type_wf(module))
209+
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))
210210
})?;
211211

212212
// NOTE: This is copy/pasted in librustdoc/core.rs and should be kept in sync.

Diff for: compiler/rustc_macros/src/query.rs

+10
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ struct QueryModifiers {
114114

115115
/// Generate a `feed` method to set the query's value from another query.
116116
feedable: Option<Ident>,
117+
118+
/// Forward the result on ensure if the query gets recomputed, and
119+
/// return `Ok(())` otherwise. Only applicable to queries returning
120+
/// `Result<(), ErrorGuaranteed>`
121+
ensure_forwards_result_if_red: Option<Ident>,
117122
}
118123

119124
fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
@@ -128,6 +133,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
128133
let mut depth_limit = None;
129134
let mut separate_provide_extern = None;
130135
let mut feedable = None;
136+
let mut ensure_forwards_result_if_red = None;
131137

132138
while !input.is_empty() {
133139
let modifier: Ident = input.parse()?;
@@ -187,6 +193,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
187193
try_insert!(separate_provide_extern = modifier);
188194
} else if modifier == "feedable" {
189195
try_insert!(feedable = modifier);
196+
} else if modifier == "ensure_forwards_result_if_red" {
197+
try_insert!(ensure_forwards_result_if_red = modifier);
190198
} else {
191199
return Err(Error::new(modifier.span(), "unknown query modifier"));
192200
}
@@ -206,6 +214,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
206214
depth_limit,
207215
separate_provide_extern,
208216
feedable,
217+
ensure_forwards_result_if_red,
209218
})
210219
}
211220

@@ -325,6 +334,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
325334
eval_always,
326335
depth_limit,
327336
separate_provide_extern,
337+
ensure_forwards_result_if_red,
328338
);
329339

330340
if modifiers.cache.is_some() {

Diff for: compiler/rustc_middle/src/query/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use crate::mir::interpret::{
2525
use crate::mir::interpret::{LitToConstError, LitToConstInput};
2626
use crate::mir::mono::CodegenUnit;
2727
use crate::query::erase::{erase, restore, Erase};
28-
use crate::query::plumbing::{query_ensure, query_get_at, CyclePlaceholder, DynamicQuery};
28+
use crate::query::plumbing::{
29+
query_ensure, query_ensure_error_guaranteed, query_get_at, CyclePlaceholder, DynamicQuery,
30+
};
2931
use crate::thir;
3032
use crate::traits::query::{
3133
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
@@ -977,6 +979,7 @@ rustc_queries! {
977979

978980
query check_mod_type_wf(key: LocalModDefId) -> Result<(), ErrorGuaranteed> {
979981
desc { |tcx| "checking that types are well-formed in {}", describe_as_module(key, tcx) }
982+
ensure_forwards_result_if_red
980983
}
981984

982985
query collect_mod_item_types(key: LocalModDefId) -> () {
@@ -1511,6 +1514,7 @@ rustc_queries! {
15111514

15121515
query check_well_formed(key: hir::OwnerId) -> Result<(), ErrorGuaranteed> {
15131516
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key) }
1517+
ensure_forwards_result_if_red
15141518
}
15151519

15161520
// The `DefId`s of all non-generic functions and statics in the given crate

Diff for: compiler/rustc_middle/src/query/plumbing.rs

+49-3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,39 @@ pub fn query_ensure<'tcx, Cache>(
173173
}
174174
}
175175

176+
#[inline]
177+
pub fn query_ensure_error_guaranteed<'tcx, Cache>(
178+
tcx: TyCtxt<'tcx>,
179+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
180+
query_cache: &Cache,
181+
key: Cache::Key,
182+
check_cache: bool,
183+
) -> Result<(), ErrorGuaranteed>
184+
where
185+
Cache: QueryCache<Value = super::erase::Erase<Result<(), ErrorGuaranteed>>>,
186+
{
187+
let key = key.into_query_param();
188+
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
189+
super::erase::restore(res)
190+
} else {
191+
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache })
192+
.map(super::erase::restore)
193+
.unwrap_or(Ok(()))
194+
}
195+
}
196+
197+
macro_rules! query_ensure {
198+
([]$($args:tt)*) => {
199+
query_ensure($($args)*)
200+
};
201+
([(ensure_forwards_result_if_red) $($rest:tt)*]$($args:tt)*) => {
202+
query_ensure_error_guaranteed($($args)*)
203+
};
204+
([$other:tt $($modifiers:tt)*]$($args:tt)*) => {
205+
query_ensure!([$($modifiers)*]$($args)*)
206+
};
207+
}
208+
176209
macro_rules! query_helper_param_ty {
177210
(DefId) => { impl IntoQueryParam<DefId> };
178211
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
@@ -220,6 +253,18 @@ macro_rules! separate_provide_extern_decl {
220253
};
221254
}
222255

256+
macro_rules! ensure_result {
257+
([][$ty:ty]) => {
258+
()
259+
};
260+
([(ensure_forwards_result_if_red) $($rest:tt)*][$ty:ty]) => {
261+
Result<(), ErrorGuaranteed>
262+
};
263+
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
264+
ensure_result!([$($modifiers)*][$($args)*])
265+
};
266+
}
267+
223268
macro_rules! separate_provide_extern_default {
224269
([][$name:ident]) => {
225270
()
@@ -343,14 +388,15 @@ macro_rules! define_callbacks {
343388
impl<'tcx> TyCtxtEnsure<'tcx> {
344389
$($(#[$attr])*
345390
#[inline(always)]
346-
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
347-
query_ensure(
391+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> ensure_result!([$($modifiers)*][$V]) {
392+
query_ensure!(
393+
[$($modifiers)*]
348394
self.tcx,
349395
self.tcx.query_system.fns.engine.$name,
350396
&self.tcx.query_system.caches.$name,
351397
key.into_query_param(),
352398
false,
353-
);
399+
)
354400
})*
355401
}
356402

0 commit comments

Comments
 (0)