Skip to content

Commit 83701f5

Browse files
committed
Use ObligationCtxt intead of dyn TraitEngine
1 parent 6d651a2 commit 83701f5

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_hir::Expr;
4646
use rustc_hir_analysis::astconv::AstConv;
4747
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
4848
use rustc_infer::infer::{Coercion, InferOk, InferResult};
49-
use rustc_infer::traits::{Obligation, TraitEngine, TraitEngineExt};
49+
use rustc_infer::traits::Obligation;
5050
use rustc_middle::lint::in_external_macro;
5151
use rustc_middle::ty::adjustment::{
5252
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast,
@@ -62,8 +62,7 @@ use rustc_span::{self, BytePos, DesugaringKind, Span};
6262
use rustc_target::spec::abi::Abi;
6363
use rustc_trait_selection::infer::InferCtxtExt as _;
6464
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
65-
use rustc_trait_selection::traits::TraitEngineExt as _;
66-
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
65+
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt};
6766

6867
use smallvec::{smallvec, SmallVec};
6968
use std::ops::Deref;
@@ -1039,9 +1038,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10391038
let Ok(ok) = coerce.coerce(source, target) else {
10401039
return false;
10411040
};
1042-
let mut fcx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx);
1043-
fcx.register_predicate_obligations(self, ok.obligations);
1044-
fcx.select_where_possible(&self).is_empty()
1041+
let ocx = ObligationCtxt::new_in_snapshot(self);
1042+
ocx.register_obligations(ok.obligations);
1043+
ocx.select_where_possible().is_empty()
10451044
})
10461045
}
10471046

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ pub mod suggestions;
44

55
use super::{
66
FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation, ObligationCause,
7-
ObligationCauseCode, OutputTypeParameterMismatch, Overflow, PredicateObligation,
8-
SelectionContext, SelectionError, TraitNotObjectSafe,
7+
ObligationCauseCode, ObligationCtxt, OutputTypeParameterMismatch, Overflow,
8+
PredicateObligation, SelectionContext, SelectionError, TraitNotObjectSafe,
99
};
1010
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1111
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1212
use crate::infer::{self, InferCtxt, TyCtxtInferExt};
13-
use crate::traits::engine::TraitEngineExt as _;
1413
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
1514
use crate::traits::query::normalize::AtExt as _;
1615
use crate::traits::specialize::to_pretty_impl_header;
@@ -29,7 +28,6 @@ use rustc_hir::Item;
2928
use rustc_hir::Node;
3029
use rustc_infer::infer::error_reporting::TypeErrCtxt;
3130
use rustc_infer::infer::TypeTrace;
32-
use rustc_infer::traits::TraitEngine;
3331
use rustc_middle::traits::select::OverflowError;
3432
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
3533
use rustc_middle::ty::error::ExpectedFound;
@@ -353,9 +351,9 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
353351
})
354352
.to_predicate(self.tcx),
355353
);
356-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx);
357-
fulfill_cx.register_predicate_obligation(self, obligation);
358-
if fulfill_cx.select_all_or_error(self).is_empty() {
354+
let ocx = ObligationCtxt::new_in_snapshot(self);
355+
ocx.register_obligation(obligation);
356+
if ocx.select_all_or_error().is_empty() {
359357
return Ok((
360358
ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
361359
.expect("expected to map DefId to ClosureKind"),

compiler/rustc_trait_selection/src/traits/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use rustc_errors::ErrorGuaranteed;
3131
use rustc_hir as hir;
3232
use rustc_hir::def_id::DefId;
3333
use rustc_hir::lang_items::LangItem;
34-
use rustc_infer::traits::TraitEngineExt as _;
3534
use rustc_middle::ty::fold::TypeFoldable;
3635
use rustc_middle::ty::visit::TypeVisitable;
3736
use rustc_middle::ty::{
@@ -395,9 +394,9 @@ pub fn fully_solve_obligation<'tcx>(
395394
infcx: &InferCtxt<'tcx>,
396395
obligation: PredicateObligation<'tcx>,
397396
) -> Vec<FulfillmentError<'tcx>> {
398-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
399-
engine.register_predicate_obligation(infcx, obligation);
400-
engine.select_all_or_error(infcx)
397+
let ocx = ObligationCtxt::new(infcx);
398+
ocx.register_obligation(obligation);
399+
ocx.select_all_or_error()
401400
}
402401

403402
/// Process a set of obligations (and any nested obligations that come from them)
@@ -406,9 +405,9 @@ pub fn fully_solve_obligations<'tcx>(
406405
infcx: &InferCtxt<'tcx>,
407406
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
408407
) -> Vec<FulfillmentError<'tcx>> {
409-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
410-
engine.register_predicate_obligations(infcx, obligations);
411-
engine.select_all_or_error(infcx)
408+
let ocx = ObligationCtxt::new(infcx);
409+
ocx.register_obligations(obligations);
410+
ocx.select_all_or_error()
412411
}
413412

414413
/// Process a bound (and any nested obligations that come from it) to completion.
@@ -421,9 +420,9 @@ pub fn fully_solve_bound<'tcx>(
421420
ty: Ty<'tcx>,
422421
bound: DefId,
423422
) -> Vec<FulfillmentError<'tcx>> {
424-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
425-
engine.register_bound(infcx, param_env, ty, bound, cause);
426-
engine.select_all_or_error(infcx)
423+
let ocx = ObligationCtxt::new(infcx);
424+
ocx.register_bound(cause, param_env, ty, bound);
425+
ocx.select_all_or_error()
427426
}
428427

