Skip to content

Commit f1126f1

Browse files
committed
Make select_* methods return Vec for TraitEngine
1 parent b307481 commit f1126f1

File tree

25 files changed

+148
-118
lines changed

25 files changed

+148
-118
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ fn try_extract_error_from_fulfill_cx<'tcx>(
339339
// We generally shouldn't have errors here because the query was
340340
// already run, but there's no point using `delay_span_bug`
341341
// when we're going to emit an error here anyway.
342-
let _errors = fulfill_cx.select_all_or_error(infcx).err().unwrap_or_else(Vec::new);
342+
let _errors = fulfill_cx.select_all_or_error(infcx);
343343

344344
let (sub_region, cause) = infcx.with_region_constraints(|region_constraints| {
345345
debug!("{:#?}", region_constraints);

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,9 @@ fn check_return_ty_is_sync(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, hir_id: HirId)
10541054
let mut fulfillment_cx = traits::FulfillmentContext::new();
10551055
let sync_def_id = tcx.require_lang_item(LangItem::Sync, Some(body.span));
10561056
fulfillment_cx.register_bound(&infcx, ty::ParamEnv::empty(), ty, sync_def_id, cause);
1057-
if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
1058-
infcx.report_fulfillment_errors(&err, None, false);
1057+
let errors = fulfillment_cx.select_all_or_error(&infcx);
1058+
if !errors.is_empty() {
1059+
infcx.report_fulfillment_errors(&errors, None, false);
10591060
}
10601061
});
10611062
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
108108
let tcx = self.tcx;
109109

110110
// Select everything, returning errors.
111-
let true_errors = fulfill_cx.select_where_possible(self).err().unwrap_or_else(Vec::new);
111+
let true_errors = fulfill_cx.select_where_possible(self);
112112
debug!("true_errors = {:#?}", true_errors);
113113

114114
if !true_errors.is_empty() {
@@ -118,7 +118,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
118118
}
119119

120120
// Anything left unselected *now* must be an ambiguity.
121-
let ambig_errors = fulfill_cx.select_all_or_error(self).err().unwrap_or_else(Vec::new);
121+
let ambig_errors = fulfill_cx.select_all_or_error(self);
122122
debug!("ambig_errors = {:#?}", ambig_errors);
123123

124124
let region_obligations = self.take_registered_region_obligations();

compiler/rustc_infer/src/traits/engine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,27 @@ pub trait TraitEngine<'tcx>: 'tcx {
4949
fn select_all_or_error(
5050
&mut self,
5151
infcx: &InferCtxt<'_, 'tcx>,
52-
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
52+
) -> Vec<FulfillmentError<'tcx>>;
5353

5454
fn select_all_with_constness_or_error(
5555
&mut self,
5656
infcx: &InferCtxt<'_, 'tcx>,
5757
_constness: hir::Constness,
58-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
58+
) -> Vec<FulfillmentError<'tcx>> {
5959
self.select_all_or_error(infcx)
6060
}
6161

6262
fn select_where_possible(
6363
&mut self,
6464
infcx: &InferCtxt<'_, 'tcx>,
65-
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
65+
) -> Vec<FulfillmentError<'tcx>>;
6666

6767
// FIXME(fee1-dead) this should not provide a default body for chalk as chalk should be updated
6868
fn select_with_constness_where_possible(
6969
&mut self,
7070
infcx: &InferCtxt<'_, 'tcx>,
7171
_constness: hir::Constness,
72-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
72+
) -> Vec<FulfillmentError<'tcx>> {
7373
self.select_where_possible(infcx)
7474
}
7575

