Skip to content

Commit b4f9e30

Browse files
Rollup merge of rust-lang#153979 - nnethercote:rename-cycle-things, r=petrochenkov
Rename various query cycle things. The existing names have bugged me for a while. Changes: - `CycleError` -> `Cycle`. Because we normally use "error" for a `Diag` with `Level::Error`, and this type is just a precursor to that. Also, many existing locals of this type are already named `cycle`. - `CycleError::cycle` -> `Cycle::frames`. Because it is a sequence of frames, and we want to avoid `Cycle::cycle` (and `cycle.cycle`). - `cycle_error` -> `find_and_handle_cycle`. Because that's what it does. The existing name is just a non-descript noun phrase. - `mk_cycle` -> `handle_cycle`. Because it doesn't make the cycle; the cycle is already made. It handles the cycle, which involves (a) creating the error, and (b) handling the error. - `report_cycle` -> `create_cycle_error`. Because that's what it does: creates the cycle error (i.e. the `Diag`). - `value_from_cycle_error` -> `handle_cycle_error_fn`. Because most cases don't produce a value, they just emit an error and quit. And the `_fn` suffix is for consistency with other vtable fns. - `from_cycle_error` -> `handle_cycle_error`. A similar story for this module. r? @petrochenkov
2 parents 351622f + 90ea993 commit b4f9e30

File tree

10 files changed

+77
-86
lines changed

10 files changed

+77
-86
lines changed

compiler/rustc_middle/src/queries.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ use crate::{mir, thir};
131131
// `Providers` that the driver creates (using several `rustc_*` crates).
132132
//
133133
// The result type of each query must implement `Clone`. Additionally
134-
// `ty::query::from_cycle_error::FromCycleError` can be implemented which produces an appropriate
134+
// `QueryVTable::handle_cycle_error_fn` can be used to produce an appropriate
135135
// placeholder (error) value if the query resulted in a query cycle.
136-
// Queries without a `FromCycleError` implementation will raise a fatal error on query
137-
// cycles instead.
136+
// Queries without a custom `handle_cycle_error_fn` implementation will raise a
137+
// fatal error on query cycles instead.
138138
rustc_queries! {
139139
/// Caches the expansion of a derive proc macro, e.g. `#[derive(Serialize)]`.
140140
/// The key is:
@@ -577,7 +577,7 @@ rustc_queries! {
577577

578578
/// Checks whether a type is representable or infinitely sized
579579
//
580-
// Infinitely sized types will cause a cycle. The `value_from_cycle_error` impl will print
580+
// Infinitely sized types will cause a cycle. The query's `handle_cycle_error_fn` will print
581581
// a custom error about the infinite size and then abort compilation. (In the past we
582582
// recovered and continued, but in practice that leads to confusing subsequent error
583583
// messages about cycles that then abort.)

compiler/rustc_middle/src/query/job.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66
use parking_lot::{Condvar, Mutex};
77
use rustc_span::Span;
88

9-
use crate::query::CycleError;
9+
use crate::query::Cycle;
1010
use crate::ty::TyCtxt;
1111

1212
/// A value uniquely identifying an active query job.
@@ -59,7 +59,7 @@ pub struct QueryWaiter<'tcx> {
5959
pub parent: Option<QueryJobId>,
6060
pub condvar: Condvar,
6161
pub span: Span,
62-
pub cycle: Mutex<Option<CycleError<'tcx>>>,
62+
pub cycle: Mutex<Option<Cycle<'tcx>>>,
6363
}
6464

6565
#[derive(Clone, Debug)]
@@ -79,7 +79,7 @@ impl<'tcx> QueryLatch<'tcx> {
7979
tcx: TyCtxt<'tcx>,
8080
query: Option<QueryJobId>,
8181
span: Span,
82-
) -> Result<(), CycleError<'tcx>> {
82+
) -> Result<(), Cycle<'tcx>> {
8383
let mut waiters_guard = self.waiters.lock();
8484
let Some(waiters) = &mut *waiters_guard else {
8585
return Ok(()); // already complete

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ pub use self::into_query_key::IntoQueryKey;
55
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
66
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
77
pub use self::plumbing::{
8-
ActiveKeyStatus, CycleError, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable,
9-
TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
8+
ActiveKeyStatus, Cycle, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable, TyCtxtAt,
9+
TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
1010
};
1111
pub use self::stack::QueryStackFrame;
1212
pub use crate::queries::Providers;

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ pub enum ActiveKeyStatus<'tcx> {
4747
}
4848

