Skip to content

Commit 0a79138

Browse files
authored
Rollup merge of #104509 - spastorino:use-obligation-ctxt, r=lcnr
Use obligation ctxt instead of dyn TraitEngine r? `@lcnr`
2 parents d3e9191 + 859b147 commit 0a79138

File tree

6 files changed

+48
-63
lines changed

6 files changed

+48
-63
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;
@@ -1055,9 +1054,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551054
let Ok(ok) = coerce.coerce(source, target) else {
10561055
return false;
10571056
};
1058-
let mut fcx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx);
1059-
fcx.register_predicate_obligations(self, ok.obligations);
1060-
fcx.select_where_possible(&self).is_empty()
1057+
let ocx = ObligationCtxt::new_in_snapshot(self);
1058+
ocx.register_obligations(ok.obligations);
1059+
ocx.select_where_possible().is_empty()
10611060
})
10621061
}
10631062

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;
@@ -30,7 +29,6 @@ use rustc_hir::Item;
3029
use rustc_hir::Node;
3130
use rustc_infer::infer::error_reporting::TypeErrCtxt;
3231
use rustc_infer::infer::TypeTrace;
33-
use rustc_infer::traits::TraitEngine;
3432
use rustc_middle::traits::select::OverflowError;
3533
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
3634
use rustc_middle::ty::error::ExpectedFound;
@@ -354,9 +352,9 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
354352
param_env,
355353
ty.rebind(ty::TraitPredicate { trait_ref, constness, polarity }),
356354
);
357-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx);
358-
fulfill_cx.register_predicate_obligation(self, obligation);
359-
if fulfill_cx.select_all_or_error(self).is_empty() {
355+
let ocx = ObligationCtxt::new_in_snapshot(self);
356+
ocx.register_obligation(obligation);
357+
if ocx.select_all_or_error().is_empty() {
360358
return Ok((
361359
ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
362360
.expect("expected to map DefId to ClosureKind"),

compiler/rustc_trait_selection/src/traits/mod.rs

+14-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::{
@@ -403,9 +402,7 @@ pub fn fully_solve_obligation<'tcx>(
403402
infcx: &InferCtxt<'tcx>,
404403
obligation: PredicateObligation<'tcx>,
405404
) -> Vec<FulfillmentError<'tcx>> {
406-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
407-
engine.register_predicate_obligation(infcx, obligation);
408-
engine.select_all_or_error(infcx)
405+
fully_solve_obligations(infcx, [obligation])
409406
}
410407

411408
/// Process a set of obligations (and any nested obligations that come from them)
@@ -414,9 +411,9 @@ pub fn fully_solve_obligations<'tcx>(
414411
infcx: &InferCtxt<'tcx>,
415412
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
416413
) -> Vec<FulfillmentError<'tcx>> {
417-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
418-
engine.register_predicate_obligations(infcx, obligations);
419-
engine.select_all_or_error(infcx)
414+
let ocx = ObligationCtxt::new(infcx);
415+
ocx.register_obligations(obligations);
416+
ocx.select_all_or_error()
420417
}
421418

422419
/// Process a bound (and any nested obligations that come from it) to completion.
@@ -429,9 +426,16 @@ pub fn fully_solve_bound<'tcx>(
429426
ty: Ty<'tcx>,
430427
bound: DefId,
431428
) -> Vec<FulfillmentError<'tcx>> {
432-
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
433-
engine.register_bound(infcx, param_env, ty, bound, cause);
434-
engine.select_all_or_error(infcx)
429+
let tcx = infcx.tcx;
430+
let trait_ref = ty::TraitRef { def_id: bound, substs: tcx.mk_substs_trait(ty, []) };
431+
let obligation = Obligation {
432+
cause,
433+
recursion_depth: 0,
434+
param_env,
435+
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
436+
};
437+
438+
fully_solve_obligation(infcx, obligation)
435439
}
436440

437441
/// 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

