1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
3 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
4 use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
5 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable};
7 use std::ops::ControlFlow;
9 ///////////////////////////////////////////////////////////////////////////
10 // OPPORTUNISTIC VAR RESOLVER
12 /// The opportunistic resolver can be used at any time. It simply replaces
13 /// type/const variables that have been unified with the things they have
14 /// been unified with (similar to `shallow_resolve`, but deep). This is
15 /// useful for printing messages etc but also required at various
16 /// points for correctness.
17 pub struct OpportunisticVarResolver<'a, 'tcx> {
18 infcx: &'a InferCtxt<'tcx>,
21 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
23 pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
24 OpportunisticVarResolver { infcx }
28 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
29 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
33 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
34 if !t.has_non_region_infer() {
35 t // micro-optimize -- if there is nothing in this type that this fold affects...
37 let t = self.infcx.shallow_resolve(t);
38 t.super_fold_with(self)
42 fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> {
43 if !ct.has_non_region_infer() {
44 ct // micro-optimize -- if there is nothing in this const that this fold affects...
46 let ct = self.infcx.shallow_resolve(ct);
47 ct.super_fold_with(self)
52 /// The opportunistic region resolver opportunistically resolves regions
53 /// variables to the variable with the least variable id. It is used when
54 /// normalizing projections to avoid hitting the recursion limit by creating
55 /// many versions of a predicate for types that in the end have to unify.
57 /// If you want to resolve type and const variables as well, call
58 /// [InferCtxt::resolve_vars_if_possible] first.
59 pub struct OpportunisticRegionResolver<'a, 'tcx> {
60 infcx: &'a InferCtxt<'tcx>,
63 impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
64 pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
65 OpportunisticRegionResolver { infcx }
69 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
70 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
74 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
75 if !t.has_infer_regions() {
76 t // micro-optimize -- if there is nothing in this type that this fold affects...
78 t.super_fold_with(self)
82 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
89 .unwrap_region_constraints()
90 .opportunistic_resolve_var(rid);
91 TypeFolder::tcx(self).reuse_or_mk_region(r, ty::ReVar(resolved))
97 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
98 if !ct.has_infer_regions() {
99 ct // micro-optimize -- if there is nothing in this const that this fold affects...
101 ct.super_fold_with(self)
106 ///////////////////////////////////////////////////////////////////////////
107 // UNRESOLVED TYPE FINDER
109 /// The unresolved type **finder** walks a type searching for
110 /// type variables that don't yet have a value. The first unresolved type is stored.
111 /// It does not construct the fully resolved type (which might
112 /// involve some hashing and so forth).
113 pub struct UnresolvedTypeFinder<'a, 'tcx> {
114 infcx: &'a InferCtxt<'tcx>,
117 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
118 pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
119 UnresolvedTypeFinder { infcx }
123 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
124 type BreakTy = (Ty<'tcx>, Option<Span>);
125 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
126 let t = self.infcx.shallow_resolve(t);
127 if t.has_infer_types() {
128 if let ty::Infer(infer_ty) = *t.kind() {
129 // Since we called `shallow_resolve` above, this must
130 // be an (as yet...) unresolved inference variable.
131 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
132 let mut inner = self.infcx.inner.borrow_mut();
133 let ty_vars = &inner.type_variables();
134 if let TypeVariableOrigin {
135 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
137 } = *ty_vars.var_origin(ty_vid)
146 ControlFlow::Break((t, ty_var_span))
148 // Otherwise, visit its contents.
149 t.super_visit_with(self)
152 // All type variables in inference types must already be resolved,
153 // - no need to visit the contents, continue visiting.
154 ControlFlow::CONTINUE
159 ///////////////////////////////////////////////////////////////////////////
160 // FULL TYPE RESOLUTION
162 /// Full type resolution replaces all type and region variables with
163 /// their concrete results. If any variable cannot be replaced (never unified, etc)
164 /// then an `Err` result is returned.
165 pub fn fully_resolve<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> FixupResult<'tcx, T>
167 T: TypeFoldable<'tcx>,
169 value.try_fold_with(&mut FullTypeResolver { infcx })
172 // N.B. This type is not public because the protocol around checking the
173 // `err` field is not enforceable otherwise.
174 struct FullTypeResolver<'a, 'tcx> {
175 infcx: &'a InferCtxt<'tcx>,
178 impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
179 type Error = FixupError<'tcx>;
181 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
185 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
186 if !t.needs_infer() {
187 Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...
189 let t = self.infcx.shallow_resolve(t);
191 ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
192 ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
193 ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
195 bug!("Unexpected type in full type resolver: {:?}", t);
197 _ => t.try_super_fold_with(self),
202 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
204 ty::ReVar(_) => Ok(self
206 .lexical_region_resolutions
209 .expect("region resolution not performed")
210 .resolve_region(self.infcx.tcx, r)),
215 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
216 if !c.needs_infer() {
217 Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
219 let c = self.infcx.shallow_resolve(c);
221 ty::ConstKind::Infer(InferConst::Var(vid)) => {
222 return Err(FixupError::UnresolvedConst(vid));
224 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
225 bug!("Unexpected const in full const resolver: {:?}", c);
229 c.try_super_fold_with(self)