compiler/rustc_trait_selection/src/autoderef.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
152152
},
153153
cause,
154154
);
155-
if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
155+
let errors = fulfillcx.select_where_possible(&self.infcx);
156+
if !errors.is_empty() {
156157
// This shouldn't happen, except for evaluate/fulfill mismatches,
157158
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
158159
// by design).
159-
debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
160+
debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", errors);
160161
return None;
161162
}
162163
let obligations = fulfillcx.pending_obligations();

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ impl<'tcx> AutoTraitFinder<'tcx> {
187187
// an additional sanity check.
188188
let mut fulfill = FulfillmentContext::new();
189189
fulfill.register_bound(&infcx, full_env, ty, trait_did, ObligationCause::dummy());
190-
fulfill.select_all_or_error(&infcx).unwrap_or_else(|e| {
191-
panic!("Unable to fulfill trait {:?} for '{:?}': {:?}", trait_did, ty, e)
192-
});
190+
let errors = fulfill.select_all_or_error(&infcx);
191+
192+
if !errors.is_empty() {
193+
panic!("Unable to fulfill trait {:?} for '{:?}': {:?}", trait_did, ty, errors);
194+
}
193195

194196
let body_id_map: FxHashMap<_, _> = infcx
195197
.inner

compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,33 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
5252
fn select_all_or_error(
5353
&mut self,
5454
infcx: &InferCtxt<'_, 'tcx>,
55-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
56-
self.select_where_possible(infcx)?;
57-
58-
if self.obligations.is_empty() {
59-
Ok(())
60-
} else {
61-
let errors = self
62-
.obligations
63-
.iter()
64-
.map(|obligation| FulfillmentError {
65-
obligation: obligation.clone(),
66-
code: FulfillmentErrorCode::CodeAmbiguity,
67-
// FIXME - does Chalk have a notation of 'root obligation'?
68-
// This is just for diagnostics, so it's okay if this is wrong
69-
root_obligation: obligation.clone(),
70-
})
71-
.collect();
72-
Err(errors)
55+
) -> Vec<FulfillmentError<'tcx>> {
56+
{
57+
let errors = self.select_where_possible(infcx);
58+
59+
if !errors.is_empty() {
60+
return errors;
61+
}
7362
}
63+
64+
// any remaining obligations are errors
65+
self
66+
.obligations
67+
.iter()
68+
.map(|obligation| FulfillmentError {
69+
obligation: obligation.clone(),
70+
code: FulfillmentErrorCode::CodeAmbiguity,
71+
// FIXME - does Chalk have a notation of 'root obligation'?
72+
// This is just for diagnostics, so it's okay if this is wrong
73+
root_obligation: obligation.clone(),
74+
})
75+
.collect()
7476
}
7577

7678
fn select_where_possible(
7779
&mut self,
7880
infcx: &InferCtxt<'_, 'tcx>,
79-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
81+
) -> Vec<FulfillmentError<'tcx>> {
8082
assert!(!infcx.is_in_snapshot());
8183

8284
let mut errors = Vec::new();
@@ -147,7 +149,7 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
147149
}
148150
}
149151

150-
if errors.is_empty() { Ok(()) } else { Err(errors) }
152+
errors
151153
}
152154

