1
Fork 0

Value recovery can take the whole CycleError

This commit is contained in:
Michael Goulet 2023-11-08 05:56:32 +00:00
parent ca663b06c5
commit 755b2da841
8 changed files with 54 additions and 31 deletions

View file

@ -53,7 +53,7 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool, fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
pub hash_result: HashResult<C::Value>, pub hash_result: HashResult<C::Value>,
pub value_from_cycle_error: pub value_from_cycle_error:
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> C::Value, fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
pub format_value: fn(&C::Value) -> String, pub format_value: fn(&C::Value) -> String,
} }

View file

@ -6,7 +6,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_middle::ty::Representability; use rustc_middle::ty::Representability;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_query_system::query::QueryInfo; use rustc_query_system::query::CycleError;
use rustc_query_system::Value; use rustc_query_system::Value;
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::{ErrorGuaranteed, Span}; use rustc_span::{ErrorGuaranteed, Span};
@ -14,7 +14,7 @@ use rustc_span::{ErrorGuaranteed, Span};
use std::fmt::Write; use std::fmt::Write;
impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> { impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`. // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
// FIXME: Represent the above fact in the trait system somehow. // FIXME: Represent the above fact in the trait system somehow.
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) } unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
@ -22,13 +22,13 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
} }
impl<'tcx> Value<TyCtxt<'tcx>> for Result<ty::EarlyBinder<Ty<'_>>, CyclePlaceholder> { impl<'tcx> Value<TyCtxt<'tcx>> for Result<ty::EarlyBinder<Ty<'_>>, CyclePlaceholder> {
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
Err(CyclePlaceholder(guar)) Err(CyclePlaceholder(guar))
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> { impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo], _guar: ErrorGuaranteed) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, _guar: ErrorGuaranteed) -> Self {
// SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`. // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
// FIXME: Represent the above fact in the trait system somehow. // FIXME: Represent the above fact in the trait system somehow.
unsafe { unsafe {
@ -40,10 +40,14 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> { impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(
tcx: TyCtxt<'tcx>,
cycle_error: &CycleError,
guar: ErrorGuaranteed,
) -> Self {
let err = Ty::new_error(tcx, guar); let err = Ty::new_error(tcx, guar);
let arity = if let Some(frame) = stack.get(0) let arity = if let Some(frame) = cycle_error.cycle.get(0)
&& frame.query.dep_kind == dep_kinds::fn_sig && frame.query.dep_kind == dep_kinds::fn_sig
&& let Some(def_id) = frame.query.def_id && let Some(def_id) = frame.query.def_id
&& let Some(node) = tcx.hir().get_if_local(def_id) && let Some(node) = tcx.hir().get_if_local(def_id)
@ -70,10 +74,14 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
} }
impl<'tcx> Value<TyCtxt<'tcx>> for Representability { impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], _guar: ErrorGuaranteed) -> Self { fn from_cycle_error(
tcx: TyCtxt<'tcx>,
cycle_error: &CycleError,
_guar: ErrorGuaranteed,
) -> Self {
let mut item_and_field_ids = Vec::new(); let mut item_and_field_ids = Vec::new();
let mut representable_ids = FxHashSet::default(); let mut representable_ids = FxHashSet::default();
for info in cycle { for info in &cycle_error.cycle {
if info.query.dep_kind == dep_kinds::representability if info.query.dep_kind == dep_kinds::representability
&& let Some(field_id) = info.query.def_id && let Some(field_id) = info.query.def_id
&& let Some(field_id) = field_id.as_local() && let Some(field_id) = field_id.as_local()
@ -87,7 +95,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
item_and_field_ids.push((item_id.expect_local(), field_id)); item_and_field_ids.push((item_id.expect_local(), field_id));
} }
} }
for info in cycle { for info in &cycle_error.cycle {
if info.query.dep_kind == dep_kinds::representability_adt_ty if info.query.dep_kind == dep_kinds::representability_adt_ty
&& let Some(def_id) = info.query.ty_adt_id && let Some(def_id) = info.query.ty_adt_id
&& let Some(def_id) = def_id.as_local() && let Some(def_id) = def_id.as_local()
@ -102,19 +110,31 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<Ty<'_>> { impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<Ty<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle, guar)) tcx: TyCtxt<'tcx>,
cycle_error: &CycleError,
guar: ErrorGuaranteed,
) -> Self {
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar))
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>> { impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle, guar)) tcx: TyCtxt<'tcx>,
cycle_error: &CycleError,
guar: ErrorGuaranteed,
) -> Self {
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar))
} }
} }
impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>> { impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>> {
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self { fn from_cycle_error(
_tcx: TyCtxt<'tcx>,
_cycle_error: &CycleError,
guar: ErrorGuaranteed,
) -> Self {
// tcx.arena.alloc cannot be used because we are not allowed to use &'tcx LayoutError under // tcx.arena.alloc cannot be used because we are not allowed to use &'tcx LayoutError under
// min_specialization. Since this is an error path anyways, leaking doesn't matter (and really, // min_specialization. Since this is an error path anyways, leaking doesn't matter (and really,
// tcx.arena.alloc is pretty much equal to leaking). // tcx.arena.alloc is pretty much equal to leaking).

View file

@ -35,7 +35,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::SerializedDepNodeIndex; use rustc_query_system::dep_graph::SerializedDepNodeIndex;
use rustc_query_system::ich::StableHashingContext; use rustc_query_system::ich::StableHashingContext;
use rustc_query_system::query::{ use rustc_query_system::query::{
get_query_incr, get_query_non_incr, HashResult, QueryCache, QueryConfig, QueryInfo, QueryMap, get_query_incr, get_query_non_incr, CycleError, HashResult, QueryCache, QueryConfig, QueryMap,
QueryMode, QueryState, QueryMode, QueryState,
}; };
use rustc_query_system::HandleCycleError; use rustc_query_system::HandleCycleError;
@ -144,10 +144,10 @@ where
fn value_from_cycle_error( fn value_from_cycle_error(
self, self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
cycle: &[QueryInfo], cycle_error: &CycleError,
guar: ErrorGuaranteed, guar: ErrorGuaranteed,
) -> Self::Value { ) -> Self::Value {
(self.dynamic.value_from_cycle_error)(tcx, cycle, guar) (self.dynamic.value_from_cycle_error)(tcx, cycle_error, guar)
} }
#[inline(always)] #[inline(always)]

View file

@ -5,7 +5,7 @@ use crate::error::HandleCycleError;
use crate::ich::StableHashingContext; use crate::ich::StableHashingContext;
use crate::query::caches::QueryCache; use crate::query::caches::QueryCache;
use crate::query::DepNodeIndex; use crate::query::DepNodeIndex;
use crate::query::{QueryContext, QueryInfo, QueryState}; use crate::query::{CycleError, QueryContext, QueryState};
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_span::ErrorGuaranteed; use rustc_span::ErrorGuaranteed;
@ -57,7 +57,7 @@ pub trait QueryConfig<Qcx: QueryContext>: Copy {
fn value_from_cycle_error( fn value_from_cycle_error(
self, self,
tcx: Qcx::DepContext, tcx: Qcx::DepContext,
cycle: &[QueryInfo], cycle_error: &CycleError,
guar: ErrorGuaranteed, guar: ErrorGuaranteed,
) -> Self::Value; ) -> Self::Value;

View file

@ -556,7 +556,7 @@ pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
#[inline(never)] #[inline(never)]
#[cold] #[cold]
pub(crate) fn report_cycle<'a>( pub fn report_cycle<'a>(
sess: &'a Session, sess: &'a Session,
CycleError { usage, cycle: stack }: &CycleError, CycleError { usage, cycle: stack }: &CycleError,
) -> DiagnosticBuilder<'a> { ) -> DiagnosticBuilder<'a> {

View file

@ -4,7 +4,9 @@ pub use self::plumbing::*;
mod job; mod job;
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
pub use self::job::deadlock; pub use self::job::deadlock;
pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap}; pub use self::job::{
print_query_stack, report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap,
};
mod caches; mod caches;
pub use self::caches::{ pub use self::caches::{

View file

@ -134,7 +134,7 @@ where
match query.handle_cycle_error() { match query.handle_cycle_error() {
Error => { Error => {
let guar = error.emit(); let guar = error.emit();
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar) query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
} }
Fatal => { Fatal => {
error.emit(); error.emit();
@ -143,7 +143,7 @@ where
} }
DelayBug => { DelayBug => {
let guar = error.delay_as_bug(); let guar = error.delay_as_bug();
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar) query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
} }
Stash => { Stash => {
let guar = if let Some(root) = cycle_error.cycle.first() let guar = if let Some(root) = cycle_error.cycle.first()
@ -154,7 +154,7 @@ where
} else { } else {
error.emit() error.emit()
}; };
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar) query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
} }
} }
} }
@ -211,7 +211,7 @@ where
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct CycleError { pub struct CycleError {
/// The query and related span that uses the cycle. /// The query and related span that uses the cycle.
pub usage: Option<(Span, QueryStackFrame)>, pub usage: Option<(Span, QueryStackFrame)>,
pub cycle: Vec<QueryInfo>, pub cycle: Vec<QueryInfo>,

View file

@ -1,20 +1,21 @@
use rustc_span::ErrorGuaranteed; use rustc_span::ErrorGuaranteed;
use crate::dep_graph::DepContext; use crate::dep_graph::DepContext;
use crate::query::QueryInfo; use crate::query::CycleError;
pub trait Value<Tcx: DepContext>: Sized { pub trait Value<Tcx: DepContext>: Sized {
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self; fn from_cycle_error(tcx: Tcx, cycle_error: &CycleError, guar: ErrorGuaranteed) -> Self;
} }
impl<Tcx: DepContext, T> Value<Tcx> for T { impl<Tcx: DepContext, T> Value<Tcx> for T {
default fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo], _guar: ErrorGuaranteed) -> T { default fn from_cycle_error(tcx: Tcx, cycle_error: &CycleError, _guar: ErrorGuaranteed) -> T {
tcx.sess().dcx().abort_if_errors(); tcx.sess().dcx().abort_if_errors();
// Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's // Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's
// non-trivial to define it earlier. // non-trivial to define it earlier.
panic!( panic!(
"<{} as Value>::from_cycle_error called without errors: {cycle:#?}", "<{} as Value>::from_cycle_error called without errors: {:#?}",
std::any::type_name::<T>() std::any::type_name::<T>(),
cycle_error.cycle,
); );
} }
} }