Use structurally_normalize
instead of manual normalizes-to
goals
This commit is contained in:
parent
b2728d5426
commit
513bfaa8bc
21 changed files with 254 additions and 108 deletions
|
@ -1338,20 +1338,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
let derive_better_type_error =
|
||||
|alias_term: ty::AliasTerm<'tcx>, expected_term: ty::Term<'tcx>| {
|
||||
let ocx = ObligationCtxt::new(self);
|
||||
let normalized_term = match expected_term.unpack() {
|
||||
ty::TermKind::Ty(_) => self.next_ty_var(DUMMY_SP).into(),
|
||||
ty::TermKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
|
||||
};
|
||||
ocx.register_obligation(Obligation::new(
|
||||
self.tcx,
|
||||
ObligationCause::dummy(),
|
||||
|
||||
let Ok(normalized_term) = ocx.structurally_normalize_term(
|
||||
&ObligationCause::dummy(),
|
||||
obligation.param_env,
|
||||
ty::PredicateKind::NormalizesTo(ty::NormalizesTo {
|
||||
alias: alias_term,
|
||||
term: normalized_term,
|
||||
}),
|
||||
));
|
||||
let _ = ocx.select_where_possible();
|
||||
alias_term.to_term(self.tcx),
|
||||
) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if let Err(terr) = ocx.eq(
|
||||
&ObligationCause::dummy(),
|
||||
obligation.param_env,
|
||||
|
|
|
@ -340,4 +340,15 @@ where
|
|||
.at(cause, param_env)
|
||||
.structurally_normalize_const(value, &mut **self.engine.borrow_mut())
|
||||
}
|
||||
|
||||
pub fn structurally_normalize_term(
|
||||
&self,
|
||||
cause: &ObligationCause<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
value: ty::Term<'tcx>,
|
||||
) -> Result<ty::Term<'tcx>, Vec<E>> {
|
||||
self.infcx
|
||||
.at(cause, param_env)
|
||||
.structurally_normalize_term(value, &mut **self.engine.borrow_mut())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,39 +12,7 @@ impl<'tcx> At<'_, 'tcx> {
|
|||
ty: Ty<'tcx>,
|
||||
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
|
||||
) -> Result<Ty<'tcx>, Vec<E>> {
|
||||
assert!(!ty.is_ty_var(), "should have resolved vars before calling");
|
||||
|
||||
if self.infcx.next_trait_solver() {
|
||||
let ty::Alias(..) = *ty.kind() else {
|
||||
return Ok(ty);
|
||||
};
|
||||
|
||||
let new_infer_ty = self.infcx.next_ty_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(
|
||||
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);
|
||||
}
|
||||
|
||||
Ok(self.infcx.resolve_vars_if_possible(new_infer_ty))
|
||||
} else {
|
||||
Ok(self.normalize(ty).into_value_registering_obligations(self.infcx, fulfill_cx))
|
||||
}
|
||||
self.structurally_normalize_term(ty.into(), fulfill_cx).map(|term| term.expect_type())
|
||||
}
|
||||
|
||||
fn structurally_normalize_const<E: 'tcx>(
|
||||
|
@ -52,14 +20,29 @@ impl<'tcx> At<'_, 'tcx> {
|
|||
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.tcx.features().generic_const_exprs() {
|
||||
return Ok(super::evaluate_const(&self.infcx, ct, self.param_env));
|
||||
}
|
||||
|
||||
self.structurally_normalize_term(ct.into(), fulfill_cx).map(|term| term.expect_const())
|
||||
}
|
||||
|
||||
fn structurally_normalize_term<E: 'tcx>(
|
||||
&self,
|
||||
term: ty::Term<'tcx>,
|
||||
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
|
||||
) -> Result<ty::Term<'tcx>, Vec<E>> {
|
||||
assert!(!term.is_infer(), "should have resolved vars before calling");
|
||||
|
||||
if self.infcx.next_trait_solver() {
|
||||
let ty::ConstKind::Unevaluated(..) = ct.kind() else {
|
||||
return Ok(ct);
|
||||
};
|
||||
if let None = term.to_alias_term() {
|
||||
return Ok(term);
|
||||
}
|
||||
|
||||
let new_infer_ct = self.infcx.next_const_var(self.cause.span);
|
||||
let new_infer = match term.unpack() {
|
||||
ty::TermKind::Ty(_) => self.infcx.next_ty_var(self.cause.span).into(),
|
||||
ty::TermKind::Const(_) => self.infcx.next_const_var(self.cause.span).into(),
|
||||
};
|
||||
|
||||
// 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
|
||||
|
@ -68,11 +51,7 @@ impl<'tcx> At<'_, 'tcx> {
|
|||
self.infcx.tcx,
|
||||
self.cause.clone(),
|
||||
self.param_env,
|
||||
ty::PredicateKind::AliasRelate(
|
||||
ct.into(),
|
||||
new_infer_ct.into(),
|
||||
ty::AliasRelationDirection::Equate,
|
||||
),
|
||||
ty::PredicateKind::AliasRelate(term, new_infer, ty::AliasRelationDirection::Equate),
|
||||
);
|
||||
|
||||
fulfill_cx.register_predicate_obligation(self.infcx, obligation);
|
||||
|
@ -81,11 +60,9 @@ impl<'tcx> At<'_, 'tcx> {
|
|||
return Err(errors);
|
||||
}
|
||||
|
||||
Ok(self.infcx.resolve_vars_if_possible(new_infer_ct))
|
||||
} else if self.infcx.tcx.features().generic_const_exprs() {
|
||||
Ok(super::evaluate_const(&self.infcx, ct, self.param_env))
|
||||
Ok(self.infcx.resolve_vars_if_possible(new_infer))
|
||||
} else {
|
||||
Ok(self.normalize(ct).into_value_registering_obligations(self.infcx, fulfill_cx))
|
||||
Ok(self.normalize(term).into_value_registering_obligations(self.infcx, fulfill_cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue