InferCtxt::is_tainted_by_errors returns ErrorGuaranteed

This commit is contained in:
Boxy 2022-11-17 20:57:45 +00:00
parent c1ec8ff14d
commit 9c510048fd
10 changed files with 33 additions and 35 deletions

View File

@ -19,7 +19,7 @@ extern crate tracing;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::graph::dominators::Dominators;
use rustc_data_structures::vec_map::VecMap;
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{Diagnostic, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_index::bit_set::ChunkedBitSet;

View File

@ -219,8 +219,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
instantiated_ty: OpaqueHiddenType<'tcx>,
origin: OpaqueTyOrigin,
) -> Ty<'tcx> {
if self.is_tainted_by_errors() {
return self.tcx.ty_error();
if let Some(e) = self.is_tainted_by_errors() {
return self.tcx.ty_error_with_guaranteed(e);
}
let definition_ty = instantiated_ty

View File

@ -104,7 +104,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
// type, `?T` is not considered unsolved, but `?I` is. The
// same is true for float variables.)
let fallback = match ty.kind() {
_ if self.is_tainted_by_errors() => self.tcx.ty_error(),
_ if let Some(e) = self.is_tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
ty::Infer(ty::IntVar(_)) => self.tcx.types.i32,
ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
_ => match diverging_fallback.get(&ty) {

View File

@ -528,7 +528,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
match self.typeck_results.borrow().node_types().get(id) {
Some(&t) => t,
None if self.is_tainted_by_errors() => self.tcx.ty_error(),
None if let Some(e) = self.is_tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
None => {
bug!(
"no type for node {}: {} in fcx {}",
@ -543,7 +543,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
match self.typeck_results.borrow().node_types().get(id) {
Some(&t) => Some(t),
None if self.is_tainted_by_errors() => Some(self.tcx.ty_error()),
None if let Some(e) = self.is_tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
None => None,
}
}
@ -1440,7 +1440,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !ty.is_ty_var() {
ty
} else {
if !self.is_tainted_by_errors() {
if let None = self.is_tainted_by_errors() {
self.err_ctxt()
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
.emit();

View File

@ -344,7 +344,7 @@ fn typeck_with_fallback<'tcx>(
fcx.select_all_obligations_or_error();
if !fcx.infcx.is_tainted_by_errors() {
if let None = fcx.infcx.is_tainted_by_errors() {
fcx.check_transmutes();
}

View File

@ -133,7 +133,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}
fn is_tainted_by_errors(&self) -> bool {
self.infcx.is_tainted_by_errors()
self.infcx.is_tainted_by_errors().is_some()
}
fn resolve_type_vars_or_error(

View File

@ -83,10 +83,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
wbcx.typeck_results.treat_byte_string_as_slice =
mem::take(&mut self.typeck_results.borrow_mut().treat_byte_string_as_slice);
if self.is_tainted_by_errors() {
// FIXME(eddyb) keep track of `ErrorGuaranteed` from where the error was emitted.
wbcx.typeck_results.tainted_by_errors =
Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
if let Some(e) = self.is_tainted_by_errors() {
wbcx.typeck_results.tainted_by_errors = Some(e);
}
debug!("writeback: typeck results for {:?} are {:#?}", item_def_id, wbcx.typeck_results);

View File

@ -1208,7 +1208,7 @@ impl<'tcx> InferCtxt<'tcx> {
/// reporting errors that often occur as a result of earlier
/// errors, but where it's hard to be 100% sure (e.g., unresolved
/// inference variables, regionck errors).
pub fn is_tainted_by_errors(&self) -> bool {
pub fn is_tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
debug!(
"is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
tainted_by_errors={})",
@ -1218,9 +1218,13 @@ impl<'tcx> InferCtxt<'tcx> {
);
if self.tcx.sess.err_count() > self.err_count_on_creation {
return true; // errors reported since this infcx was made
// errors reported since this infcx was made
return Some(self.tcx.sess.delay_span_bug(
DUMMY_SP,
"`tcx.sess.error_count()` incorrectly returned non zero value",
));
}
self.tainted_by_errors.get().is_some()
self.tainted_by_errors.get()
}
/// Set the "tainted by errors" flag to true. We call this when we
@ -1270,7 +1274,7 @@ impl<'tcx> InferCtxt<'tcx> {
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;
assert!(
self.is_tainted_by_errors() || inner.region_obligations.is_empty(),
self.is_tainted_by_errors().is_some() || inner.region_obligations.is_empty(),
"region_obligations not empty: {:#?}",
inner.region_obligations
);
@ -1707,7 +1711,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
) {
let errors = self.resolve_regions(outlives_env);
if !self.is_tainted_by_errors() {
if let None = self.is_tainted_by_errors() {
// As a heuristic, just skip reporting region errors
// altogether if other errors have been reported while
// this infcx was in use. This is totally hokey but

View File

@ -2060,7 +2060,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// check upstream for type errors and don't add the obligations to
// begin with in those cases.
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
if !self.is_tainted_by_errors() {
if let None = self.is_tainted_by_errors() {
self.emit_inference_failure_err(
body_id,
span,
@ -2115,16 +2115,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if impls.len() > 1 && impls.len() < 5 && has_non_region_infer {
self.annotate_source_of_ambiguity(&mut err, &impls, predicate);
} else {
if self.is_tainted_by_errors() {
err.delay_as_bug();
if self.is_tainted_by_errors().is_some() {
return;
}
err.note(&format!("cannot satisfy `{}`", predicate));
}
}
_ => {
if self.is_tainted_by_errors() {
err.delay_as_bug();
if self.is_tainted_by_errors().is_some() {
return;
}
err.note(&format!("cannot satisfy `{}`", predicate));
@ -2226,7 +2224,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
] = path.segments
&& data.trait_ref.def_id == *trait_id
&& self.tcx.trait_of_item(*item_id) == Some(*trait_id)
&& !self.is_tainted_by_errors()
&& let None = self.is_tainted_by_errors()
{
let (verb, noun) = match self.tcx.associated_item(item_id).kind {
ty::AssocKind::Const => ("refer to the", "constant"),
@ -2295,7 +2293,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// with error messages.
if arg.references_error()
|| self.tcx.sess.has_errors().is_some()
|| self.is_tainted_by_errors()
|| self.is_tainted_by_errors().is_some()
{
return;
}
@ -2306,7 +2304,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
ty::PredicateKind::Subtype(data) => {
if data.references_error()
|| self.tcx.sess.has_errors().is_some()
|| self.is_tainted_by_errors()
|| self.is_tainted_by_errors().is_some()
{
// no need to overload user in such cases
return;
@ -2317,7 +2315,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282, true)
}
ty::PredicateKind::Projection(data) => {
if predicate.references_error() || self.is_tainted_by_errors() {
if predicate.references_error() || self.is_tainted_by_errors().is_some() {
return;
}
let subst = data
@ -2351,7 +2349,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
ty::PredicateKind::ConstEvaluatable(data) => {
if predicate.references_error() || self.is_tainted_by_errors() {
if predicate.references_error() || self.is_tainted_by_errors().is_some() {
return;
}
let subst = data.walk().find(|g| g.is_non_region_infer());
@ -2378,7 +2376,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
}
_ => {
if self.tcx.sess.has_errors().is_some() || self.is_tainted_by_errors() {
if self.tcx.sess.has_errors().is_some() || self.is_tainted_by_errors().is_some() {
return;
}
let mut err = struct_span_err!(
@ -2422,7 +2420,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
post.sort();
post.dedup();
if self.is_tainted_by_errors()
if self.is_tainted_by_errors().is_some()
&& (crate_names.len() == 1
&& spans.len() == 0
&& ["`core`", "`alloc`", "`std`"].contains(&crate_names[0].as_str())

View File

@ -33,7 +33,7 @@ use crate::traits::ProjectionCacheKey;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{Diagnostic, ErrorGuaranteed};
use rustc_errors::Diagnostic;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::LateBoundRegionConversionTime;
@ -1089,10 +1089,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
if !self.infcx.tcx.recursion_limit().value_within_limit(depth) {
match self.query_mode {
TraitQueryMode::Standard => {
if self.infcx.is_tainted_by_errors() {
return Err(OverflowError::Error(
ErrorGuaranteed::unchecked_claim_error_was_emitted(),
));
if let Some(e) = self.infcx.is_tainted_by_errors() {
return Err(OverflowError::Error(e));
}
self.infcx.err_ctxt().report_overflow_error(error_obligation, true);
}