429428
/// Normalizes the predicates and checks whether they hold in an empty environment. If this

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
1111
1212
pub mod specialization_graph;
13-
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
1413
use specialization_graph::GraphExt;
1514

1615
use crate::errors::NegativePositiveConflict;
1716
use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt};
18-
use crate::traits::engine::TraitEngineExt as _;
1917
use crate::traits::select::IntercrateAmbiguityCause;
20-
use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause};
18+
use crate::traits::{
19+
self, coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt,
20+
};
2121
use rustc_data_structures::fx::FxIndexSet;
2222
use rustc_errors::{error_code, DelayDm, Diagnostic};
2323
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -204,12 +204,12 @@ fn fulfill_implication<'tcx>(
204204

205205
// Needs to be `in_snapshot` because this function is used to rebase
206206
// substitutions, which may happen inside of a select within a probe.
207-
let mut engine = <dyn TraitEngine<'tcx>>::new_in_snapshot(infcx.tcx);
207+
let ocx = ObligationCtxt::new_in_snapshot(infcx);
208208
// attempt to prove all of the predicates for impl2 given those for impl1
209209
// (which are packed up in penv)
210-
engine.register_predicate_obligations(infcx, obligations.chain(more_obligations));
210+
ocx.register_obligations(obligations.chain(more_obligations));
211211

212-
let errors = engine.select_all_or_error(infcx);
212+
let errors = ocx.select_all_or_error();
213213
if !errors.is_empty() {
214214
// no dice!
215215
debug!(

compiler/rustc_trait_selection/src/traits/structural_match.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::infer::{InferCtxt, TyCtxtInferExt};
2-
use crate::traits::ObligationCause;
3-
use crate::traits::{TraitEngine, TraitEngineExt};
2+
use crate::traits::{ObligationCause, ObligationCtxt};
43

54
use rustc_data_structures::fx::FxHashSet;
65
use rustc_hir as hir;
@@ -72,28 +71,16 @@ fn type_marked_structural<'tcx>(
7271
adt_ty: Ty<'tcx>,
7372
cause: ObligationCause<'tcx>,
7473
) -> bool {
75-
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
74+
let ocx = ObligationCtxt::new(infcx);
7675
// require `#[derive(PartialEq)]`
7776
let structural_peq_def_id =
7877
infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span));
79-
fulfillment_cx.register_bound(
80-
infcx,
81-
ty::ParamEnv::empty(),
82-
adt_ty,
83-
structural_peq_def_id,
84-
cause.clone(),
85-
);
78+
ocx.register_bound(cause.clone(), ty::ParamEnv::empty(), adt_ty, structural_peq_def_id);
8679
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
8780
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
8881
let structural_teq_def_id =
8982
infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span));
90-
fulfillment_cx.register_bound(
91-
infcx,
92-
ty::ParamEnv::empty(),
93-
adt_ty,
94-
structural_teq_def_id,
95-
cause,
96-
);
83+
ocx.register_bound(cause, ty::ParamEnv::empty(), adt_ty, structural_teq_def_id);
9784

9885
// We deliberately skip *reporting* fulfillment errors (via
9986
// `report_fulfillment_errors`), for two reasons:
@@ -104,7 +91,7 @@ fn type_marked_structural<'tcx>(
10491
//
10592
// 2. We are sometimes doing future-incompatibility lints for
10693
// now, so we do not want unconditional errors here.
107-
fulfillment_cx.select_all_or_error(infcx).is_empty()
94+
ocx.select_all_or_error().is_empty()
10895
}
10996

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

0 commit comments

Comments
 (0)