Skip to content

Commit 65b76a4

Browse files
committed
Rename trait Value as FromCycleError.
`Value` is an unhelpfully generic name. Standard naming procedure for a trait with a single method is for the trait name to match the method name, which is what this commit does. Likewise, the enclosing module is renamed from `values` to `from_cycle_error`. Also add a comment about some non-obvious behaviour.
1 parent 19b3617 commit 65b76a4

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ use crate::{dep_graph, mir, thir};
150150
// `Providers` that the driver creates (using several `rustc_*` crates).
151151
//
152152
// The result type of each query must implement `Clone`, and additionally
153-
// `ty::query::values::Value`, which produces an appropriate placeholder
154-
// (error) value if the query resulted in a query cycle.
153+
// `ty::query::from_cycle_error::FromCycleError`, which produces an appropriate
154+
// placeholder (error) value if the query resulted in a query cycle.
155155
// Queries marked with `cycle_fatal` do not need the latter implementation,
156-
// as they will raise an fatal error on query cycles instead.
156+
// as they will raise a fatal error on query cycles instead.
157157
rustc_queries! {
158158
/// Caches the expansion of a derive proc macro, e.g. `#[derive(Serialize)]`.
159159
/// The key is:

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ use rustc_span::{ErrorGuaranteed, Span};
1717

1818
use crate::job::report_cycle;
1919

20-
pub(crate) trait Value<'tcx>: Sized {
20+
pub(crate) trait FromCycleError<'tcx>: Sized {
21+
/// Try to produce a `Self` value that represents an error form (e.g. `TyKind::Error`).
22+
///
23+
/// Note: the default impl calls `raise_fatal`, ending compilation immediately! Only a few
24+
/// types override this with a non-fatal impl.
2125
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self;
2226
}
2327

24-
impl<'tcx, T> Value<'tcx> for T {
28+
impl<'tcx, T> FromCycleError<'tcx> for T {
2529
default fn from_cycle_error(
2630
tcx: TyCtxt<'tcx>,
2731
cycle_error: CycleError,
2832
_guar: ErrorGuaranteed,
2933
) -> T {
3034
let Some(guar) = tcx.sess.dcx().has_errors() else {
3135
bug!(
32-
"<{} as Value>::from_cycle_error called without errors: {:#?}",
36+
"<{} as FromCycleError>::from_cycle_error called without errors: {:#?}",
3337
std::any::type_name::<T>(),
3438
cycle_error.cycle,
3539
);
@@ -38,21 +42,21 @@ impl<'tcx, T> Value<'tcx> for T {
3842
}
3943
}
4044

41-
impl<'tcx> Value<'tcx> for Ty<'_> {
45+
impl<'tcx> FromCycleError<'tcx> for Ty<'_> {
4246
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
4347
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
4448
// FIXME: Represent the above fact in the trait system somehow.
4549
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
4650
}
4751
}
4852

49-
impl<'tcx> Value<'tcx> for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
53+
impl<'tcx> FromCycleError<'tcx> for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
5054
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
5155
Err(CyclePlaceholder(guar))
5256
}
5357
}
5458

55-
impl<'tcx> Value<'tcx> for ty::SymbolName<'_> {
59+
impl<'tcx> FromCycleError<'tcx> for ty::SymbolName<'_> {
5660
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: CycleError, _guar: ErrorGuaranteed) -> Self {
5761
// SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
5862
// FIXME: Represent the above fact in the trait system somehow.
@@ -64,7 +68,7 @@ impl<'tcx> Value<'tcx> for ty::SymbolName<'_> {
6468
}
6569
}
6670

67-
impl<'tcx> Value<'tcx> for ty::Binder<'_, ty::FnSig<'_>> {
71+
impl<'tcx> FromCycleError<'tcx> for ty::Binder<'_, ty::FnSig<'_>> {
6872
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
6973
let err = Ty::new_error(tcx, guar);
7074

@@ -94,7 +98,7 @@ impl<'tcx> Value<'tcx> for ty::Binder<'_, ty::FnSig<'_>> {
9498
}
9599
}
96100

97-
impl<'tcx> Value<'tcx> for Representability {
101+
impl<'tcx> FromCycleError<'tcx> for Representability {
98102
fn from_cycle_error(
99103
tcx: TyCtxt<'tcx>,
100104
cycle_error: CycleError,
@@ -130,19 +134,19 @@ impl<'tcx> Value<'tcx> for Representability {
130134
}
131135
}
132136

133-
impl<'tcx> Value<'tcx> for ty::EarlyBinder<'_, Ty<'_>> {
137+
impl<'tcx> FromCycleError<'tcx> for ty::EarlyBinder<'_, Ty<'_>> {
134138
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
135139
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar))
136140
}
137141
}
138142

139-
impl<'tcx> Value<'tcx> for ty::EarlyBinder<'_, ty::Binder<'_, ty::FnSig<'_>>> {
143+
impl<'tcx> FromCycleError<'tcx> for ty::EarlyBinder<'_, ty::Binder<'_, ty::FnSig<'_>>> {
140144
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
141145
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar))
142146
}
143147
}
144148

145-
impl<'tcx> Value<'tcx> for &[ty::Variance] {
149+
impl<'tcx> FromCycleError<'tcx> for &[ty::Variance] {
146150
fn from_cycle_error(
147151
tcx: TyCtxt<'tcx>,
148152
cycle_error: CycleError,
@@ -190,7 +194,7 @@ fn search_for_cycle_permutation<Q, T>(
190194
otherwise()
191195
}
192196

193-
impl<'tcx, T> Value<'tcx> for Result<T, &'_ ty::layout::LayoutError<'_>> {
197+
impl<'tcx, T> FromCycleError<'tcx> for Result<T, &'_ ty::layout::LayoutError<'_>> {
194198
fn from_cycle_error(
195199
tcx: TyCtxt<'tcx>,
196200
cycle_error: CycleError,

compiler/rustc_query_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ use rustc_middle::ty::TyCtxt;
1818
use rustc_span::Span;
1919

2020
pub use crate::dep_kind_vtables::make_dep_kind_vtables;
21+
use crate::from_cycle_error::FromCycleError;
2122
pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack};
2223
pub use crate::plumbing::{collect_active_jobs_from_all_queries, query_key_hash_verify_all};
2324
use crate::plumbing::{encode_all_query_results, try_mark_green};
2425
use crate::profiling_support::QueryKeyStringCache;
2526
pub use crate::profiling_support::alloc_self_profile_query_strings;
26-
use crate::values::Value;
2727

2828
#[macro_use]
2929
mod plumbing;
3030

3131
mod dep_kind_vtables;
3232
mod error;
3333
mod execution;
34+
mod from_cycle_error;
3435
mod job;
3536
mod profiling_support;
36-
mod values;
3737

3838
/// Trait that knows how to look up the [`QueryVTable`] for a particular query.
3939
///

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ macro_rules! define_queries {
603603
None
604604
}),
605605
value_from_cycle_error: |tcx, cycle, guar| {
606-
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
606+
let result: queries::$name::Value<'tcx> =
607+
FromCycleError::from_cycle_error(tcx, cycle, guar);
607608
erase::erase_val(result)
608609
},
609610
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),

0 commit comments

Comments
 (0)