Skip to content

Commit 41e4218

Browse files
Use TraitEngine less
1 parent 11ebe65 commit 41e4218

File tree

6 files changed

+57
-86
lines changed

6 files changed

+57
-86
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_span::hygiene::DesugaringKind;
2323
use rustc_span::symbol::sym;
2424
use rustc_span::{BytePos, Span, Symbol};
2525
use rustc_trait_selection::infer::InferCtxtExt;
26-
use rustc_trait_selection::traits::TraitEngineExt as _;
2726

2827
use crate::borrow_set::TwoPhaseActivation;
2928
use crate::borrowck_errors;
@@ -613,24 +612,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
613612
else { return; };
614613
// Try to find predicates on *generic params* that would allow copying `ty`
615614
let infcx = tcx.infer_ctxt().build();
616-
let mut fulfill_cx = <dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
617-
618615
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
619616
let cause = ObligationCause::new(
620617
span,
621618
self.mir_hir_id(),
622619
rustc_infer::traits::ObligationCauseCode::MiscObligation,
623620
);
624-
fulfill_cx.register_bound(
621+
let errors = rustc_trait_selection::traits::fully_solve_bound(
625622
&infcx,
623+
cause,
626624
self.param_env,
627625
// Erase any region vids from the type, which may not be resolved
628626
infcx.tcx.erase_regions(ty),
629627
copy_did,
630-
cause,
631628
);
632-
// Select all, including ambiguous predicates
633-
let errors = fulfill_cx.select_all_or_error(&infcx);
634629

635630
// Only emit suggestion if all required predicates are on generic
636631
let predicates: Result<Vec<_>, _> = errors

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use rustc_hir::def_id::LocalDefId;
44
use rustc_hir::OpaqueTyOrigin;
55
use rustc_infer::infer::TyCtxtInferExt as _;
66
use rustc_infer::infer::{DefiningAnchor, InferCtxt};
7-
use rustc_infer::traits::{Obligation, ObligationCause, TraitEngine};
7+
use rustc_infer::traits::{Obligation, ObligationCause};
88
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
99
use rustc_middle::ty::visit::TypeVisitable;
1010
use rustc_middle::ty::{
1111
self, OpaqueHiddenType, OpaqueTypeKey, ToPredicate, Ty, TyCtxt, TypeFoldable,
1212
};
1313
use rustc_span::Span;
1414
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
15-
use rustc_trait_selection::traits::TraitEngineExt as _;
15+
use rustc_trait_selection::traits::ObligationCtxt;
1616

1717
use super::RegionInferenceContext;
1818

@@ -252,48 +252,45 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
252252
// type-alias-impl-trait/issue-67844-nested-opaque.rs
253253
let infcx =
254254
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
255+
let ocx = ObligationCtxt::new(&infcx);
255256
// Require the hidden type to be well-formed with only the generics of the opaque type.
256257
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
257258
// hidden type is well formed even without those bounds.
258259
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
259260
.to_predicate(infcx.tcx);
260-
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
261261

262262
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
263263

264264
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
265265
// the bounds that the function supplies.
266266
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
267-
match infcx
268-
.at(&ObligationCause::misc(instantiated_ty.span, body_id), param_env)
269-
.eq(opaque_ty, definition_ty)
270-
{
271-
Ok(infer_ok) => {
272-
for obligation in infer_ok.obligations {
273-
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
274-
}
275-
}
276-
Err(err) => {
277-
infcx
278-
.err_ctxt()
279-
.report_mismatched_types(
280-
&ObligationCause::misc(instantiated_ty.span, body_id),
281-
opaque_ty,
282-
definition_ty,
283-
err,
284-
)
285-
.emit();
286-
}
267+
if let Err(err) = ocx.eq(
268+
&ObligationCause::misc(instantiated_ty.span, body_id),
269+
param_env,
270+
opaque_ty,
271+
definition_ty,
272+
) {
273+
infcx
274+
.err_ctxt()
275+
.report_mismatched_types(
276+
&ObligationCause::misc(instantiated_ty.span, body_id),
277+
opaque_ty,
278+
definition_ty,
279+
err,
280+
)
281+
.emit();
287282
}
288283

289-
fulfillment_cx.register_predicate_obligation(
290-
&infcx,
291-
Obligation::misc(instantiated_ty.span, body_id, param_env, predicate),
292-
);
284+
ocx.register_obligation(Obligation::misc(
285+
instantiated_ty.span,
286+
body_id,
287+
param_env,
288+
predicate,
289+
));
293290

294291
// Check that all obligations are satisfied by the implementation's
295292
// version.
296-
let errors = fulfillment_cx.select_all_or_error(&infcx);
293+
let errors = ocx.select_all_or_error();
297294

298295
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
299296
// tests to pass

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

+15-27
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty,
1313
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef, TypeVisitable};
1414
use rustc_mir_dataflow::{self, Analysis};
1515
use rustc_span::{sym, Span, Symbol};
16-
use rustc_trait_selection::infer::InferCtxtExt;
1716
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
18-
use rustc_trait_selection::traits::{
19-
self, ObligationCauseCode, SelectionContext, TraitEngine, TraitEngineExt,
20-
};
17+
use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt, SelectionContext};
2118

