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

Lines changed: 6 additions & 7 deletions
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

Lines changed: 3 additions & 7 deletions
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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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 {

0 commit comments

Comments
 (0)