153155
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {

compiler/rustc_trait_selection/src/traits/codegen.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ where
120120
// In principle, we only need to do this so long as `result`
121121
// contains unbound type parameters. It could be a slight
122122
// optimization to stop iterating early.
123-
if let Err(errors) = fulfill_cx.select_all_or_error(infcx) {
123+
let errors = fulfill_cx.select_all_or_error(infcx);
124+
if !errors.is_empty() {
124125
infcx.tcx.sess.delay_span_bug(
125126
rustc_span::DUMMY_SP,
126127
&format!("Encountered errors `{:?}` resolving bounds after type-checking", errors),

compiler/rustc_trait_selection/src/traits/fulfill.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a, 'tcx> FulfillmentContext<'tcx> {
129129
fn select(
130130
&mut self,
131131
selcx: &mut SelectionContext<'a, 'tcx>,
132-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
132+
) -> Vec<FulfillmentError<'tcx>> {
133133
let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
134134
let _enter = span.enter();
135135

@@ -163,7 +163,7 @@ impl<'a, 'tcx> FulfillmentContext<'tcx> {
163163
errors.len()
164164
);
165165

166-
if errors.is_empty() { Ok(()) } else { Err(errors) }
166+
errors
167167
}
168168
}
169169

@@ -226,38 +226,46 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
226226
fn select_all_or_error(
227227
&mut self,
228228
infcx: &InferCtxt<'_, 'tcx>,
229-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
230-
self.select_where_possible(infcx)?;
229+
) -> Vec<FulfillmentError<'tcx>> {
230+
{
231+
let errors = self.select_where_possible(infcx);
232+
if !errors.is_empty() {
233+
return errors;
234+
}
235+
}
231236

232-
let errors: Vec<_> = self
237+
self
233238
.predicates
234239
.to_errors(CodeAmbiguity)
235240
.into_iter()
236241
.map(to_fulfillment_error)
237-
.collect();
238-
if errors.is_empty() { Ok(()) } else { Err(errors) }
242+
.collect()
239243
}
240244

241245
fn select_all_with_constness_or_error(
242246
&mut self,
243247
infcx: &InferCtxt<'_, 'tcx>,
244248
constness: rustc_hir::Constness,
245-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
246-
self.select_with_constness_where_possible(infcx, constness)?;
249+
) -> Vec<FulfillmentError<'tcx>> {
250+
{
251+
let errors = self.select_with_constness_where_possible(infcx, constness);
252+
if !errors.is_empty() {
253+
return errors;
254+
}
255+
}
247256

248-
let errors: Vec<_> = self
257+
self
249258
.predicates
250259
.to_errors(CodeAmbiguity)
251260
.into_iter()
252261
.map(to_fulfillment_error)
253-
.collect();
254-
if errors.is_empty() { Ok(()) } else { Err(errors) }
262+
.collect()
255263
}
256264

257265
fn select_where_possible(
258266
&mut self,
259267
infcx: &InferCtxt<'_, 'tcx>,
260-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
268+
) -> Vec<FulfillmentError<'tcx>> {
261269
let mut selcx = SelectionContext::new(infcx);
262270
self.select(&mut selcx)
263271
}
@@ -266,7 +274,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
266274
&mut self,
267275
infcx: &InferCtxt<'_, 'tcx>,
268276
constness: hir::Constness,
269-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
277+
) -> Vec<FulfillmentError<'tcx>> {
270278
let mut selcx = SelectionContext::with_constness(infcx, constness);
271279
self.select(&mut selcx)
272280
}

compiler/rustc_trait_selection/src/traits/mod.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,21 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
180180
// Note: we only assume something is `Copy` if we can
181181
// *definitively* show that it implements `Copy`. Otherwise,
182182
// assume it is move; linear is always ok.
183-
match fulfill_cx.select_all_or_error(infcx) {
184-
Ok(()) => {
183+
match fulfill_cx.select_all_or_error(infcx).as_slice() {
184+
[] => {
185185
debug!(
186186
"type_known_to_meet_bound_modulo_regions: ty={:?} bound={} success",
187187
ty,
188188
infcx.tcx.def_path_str(def_id)
189189
);
190190
true
191191
}
192-
Err(e) => {
192+
errors => {
193193
debug!(
194-
"type_known_to_meet_bound_modulo_regions: ty={:?} bound={} errors={:?}",
195-
ty,
196-
infcx.tcx.def_path_str(def_id),
197-
e
194+
?ty,
195+
bound = %infcx.tcx.def_path_str(def_id),
196+
?errors,
197+
"type_known_to_meet_bound_modulo_regions"
198198
);
199199
false
200200
}
@@ -410,7 +410,10 @@ where
410410
}
411411

412412
debug!("fully_normalize: select_all_or_error start");
413-
fulfill_cx.select_all_or_error(infcx)?;
413+
let errors = fulfill_cx.select_all_or_error(infcx);
414+
if !errors.is_empty() {
415+
return Err(errors);
416+
}
414417
debug!("fully_normalize: select_all_or_error complete");
415418
let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
416419
debug!("fully_normalize: resolved_value={:?}", resolved_value);
@@ -441,7 +444,9 @@ pub fn impossible_predicates<'tcx>(
441444
fulfill_cx.register_predicate_obligation(&infcx, obligation);
442445
}
443446

444-
fulfill_cx.select_all_or_error(&infcx).is_err()
447+
let errors = fulfill_cx.select_all_or_error(&infcx);
448+
449+
!errors.is_empty()
445450
});
446451
debug!("impossible_predicates = {:?}", result);
447452
result

