Skip to content

Commit 1a449dc

Browse files
committed
Auto merge of #113308 - compiler-errors:poly-select, r=lcnr
Split `SelectionContext::select` into fns that take a binder and don't *most* usages of `SelectionContext::select` don't need to use a binder, but wrap them in a dummy because of the signature. Let's split this out into `SelectionContext::{select,poly_select}` and limit the usages of the latter. Right now, we only have 3 places where we're calling `poly_select` -- fulfillment, internally within the old solver, and the auto-trait finder. r? `@lcnr`
2 parents 921f669 + 3f8919c commit 1a449dc

File tree

21 files changed

+125
-112
lines changed

21 files changed

+125
-112
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
1212
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
13-
use rustc_middle::ty::{Binder, TraitRef, TypeVisitableExt};
13+
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
1414
use rustc_mir_dataflow::{self, Analysis};
1515
use rustc_span::{sym, Span, Symbol};
1616
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@@ -755,10 +755,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
755755
}
756756

757757
let trait_ref = TraitRef::from_method(tcx, trait_id, substs);
758-
let poly_trait_pred =
759-
Binder::dummy(trait_ref).with_constness(ty::BoundConstness::ConstIfConst);
758+
let trait_ref = trait_ref.with_constness(ty::BoundConstness::ConstIfConst);
760759
let obligation =
761-
Obligation::new(tcx, ObligationCause::dummy(), param_env, poly_trait_pred);
760+
Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
762761

763762
let implsrc = {
764763
let infcx = tcx.infer_ctxt().build();
@@ -776,11 +775,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
776775
}
777776
// Closure: Fn{Once|Mut}
778777
Ok(Some(ImplSource::Builtin(_)))
779-
if poly_trait_pred.self_ty().skip_binder().is_closure()
778+
if trait_ref.self_ty().is_closure()
780779
&& tcx.fn_trait_kind_from_def_id(trait_id).is_some() =>
781780
{
782781
let ty::Closure(closure_def_id, substs) =
783-
*poly_trait_pred.self_ty().no_bound_vars().unwrap().kind()
782+
*trait_ref.self_ty().kind()
784783
else {
785784
unreachable!()
786785
};
@@ -840,7 +839,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
840839
tcx,
841840
ObligationCause::dummy_with_span(*fn_span),
842841
param_env,
843-
poly_trait_pred,
842+
trait_ref,
844843
);
845844

