Skip to content

Commit 3d4da98

Browse files
Make TraitEngine::new use the right solver, add compare mode
1 parent b637048 commit 3d4da98

File tree

8 files changed

+49
-32
lines changed

8 files changed

+49
-32
lines changed

compiler/rustc_hir_analysis/src/autoderef.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
161161
&self,
162162
ty: Ty<'tcx>,
163163
) -> Option<(Ty<'tcx>, Vec<traits::PredicateObligation<'tcx>>)> {
164-
let tcx = self.infcx.tcx;
165-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new_in_snapshot(tcx);
164+
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.infcx);
166165

167166
let cause = traits::ObligationCause::misc(self.span, self.body_id);
168167
let normalized_ty = match self

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ pub(super) fn check_generator_obligations(tcx: TyCtxt<'_>, def_id: LocalDefId) {
15491549
.with_opaque_type_inference(DefiningAnchor::Bind(def_id))
15501550
.build();
15511551

1552-
let mut fulfillment_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
1552+
let mut fulfillment_cx = <dyn TraitEngine<'_>>::new(&infcx);
15531553
for (predicate, cause) in generator_interior_predicates {
15541554
let obligation = Obligation::new(tcx, cause.clone(), param_env, *predicate);
15551555
fulfillment_cx.register_predicate_obligation(&infcx, obligation);

compiler/rustc_hir_typeck/src/inherited.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ impl<'tcx> Inherited<'tcx> {
8686

8787
Inherited {
8888
typeck_results,
89+
fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(&infcx)),
8990
infcx,
90-
fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
9191
locals: RefCell::new(Default::default()),
9292
deferred_sized_obligations: RefCell::new(Vec::new()),
9393
deferred_call_resolutions: RefCell::new(Default::default()),

compiler/rustc_infer/src/infer/combine.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,15 @@ impl<'tcx> InferCtxt<'tcx> {
231231
{
232232
let (a, b) = if relation.a_is_expected() { (a, b) } else { (b, a) };
233233

234-
relation.register_predicates([ty::Binder::dummy(
235-
if self.next_trait_solver() {
236-
ty::PredicateKind::AliasRelate(
237-
a.into(),
238-
b.into(),
239-
ty::AliasRelationDirection::Equate,
240-
)
241-
} else {
242-
ty::PredicateKind::ConstEquate(a, b)
243-
},
244-
)]);
234+
relation.register_predicates([ty::Binder::dummy(if self.next_trait_solver() {
235+
ty::PredicateKind::AliasRelate(
236+
a.into(),
237+
b.into(),
238+
ty::AliasRelationDirection::Equate,
239+
)
240+
} else {
241+
ty::PredicateKind::ConstEquate(a, b)
242+
})]);
245243

246244
return Ok(b);
247245
}

compiler/rustc_trait_selection/src/traits/engine.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,42 @@ use rustc_session::config::TraitSolver;
2727
use rustc_span::Span;
2828

2929
pub trait TraitEngineExt<'tcx> {
30-
fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
31-
fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self>;
30+
fn new(infcx: &InferCtxt<'tcx>) -> Box<Self>;
31+
fn new_in_snapshot(infcx: &InferCtxt<'tcx>) -> Box<Self>;
3232
}
3333

3434
impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
35-
fn new(tcx: TyCtxt<'tcx>) -> Box<Self> {
36-
match tcx.sess.opts.unstable_opts.trait_solver {
37-
TraitSolver::Classic => Box::new(FulfillmentContext::new()),
38-
TraitSolver::NextCoherence => Box::new(FulfillmentContext::new()),
39-
TraitSolver::Chalk => Box::new(ChalkFulfillmentContext::new()),
40-
TraitSolver::Next => Box::new(NextFulfillmentCtxt::new()),
35+
fn new(infcx: &InferCtxt<'tcx>) -> Box<Self> {
36+
match (infcx.tcx.sess.opts.unstable_opts.trait_solver, infcx.next_trait_solver()) {
37+
(TraitSolver::Classic, false) | (TraitSolver::NextCoherence, false) => {
38+
Box::new(FulfillmentContext::new())
39+
}
40+
(TraitSolver::Next | TraitSolver::NextCoherence, true) => {
41+
Box::new(NextFulfillmentCtxt::new())
42+
}
43+
(TraitSolver::Chalk, false) => Box::new(ChalkFulfillmentContext::new()),
44+
_ => bug!(
45+
"incompatible combination of -Ztrait-solver flag ({:?}) and InferCtxt::next_trait_solver ({:?})",
46+
infcx.tcx.sess.opts.unstable_opts.trait_solver,
47+
infcx.next_trait_solver()
48+
),
4149
}
4250
}
4351

44-
fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self> {
45-
match tcx.sess.opts.unstable_opts.trait_solver {
46-
TraitSolver::Classic => Box::new(FulfillmentContext::new_in_snapshot()),
47-
TraitSolver::NextCoherence => Box::new(FulfillmentContext::new_in_snapshot()),
48-
TraitSolver::Chalk => Box::new(ChalkFulfillmentContext::new_in_snapshot()),
49-
TraitSolver::Next => Box::new(NextFulfillmentCtxt::new()),
52+
fn new_in_snapshot(infcx: &InferCtxt<'tcx>) -> Box<Self> {
53+
match (infcx.tcx.sess.opts.unstable_opts.trait_solver, infcx.next_trait_solver()) {
54+
(TraitSolver::Classic, false) | (TraitSolver::NextCoherence, false) => {
55+
Box::new(FulfillmentContext::new_in_snapshot())
56+
}
57+
(TraitSolver::Next | TraitSolver::NextCoherence, true) => {
58+
Box::new(NextFulfillmentCtxt::new())
59+
}
60+
(TraitSolver::Chalk, false) => Box::new(ChalkFulfillmentContext::new_in_snapshot()),
61+
_ => bug!(
62+
"incompatible combination of -Ztrait-solver flag ({:?}) and InferCtxt::next_trait_solver ({:?})",
63+
infcx.tcx.sess.opts.unstable_opts.trait_solver,
64+
infcx.next_trait_solver()
65+
),
5066
}
5167
}
5268
}
@@ -60,11 +76,11 @@ pub struct ObligationCtxt<'a, 'tcx> {
6076

6177
impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
6278
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
63-
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
79+
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx)) }
6480
}
6581

6682
pub fn new_in_snapshot(infcx: &'a InferCtxt<'tcx>) -> Self {
67-
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx.tcx)) }
83+
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx)) }
6884
}
6985

