Skip to content

Commit 77a44ab

Browse files
authored
Rollup merge of #103865 - compiler-errors:fallback-has-occurred-tracking, r=eholk
Move `fallback_has_occurred` state tracking to `FnCtxt` Removes a ton of callsites that defaulted to `false`
2 parents 4946ee7 + bc345d7 commit 77a44ab

File tree

21 files changed

+52
-64
lines changed

21 files changed

+52
-64
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
299299
if errors.is_empty() {
300300
definition_ty
301301
} else {
302-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
302+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
303303
self.tcx.ty_error()
304304
}
305305
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
765765

766766
let errors = ocx.select_all_or_error();
767767
if !errors.is_empty() {
768-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
768+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
769769
}
770770
}
771771

@@ -831,7 +831,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
831831
obligation.clone(),
832832
&obligation,
833833
&e,
834-
false,
835834
);
836835
}
837836

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ fn check_opaque_meets_bounds<'tcx>(
471471
// version.
472472
let errors = ocx.select_all_or_error();
473473
if !errors.is_empty() {
474-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
474+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
475475
}
476476
match origin {
477477
// Checked when type checking the function containing them.

compiler/rustc_hir_analysis/src/check/compare_method.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn compare_predicate_entailment<'tcx>(
405405
// version.
406406
let errors = ocx.select_all_or_error();
407407
if !errors.is_empty() {
408-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
408+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
409409
return Err(reported);
410410
}
411411

@@ -538,7 +538,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
538538
// RPITs.
539539
let errors = ocx.select_all_or_error();
540540
if !errors.is_empty() {
541-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
541+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
542542
return Err(reported);
543543
}
544544

@@ -1431,7 +1431,7 @@ pub(crate) fn raw_compare_const_impl<'tcx>(
14311431
// version.
14321432
let errors = ocx.select_all_or_error();
14331433
if !errors.is_empty() {
1434-
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors, None, false));
1434+
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors, None));
14351435
}
14361436

14371437
// FIXME return `ErrorReported` if region obligations error?
@@ -1549,7 +1549,7 @@ fn compare_type_predicate_entailment<'tcx>(
15491549
// version.
15501550
let errors = ocx.select_all_or_error();
15511551
if !errors.is_empty() {
1552-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
1552+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
15531553
return Err(reported);
15541554
}
15551555

@@ -1769,7 +1769,7 @@ pub fn check_type_bounds<'tcx>(
17691769
// version.
17701770
let errors = ocx.select_all_or_error();
17711771
if !errors.is_empty() {
1772-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
1772+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
17731773
return Err(reported);
17741774
}
17751775

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
105105
f(&mut wfcx);
106106
let errors = wfcx.select_all_or_error();
107107
if !errors.is_empty() {
108-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
108+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
109109
return;
110110
}
111111

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn visit_implementation_of_dispatch_from_dyn<'tcx>(tcx: TyCtxt<'tcx>, impl_did:
321321
}),
322322
);
323323
if !errors.is_empty() {
324-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
324+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
325325
}
326326

327327
// Finally, resolve all regions.
@@ -561,7 +561,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
561561
predicate_for_trait_def(tcx, param_env, cause, trait_def_id, 0, source, &[target.into()]);
562562
let errors = traits::fully_solve_obligation(&infcx, predicate);
563563
if !errors.is_empty() {
564-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
564+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
565565
}
566566

567567
// Finally, resolve all regions.

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn get_impl_substs<'tcx>(
155155

156156
let errors = ocx.select_all_or_error();
157157
if !errors.is_empty() {
158-
ocx.infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
158+
ocx.infcx.err_ctxt().report_fulfillment_errors(&errors, None);
159159
return None;
160160
}
161161

compiler/rustc_hir_analysis/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn require_same_types<'tcx>(
173173
match &errors[..] {
174174
[] => true,
175175
errors => {
176-
infcx.err_ctxt().report_fulfillment_errors(errors, None, false);
176+
infcx.err_ctxt().report_fulfillment_errors(errors, None);
177177
false
178178
}
179179
}
@@ -336,7 +336,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
336336
ocx.register_bound(cause, param_env, norm_return_ty, term_did);
337337
let errors = ocx.select_all_or_error();
338338
if !errors.is_empty() {
339-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
339+
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
340340
error = true;
341341
}
342342
// now we can take the return type of the given main function

compiler/rustc_hir_typeck/src/coercion.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
705705

