Automatically taint InferCtxt when errors are emitted

This commit is contained in:
Oli Scherer 2024-06-26 16:01:38 +00:00
parent 5988078aa2
commit 86c8eae774
56 changed files with 614 additions and 583 deletions

View file

@ -305,16 +305,17 @@ fn label_msg_span(
}
}
#[instrument(level = "trace", skip(tcx))]
pub fn unexpected_hidden_region_diagnostic<'tcx>(
tcx: TyCtxt<'tcx>,
#[instrument(level = "trace", skip(infcx))]
pub fn unexpected_hidden_region_diagnostic<'a, 'tcx>(
infcx: &'a InferCtxt<'tcx>,
generic_param_scope: LocalDefId,
span: Span,
hidden_ty: Ty<'tcx>,
hidden_region: ty::Region<'tcx>,
opaque_ty_key: ty::OpaqueTypeKey<'tcx>,
) -> Diag<'tcx> {
let mut err = tcx.dcx().create_err(errors::OpaqueCapturesLifetime {
) -> Diag<'a> {
let tcx = infcx.tcx;
let mut err = infcx.dcx().create_err(errors::OpaqueCapturesLifetime {
span,
opaque_ty: Ty::new_opaque(tcx, opaque_ty_key.def_id.to_def_id(), opaque_ty_key.args),
opaque_ty_span: tcx.def_span(opaque_ty_key.def_id),
@ -2215,7 +2216,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
self.type_error_additional_suggestions(&trace, terr),
);
let mut diag = self.tcx.dcx().create_err(failure_code);
let mut diag = self.dcx().create_err(failure_code);
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr, false, false);
diag
}
@ -2357,14 +2358,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
origin: Option<SubregionOrigin<'tcx>>,
bound_kind: GenericKind<'tcx>,
sub: Region<'tcx>,
) -> Diag<'tcx> {
) -> Diag<'a> {
if let Some(SubregionOrigin::CompareImplItemObligation {
span,
impl_item_def_id,
trait_item_def_id,
}) = origin
{
return self.report_extra_impl_obligation(
return self.infcx.report_extra_impl_obligation(
span,
impl_item_def_id,
trait_item_def_id,
@ -2790,7 +2791,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
}
impl<'tcx> InferCtxt<'tcx> {
fn report_inference_failure(&self, var_origin: RegionVariableOrigin) -> Diag<'tcx> {
fn report_inference_failure(&self, var_origin: RegionVariableOrigin) -> Diag<'_> {
let br_string = |br: ty::BoundRegionKind| {
let mut s = match br {
ty::BrNamed(_, name) => name.to_string(),
@ -2829,7 +2830,7 @@ impl<'tcx> InferCtxt<'tcx> {
};
struct_span_code_err!(
self.tcx.dcx(),
self.dcx(),
var_origin.span(),
E0495,
"cannot infer an appropriate lifetime{} due to conflicting requirements",

View file

@ -245,7 +245,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
}
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
let mut err = self.report_extra_impl_obligation(
let mut err = self.infcx.report_extra_impl_obligation(
span,
impl_item_def_id,
trait_item_def_id,

View file

@ -685,7 +685,7 @@ impl<'tcx> InferOk<'tcx, ()> {
impl<'tcx> InferCtxt<'tcx> {
pub fn dcx(&self) -> DiagCtxtHandle<'_> {
self.tcx.dcx()
self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
}
pub fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
@ -1089,19 +1089,7 @@ impl<'tcx> InferCtxt<'tcx> {
/// inference variables, regionck errors).
#[must_use = "this method does not have any side effects"]
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
if let Some(guar) = self.tainted_by_errors.get() {
Some(guar)
} else if self.dcx().err_count_excluding_lint_errs() > self.err_count_on_creation {
// Errors reported since this infcx was made. Lint errors are
// excluded to avoid some being swallowed in the presence of
// non-lint errors. (It's arguable whether or not this exclusion is
// important.)
let guar = self.dcx().has_errors().unwrap();
self.set_tainted_by_errors(guar);
Some(guar)
} else {
None
}
self.tainted_by_errors.get()
}
/// Set the "tainted by errors" flag to true. We call this when we
@ -1328,8 +1316,7 @@ impl<'tcx> InferCtxt<'tcx> {
bug!("`{value:?}` is not fully resolved");
}
if value.has_infer_regions() {
let guar =
self.tcx.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
let guar = self.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
Ok(self.tcx.fold_regions(value, |re, _| {
if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }
}))

View file

@ -156,7 +156,7 @@ impl<'tcx> InferCtxt<'tcx> {
if self.can_define_opaque_ty(b_def_id)
&& self.tcx.is_type_alias_impl_trait(b_def_id)
{
self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag {
self.dcx().emit_err(OpaqueHiddenTypeDiag {
span,
hidden_type: self.tcx.def_span(b_def_id),
opaque_type: self.tcx.def_span(def_id),

View file

@ -12,15 +12,15 @@ use std::fmt;
use std::iter;
impl<'tcx> InferCtxt<'tcx> {
pub fn report_extra_impl_obligation(
&self,
pub fn report_extra_impl_obligation<'a>(
&'a self,
error_span: Span,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
requirement: &dyn fmt::Display,
) -> Diag<'tcx> {
) -> Diag<'a> {
let mut err = struct_span_code_err!(
self.tcx.dcx(),
self.dcx(),
error_span,
E0276,
"impl has stricter requirements than trait"