7086
pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {

compiler/rustc_traits/src/codegen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn codegen_select_candidate<'tcx>(
5555
// Currently, we use a fulfillment context to completely resolve
5656
// all nested obligations. This is because they can inform the
5757
// inference of the impl's type parameters.
58-
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
58+
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(&infcx);
5959
let impl_source = selection.map(|predicate| {
6060
fulfill_cx.register_predicate_obligation(&infcx, predicate);
6161
});

src/tools/compiletest/src/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ string_enum! {
108108
Polonius => "polonius",
109109
Chalk => "chalk",
110110
NextSolver => "next-solver",
111+
NextSolverCoherence => "next-solver-coherence",
111112
SplitDwarf => "split-dwarf",
112113
SplitDwarfSingle => "split-dwarf-single",
113114
}

src/tools/compiletest/src/runtest.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,9 @@ impl<'test> TestCx<'test> {
21272127
Some(CompareMode::NextSolver) => {
21282128
rustc.args(&["-Ztrait-solver=next"]);
21292129
}
2130+
Some(CompareMode::NextSolverCoherence) => {
2131+
rustc.args(&["-Ztrait-solver=next-coherence"]);
2132+
}
21302133
Some(CompareMode::SplitDwarf) if self.config.target.contains("windows") => {
21312134
rustc.args(&["-Csplit-debuginfo=unpacked", "-Zunstable-options"]);
21322135
}

0 commit comments

Comments
 (0)