706706
// Object safety violations or miscellaneous.
707707
Err(err) => {
708-
self.err_ctxt().report_selection_error(
709-
obligation.clone(),
710-
&obligation,
711-
&err,
712-
false,
713-
);
708+
self.err_ctxt().report_selection_error(obligation.clone(), &obligation, &err);
714709
// Treat this like an obligation and follow through
715710
// with the unsizing - the lack of a coercion should
716711
// be silent, as it causes a type mismatch later.

compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
843843
{
844844
// Point any obligations that were registered due to opaque type
845845
// inference at the return expression.
846-
self.select_obligations_where_possible(false, |errors| {
846+
self.select_obligations_where_possible(|errors| {
847847
self.point_at_return_for_opaque_ty_error(errors, span, return_expr_ty);
848848
});
849849
}
@@ -2738,7 +2738,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27382738
Some((index_ty, element_ty)) => {
27392739
// two-phase not needed because index_ty is never mutable
27402740
self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
2741-
self.select_obligations_where_possible(false, |errors| {
2741+
self.select_obligations_where_possible(|errors| {
27422742
self.point_at_index_if_possible(errors, idx.span)
27432743
});
27442744
element_ty

compiler/rustc_hir_typeck/src/fallback.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ use rustc_data_structures::{
77
use rustc_middle::ty::{self, Ty};
88

99
impl<'tcx> FnCtxt<'_, 'tcx> {
10-
/// Performs type inference fallback, returning true if any fallback
11-
/// occurs.
12-
pub(super) fn type_inference_fallback(&self) -> bool {
10+
/// Performs type inference fallback, setting `FnCtxt::fallback_has_occurred`
11+
/// if fallback has occurred.
12+
pub(super) fn type_inference_fallback(&self) {
1313
debug!(
1414
"type-inference-fallback start obligations: {:#?}",
1515
self.fulfillment_cx.borrow_mut().pending_obligations()
1616
);
1717

1818
// All type checking constraints were added, try to fallback unsolved variables.
19-
self.select_obligations_where_possible(false, |_| {});
19+
self.select_obligations_where_possible(|_| {});
2020

2121
debug!(
2222
"type-inference-fallback post selection obligations: {:#?}",
@@ -26,18 +26,17 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
2626
// Check if we have any unsolved variables. If not, no need for fallback.
2727
let unsolved_variables = self.unsolved_variables();
2828
if unsolved_variables.is_empty() {
29-
return false;
29+
return;
3030
}
3131

3232
let diverging_fallback = self.calculate_diverging_fallback(&unsolved_variables);
3333

34-
let mut fallback_has_occurred = false;
3534
// We do fallback in two passes, to try to generate
3635
// better error messages.
3736
// The first time, we do *not* replace opaque types.
3837
for ty in unsolved_variables {
3938
debug!("unsolved_variable = {:?}", ty);
40-
fallback_has_occurred |= self.fallback_if_possible(ty, &diverging_fallback);
39+
self.fallback_if_possible(ty, &diverging_fallback);
4140
}
4241

4342
// We now see if we can make progress. This might cause us to
@@ -63,9 +62,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
6362
// If we had tried to fallback the opaque inference variable to `MyType`,
6463
// we will generate a confusing type-check error that does not explicitly
6564
// refer to opaque types.
66-
self.select_obligations_where_possible(fallback_has_occurred, |_| {});
67-
68-
fallback_has_occurred
65+
self.select_obligations_where_possible(|_| {});
6966
}
7067

7168
// Tries to apply a fallback to `ty` if it is an unsolved variable.
@@ -81,12 +78,13 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
8178
// Fallback becomes very dubious if we have encountered
8279
// type-checking errors. In that case, fallback to Error.
8380
//
84-
// The return value indicates whether fallback has occurred.
81+
// Sets `FnCtxt::fallback_has_occurred` if fallback is performed
82+
// during this call.
8583
fn fallback_if_possible(
8684
&self,
8785
ty: Ty<'tcx>,
8886
diverging_fallback: &FxHashMap<Ty<'tcx>, Ty<'tcx>>,
89-
) -> bool {
87+
) {
9088
// Careful: we do NOT shallow-resolve `ty`. We know that `ty`
9189
// is an unsolved variable, and we determine its fallback
9290
// based solely on how it was created, not what other type
@@ -111,7 +109,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
111109
ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
112110
_ => match diverging_fallback.get(&ty) {
113111
Some(&fallback_ty) => fallback_ty,
114-
None => return false,
112+
None => return,
115113
},
116114
};
117115
debug!("fallback_if_possible(ty={:?}): defaulting to `{:?}`", ty, fallback);
@@ -122,7 +120,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
122120
.map(|origin| origin.span)
123121
.unwrap_or(rustc_span::DUMMY_SP);
124122
self.demand_eqtype(span, ty, fallback);
125-
true
123+
self.fallback_has_occurred.set(true);
126124
}
127125

128126
/// The "diverging fallback" system is rather complicated. This is

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
106106
// possible. This can help substantially when there are
107107
// indirect dependencies that don't seem worth tracking
108108
// precisely.
109-
self.select_obligations_where_possible(false, mutate_fulfillment_errors);
109+
self.select_obligations_where_possible(mutate_fulfillment_errors);
110110
self.resolve_vars_if_possible(ty)
111111
}
112112

@@ -600,7 +600,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
600600
pub(in super::super) fn resolve_generator_interiors(&self, def_id: DefId) {
601601
let mut generators = self.deferred_generator_interiors.borrow_mut();
602602
for (body_id, interior, kind) in generators.drain(..) {
603-
self.select_obligations_where_possible(false, |_| {});
603+
self.select_obligations_where_possible(|_| {});
604604
crate::generator_interior::resolve_interior(self, def_id, body_id, interior, kind);
605605
}
606606
}
@@ -611,25 +611,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
611611

612612
if !errors.is_empty() {
613613
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
614-
self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id, false);
614+
self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id);
615615
}
616616
}
617617

618618
/// Select as many obligations as we can at present.
619619
pub(in super::super) fn select_obligations_where_possible(
620620
&self,
621-
fallback_has_occurred: bool,
622621
mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
623622
) {
624623
let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
625624
if !result.is_empty() {
626625
mutate_fulfillment_errors(&mut result);
627626
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
628-
self.err_ctxt().report_fulfillment_errors(
629-
&result,
630-
self.inh.body_id,
631-
fallback_has_occurred,
632-
);
627+
self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id);
633628
}
634629
}
635630

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
345345
// an "opportunistic" trait resolution of any trait bounds on
346346
// the call. This helps coercions.
347347
if check_closures {
348-
self.select_obligations_where_possible(false, |_| {})
348+
self.select_obligations_where_possible(|_| {})
349349
}
350350

351351
// Check each argument, to satisfy the input it was provided for

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ pub struct FnCtxt<'a, 'tcx> {
115115
pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
116116

117117
pub(super) inh: &'a Inherited<'tcx>,
118+
119+
pub(super) fallback_has_occurred: Cell<bool>,
118120
}
119121

120122
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -138,6 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
138140
by_id: Default::default(),
139141
}),
140142
inh,
143+
fallback_has_occurred: Cell::new(false),
141144
}
142145
}
143146