4949
#[derive(Debug)]
50-
pub struct CycleError<'tcx> {
50+
pub struct Cycle<'tcx> {
5151
/// The query and related span that uses the cycle.
5252
pub usage: Option<QueryStackFrame<'tcx>>,
5353

5454
/// The span here corresponds to the reason for which this query was required.
55-
pub cycle: Vec<QueryStackFrame<'tcx>>,
55+
pub frames: Vec<QueryStackFrame<'tcx>>,
5656
}
5757

5858
#[derive(Debug)]
@@ -111,13 +111,10 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
111111

112112
/// Function pointer that handles a cycle error. `error` must be consumed, e.g. with `emit` (if
113113
/// it should be emitted) or `delay_as_bug` (if it need not be emitted because an alternative
114-
/// error is created and emitted).
115-
pub value_from_cycle_error: fn(
116-
tcx: TyCtxt<'tcx>,
117-
key: C::Key,
118-
cycle_error: CycleError<'tcx>,
119-
error: Diag<'_>,
120-
) -> C::Value,
114+
/// error is created and emitted). A value may be returned, or (more commonly) the function may
115+
/// just abort after emitting the error.
116+
pub handle_cycle_error_fn:
117+
fn(tcx: TyCtxt<'tcx>, key: C::Key, cycle: Cycle<'tcx>, error: Diag<'_>) -> C::Value,
121118

122119
pub format_value: fn(&C::Value) -> String,
123120

compiler/rustc_query_impl/src/execution.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use rustc_data_structures::{outline, sharded, sync};
88
use rustc_errors::FatalError;
99
use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, SerializedDepNodeIndex};
1010
use rustc_middle::query::{
11-
ActiveKeyStatus, CycleError, EnsureMode, QueryCache, QueryJob, QueryJobId, QueryKey,
12-
QueryLatch, QueryMode, QueryState, QueryVTable,
11+
ActiveKeyStatus, Cycle, EnsureMode, QueryCache, QueryJob, QueryJobId, QueryKey, QueryLatch,
12+
QueryMode, QueryState, QueryVTable,
1313
};
1414
use rustc_middle::ty::TyCtxt;
1515
use rustc_middle::verify_ich::incremental_verify_ich;
1616
use rustc_span::{DUMMY_SP, Span};
1717
use tracing::warn;
1818

1919
use crate::dep_graph::{DepNode, DepNodeIndex};
20-
use crate::job::{QueryJobInfo, QueryJobMap, find_cycle_in_stack, report_cycle};
20+
use crate::job::{QueryJobInfo, QueryJobMap, create_cycle_error, find_cycle_in_stack};
2121
use crate::plumbing::{current_query_job, loadable_from_disk, next_job_id, start_query};
2222
use crate::query_impl::for_each_query_vtable;
2323

@@ -108,14 +108,14 @@ fn collect_active_query_jobs_inner<'tcx, C>(
108108

109109
#[cold]
110110
#[inline(never)]
111-
fn mk_cycle<'tcx, C: QueryCache>(
111+
fn handle_cycle<'tcx, C: QueryCache>(
112112
query: &'tcx QueryVTable<'tcx, C>,
113113
tcx: TyCtxt<'tcx>,
114114
key: C::Key,
115-
cycle_error: CycleError<'tcx>,
115+
cycle: Cycle<'tcx>,
116116
) -> C::Value {
117-
let error = report_cycle(tcx, &cycle_error);
118-
(query.value_from_cycle_error)(tcx, key, cycle_error, error)
117+
let error = create_cycle_error(tcx, &cycle);
118+
(query.handle_cycle_error_fn)(tcx, key, cycle, error)
119119
}
120120

121121
/// Guard object representing the responsibility to execute a query job and
@@ -194,7 +194,7 @@ where
194194

