2023-04-23 19:58:24 +00:00
|
|
|
use rustc_infer::infer::at::At;
|
2024-06-03 09:27:48 -04:00
|
|
|
use rustc_infer::traits::TraitEngine;
|
2024-04-29 08:53:45 +10:00
|
|
|
use rustc_macros::extension;
|
2023-04-23 19:58:24 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
|
2023-12-18 19:22:43 +00:00
|
|
|
use crate::traits::{NormalizeExt, Obligation};
|
2023-04-23 19:58:24 +00:00
|
|
|
|
2024-02-14 17:18:56 +00:00
|
|
|
#[extension(pub trait StructurallyNormalizeExt<'tcx>)]
|
|
|
|
impl<'tcx> At<'_, 'tcx> {
|
2024-06-03 09:27:48 -04:00
|
|
|
fn structurally_normalize<E: 'tcx>(
|
2023-04-23 19:58:24 +00:00
|
|
|
&self,
|
2023-12-18 19:22:43 +00:00
|
|
|
ty: Ty<'tcx>,
|
2024-06-01 14:12:34 -04:00
|
|
|
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
|
|
|
|
) -> Result<Ty<'tcx>, Vec<E>> {
|
2023-04-23 19:58:24 +00:00
|
|
|
assert!(!ty.is_ty_var(), "should have resolved vars before calling");
|
|
|
|
|
2023-05-31 01:02:32 +00:00
|
|
|
if self.infcx.next_trait_solver() {
|
2024-01-10 16:30:07 +00:00
|
|
|
let ty::Alias(..) = *ty.kind() else {
|
2023-12-18 19:22:43 +00:00
|
|
|
return Ok(ty);
|
|
|
|
};
|
|
|
|
|
2024-05-10 09:06:47 +10:00
|
|
|
let new_infer_ty = self.infcx.next_ty_var(self.cause.span);
|
2023-12-18 19:22:43 +00:00
|
|
|
|
|
|
|
// We simply emit an `alias-eq` goal here, since that will take care of
|
|
|
|
// normalizing the LHS of the projection until it is a rigid projection
|
|
|
|
// (or a not-yet-defined opaque in scope).
|
|
|
|
let obligation = Obligation::new(
|
|
|
|
self.infcx.tcx,
|
|
|
|
self.cause.clone(),
|
|
|
|
self.param_env,
|
|
|
|
ty::PredicateKind::AliasRelate(
|
|
|
|
ty.into(),
|
|
|
|
new_infer_ty.into(),
|
|
|
|
ty::AliasRelationDirection::Equate,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
fulfill_cx.register_predicate_obligation(self.infcx, obligation);
|
|
|
|
let errors = fulfill_cx.select_where_possible(self.infcx);
|
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors);
|
2023-04-23 19:58:24 +00:00
|
|
|
}
|
2023-09-20 21:41:29 +02:00
|
|
|
|
2023-12-18 19:22:43 +00:00
|
|
|
Ok(self.infcx.resolve_vars_if_possible(new_infer_ty))
|
2023-04-23 19:58:24 +00:00
|
|
|
} else {
|
|
|
|
Ok(self.normalize(ty).into_value_registering_obligations(self.infcx, fulfill_cx))
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 13:10:12 -04:00
|
|
|
|
|
|
|
fn structurally_normalize_const<E: 'tcx>(
|
|
|
|
&self,
|
|
|
|
ct: ty::Const<'tcx>,
|
|
|
|
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
|
|
|
|
) -> Result<ty::Const<'tcx>, Vec<E>> {
|
|
|
|
assert!(!ct.is_ct_infer(), "should have resolved vars before calling");
|
|
|
|
|
|
|
|
if self.infcx.next_trait_solver() {
|
|
|
|
let ty::ConstKind::Unevaluated(..) = ct.kind() else {
|
|
|
|
return Ok(ct);
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_infer_ct = self.infcx.next_const_var(self.cause.span);
|
|
|
|
|
|
|
|
// We simply emit an `alias-eq` goal here, since that will take care of
|
|
|
|
// normalizing the LHS of the projection until it is a rigid projection
|
|
|
|
// (or a not-yet-defined opaque in scope).
|
|
|
|
let obligation = Obligation::new(
|
|
|
|
self.infcx.tcx,
|
|
|
|
self.cause.clone(),
|
|
|
|
self.param_env,
|
|
|
|
ty::PredicateKind::AliasRelate(
|
|
|
|
ct.into(),
|
|
|
|
new_infer_ct.into(),
|
|
|
|
ty::AliasRelationDirection::Equate,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
fulfill_cx.register_predicate_obligation(self.infcx, obligation);
|
|
|
|
let errors = fulfill_cx.select_where_possible(self.infcx);
|
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.infcx.resolve_vars_if_possible(new_infer_ct))
|
|
|
|
} else {
|
|
|
|
Ok(self.normalize(ct).into_value_registering_obligations(self.infcx, fulfill_cx))
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 19:58:24 +00:00
|
|
|
}
|