846845
// improve diagnostics by showing what failed. Our requirements are stricter this time

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1010
use rustc_middle::mir::{self, CallSource};
1111
use rustc_middle::ty::print::with_no_trimmed_paths;
1212
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
13+
use rustc_middle::ty::TraitRef;
1314
use rustc_middle::ty::{suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, Ty};
14-
use rustc_middle::ty::{Binder, TraitRef};
1515
use rustc_middle::util::{call_kind, CallDesugaringKind, CallKind};
1616
use rustc_session::parse::feature_err;
1717
use rustc_span::symbol::sym;
@@ -137,12 +137,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
137137
}
138138
}
139139
Adt(..) => {
140-
let obligation = Obligation::new(
141-
tcx,
142-
ObligationCause::dummy(),
143-
param_env,
144-
Binder::dummy(trait_ref),
145-
);
140+
let obligation =
141+
Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
146142

147143
let infcx = tcx.infer_ctxt().build();
148144
let mut selcx = SelectionContext::new(&infcx);

compiler/rustc_hir_typeck/src/coercion.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
632632
while !queue.is_empty() {
633633
let obligation = queue.remove(0);
634634
debug!("coerce_unsized resolve step: {:?}", obligation);
635-
let bound_predicate = obligation.predicate.kind();
636-
let trait_pred = match bound_predicate.skip_binder() {
637-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))
635+
let trait_pred = match obligation.predicate.kind().no_bound_vars() {
636+
Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)))
638637
if traits.contains(&trait_pred.def_id()) =>
639638
{
640639
if unsize_did == trait_pred.def_id() {
@@ -652,7 +651,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
652651
has_unsized_tuple_coercion = true;
653652
}
654653
}
655-
bound_predicate.rebind(trait_pred)
654+
trait_pred
656655
}
657656
_ => {
658657
coercion.obligations.push(obligation);
@@ -664,8 +663,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
664663
Ok(None) => {
665664
if trait_pred.def_id() == unsize_did {
666665
let trait_pred = self.resolve_vars_if_possible(trait_pred);
667-
let self_ty = trait_pred.skip_binder().self_ty();
668-
let unsize_ty = trait_pred.skip_binder().trait_ref.substs[1].expect_ty();
666+
let self_ty = trait_pred.self_ty();
667+
let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty();
669668
debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
670669
match (self_ty.kind(), unsize_ty.kind()) {
671670
(&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19821982
self.tcx,
19831983
traits::ObligationCause::dummy(),
19841984
self.param_env,
1985-
ty::Binder::dummy(trait_ref),
1985+
trait_ref,
19861986
);
19871987
match SelectionContext::new(&self).select(&obligation) {
19881988
Ok(Some(traits::ImplSource::UserDefined(impl_source))) => {

compiler/rustc_hir_typeck/src/method/probe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14411441
trait_ref: ty::TraitRef<'tcx>,
14421442
) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
14431443
let cause = traits::ObligationCause::misc(self.span, self.body_id);
1444-
let predicate = ty::Binder::dummy(trait_ref);
1445-
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, predicate);
1444+
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref);
14461445
traits::SelectionContext::new(self).select(&obligation)
14471446
}
14481447

compiler/rustc_infer/src/traits/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl<'tcx, P> From<Obligation<'tcx, P>> for solve::Goal<'tcx, P> {
6262
}
6363

6464
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
65-
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
65+
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::TraitPredicate<'tcx>>;
66+
pub type PolyTraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
6667

6768
impl<'tcx> PredicateObligation<'tcx> {
6869
/// Flips the polarity of the inner predicate.
@@ -86,7 +87,7 @@ impl<'tcx> PredicateObligation<'tcx> {
8687
}
8788
}
8889

89-
impl<'tcx> TraitObligation<'tcx> {
90+
impl<'tcx> PolyTraitObligation<'tcx> {
9091
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
9192
pub fn is_const(&self) -> bool {
9293
matches!(
@@ -193,7 +194,7 @@ impl<'tcx> FulfillmentError<'tcx> {
193194
}
194195
}
195196

196-
impl<'tcx> TraitObligation<'tcx> {
197+
impl<'tcx> PolyTraitObligation<'tcx> {
197198
pub fn polarity(&self) -> ty::ImplPolarity {
198199
self.predicate.skip_binder().polarity
199200
}

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
318318
}
319319
}
320320

321-
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
321+
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) {
322322
type CacheSelector = DefaultCacheSelector<Self>;
323323

324324
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
325-
tcx.def_span(self.1.def_id())
325+
tcx.def_span(self.1.def_id)
326326
}
327327
}
328328

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ rustc_queries! {
12781278
}
12791279

12801280
query codegen_select_candidate(
1281-
key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)
1281+
key: (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>)
12821282
) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
12831283
cache_on_disk_if { true }
12841284
desc { |tcx| "computing candidate for `{}`", key.1 }

compiler/rustc_middle/src/ty/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,13 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
13061306
}
13071307
}
13081308

