1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
4 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable, TypeVisitor};
5 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
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<'a, 'tcx>,
21 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
23 pub fn new(infcx: &'a InferCtxt<'a, '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_infer_types_or_consts() {
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_infer_types_or_consts() {
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)
51 fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
52 constant.super_fold_with(self)
56 /// The opportunistic region resolver opportunistically resolves regions
57 /// variables to the variable with the least variable id. It is used when
58 /// normalizing projections to avoid hitting the recursion limit by creating
59 /// many versions of a predicate for types that in the end have to unify.
61 /// If you want to resolve type and const variables as well, call
62 /// [InferCtxt::resolve_vars_if_possible] first.
63 pub struct OpportunisticRegionResolver<'a, 'tcx> {
64 infcx: &'a InferCtxt<'a, 'tcx>,
67 impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
68 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
69 OpportunisticRegionResolver { infcx }
73 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
74 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
78 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
79 if !t.has_infer_regions() {
80 t // micro-optimize -- if there is nothing in this type that this fold affects...
82 t.super_fold_with(self)
86 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
93 .unwrap_region_constraints()
94 .opportunistic_resolve_var(rid);
95 self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
101 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
102 if !ct.has_infer_regions() {
103 ct // micro-optimize -- if there is nothing in this const that this fold affects...
105 ct.super_fold_with(self)
110 ///////////////////////////////////////////////////////////////////////////
111 // UNRESOLVED TYPE FINDER
113 /// The unresolved type **finder** walks a type searching for
114 /// type variables that don't yet have a value. The first unresolved type is stored.
115 /// It does not construct the fully resolved type (which might
116 /// involve some hashing and so forth).
117 pub struct UnresolvedTypeFinder<'a, 'tcx> {
118 infcx: &'a InferCtxt<'a, 'tcx>,
121 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
122 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
123 UnresolvedTypeFinder { infcx }
127 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
128 type BreakTy = (Ty<'tcx>, Option<Span>);
129 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
130 let t = self.infcx.shallow_resolve(t);
131 if t.has_infer_types() {
132 if let ty::Infer(infer_ty) = *t.kind() {
133 // Since we called `shallow_resolve` above, this must
134 // be an (as yet...) unresolved inference variable.
135 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
136 let mut inner = self.infcx.inner.borrow_mut();
137 let ty_vars = &inner.type_variables();
138 if let TypeVariableOrigin {
139 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
141 } = *ty_vars.var_origin(ty_vid)
150 ControlFlow::Break((t, ty_var_span))
152 // Otherwise, visit its contents.
153 t.super_visit_with(self)
156 // All type variables in inference types must already be resolved,
157 // - no need to visit the contents, continue visiting.
158 ControlFlow::CONTINUE
163 ///////////////////////////////////////////////////////////////////////////
164 // FULL TYPE RESOLUTION
166 /// Full type resolution replaces all type and region variables with
167 /// their concrete results. If any variable cannot be replaced (never unified, etc)
168 /// then an `Err` result is returned.
169 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> FixupResult<'tcx, T>
171 T: TypeFoldable<'tcx>,
173 value.try_fold_with(&mut FullTypeResolver { infcx })
176 // N.B. This type is not public because the protocol around checking the
177 // `err` field is not enforceable otherwise.
178 struct FullTypeResolver<'a, 'tcx> {
179 infcx: &'a InferCtxt<'a, 'tcx>,
182 impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
183 type Error = FixupError<'tcx>;
185 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
190 impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
191 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
192 if !t.needs_infer() {
193 Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...
195 let t = self.infcx.shallow_resolve(t);
197 ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
198 ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
199 ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
201 bug!("Unexpected type in full type resolver: {:?}", t);
203 _ => t.try_super_fold_with(self),
208 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
210 ty::ReVar(rid) => Ok(self
212 .lexical_region_resolutions
215 .expect("region resolution not performed")
221 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
222 if !c.needs_infer() {
223 Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
225 let c = self.infcx.shallow_resolve(c);
227 ty::ConstKind::Infer(InferConst::Var(vid)) => {
228 return Err(FixupError::UnresolvedConst(vid));
230 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
231 bug!("Unexpected const in full const resolver: {:?}", c);
235 c.try_super_fold_with(self)