Skip to content

Commit cb873b2

Browse files
committed
Separate trait selection from ambiguity reporting.
1 parent 2870ce0 commit cb873b2

File tree

8 files changed

+25
-30
lines changed

8 files changed

+25
-30
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
525525
}
526526

527527
#[instrument(skip(self), level = "debug")]
528-
pub(in super::super) fn select_all_obligations_or_error(&self) {
529-
let mut errors = self.fulfillment_cx.borrow_mut().select_all_or_error(&self);
528+
pub(in super::super) fn report_ambiguity_errors(&self) {
529+
let mut errors = self.fulfillment_cx.borrow_mut().collect_remaining_errors();
530530

531531
if !errors.is_empty() {
532532
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ fn typeck_with_fallback<'tcx>(
301301
fcx.require_type_is_sized(ty, span, code);
302302
}
303303

304-
fcx.select_all_obligations_or_error();
304+
fcx.select_obligations_where_possible(|_| {});
305+
306+
if let None = fcx.infcx.tainted_by_errors() {
307+
fcx.report_ambiguity_errors();
308+
}
305309

306310
if let None = fcx.infcx.tainted_by_errors() {
307311
fcx.check_transmutes();

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::infer::region_constraints::{Constraint, RegionConstraintData};
1717
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
1919
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
20-
use crate::traits::{PredicateObligations, TraitEngine};
20+
use crate::traits::{PredicateObligations, TraitEngine, TraitEngineExt};
2121
use rustc_data_structures::captures::Captures;
2222
use rustc_index::vec::Idx;
2323
use rustc_index::vec::IndexVec;

compiler/rustc_infer/src/traits/engine.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pub trait TraitEngine<'tcx>: 'tcx {
3636
obligation: PredicateObligation<'tcx>,
3737
);
3838

39-
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
40-
4139
fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
4240

41+
fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>>;
42+
4343
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
4444
}
4545

@@ -49,6 +49,8 @@ pub trait TraitEngineExt<'tcx> {
4949
infcx: &InferCtxt<'tcx>,
5050
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
5151
);
52+
53+
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
5254
}
5355

5456
impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
@@ -61,4 +63,13 @@ impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
6163
self.register_predicate_obligation(infcx, obligation);
6264
}
6365
}
66+
67+
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
68+
let errors = self.select_where_possible(infcx);
69+
if !errors.is_empty() {
70+
return errors;
71+
}
72+
73+
self.collect_remaining_errors()
74+
}
6475
}

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
4040
self.obligations.push(obligation);
4141
}
4242

43-
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
44-
let errors = self.select_where_possible(infcx);
45-
if !errors.is_empty() {
46-
return errors;
47-
}
48-
43+
fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>> {
4944
self.obligations
5045
.drain(..)
5146
.map(|obligation| FulfillmentError {

compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
4040
self.obligations.insert(obligation);
4141
}
4242

43-
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
44-
{
45-
let errors = self.select_where_possible(infcx);
46-
47-
if !errors.is_empty() {
48-
return errors;
49-
}
50-
}
51-
43+
fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>> {
5244
// any remaining obligations are errors
5345
self.obligations
5446
.iter()

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
132132
.register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
133133
}
134134

135-
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
136-
{
137-
let errors = self.select_where_possible(infcx);
138-
if !errors.is_empty() {
139-
return errors;
140-
}
141-
}
142-
135+
fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>> {
143136
self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
144137
}
145138

compiler/rustc_traits/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// general routines.
55

66
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
7-
use rustc_infer::traits::FulfillmentErrorCode;
7+
use rustc_infer::traits::{FulfillmentErrorCode, TraitEngineExt as _};
88
use rustc_middle::traits::CodegenObligationError;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;

0 commit comments

Comments
 (0)