Skip to content

Commit 0bf1d73

Browse files
committed
Don't go through TraitRef to relate projections
1 parent 9bbd3e0 commit 0bf1d73

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

compiler/rustc_infer/src/infer/at.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5555

5656
pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
5757
fn to_trace(
58+
tcx: TyCtxt<'tcx>,
5859
cause: &ObligationCause<'tcx>,
5960
a_is_expected: bool,
6061
a: Self,
@@ -178,7 +179,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
178179
where
179180
T: ToTrace<'tcx>,
180181
{
181-
let trace = ToTrace::to_trace(self.cause, a_is_expected, a, b);
182+
let trace = ToTrace::to_trace(self.infcx.tcx, self.cause, a_is_expected, a, b);
182183
Trace { at: self, trace, a_is_expected }
183184
}
184185
}
@@ -251,6 +252,7 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
251252

252253
impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
253254
fn to_trace(
255+
_: TyCtxt<'tcx>,
254256
cause: &ObligationCause<'tcx>,
255257
a_is_expected: bool,
256258
a: Self,
@@ -262,6 +264,7 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
262264

263265
impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
264266
fn to_trace(
267+
_: TyCtxt<'tcx>,
265268
cause: &ObligationCause<'tcx>,
266269
a_is_expected: bool,
267270
a: Self,
@@ -273,6 +276,7 @@ impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
273276

274277
impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
275278
fn to_trace(
279+
_: TyCtxt<'tcx>,
276280
cause: &ObligationCause<'tcx>,
277281
a_is_expected: bool,
278282
a: Self,
@@ -284,6 +288,7 @@ impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
284288

285289
impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
286290
fn to_trace(
291+
_: TyCtxt<'tcx>,
287292
cause: &ObligationCause<'tcx>,
288293
a_is_expected: bool,
289294
a: Self,
@@ -298,6 +303,7 @@ impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
298303

299304
impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
300305
fn to_trace(
306+
_: TyCtxt<'tcx>,
301307
cause: &ObligationCause<'tcx>,
302308
a_is_expected: bool,
303309
a: Self,
@@ -309,3 +315,20 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
309315
}
310316
}
311317
}
318+
319+
impl<'tcx> ToTrace<'tcx> for ty::ProjectionTy<'tcx> {
320+
fn to_trace(
321+
tcx: TyCtxt<'tcx>,
322+
cause: &ObligationCause<'tcx>,
323+
a_is_expected: bool,
324+
a: Self,
325+
b: Self,
326+
) -> TypeTrace<'tcx> {
327+
let a_ty = tcx.mk_projection(a.item_def_id, a.substs);
328+
let b_ty = tcx.mk_projection(b.item_def_id, b.substs);
329+
TypeTrace {
330+
cause: cause.clone(),
331+
values: Types(ExpectedFound::new(a_is_expected, a_ty, b_ty)),
332+
}
333+
}
334+
}

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>(
921921
&& infcx.probe(|_| {
922922
selcx.match_projection_projections(
923923
obligation,
924-
obligation_trait_ref,
925-
&data,
924+
data,
926925
potentially_unnormalized_candidates,
927926
)
928927
});
@@ -1344,25 +1343,25 @@ fn confirm_param_env_candidate<'cx, 'tcx>(
13441343
poly_cache_entry,
13451344
);
13461345

1347-
let cache_trait_ref = cache_entry.projection_ty.trait_ref(infcx.tcx);
1348-
let obligation_trait_ref = obligation.predicate.trait_ref(infcx.tcx);
1346+
let cache_projection = cache_entry.projection_ty;
1347+
let obligation_projection = obligation.predicate;
13491348
let mut nested_obligations = Vec::new();
1350-
let cache_trait_ref = if potentially_unnormalized_candidate {
1349+
let cache_projection = if potentially_unnormalized_candidate {
13511350
ensure_sufficient_stack(|| {
13521351
normalize_with_depth_to(
13531352
selcx,
13541353
obligation.param_env,
13551354
obligation.cause.clone(),
13561355
obligation.recursion_depth + 1,
1357-
cache_trait_ref,
1356+
cache_projection,
13581357
&mut nested_obligations,
13591358
)
13601359
})
13611360
} else {
1362-
cache_trait_ref
1361+
cache_projection
13631362
};
13641363

1365-
match infcx.at(cause, param_env).eq(cache_trait_ref, obligation_trait_ref) {
1364+
match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
13661365
Ok(InferOk { value: _, obligations }) => {
13671366
nested_obligations.extend(obligations);
13681367
assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use rustc_errors::ErrorReported;
3232
use rustc_hir as hir;
3333
use rustc_hir::def_id::DefId;
3434
use rustc_hir::Constness;
35+
use rustc_infer::infer::LateBoundRegionConversionTime;
3536
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
3637
use rustc_middle::mir::interpret::ErrorHandled;
3738
use rustc_middle::ty::fast_reject;
@@ -1254,32 +1255,33 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12541255
pub(super) fn match_projection_projections(
12551256
&mut self,
12561257
obligation: &ProjectionTyObligation<'tcx>,
1257-
obligation_trait_ref: &ty::TraitRef<'tcx>,
1258-
data: &PolyProjectionPredicate<'tcx>,
1258+
env_predicate: PolyProjectionPredicate<'tcx>,
12591259
potentially_unnormalized_candidates: bool,
12601260
) -> bool {
12611261
let mut nested_obligations = Vec::new();
1262-
let projection_ty = if potentially_unnormalized_candidates {
1262+
let (infer_predicate, _) = self.infcx.replace_bound_vars_with_fresh_vars(
1263+
obligation.cause.span,
1264+
LateBoundRegionConversionTime::HigherRankedType,
1265+
env_predicate,
1266+
);
1267+
let infer_projection = if potentially_unnormalized_candidates {
12631268
ensure_sufficient_stack(|| {
12641269
project::normalize_with_depth_to(
12651270
self,
12661271
obligation.param_env,
12671272
obligation.cause.clone(),
12681273
obligation.recursion_depth + 1,
1269-
data.map_bound(|data| data.projection_ty),
1274+
infer_predicate.projection_ty,
12701275
&mut nested_obligations,
12711276
)
12721277
})
12731278
} else {
1274-
data.map_bound(|data| data.projection_ty)
1279+
infer_predicate.projection_ty
12751280
};
12761281

1277-
// FIXME(generic_associated_types): Compare the whole projections
1278-
let data_poly_trait_ref = projection_ty.map_bound(|proj| proj.trait_ref(self.tcx()));
1279-
let obligation_poly_trait_ref = ty::Binder::dummy(*obligation_trait_ref);
12801282
self.infcx
12811283
.at(&obligation.cause, obligation.param_env)
1282-
.sup(obligation_poly_trait_ref, data_poly_trait_ref)
1284+
.sup(obligation.predicate, infer_projection)
12831285
.map_or(false, |InferOk { obligations, value: () }| {
12841286
self.evaluate_predicates_recursively(
12851287
TraitObligationStackList::empty(&ProvisionalEvaluationCache::default()),

0 commit comments

Comments
 (0)