195195
#[cold]
196196
#[inline(never)]
197-
fn cycle_error<'tcx, C: QueryCache>(
197+
fn find_and_handle_cycle<'tcx, C: QueryCache>(
198198
query: &'tcx QueryVTable<'tcx, C>,
199199
tcx: TyCtxt<'tcx>,
200200
key: C::Key,
@@ -205,8 +205,8 @@ fn cycle_error<'tcx, C: QueryCache>(
205205
// We need the complete map to ensure we find a cycle to break.
206206
let job_map = collect_active_query_jobs(tcx, CollectActiveJobsKind::FullNoContention);
207207

208-
let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(), span);
209-
(mk_cycle(query, tcx, key, error), None)
208+
let cycle = find_cycle_in_stack(try_execute, job_map, &current_query_job(), span);
209+
(handle_cycle(query, tcx, key, cycle), None)
210210
}
211211

212212
#[inline(always)]
@@ -250,7 +250,7 @@ fn wait_for_query<'tcx, C: QueryCache>(
250250

251251
(v, Some(index))
252252
}
253-
Err(cycle) => (mk_cycle(query, tcx, key, cycle), None),
253+
Err(cycle) => (handle_cycle(query, tcx, key, cycle), None),
254254
}
255255
}
256256

@@ -334,7 +334,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
334334

335335
// If we are single-threaded we know that we have cycle error,
336336
// so we just return the error.
337-
cycle_error(query, tcx, key, id, span)
337+
find_and_handle_cycle(query, tcx, key, id, span)
338338
}
339339
}
340340
ActiveKeyStatus::Poisoned => FatalError.raise(),

compiler/rustc_query_impl/src/from_cycle_error.rs renamed to compiler/rustc_query_impl/src/handle_cycle_error.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, Res};
1111
use rustc_middle::bug;
1212
use rustc_middle::queries::{QueryVTables, TaggedQueryKey};
13-
use rustc_middle::query::CycleError;
13+
use rustc_middle::query::Cycle;
1414
use rustc_middle::query::erase::erase_val;
1515
use rustc_middle::ty::layout::LayoutError;
1616
use rustc_middle::ty::{self, Ty, TyCtxt};
1717
use rustc_span::def_id::{DefId, LocalDefId};
1818
use rustc_span::{ErrorGuaranteed, Span};
1919

20-
use crate::job::report_cycle;
20+
use crate::job::create_cycle_error;
2121

2222
pub(crate) fn specialize_query_vtables<'tcx>(vtables: &mut QueryVTables<'tcx>) {
23-
vtables.fn_sig.value_from_cycle_error = |tcx, key, _, err| {
23+
vtables.fn_sig.handle_cycle_error_fn = |tcx, key, _, err| {
2424
let guar = err.delay_as_bug();
2525
erase_val(fn_sig(tcx, key, guar))
2626
};
2727

28-
vtables.check_representability.value_from_cycle_error =
28+
vtables.check_representability.handle_cycle_error_fn =
2929
|tcx, _, cycle, _err| check_representability(tcx, cycle);
3030

31-
vtables.check_representability_adt_ty.value_from_cycle_error =
31+
vtables.check_representability_adt_ty.handle_cycle_error_fn =
3232
|tcx, _, cycle, _err| check_representability(tcx, cycle);
3333

34-
vtables.variances_of.value_from_cycle_error = |tcx, key, _, err| {
34+
vtables.variances_of.handle_cycle_error_fn = |tcx, key, _, err| {
3535
let _guar = err.delay_as_bug();
3636
erase_val(variances_of(tcx, key))
3737
};
3838

39-
vtables.layout_of.value_from_cycle_error = |tcx, _, cycle, err| {
39+
vtables.layout_of.handle_cycle_error_fn = |tcx, _, cycle, err| {
4040
let _guar = err.delay_as_bug();
4141
erase_val(Err(layout_of(tcx, cycle)))
4242
}
@@ -72,10 +72,10 @@ fn fn_sig<'tcx>(
7272
)))
7373
}
7474

