Pass ErrorGuaranteed to cycle error

This commit is contained in:
Michael Goulet 2023-08-27 21:32:55 +00:00
parent 668bf8c593
commit e7b3c94b0e
7 changed files with 59 additions and 24 deletions

View file

@ -19,7 +19,7 @@ use rustc_query_system::dep_graph::SerializedDepNodeIndex;
pub(crate) use rustc_query_system::query::QueryJobId; pub(crate) use rustc_query_system::query::QueryJobId;
use rustc_query_system::query::*; use rustc_query_system::query::*;
use rustc_query_system::HandleCycleError; use rustc_query_system::HandleCycleError;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
use std::ops::Deref; use std::ops::Deref;
pub struct QueryKeyStringCache { pub struct QueryKeyStringCache {
@ -52,7 +52,8 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
pub loadable_from_disk: pub loadable_from_disk:
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: fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> C::Value, pub value_from_cycle_error:
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>], guar: ErrorGuaranteed) -> C::Value,
pub format_value: fn(&C::Value) -> String, pub format_value: fn(&C::Value) -> String,
} }

View file

@ -8,20 +8,28 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_query_system::query::QueryInfo; use rustc_query_system::query::QueryInfo;
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::Span; use rustc_span::{ErrorGuaranteed, Span};
use std::fmt::Write; use std::fmt::Write;
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Ty<'_> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Ty<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
tcx: TyCtxt<'tcx>,
_: &[QueryInfo<DepKind>],
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_misc_error(tcx)) } unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::SymbolName<'_> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::SymbolName<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
tcx: TyCtxt<'tcx>,
_: &[QueryInfo<DepKind>],
_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 {
@ -33,8 +41,12 @@ impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::SymbolName<'_> {
} }
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::Binder<'_, ty::FnSig<'_>> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::Binder<'_, ty::FnSig<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
let err = Ty::new_misc_error(tcx); tcx: TyCtxt<'tcx>,
stack: &[QueryInfo<DepKind>],
guar: ErrorGuaranteed,
) -> Self {
let err = Ty::new_error(tcx, guar);
let arity = if let Some(frame) = stack.get(0) let arity = if let Some(frame) = stack.get(0)
&& frame.query.dep_kind == DepKind::fn_sig && frame.query.dep_kind == DepKind::fn_sig
@ -63,7 +75,11 @@ impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::Binder<'_, ty::FnSig<'_>> {
} }
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Representability { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Representability {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
tcx: TyCtxt<'tcx>,
cycle: &[QueryInfo<DepKind>],
_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 {
@ -95,22 +111,35 @@ impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Representability {
} }
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::EarlyBinder<Ty<'_>> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::EarlyBinder<Ty<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle)) tcx: TyCtxt<'tcx>,
cycle: &[QueryInfo<DepKind>],
guar: ErrorGuaranteed,
) -> Self {
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle, guar))
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle)) tcx: TyCtxt<'tcx>,
cycle: &[QueryInfo<DepKind>],
guar: ErrorGuaranteed,
) -> Self {
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle, guar))
} }
} }
impl<'tcx, T> Value<TyCtxt<'tcx>, DepKind> for Result<T, &'_ ty::layout::LayoutError<'_>> { impl<'tcx, T> Value<TyCtxt<'tcx>, DepKind> for Result<T, &'_ ty::layout::LayoutError<'_>> {
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _cycle: &[QueryInfo<DepKind>]) -> Self { fn from_cycle_error(
_tcx: TyCtxt<'tcx>,
_cycle: &[QueryInfo<DepKind>],
_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).
// FIXME: `Cycle` should carry the ErrorGuaranteed
Err(Box::leak(Box::new(ty::layout::LayoutError::Cycle))) Err(Box::leak(Box::new(ty::layout::LayoutError::Cycle)))
} }
} }

View file

@ -41,7 +41,7 @@ use rustc_query_system::query::{
}; };
use rustc_query_system::HandleCycleError; use rustc_query_system::HandleCycleError;
use rustc_query_system::Value; use rustc_query_system::Value;
use rustc_span::Span; use rustc_span::{ErrorGuaranteed, Span};
#[macro_use] #[macro_use]
mod plumbing; mod plumbing;
@ -146,8 +146,9 @@ where
self, self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
cycle: &[QueryInfo<DepKind>], cycle: &[QueryInfo<DepKind>],
guar: ErrorGuaranteed,
) -> Self::Value { ) -> Self::Value {
(self.dynamic.value_from_cycle_error)(tcx, cycle) (self.dynamic.value_from_cycle_error)(tcx, cycle, guar)
} }
#[inline(always)] #[inline(always)]

View file

@ -605,8 +605,8 @@ macro_rules! define_queries {
} { } {
|_tcx, _key, _prev_index, _index| None |_tcx, _key, _prev_index, _index| None
}), }),
value_from_cycle_error: |tcx, cycle| { value_from_cycle_error: |tcx, cycle, guar| {
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle); let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
erase(result) erase(result)
}, },
loadable_from_disk: |_tcx, _key, _index| { loadable_from_disk: |_tcx, _key, _index| {

View file

@ -8,6 +8,7 @@ use crate::query::DepNodeIndex;
use crate::query::{QueryContext, QueryInfo, QueryState}; use crate::query::{QueryContext, QueryInfo, QueryState};
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_span::ErrorGuaranteed;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
@ -57,6 +58,7 @@ pub trait QueryConfig<Qcx: QueryContext>: Copy {
self, self,
tcx: Qcx::DepContext, tcx: Qcx::DepContext,
cycle: &[QueryInfo<Qcx::DepKind>], cycle: &[QueryInfo<Qcx::DepKind>],
guar: ErrorGuaranteed,
) -> Self::Value; ) -> Self::Value;
fn anon(self) -> bool; fn anon(self) -> bool;

View file

@ -148,8 +148,8 @@ where
use HandleCycleError::*; use HandleCycleError::*;
match query.handle_cycle_error() { match query.handle_cycle_error() {
Error => { Error => {
error.emit(); let guar = error.emit();
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle) query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
} }
Fatal => { Fatal => {
error.emit(); error.emit();
@ -157,8 +157,8 @@ where
unreachable!() unreachable!()
} }
DelayBug => { DelayBug => {
error.delay_as_bug(); let guar = error.delay_as_bug();
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle) query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
} }
} }
} }

View file

@ -1,12 +1,14 @@
use rustc_span::ErrorGuaranteed;
use crate::dep_graph::{DepContext, DepKind}; use crate::dep_graph::{DepContext, DepKind};
use crate::query::QueryInfo; use crate::query::QueryInfo;
pub trait Value<Tcx: DepContext, D: DepKind>: Sized { pub trait Value<Tcx: DepContext, D: DepKind>: Sized {
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>]) -> Self; fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>], guar: ErrorGuaranteed) -> Self;
} }
impl<Tcx: DepContext, T, D: DepKind> Value<Tcx, D> for T { impl<Tcx: DepContext, T, D: DepKind> Value<Tcx, D> for T {
default fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>]) -> T { default fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>], _guar: ErrorGuaranteed) -> T {
tcx.sess().abort_if_errors(); tcx.sess().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.