1
Fork 0
rust/compiler/rustc_infer/src/infer/projection.rs

50 lines
2.2 KiB
Rust
Raw Normal View History

use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{self, Ty};
use crate::traits::{Obligation, PredicateObligation};
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use super::InferCtxt;
2022-09-09 13:01:06 -05:00
impl<'tcx> InferCtxt<'tcx> {
2021-09-18 09:34:06 +02:00
/// Instead of normalizing an associated type projection,
/// this function generates an inference variable and registers
/// an obligation that this inference variable must be the result
/// of the given projection. This allows us to proceed with projections
/// while they cannot be resolved yet due to missing information or
/// simply due to the lack of access to the trait resolution machinery.
pub fn infer_projection(
&self,
param_env: ty::ParamEnv<'tcx>,
projection_ty: ty::AliasTy<'tcx>,
cause: ObligationCause<'tcx>,
recursion_depth: usize,
obligations: &mut Vec<PredicateObligation<'tcx>>,
) -> Ty<'tcx> {
if self.next_trait_solver() {
2023-02-10 14:54:50 +00:00
// FIXME(-Ztrait-solver=next): Instead of branching here,
// completely change the normalization routine with the new solver.
//
// The new solver correctly handles projection equality so this hack
2023-03-21 22:11:40 +00:00
// is not necessary. if re-enabled it should emit `PredicateKind::AliasRelate`
2023-06-16 05:59:42 +00:00
// not `PredicateKind::Clause(ClauseKind::Projection(..))` as in the new solver
2023-02-10 14:54:50 +00:00
// `Projection` is used as `normalizes-to` which will fail for `<T as Trait>::Assoc eq ?0`.
return projection_ty.to_ty(self.tcx);
} else {
let def_id = projection_ty.def_id;
let ty_var = self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::NormalizeProjectionType,
span: self.tcx.def_span(def_id),
});
2023-06-16 05:59:42 +00:00
let projection =
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Projection(
ty::ProjectionPredicate { projection_ty, term: ty_var.into() },
)));
2023-02-10 14:54:50 +00:00
let obligation =
Obligation::with_depth(self.tcx, cause, recursion_depth, param_env, projection);
obligations.push(obligation);
ty_var
}
}
}