75-
fn check_representability<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>) -> ! {
75+
fn check_representability<'tcx>(tcx: TyCtxt<'tcx>, cycle: Cycle<'tcx>) -> ! {
7676
let mut item_and_field_ids = Vec::new();
7777
let mut representable_ids = FxHashSet::default();
78-
for frame in &cycle_error.cycle {
78+
for frame in &cycle.frames {
7979
if let TaggedQueryKey::check_representability(def_id) = frame.tagged_key
8080
&& tcx.def_kind(def_id) == DefKind::Field
8181
{
@@ -88,7 +88,7 @@ fn check_representability<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>
8888
item_and_field_ids.push((item_id.expect_local(), field_id));
8989
}
9090
}
91-
for frame in &cycle_error.cycle {
91+
for frame in &cycle.frames {
9292
if let TaggedQueryKey::check_representability_adt_ty(key) = frame.tagged_key
9393
&& let Some(adt) = key.ty_adt_def()
9494
&& let Some(def_id) = adt.did().as_local()
@@ -127,14 +127,11 @@ fn search_for_cycle_permutation<Q, T>(
127127
otherwise()
128128
}
129129

130-
fn layout_of<'tcx>(
131-
tcx: TyCtxt<'tcx>,
132-
cycle_error: CycleError<'tcx>,
133-
) -> &'tcx ty::layout::LayoutError<'tcx> {
130+
fn layout_of<'tcx>(tcx: TyCtxt<'tcx>, cycle: Cycle<'tcx>) -> &'tcx ty::layout::LayoutError<'tcx> {
134131
let diag = search_for_cycle_permutation(
135-
&cycle_error.cycle,
136-
|cycle| {
137-
if let TaggedQueryKey::layout_of(key) = cycle[0].tagged_key
132+
&cycle.frames,
133+
|frames| {
134+
if let TaggedQueryKey::layout_of(key) = frames[0].tagged_key
138135
&& let ty::Coroutine(def_id, _) = key.value.kind()
139136
&& let Some(def_id) = def_id.as_local()
140137
&& let def_kind = tcx.def_kind(def_id)
@@ -158,7 +155,7 @@ fn layout_of<'tcx>(
158155
tcx.def_kind_descr_article(def_kind, def_id.to_def_id()),
159156
tcx.def_kind_descr(def_kind, def_id.to_def_id()),
160157
);
161-
for (i, frame) in cycle.iter().enumerate() {
158+
for (i, frame) in frames.iter().enumerate() {
162159
let TaggedQueryKey::layout_of(frame_key) = frame.tagged_key else {
163160
continue;
164161
};
@@ -169,7 +166,7 @@ fn layout_of<'tcx>(
169166
continue;
170167
};
171168
let frame_span =
172-
frame.tagged_key.default_span(tcx, cycle[(i + 1) % cycle.len()].span);
169+
frame.tagged_key.default_span(tcx, frames[(i + 1) % frames.len()].span);
173170
if frame_span.is_dummy() {
174171
continue;
175172
}
@@ -203,7 +200,7 @@ fn layout_of<'tcx>(
203200
ControlFlow::Continue(())
204201
}
205202
},
206-
|| report_cycle(tcx, &cycle_error),
203+
|| create_cycle_error(tcx, &cycle),
207204
);
208205

209206
let guar = diag.emit();

compiler/rustc_query_impl/src/job.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_errors::{Diag, DiagCtxtHandle};
88
use rustc_hir::def::DefKind;
99
use rustc_middle::queries::TaggedQueryKey;
10-
use rustc_middle::query::{
11-
CycleError, QueryJob, QueryJobId, QueryLatch, QueryStackFrame, QueryWaiter,
12-
};
10+
use rustc_middle::query::{Cycle, QueryJob, QueryJobId, QueryLatch, QueryStackFrame, QueryWaiter};
1311
use rustc_middle::ty::TyCtxt;
1412
use rustc_span::{DUMMY_SP, Span};
1513

@@ -58,29 +56,28 @@ pub(crate) fn find_cycle_in_stack<'tcx>(
5856
job_map: QueryJobMap<'tcx>,
5957
current_job: &Option<QueryJobId>,
6058
span: Span,
61-
) -> CycleError<'tcx> {
62-
// Find the waitee amongst `current_job` parents
63-
let mut cycle = Vec::new();
59+
) -> Cycle<'tcx> {
60+
// Find the waitee amongst `current_job` parents.
61+
let mut frames = Vec::new();
6462
let mut current_job = Option::clone(current_job);
6563

6664
while let Some(job) = current_job {
6765
let info = &job_map.map[&job];
68-
cycle.push(QueryStackFrame { span: info.job.span, tagged_key: info.tagged_key });
66+
frames.push(QueryStackFrame { span: info.job.span, tagged_key: info.tagged_key });
6967

7068
if job == id {
71-
cycle.reverse();
72-
73-
// This is the end of the cycle
74-
// The span entry we included was for the usage
75-
// of the cycle itself, and not part of the cycle
76-
// Replace it with the span which caused the cycle to form
77-
cycle[0].span = span;
78-
// Find out why the cycle itself was used
69+
frames.reverse();
70+
71+
// This is the end of the cycle. The span entry we included was for
72+
// the usage of the cycle itself, and not part of the cycle.
73+
// Replace it with the span which caused the cycle to form.
74+
frames[0].span = span;
75+
// Find out why the cycle itself was used.
7976
let usage = try {
8077
let parent = info.job.parent?;
8178
QueryStackFrame { span: info.job.span, tagged_key: job_map.tagged_key_of(parent) }
8279
};
83-
return CycleError { usage, cycle };
80+
return Cycle { usage, frames };
8481
}
8582

8683
current_job = info.job.parent;
@@ -319,9 +316,9 @@ fn remove_cycle<'tcx>(
319316
.map(|(span, job)| QueryStackFrame { span, tagged_key: job_map.tagged_key_of(job) });
320317

321318
// Create the cycle error
322-
let error = CycleError {
319+
let error = Cycle {
323320
usage,
324-
cycle: stack
321+
frames: stack
325322
.iter()
326323
.map(|&(span, job)| QueryStackFrame {
327324
span,
@@ -454,27 +451,27 @@ pub fn print_query_stack<'tcx>(
454451

455452
#[inline(never)]
456453
#[cold]
457-
pub(crate) fn report_cycle<'tcx>(
454+
pub(crate) fn create_cycle_error<'tcx>(
458455
tcx: TyCtxt<'tcx>,
459-
CycleError { usage, cycle: stack }: &CycleError<'tcx>,
456+
Cycle { usage, frames }: &Cycle<'tcx>,
460457
) -> Diag<'tcx> {
461-
assert!(!stack.is_empty());
458+
assert!(!frames.is_empty());
462459

463-
let span = stack[0].tagged_key.default_span(tcx, stack[1 % stack.len()].span);
460+
let span = frames[0].tagged_key.default_span(tcx, frames[1 % frames.len()].span);
464461

465462
let mut cycle_stack = Vec::new();
466463

467464
use crate::error::StackCount;
468-
let stack_bottom = stack[0].tagged_key.description(tcx);
469-
let stack_count = if stack.len() == 1 {
465+
let stack_bottom = frames[0].tagged_key.description(tcx);
466+
let stack_count = if frames.len() == 1 {
470467
StackCount::Single { stack_bottom: stack_bottom.clone() }
471468
} else {
472469
StackCount::Multiple { stack_bottom: stack_bottom.clone() }
473470
};
474471

475-
for i in 1..stack.len() {
476-
let frame = &stack[i];
477-
let span = frame.tagged_key.default_span(tcx, stack[(i + 1) % stack.len()].span);
472+
for i in 1..frames.len() {
473+
let frame = &frames[i];
474+
let span = frame.tagged_key.default_span(tcx, frames[(i + 1) % frames.len()].span);
478475
cycle_stack
479476
.push(crate::error::CycleStack { span, desc: frame.tagged_key.description(tcx) });
480477
}
@@ -484,12 +481,12 @@ pub(crate) fn report_cycle<'tcx>(
484481
usage: usage.tagged_key.description(tcx),
485482
});
486483

487-
let alias = if stack
484+
let alias = if frames
488485
.iter()
489486
.all(|frame| frame.tagged_key.def_kind(tcx) == Some(DefKind::TyAlias))
490487
{
491488
Some(crate::error::Alias::Ty)
492-
} else if stack.iter().all(|frame| frame.tagged_key.def_kind(tcx) == Some(DefKind::TraitAlias))
489+
} else if frames.iter().all(|frame| frame.tagged_key.def_kind(tcx) == Some(DefKind::TraitAlias))
493490
{
494491
Some(crate::error::Alias::Trait)
495492
} else {

0 commit comments

Comments
 (0)