@@ -159,7 +162,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
159162
///
160163
/// [`InferCtxt::err_ctxt`]: infer::InferCtxt::err_ctxt
161164
pub fn err_ctxt(&'a self) -> TypeErrCtxt<'a, 'tcx> {
162-
TypeErrCtxt { infcx: &self.infcx, typeck_results: Some(self.typeck_results.borrow()) }
165+
TypeErrCtxt {
166+
infcx: &self.infcx,
167+
typeck_results: Some(self.typeck_results.borrow()),
168+
fallback_has_occurred: self.fallback_has_occurred.get(),
169+
}
163170
}
164171

165172
pub fn errors_reported_since_creation(&self) -> bool {

compiler/rustc_hir_typeck/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ fn typeck_with_fallback<'tcx>(
316316
fcx
317317
};
318318

319-
let fallback_has_occurred = fcx.type_inference_fallback();
319+
fcx.type_inference_fallback();
320320

321321
// Even though coercion casts provide type hints, we check casts after fallback for
322322
// backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
323323
fcx.check_casts();
324-
fcx.select_obligations_where_possible(fallback_has_occurred, |_| {});
324+
fcx.select_obligations_where_possible(|_| {});
325325

326326
// Closure and generator analysis may run after fallback
327327
// because they don't constrain other type variables.

compiler/rustc_hir_typeck/src/op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
772772
match (method, trait_did) {
773773
(Some(ok), _) => {
774774
let method = self.register_infer_ok_obligations(ok);
775-
self.select_obligations_where_possible(false, |_| {});
775+
self.select_obligations_where_possible(|_| {});
776776
Ok(method)
777777
}
778778
(None, None) => Err(vec![]),

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub mod nice_region_error;
9191
pub struct TypeErrCtxt<'a, 'tcx> {
9292
pub infcx: &'a InferCtxt<'tcx>,
9393
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
94+
pub fallback_has_occurred: bool,
9495
}
9596

9697
impl TypeErrCtxt<'_, '_> {

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,9 @@ pub struct CombinedSnapshot<'tcx> {
677677

678678
impl<'tcx> InferCtxt<'tcx> {
679679
/// Creates a `TypeErrCtxt` for emitting various inference errors.
680-
/// During typeck, use `FnCtxt::infer_err` instead.
680+
/// During typeck, use `FnCtxt::err_ctxt` instead.
681681
pub fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
682-
TypeErrCtxt { infcx: self, typeck_results: None }
682+
TypeErrCtxt { infcx: self, typeck_results: None, fallback_has_occurred: false }
683683
}
684684

685685
/// calls `tcx.try_unify_abstract_consts` after

0 commit comments

Comments
 (0)