Skip to content

Commit b62bee6

Browse files
Instantiate binders when checking supertrait upcasting
1 parent d4ee408 commit b62bee6

File tree

4 files changed

+137
-29
lines changed

4 files changed

+137
-29
lines changed

Diff for: compiler/rustc_infer/src/infer/at.rs

+30
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,36 @@ impl<'a, 'tcx> At<'a, 'tcx> {
178178
})
179179
}
180180

181+
/// Makes `expected == actual`.
182+
pub fn eq_trace<T>(
183+
self,
184+
define_opaque_types: DefineOpaqueTypes,
185+
trace: TypeTrace<'tcx>,
186+
expected: T,
187+
actual: T,
188+
) -> InferResult<'tcx, ()>
189+
where
190+
T: Relate<TyCtxt<'tcx>>,
191+
{
192+
let mut fields = CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types);
193+
fields.equate(StructurallyRelateAliases::No).relate(expected, actual)?;
194+
Ok(InferOk {
195+
value: (),
196+
obligations: fields
197+
.goals
198+
.into_iter()
199+
.map(|goal| {
200+
Obligation::new(
201+
self.infcx.tcx,
202+
fields.trace.cause.clone(),
203+
goal.param_env,
204+
goal.predicate,
205+
)
206+
})
207+
.collect(),
208+
})
209+
}
210+
181211
/// Equates `expected` and `found` while structurally relating aliases.
182212
/// This should only be used inside of the next generation trait solver
183213
/// when relating rigid aliases.

Diff for: compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,16 @@ where
848848
self.delegate.enter_forall(value, f)
849849
}
850850

851+
/// `enter_forall`, but takes `&mut self` and passes it back through the
852+
/// callback since it can't be aliased during the call.
853+
pub(super) fn enter_forall_mut<T: TypeFoldable<I> + Copy, U>(
854+
&mut self,
855+
value: ty::Binder<I, T>,
856+
f: impl FnOnce(&mut Self, T) -> U,
857+
) -> U {
858+
self.delegate.enter_forall(value, |value| f(self, value))
859+
}
860+
851861
pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
852862
where
853863
T: TypeFoldable<I>,

