1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
3 use rustc_middle::ty::fold::{TypeFolder, TypeVisitor};
4 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
6 use std::ops::ControlFlow;
8 ///////////////////////////////////////////////////////////////////////////
9 // OPPORTUNISTIC VAR RESOLVER
11 /// The opportunistic resolver can be used at any time. It simply replaces
12 /// type/const variables that have been unified with the things they have
13 /// been unified with (similar to `shallow_resolve`, but deep). This is
14 /// useful for printing messages etc but also required at various
15 /// points for correctness.
16 pub struct OpportunisticVarResolver<'a, 'tcx> {
17 infcx: &'a InferCtxt<'a, 'tcx>,
20 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
22 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
23 OpportunisticVarResolver { infcx }
27 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
28 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
32 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
33 if !t.has_infer_types_or_consts() {
34 t // micro-optimize -- if there is nothing in this type that this fold affects...
36 let t = self.infcx.shallow_resolve(t);
37 t.super_fold_with(self)
41 fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
42 if !ct.has_infer_types_or_consts() {
43 ct // micro-optimize -- if there is nothing in this const that this fold affects...
45 let ct = self.infcx.shallow_resolve(ct);
46 ct.super_fold_with(self)
51 /// The opportunistic region resolver opportunistically resolves regions
52 /// variables to the variable with the least variable id. It is used when
53 /// normlizing projections to avoid hitting the recursion limit by creating
54 /// many versions of a predicate for types that in the end have to unify.
56 /// If you want to resolve type and const variables as well, call
57 /// [InferCtxt::resolve_vars_if_possible] first.
58 pub struct OpportunisticRegionResolver<'a, 'tcx> {
59 infcx: &'a InferCtxt<'a, 'tcx>,
62 impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
63 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
64 OpportunisticRegionResolver { infcx }
68 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
69 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
73 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
74 if !t.has_infer_regions() {
75 t // micro-optimize -- if there is nothing in this type that this fold affects...
77 t.super_fold_with(self)
81 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
88 .unwrap_region_constraints()
89 .opportunistic_resolve_var(rid);
90 self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
96 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
97 if !ct.has_infer_regions() {
98 ct // micro-optimize -- if there is nothing in this const that this fold affects...
100 ct.super_fold_with(self)
105 ///////////////////////////////////////////////////////////////////////////
106 // UNRESOLVED TYPE FINDER
108 /// The unresolved type **finder** walks a type searching for
109 /// type variables that don't yet have a value. The first unresolved type is stored.
110 /// It does not construct the fully resolved type (which might
111 /// involve some hashing and so forth).
112 pub struct UnresolvedTypeFinder<'a, 'tcx> {
113 infcx: &'a InferCtxt<'a, 'tcx>,
116 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
117 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
118 UnresolvedTypeFinder { infcx }
122 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
123 type BreakTy = (Ty<'tcx>, Option<Span>);
124 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
125 let t = self.infcx.shallow_resolve(t);
126 if t.has_infer_types() {
127 if let ty::Infer(infer_ty) = *t.kind() {
128 // Since we called `shallow_resolve` above, this must
129 // be an (as yet...) unresolved inference variable.
130 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
131 let mut inner = self.infcx.inner.borrow_mut();
132 let ty_vars = &inner.type_variables();
133 if let TypeVariableOrigin {
134 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
136 } = *ty_vars.var_origin(ty_vid)
145 ControlFlow::Break((t, ty_var_span))
147 // Otherwise, visit its contents.
148 t.super_visit_with(self)
151 // All type variables in inference types must already be resolved,
152 // - no need to visit the contents, continue visiting.
153 ControlFlow::CONTINUE
158 ///////////////////////////////////////////////////////////////////////////
159 // FULL TYPE RESOLUTION
161 /// Full type resolution replaces all type and region variables with
162 /// their concrete results. If any variable cannot be replaced (never unified, etc)
163 /// then an `Err` result is returned.
164 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> FixupResult<'tcx, T>
166 T: TypeFoldable<'tcx>,
168 let mut full_resolver = FullTypeResolver { infcx, err: None };
169 let result = value.fold_with(&mut full_resolver);
170 match full_resolver.err {
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>,
180 err: Option<FixupError<'tcx>>,
183 impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
184 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
188 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
189 if !t.needs_infer() {
190 t // micro-optimize -- if there is nothing in this type that this fold affects...
192 let t = self.infcx.shallow_resolve(t);
194 ty::Infer(ty::TyVar(vid)) => {
195 self.err = Some(FixupError::UnresolvedTy(vid));
196 self.tcx().ty_error()
198 ty::Infer(ty::IntVar(vid)) => {
199 self.err = Some(FixupError::UnresolvedIntTy(vid));
200 self.tcx().ty_error()
202 ty::Infer(ty::FloatVar(vid)) => {
203 self.err = Some(FixupError::UnresolvedFloatTy(vid));
204 self.tcx().ty_error()
207 bug!("Unexpected type in full type resolver: {:?}", t);
209 _ => t.super_fold_with(self),
214 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
216 ty::ReVar(rid) => self
218 .lexical_region_resolutions
221 .expect("region resolution not performed")
227 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
228 if !c.needs_infer() {
229 c // micro-optimize -- if there is nothing in this const that this fold affects...
231 let c = self.infcx.shallow_resolve(c);
233 ty::ConstKind::Infer(InferConst::Var(vid)) => {
234 self.err = Some(FixupError::UnresolvedConst(vid));
235 return self.tcx().const_error(c.ty);
237 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
238 bug!("Unexpected const in full const resolver: {:?}", c);
242 c.super_fold_with(self)