1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
4 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
5 use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
6 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable};
8 use std::ops::ControlFlow;
10 ///////////////////////////////////////////////////////////////////////////
11 // OPPORTUNISTIC VAR RESOLVER
13 /// The opportunistic resolver can be used at any time. It simply replaces
14 /// type/const variables that have been unified with the things they have
15 /// been unified with (similar to `shallow_resolve`, but deep). This is
16 /// useful for printing messages etc but also required at various
17 /// points for correctness.
18 pub struct OpportunisticVarResolver<'a, 'tcx> {
19 infcx: &'a InferCtxt<'a, 'tcx>,
22 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
24 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
25 OpportunisticVarResolver { infcx }
29 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
30 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
34 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
35 if !t.has_infer_types_or_consts() {
36 t // micro-optimize -- if there is nothing in this type that this fold affects...
38 let t = self.infcx.shallow_resolve(t);
39 t.super_fold_with(self)
43 fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> {
44 if !ct.has_infer_types_or_consts() {
45 ct // micro-optimize -- if there is nothing in this const that this fold affects...
47 let ct = self.infcx.shallow_resolve(ct);
48 ct.super_fold_with(self)
52 fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
53 constant.super_fold_with(self)
57 /// The opportunistic region resolver opportunistically resolves regions
58 /// variables to the variable with the least variable id. It is used when
59 /// normalizing projections to avoid hitting the recursion limit by creating
60 /// many versions of a predicate for types that in the end have to unify.
62 /// If you want to resolve type and const variables as well, call
63 /// [InferCtxt::resolve_vars_if_possible] first.
64 pub struct OpportunisticRegionResolver<'a, 'tcx> {
65 infcx: &'a InferCtxt<'a, 'tcx>,
68 impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
69 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
70 OpportunisticRegionResolver { infcx }
74 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
75 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
79 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
80 if !t.has_infer_regions() {
81 t // micro-optimize -- if there is nothing in this type that this fold affects...
83 t.super_fold_with(self)
87 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
94 .unwrap_region_constraints()
95 .opportunistic_resolve_var(rid);
96 TypeFolder::tcx(self).reuse_or_mk_region(r, ty::ReVar(resolved))
102 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
103 if !ct.has_infer_regions() {
104 ct // micro-optimize -- if there is nothing in this const that this fold affects...
106 ct.super_fold_with(self)
111 ///////////////////////////////////////////////////////////////////////////
112 // UNRESOLVED TYPE FINDER
114 /// The unresolved type **finder** walks a type searching for
115 /// type variables that don't yet have a value. The first unresolved type is stored.
116 /// It does not construct the fully resolved type (which might
117 /// involve some hashing and so forth).
118 pub struct UnresolvedTypeFinder<'a, 'tcx> {
119 infcx: &'a InferCtxt<'a, 'tcx>,
122 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
123 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
124 UnresolvedTypeFinder { infcx }
128 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
129 type BreakTy = (Ty<'tcx>, Option<Span>);
130 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
131 let t = self.infcx.shallow_resolve(t);
132 if t.has_infer_types() {
133 if let ty::Infer(infer_ty) = *t.kind() {
134 // Since we called `shallow_resolve` above, this must
135 // be an (as yet...) unresolved inference variable.
136 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
137 let mut inner = self.infcx.inner.borrow_mut();
138 let ty_vars = &inner.type_variables();
139 if let TypeVariableOrigin {
140 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
142 } = *ty_vars.var_origin(ty_vid)
151 ControlFlow::Break((t, ty_var_span))
153 // Otherwise, visit its contents.
154 t.super_visit_with(self)
157 // All type variables in inference types must already be resolved,
158 // - no need to visit the contents, continue visiting.
159 ControlFlow::CONTINUE
164 ///////////////////////////////////////////////////////////////////////////
165 // FULL TYPE RESOLUTION
167 /// Full type resolution replaces all type and region variables with
168 /// their concrete results. If any variable cannot be replaced (never unified, etc)
169 /// then an `Err` result is returned.
170 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> FixupResult<'tcx, T>
172 T: TypeFoldable<'tcx>,
174 value.try_fold_with(&mut FullTypeResolver { infcx })
177 // N.B. This type is not public because the protocol around checking the
178 // `err` field is not enforceable otherwise.
179 struct FullTypeResolver<'a, 'tcx> {
180 infcx: &'a InferCtxt<'a, 'tcx>,
183 impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
184 type Error = FixupError<'tcx>;
186 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
190 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
191 if !t.needs_infer() {
192 Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...
194 let t = self.infcx.shallow_resolve(t);
196 ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
197 ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
198 ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
200 bug!("Unexpected type in full type resolver: {:?}", t);
202 _ => t.try_super_fold_with(self),
207 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
209 ty::ReVar(_) => Ok(self
211 .lexical_region_resolutions
214 .expect("region resolution not performed")
215 .resolve_region(self.infcx.tcx, r)),
220 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
221 if !c.needs_infer() {
222 Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
224 let c = self.infcx.shallow_resolve(c);
226 ty::ConstKind::Infer(InferConst::Var(vid)) => {
227 return Err(FixupError::UnresolvedConst(vid));
229 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
230 bug!("Unexpected const in full const resolver: {:?}", c);
234 c.try_super_fold_with(self)