Diff for: compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,13 @@ where
896896
&& ecx
897897
.probe(|_| ProbeKind::UpcastProjectionCompatibility)
898898
.enter(|ecx| -> Result<(), NoSolution> {
899-
ecx.sub(param_env, source_projection, target_projection)?;
900-
let _ = ecx.try_evaluate_added_goals()?;
901-
Ok(())
899+
ecx.enter_forall_mut(target_projection, |ecx, target_projection| {
900+
let source_projection =
901+
ecx.instantiate_binder_with_infer(source_projection);
902+
ecx.eq(param_env, source_projection, target_projection)?;
903+
let _ = ecx.try_evaluate_added_goals()?;
904+
Ok(())
905+
})
902906
})
903907
.is_ok()
904908
};
@@ -909,11 +913,14 @@ where
909913
// Check that a's supertrait (upcast_principal) is compatible
910914
// with the target (b_ty).
911915
ty::ExistentialPredicate::Trait(target_principal) => {
912-
ecx.sub(
913-
param_env,
914-
upcast_principal.unwrap(),
915-
bound.rebind(target_principal),
916-
)?;
916+
let source_principal = upcast_principal.unwrap();
917+
let target_principal = bound.rebind(target_principal);
918+
ecx.enter_forall_mut(target_principal, |ecx, target_principal| {
919+
let source_projection =
920+
ecx.instantiate_binder_with_infer(source_principal);
921+
ecx.eq(param_env, source_projection, target_principal)?;
922+
ecx.try_evaluate_added_goals()
923+
})?;
917924
}
918925
// Check that b_ty's projection is satisfied by exactly one of
919926
// a_ty's projections. First, we look through the list to see if
@@ -934,7 +941,12 @@ where
934941
Certainty::AMBIGUOUS,
935942
);
936943
}
937-
ecx.sub(param_env, source_projection, target_projection)?;
944+
ecx.enter_forall_mut(target_projection, |ecx, target_projection| {
945+
let source_projection =
946+
ecx.instantiate_binder_with_infer(source_projection);
947+
ecx.eq(param_env, source_projection, target_projection)?;
948+
ecx.try_evaluate_added_goals()
949+
})?;
938950
}
939951
// Check that b_ty's auto traits are present in a_ty's bounds.
940952
ty::ExistentialPredicate::AutoTrait(def_id) => {

Diff for: compiler/rustc_trait_selection/src/traits/select/mod.rs

+76-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::LangItem;
1616
use rustc_hir::def_id::DefId;
1717
use rustc_infer::infer::BoundRegionConversionTime::{self, HigherRankedType};
1818
use rustc_infer::infer::DefineOpaqueTypes;
19+
use rustc_infer::infer::at::ToTrace;
1920
use rustc_infer::infer::relate::TypeRelation;
2021
use rustc_infer::traits::TraitObligation;
2122
use rustc_middle::bug;
@@ -44,7 +45,7 @@ use super::{
4445
TraitQueryMode, const_evaluatable, project, util, wf,
4546
};
4647
use crate::error_reporting::InferCtxtErrorExt;
47-
use crate::infer::{InferCtxt, InferCtxtExt, InferOk, TypeFreshener};
48+
use crate::infer::{InferCtxt, InferOk, TypeFreshener};
4849
use crate::solve::InferCtxtSelectExt as _;
4950
use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to};
5051
use crate::traits::project::{ProjectAndUnifyResult, ProjectionCacheKeyExt};
@@ -2579,16 +2580,32 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25792580
// Check that a_ty's supertrait (upcast_principal) is compatible
25802581
// with the target (b_ty).
25812582
ty::ExistentialPredicate::Trait(target_principal) => {
2583+
let hr_source_principal = upcast_principal.map_bound(|trait_ref| {
2584+
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
2585+
});
2586+
let hr_target_principal = bound.rebind(target_principal);
2587+
25822588
nested.extend(
25832589
self.infcx
2584-
.at(&obligation.cause, obligation.param_env)
2585-
.sup(
2586-
DefineOpaqueTypes::Yes,
2587-
bound.rebind(target_principal),
2588-
upcast_principal.map_bound(|trait_ref| {
2589-
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
2590-
}),
2591-
)
2590+
.enter_forall(hr_target_principal, |target_principal| {
2591+
let source_principal =
2592+
self.infcx.instantiate_binder_with_fresh_vars(
2593+
obligation.cause.span,
2594+
HigherRankedType,
2595+
hr_source_principal,
2596+
);
2597+
self.infcx.at(&obligation.cause, obligation.param_env).eq_trace(
2598+
DefineOpaqueTypes::Yes,
2599+
ToTrace::to_trace(
2600+
&obligation.cause,
2601+
true,
2602+
hr_target_principal,
2603+
hr_source_principal,
2604+
),
2605+
target_principal,
2606+
source_principal,
2607+
)
2608+
})
25922609
.map_err(|_| SelectionError::Unimplemented)?
25932610
.into_obligations(),
25942611
);
@@ -2599,28 +2616,67 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25992616
// return ambiguity. Otherwise, if exactly one matches, equate
26002617
// it with b_ty's projection.
26012618
ty::ExistentialPredicate::Projection(target_projection) => {
2602-
let target_projection = bound.rebind(target_projection);
2619+
let hr_target_projection = bound.rebind(target_projection);
2620+
26032621
let mut matching_projections =
2604-
a_data.projection_bounds().filter(|source_projection| {
2622+
a_data.projection_bounds().filter(|&hr_source_projection| {
26052623
// Eager normalization means that we can just use can_eq
26062624
// here instead of equating and processing obligations.
2607-
source_projection.item_def_id() == target_projection.item_def_id()
2608-
&& self.infcx.can_eq(
2609-
obligation.param_env,
2610-
*source_projection,
2611-
target_projection,
2612-
)
2625+
hr_source_projection.item_def_id() == hr_target_projection.item_def_id()
2626+
&& self.infcx.probe(|_| {
2627+
self.infcx
2628+
.enter_forall(hr_target_projection, |target_projection| {
2629+
let source_projection =
2630+
self.infcx.instantiate_binder_with_fresh_vars(
2631+
obligation.cause.span,
2632+
HigherRankedType,
2633+
hr_source_projection,
2634+
);
2635+
self.infcx
2636+
.at(&obligation.cause, obligation.param_env)
2637+
.eq_trace(
2638+
DefineOpaqueTypes::Yes,
2639+
ToTrace::to_trace(
2640+
&obligation.cause,
2641+
true,
2642+
hr_target_projection,
2643+
hr_source_projection,
2644+
),
2645+
target_projection,
2646+
source_projection,
2647+
)
2648+
})
2649+
.is_ok()
2650+
})
26132651
});
2614-
let Some(source_projection) = matching_projections.next() else {
2652+
2653+
let Some(hr_source_projection) = matching_projections.next() else {
26152654
return Err(SelectionError::Unimplemented);
26162655
};
26172656
if matching_projections.next().is_some() {
26182657
return Ok(None);
26192658
}
26202659
nested.extend(
26212660
self.infcx
2622-
.at(&obligation.cause, obligation.param_env)
2623-
.sup(DefineOpaqueTypes::Yes, target_projection, source_projection)
2661+
.enter_forall(hr_target_projection, |target_projection| {
2662+
let source_projection =
2663+
self.infcx.instantiate_binder_with_fresh_vars(
2664+
obligation.cause.span,
2665+
HigherRankedType,
2666+
hr_source_projection,
2667+
);
2668+
self.infcx.at(&obligation.cause, obligation.param_env).eq_trace(
2669+
DefineOpaqueTypes::Yes,
2670+
ToTrace::to_trace(
2671+
&obligation.cause,
2672+
true,
2673+
hr_target_projection,
2674+
hr_source_projection,
2675+
),
2676+
target_projection,
2677+
source_projection,
2678+
)
2679+
})
26242680
.map_err(|_| SelectionError::Unimplemented)?
26252681
.into_obligations(),
26262682
);

0 commit comments

Comments
 (0)