1309+
impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
1310+
#[inline(always)]
1311+
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
1312+
self.without_const()
1313+
}
1314+
}
1315+
13091316
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for TraitRef<'tcx> {
13101317
#[inline(always)]
13111318
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {

compiler/rustc_monomorphize/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ fn custom_coerce_unsize_info<'tcx>(
3131
source_ty: Ty<'tcx>,
3232
target_ty: Ty<'tcx>,
3333
) -> CustomCoerceUnsized {
34-
let trait_ref = ty::Binder::dummy(ty::TraitRef::from_lang_item(
34+
let trait_ref = ty::TraitRef::from_lang_item(
3535
tcx.tcx,
3636
LangItem::CoerceUnsized,
3737
tcx.span,
3838
[source_ty, target_ty],
39-
));
39+
);
4040

4141
match tcx.codegen_select_candidate((ty::ParamEnv::reveal_all(), trait_ref)) {
4242
Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {

compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir::def_id::DefId;
44
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk};
55
use rustc_infer::traits::util::supertraits;
66
use rustc_infer::traits::{
7-
Obligation, PredicateObligation, Selection, SelectionResult, TraitObligation,
7+
Obligation, PolyTraitObligation, PredicateObligation, Selection, SelectionResult,
88
};
99
use rustc_middle::traits::solve::{CanonicalInput, Certainty, Goal};
1010
use rustc_middle::traits::{
@@ -23,14 +23,14 @@ use crate::traits::vtable::{count_own_vtable_entries, prepare_vtable_segments, V
2323
pub trait InferCtxtSelectExt<'tcx> {
2424
fn select_in_new_trait_solver(
2525
&self,
26-
obligation: &TraitObligation<'tcx>,
26+
obligation: &PolyTraitObligation<'tcx>,
2727
) -> SelectionResult<'tcx, Selection<'tcx>>;
2828
}
2929

3030
impl<'tcx> InferCtxtSelectExt<'tcx> for InferCtxt<'tcx> {
3131
fn select_in_new_trait_solver(
3232
&self,
33-
obligation: &TraitObligation<'tcx>,
33+
obligation: &PolyTraitObligation<'tcx>,
3434
) -> SelectionResult<'tcx, Selection<'tcx>> {
3535
assert!(self.next_trait_solver());
3636

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ impl<'tcx> AutoTraitFinder<'tcx> {
9595
tcx,
9696
ObligationCause::dummy(),
9797
orig_env,
98-
ty::Binder::dummy(ty::TraitPredicate {
98+
ty::TraitPredicate {
9999
trait_ref,
100100
constness: ty::BoundConstness::NotConst,
101101
polarity: if polarity {
102102
ImplPolarity::Positive
103103
} else {
104104
ImplPolarity::Negative
105105
},
106-
}),
106+
},
107107
));
108108
if let Ok(Some(ImplSource::UserDefined(_))) = result {
109109
debug!(
@@ -292,7 +292,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
292292
new_env,
293293
pred,
294294
));
295-
let result = select.select(&obligation);
295+
let result = select.poly_select(&obligation);
296296

297297
match result {
298298
Ok(Some(ref impl_source)) => {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::def_id::DefId;
22
use rustc_infer::infer::{InferCtxt, LateBoundRegionConversionTime};
33
use rustc_infer::traits::util::elaborate;
4-
use rustc_infer::traits::{Obligation, ObligationCause, TraitObligation};
4+
use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation};
55
use rustc_middle::ty;
66
use rustc_span::{Span, DUMMY_SP};
77

@@ -14,7 +14,7 @@ pub enum Ambiguity {
1414

1515
pub fn recompute_applicable_impls<'tcx>(
1616
infcx: &InferCtxt<'tcx>,
17-
obligation: &TraitObligation<'tcx>,
17+
obligation: &PolyTraitObligation<'tcx>,
1818
) -> Vec<Ambiguity> {
1919
let tcx = infcx.tcx;
2020
let param_env = obligation.param_env;

compiler/rustc_trait_selection/src/traits/fulfill.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome}
44
use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
55
use rustc_infer::infer::DefineOpaqueTypes;
66
use rustc_infer::traits::ProjectionCacheKey;
7-
use rustc_infer::traits::{SelectionError, TraitEngine, TraitObligation};
7+
use rustc_infer::traits::{PolyTraitObligation, SelectionError, TraitEngine};
88
use rustc_middle::mir::interpret::ErrorHandled;
99
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1010
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -667,7 +667,7 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
667667
fn process_trait_obligation(
668668
&mut self,
669669
obligation: &PredicateObligation<'tcx>,
670-
trait_obligation: TraitObligation<'tcx>,
670+
trait_obligation: PolyTraitObligation<'tcx>,
671671
stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
672672
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
673673
let infcx = self.selcx.infcx;
@@ -683,7 +683,7 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
683683
}
684684
}
685685

686-
match self.selcx.select(&trait_obligation) {
686+
match self.selcx.poly_select(&trait_obligation) {
687687
Ok(Some(impl_source)) => {
688688
debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
689689
ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))

compiler/rustc_trait_selection/src/traits/project.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,6 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>(
15451545
let trait_def_id = tcx.parent(trait_fn_def_id);
15461546
let trait_substs =
15471547
obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
1548-
// FIXME(named-returns): Binders
15491548
let trait_predicate = ty::TraitRef::new(tcx, trait_def_id, trait_substs);
15501549

15511550
let _ = selcx.infcx.commit_if_ok(|_| {
@@ -1747,8 +1746,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
17471746

17481747
// If we are resolving `<T as TraitRef<...>>::Item == Type`,
17491748
// start out by selecting the predicate `T as TraitRef<...>`:
1750-
let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
1751-
let trait_obligation = obligation.with(selcx.tcx(), poly_trait_ref);
1749+
let trait_ref = obligation.predicate.trait_ref(selcx.tcx());
1750+
let trait_obligation = obligation.with(selcx.tcx(), trait_ref);
17521751
let _ = selcx.infcx.commit_if_ok(|_| {
17531752
let impl_source = match selcx.select(&trait_obligation) {
17541753
Ok(Some(impl_source)) => impl_source,
@@ -1802,7 +1801,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
18021801
if obligation.param_env.reveal() == Reveal::All {
18031802
// NOTE(eddyb) inference variables can resolve to parameters, so
18041803
// assume `poly_trait_ref` isn't monomorphic, if it contains any.
1805-
let poly_trait_ref = selcx.infcx.resolve_vars_if_possible(poly_trait_ref);
1804+
let poly_trait_ref = selcx.infcx.resolve_vars_if_possible(trait_ref);
18061805
!poly_trait_ref.still_further_specializable()
18071806
} else {
18081807
debug!(
@@ -1821,11 +1820,11 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
18211820
let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
18221821

18231822
let lang_items = selcx.tcx().lang_items();
1824-
if [lang_items.gen_trait(), lang_items.future_trait()].contains(&Some(poly_trait_ref.def_id()))
1825-
|| selcx.tcx().fn_trait_kind_from_def_id(poly_trait_ref.def_id()).is_some()
1823+
if [lang_items.gen_trait(), lang_items.future_trait()].contains(&Some(trait_ref.def_id))
1824+
|| selcx.tcx().fn_trait_kind_from_def_id(trait_ref.def_id).is_some()
18261825
{
18271826
true
1828-
} else if lang_items.discriminant_kind_trait() == Some(poly_trait_ref.def_id()) {
1827+
} else if lang_items.discriminant_kind_trait() == Some(trait_ref.def_id) {
18291828
match self_ty.kind() {
18301829
ty::Bool
18311830
| ty::Char
@@ -1860,7 +1859,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
18601859
| ty::Infer(..)
18611860
| ty::Error(_) => false,
18621861
}
1863-
} else if lang_items.pointee_trait() == Some(poly_trait_ref.def_id()) {
1862+
} else if lang_items.pointee_trait() == Some(trait_ref.def_id) {
18641863
let tail = selcx.tcx().struct_tail_with_normalize(
18651864
self_ty,
18661865
|ty| {
@@ -1935,7 +1934,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
19351934
}
19361935
}
19371936
} else {
1938-
bug!("unexpected builtin trait with associated type: {poly_trait_ref:?}")
1937+
bug!("unexpected builtin trait with associated type: {trait_ref:?}")
19391938
}
19401939
}
19411940
super::ImplSource::Param(..) => {

0 commit comments

Comments
 (0)