Skip to content

Commit 054e1e3

Browse files
committed
Track ErrorGuaranteed instead of conjuring it from thin air
1 parent 3042da0 commit 054e1e3

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

compiler/rustc_infer/src/infer/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ pub struct InferCtxt<'tcx> {
278278

279279
/// The set of predicates on which errors have been reported, to
280280
/// avoid reporting the same error twice.
281-
pub reported_trait_errors: RefCell<FxIndexMap<Span, Vec<ty::Predicate<'tcx>>>>,
281+
pub reported_trait_errors:
282+
RefCell<FxIndexMap<Span, (Vec<ty::Predicate<'tcx>>, ErrorGuaranteed)>>,
282283

283284
pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
284285

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ pub trait TypeErrCtxtExt<'tcx> {
107107
trait_ref: ty::PolyTraitRef<'tcx>,
108108
) -> Option<ErrorGuaranteed>;
109109

110-
fn fn_arg_obligation(&self, obligation: &PredicateObligation<'tcx>) -> bool;
110+
fn fn_arg_obligation(
111+
&self,
112+
obligation: &PredicateObligation<'tcx>,
113+
) -> Result<(), ErrorGuaranteed>;
111114

112115
fn try_conversion_context(
113116
&self,
@@ -142,6 +145,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
142145
(
143146
span,
144147
predicates
148+
.0
145149
.iter()
146150
.map(|&predicate| ErrorDescriptor { predicate, index: None })
147151
.collect(),
@@ -213,7 +217,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
213217
for from_expansion in [false, true] {
214218
for (error, suppressed) in iter::zip(&errors, &is_suppressed) {
215219
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
216-
reported = Some(self.report_fulfillment_error(error));
220+
let guar = self.report_fulfillment_error(error);
221+
reported = Some(guar);
217222
// We want to ignore desugarings here: spans are equivalent even
218223
// if one is the result of a desugaring and the other is not.
219224
let mut span = error.obligation.cause.span;
@@ -224,7 +229,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
224229
self.reported_trait_errors
225230
.borrow_mut()
226231
.entry(span)
227-
.or_default()
232+
.or_insert_with(|| (vec![], guar))
233+
.0
228234
.push(error.obligation.predicate);
229235
}
230236
}
@@ -447,10 +453,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
447453
{
448454
return guar;
449455
}
450-
if self.fn_arg_obligation(&obligation) {
451-
// Silence redundant errors on binding acccess that are already
452-
// reported on the binding definition (#56607).
453-
return self.dcx().span_delayed_bug(obligation.cause.span, "error already reported");
456+
// Silence redundant errors on binding acccess that are already
457+
// reported on the binding definition (#56607).
458+
if let Err(guar) = self.fn_arg_obligation(&obligation) {
459+
return guar;
454460
}
455461
let mut file = None;
456462
let (post_message, pre_message, type_def) = self
@@ -981,7 +987,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
981987
}
982988
}
983989

984-
fn fn_arg_obligation(&self, obligation: &PredicateObligation<'tcx>) -> bool {
990+
fn fn_arg_obligation(
991+
&self,
992+
obligation: &PredicateObligation<'tcx>,
993+
) -> Result<(), ErrorGuaranteed> {
985994
if let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } =
986995
obligation.cause.code()
987996
&& let Some(Node::Expr(arg)) = self.tcx.opt_hir_node(*arg_hir_id)
@@ -991,12 +1000,12 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
9911000
hir::Path { res: hir::def::Res::Local(hir_id), .. },
9921001
)) = arg.kind
9931002
&& let Some(Node::Pat(pat)) = self.tcx.opt_hir_node(*hir_id)
994-
&& let Some(preds) = self.reported_trait_errors.borrow().get(&pat.span)
1003+
&& let Some((preds, guar)) = self.reported_trait_errors.borrow().get(&pat.span)
9951004
&& preds.contains(&obligation.predicate)
9961005
{
997-
return true;
1006+
return Err(*guar);
9981007
}
999-
false
1008+
Ok(())
10001009
}
10011010

10021011
/// When the `E` of the resulting `Result<T, E>` in an expression `foo().bar().baz()?`,

0 commit comments

Comments
 (0)