compiler/rustc_trait_selection/src/traits/query/type_op/custom.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ fn scrape_region_constraints<'tcx, Op: super::TypeOp<'tcx, Output = R>, R>(
7777
let InferOk { value, obligations } = infcx.commit_if_ok(|_| op())?;
7878
debug_assert!(obligations.iter().all(|o| o.cause.body_id == dummy_body_id));
7979
fulfill_cx.register_predicate_obligations(infcx, obligations);
80-
if let Err(e) = fulfill_cx.select_all_or_error(infcx) {
80+
let errors = fulfill_cx.select_all_or_error(infcx);
81+
if !errors.is_empty() {
8182
infcx.tcx.sess.diagnostic().delay_span_bug(
8283
DUMMY_SP,
83-
&format!("errors selecting obligation during MIR typeck: {:?}", e),
84+
&format!("errors selecting obligation during MIR typeck: {:?}", errors),
8485
);
8586
}
8687

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,18 @@ fn fulfill_implication<'a, 'tcx>(
225225
for oblig in obligations.chain(more_obligations) {
226226
fulfill_cx.register_predicate_obligation(&infcx, oblig);
227227
}
228-
match fulfill_cx.select_all_or_error(infcx) {
229-
Err(errors) => {
228+
match fulfill_cx.select_all_or_error(infcx).as_slice() {
229+
[] => {
230+
debug!(
231+
"fulfill_implication: an impl for {:?} specializes {:?}",
232+
source_trait_ref, target_trait_ref
233+
);
234+
235+
// Now resolve the *substitution* we built for the target earlier, replacing
236+
// the inference variables inside with whatever we got from fulfillment.
237+
Ok(infcx.resolve_vars_if_possible(target_substs))
238+
}
239+
errors => {
230240
// no dice!
231241
debug!(
232242
"fulfill_implication: for impls on {:?} and {:?}, \
@@ -238,17 +248,6 @@ fn fulfill_implication<'a, 'tcx>(
238248
);
239249
Err(())
240250
}
241-
242-
Ok(()) => {
243-
debug!(
244-
"fulfill_implication: an impl for {:?} specializes {:?}",
245-
source_trait_ref, target_trait_ref
246-
);
247-
248-
// Now resolve the *substitution* we built for the target earlier, replacing
249-
// the inference variables inside with whatever we got from fulfillment.
250-
Ok(infcx.resolve_vars_if_possible(target_substs))
251-
}
252251
}
253252
})
254253
}

compiler/rustc_trait_selection/src/traits/structural_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn type_marked_structural(
103103
//
104104
// 2. We are sometimes doing future-incompatibility lints for
105105
// now, so we do not want unconditional errors here.
106-
fulfillment_cx.select_all_or_error(infcx).is_ok()
106+
fulfillment_cx.select_all_or_error(infcx).is_empty()
107107
}
108108

109109
/// This implements the traversal over the structure of a given type to try to

compiler/rustc_traits/src/implied_outlives_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ fn compute_implied_outlives_bounds<'tcx>(
128128

129129
// Ensure that those obligations that we had to solve
130130
// get solved *here*.
131-
match fulfill_cx.select_all_or_error(infcx) {
132-
Ok(()) => Ok(implied_bounds),
133-
Err(_) => Err(NoSolution),
131+
match fulfill_cx.select_all_or_error(infcx).as_slice() {
132+
[] => Ok(implied_bounds),
133+
_ => Err(NoSolution),
134134
}
135135
}
136136

compiler/rustc_typeck/src/check/check.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,9 @@ fn check_opaque_meets_bounds<'tcx>(
663663

664664
// Check that all obligations are satisfied by the implementation's
665665
// version.
666-
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
667-
infcx.report_fulfillment_errors(errors, None, false);
666+
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
667+
if !errors.is_empty() {
668+
infcx.report_fulfillment_errors(&errors, None, false);
668669
}
669670

670671
// Finally, resolve all regions. This catches wily misuses of

0 commit comments

Comments
 (0)