compiler/rustc_traits/src/implied_outlives_bounds.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
use rustc_hir as hir;
66
use rustc_infer::infer::canonical::{self, Canonical};
77
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
8-
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
8+
use rustc_infer::infer::TyCtxtInferExt;
99
use rustc_infer::traits::query::OutlivesBound;
10-
use rustc_infer::traits::TraitEngineExt as _;
1110
use rustc_middle::ty::query::Providers;
1211
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
1312
use rustc_span::source_map::DUMMY_SP;
1413
use rustc_trait_selection::infer::InferCtxtBuilderExt;
1514
use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution};
1615
use rustc_trait_selection::traits::wf;
17-
use rustc_trait_selection::traits::{TraitEngine, TraitEngineExt};
16+
use rustc_trait_selection::traits::ObligationCtxt;
1817
use smallvec::{smallvec, SmallVec};
1918

2019
pub(crate) fn provide(p: &mut Providers) {
@@ -30,16 +29,16 @@ fn implied_outlives_bounds<'tcx>(
3029
> {
3130
tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
3231
let (param_env, ty) = key.into_parts();
33-
compute_implied_outlives_bounds(&ocx.infcx, param_env, ty)
32+
compute_implied_outlives_bounds(ocx, param_env, ty)
3433
})
3534
}
3635

3736
fn compute_implied_outlives_bounds<'tcx>(
38-
infcx: &InferCtxt<'tcx>,
37+
ocx: &ObligationCtxt<'_, 'tcx>,
3938
param_env: ty::ParamEnv<'tcx>,
4039
ty: Ty<'tcx>,
4140
) -> Fallible<Vec<OutlivesBound<'tcx>>> {
42-
let tcx = infcx.tcx;
41+
let tcx = ocx.infcx.tcx;
4342

4443
// Sometimes when we ask what it takes for T: WF, we get back that
4544
// U: WF is required; in that case, we push U onto this stack and
@@ -52,8 +51,6 @@ fn compute_implied_outlives_bounds<'tcx>(
5251
let mut outlives_bounds: Vec<ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>> =
5352
vec![];
5453

55-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
56-
5754
while let Some(arg) = wf_args.pop() {
5855
if !checked_wf_args.insert(arg) {
5956
continue;
@@ -70,15 +67,15 @@ fn compute_implied_outlives_bounds<'tcx>(
7067
// FIXME(@lcnr): It's not really "always fine", having fewer implied
7168
// bounds can be backward incompatible, e.g. #101951 was caused by
7269
// us not dealing with inference vars in `TypeOutlives` predicates.
73-
let obligations = wf::obligations(infcx, param_env, hir::CRATE_HIR_ID, 0, arg, DUMMY_SP)
74-
.unwrap_or_default();
70+
let obligations =
71+
wf::obligations(ocx.infcx, param_env, hir::CRATE_HIR_ID, 0, arg, DUMMY_SP)
72+
.unwrap_or_default();
7573

7674
// While these predicates should all be implied by other parts of
7775
// the program, they are still relevant as they may constrain
7876
// inference variables, which is necessary to add the correct
7977
// implied bounds in some cases, mostly when dealing with projections.
80-
fulfill_cx.register_predicate_obligations(
81-
infcx,
78+
ocx.register_obligations(
8279
obligations.iter().filter(|o| o.predicate.has_non_region_infer()).cloned(),
8380
);
8481

@@ -116,9 +113,9 @@ fn compute_implied_outlives_bounds<'tcx>(
116113
}));
117114
}
118115

119-
// Ensure that those obligations that we had to solve
120-
// get solved *here*.
121-
match fulfill_cx.select_all_or_error(infcx).as_slice() {
116+
// This call to `select_all_or_error` is necessary to constrain inference variables, which we
117+
// use further down when computing the implied bounds.
118+
match ocx.select_all_or_error().as_slice() {
122119
[] => (),
123120
_ => return Err(NoSolution),
124121
}
@@ -130,7 +127,7 @@ fn compute_implied_outlives_bounds<'tcx>(
130127
.flat_map(|ty::OutlivesPredicate(a, r_b)| match a.unpack() {
131128
ty::GenericArgKind::Lifetime(r_a) => vec![OutlivesBound::RegionSubRegion(r_b, r_a)],
132129
ty::GenericArgKind::Type(ty_a) => {
133-
let ty_a = infcx.resolve_vars_if_possible(ty_a);
130+
let ty_a = ocx.infcx.resolve_vars_if_possible(ty_a);
134131
let mut components = smallvec![];
135132
push_outlives_components(tcx, ty_a, &mut components);
136133
implied_bounds_from_components(r_b, components)

0 commit comments

Comments
 (0)