2219
use std::mem;
2320
use std::ops::Deref;
@@ -747,35 +744,26 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
747744
// "non-const" check. This is required for correctness here.
748745
{
749746
let infcx = tcx.infer_ctxt().build();
750-
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
747+
let ocx = ObligationCtxt::new(&infcx);
748+
751749
let predicates = tcx.predicates_of(callee).instantiate(tcx, substs);
752750
let hir_id = tcx
753751
.hir()
754752
.local_def_id_to_hir_id(self.body.source.def_id().expect_local());
755-
let cause = || {
756-
ObligationCause::new(
757-
terminator.source_info.span,
758-
hir_id,
759-
ObligationCauseCode::ItemObligation(callee),
760-
)
761-
};
762-
let normalized = infcx.partially_normalize_associated_types_in(
763-
cause(),
764-
param_env,
765-
predicates,
753+
let cause = ObligationCause::new(
754+
terminator.source_info.span,
755+
hir_id,
756+
ObligationCauseCode::ItemObligation(callee),
766757
);
767-
768-
for p in normalized.obligations {
769-
fulfill_cx.register_predicate_obligation(&infcx, p);
770-
}
771-
for obligation in traits::predicates_for_generics(
772-
|_, _| cause(),
758+
let normalized_predicates =
759+
ocx.normalize(cause.clone(), param_env, predicates);
760+
ocx.register_obligations(traits::predicates_for_generics(
761+
|_, _| cause.clone(),
773762
self.param_env,
774-
normalized.value,
775-
) {
776-
fulfill_cx.register_predicate_obligation(&infcx, obligation);
777-
}
778-
let errors = fulfill_cx.select_all_or_error(&infcx);
763+
normalized_predicates,
764+
));
765+
766+
let errors = ocx.select_all_or_error();
779767
if !errors.is_empty() {
780768
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
781769
}

compiler/rustc_hir_typeck/src/op.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_span::symbol::{sym, Ident};
1919
use rustc_span::Span;
2020
use rustc_trait_selection::infer::InferCtxtExt;
2121
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt as _;
22-
use rustc_trait_selection::traits::{FulfillmentError, TraitEngine, TraitEngineExt};
22+
use rustc_trait_selection::traits::FulfillmentError;
2323
use rustc_type_ir::sty::TyKind::*;
2424

2525
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -785,9 +785,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
785785
other_ty_expr,
786786
expected,
787787
);
788-
let mut fulfill = <dyn TraitEngine<'_>>::new(self.tcx);
789-
fulfill.register_predicate_obligation(self, obligation);
790-
Err(fulfill.select_where_possible(&self.infcx))
788+
Err(rustc_trait_selection::traits::fully_solve_obligation(self, obligation))
791789
}
792790
}
793791
}

compiler/rustc_lint/src/for_loops_over_fallibles.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use crate::{LateContext, LateLintPass, LintContext};
33
use hir::{Expr, Pat};
44
use rustc_errors::{Applicability, DelayDm};
55
use rustc_hir as hir;
6-
use rustc_infer::traits::TraitEngine;
76
use rustc_infer::{infer::TyCtxtInferExt, traits::ObligationCause};
87
use rustc_middle::ty::{self, List};
98
use rustc_span::{sym, Span};
10-
use rustc_trait_selection::traits::TraitEngineExt;
119

1210
declare_lint! {
1311
/// The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values.
@@ -160,24 +158,19 @@ fn suggest_question_mark<'tcx>(
160158

161159
let ty = substs.type_at(0);
162160
let infcx = cx.tcx.infer_ctxt().build();
163-
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
164-
165161
let cause = ObligationCause::new(
166162
span,
167163
body_id.hir_id,
168164
rustc_infer::traits::ObligationCauseCode::MiscObligation,
169165
);
170-
fulfill_cx.register_bound(
166+
let errors = rustc_trait_selection::traits::fully_solve_bound(
171167
&infcx,
168+
cause,
172169
ty::ParamEnv::empty(),
173170
// Erase any region vids from the type, which may not be resolved
174171
infcx.tcx.erase_regions(ty),
175172
into_iterator_did,
176-
cause,
177173
);
178174

179-
// Select all, including ambiguous predicates
180-
let errors = fulfill_cx.select_all_or_error(&infcx);
181-
182175
errors.is_empty()
183176
}

compiler/rustc_trait_selection/src/traits/outlives_bounds.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
33
use crate::traits::query::NoSolution;
4-
use crate::traits::{ObligationCause, TraitEngine, TraitEngineExt};
4+
use crate::traits::ObligationCause;
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_hir as hir;
77
use rustc_hir::HirId;
@@ -74,20 +74,20 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
7474
debug!(?constraints);
7575
// Instantiation may have produced new inference variables and constraints on those
7676
// variables. Process these constraints.
77-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(self.tcx);
7877
let cause = ObligationCause::misc(span, body_id);
79-
for &constraint in &constraints.outlives {
80-
let obligation = self.query_outlives_constraint_to_obligation(
81-
constraint,
82-
cause.clone(),
83-
param_env,
84-
);
85-
fulfill_cx.register_predicate_obligation(self, obligation);
86-
}
78+
let errors = super::fully_solve_obligations(
79+
self,
80+
constraints.outlives.iter().map(|constraint| {
81+
self.query_outlives_constraint_to_obligation(
82+
*constraint,
83+
cause.clone(),
84+
param_env,
85+
)
86+
}),
87+
);
8788
if !constraints.member_constraints.is_empty() {
8889
span_bug!(span, "{:#?}", constraints.member_constraints);
8990
}
90-
let errors = fulfill_cx.select_all_or_error(self);
9191
if !errors.is_empty() {
9292
self.tcx.sess.delay_span_bug(
9393
span,

0 commit comments

Comments
 (0)