]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_infer/src/infer/projection.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / compiler / rustc_infer / src / infer / projection.rs
1 use rustc_middle::traits::ObligationCause;
2 use rustc_middle::ty::{self, Ty};
3
4 use crate::traits::{Obligation, PredicateObligation};
5
6 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
7 use super::InferCtxt;
8
9 impl<'tcx> InferCtxt<'tcx> {
10 /// Instead of normalizing an associated type projection,
11 /// this function generates an inference variable and registers
12 /// an obligation that this inference variable must be the result
13 /// of the given projection. This allows us to proceed with projections
14 /// while they cannot be resolved yet due to missing information or
15 /// simply due to the lack of access to the trait resolution machinery.
16 pub fn infer_projection(
17 &self,
18 param_env: ty::ParamEnv<'tcx>,
19 projection_ty: ty::AliasTy<'tcx>,
20 cause: ObligationCause<'tcx>,
21 recursion_depth: usize,
22 obligations: &mut Vec<PredicateObligation<'tcx>>,
23 ) -> Ty<'tcx> {
24 debug_assert!(!self.next_trait_solver());
25 let def_id = projection_ty.def_id;
26 let ty_var = self.next_ty_var(TypeVariableOrigin {
27 kind: TypeVariableOriginKind::NormalizeProjectionType,
28 span: self.tcx.def_span(def_id),
29 });
30 let projection = ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Projection(
31 ty::ProjectionPredicate { projection_ty, term: ty_var.into() },
32 )));
33 let obligation =
34 Obligation::with_depth(self.tcx, cause, recursion_depth, param_env, projection);
35 obligations.push(obligation);
36 ty_var
37 }
38 }