1 use super::{InferCtxt, FixupError, FixupResult, Span, type_variable::TypeVariableOrigin};
2 use crate::mir::interpret::ConstValue;
3 use crate::ty::{self, Ty, TyCtxt, TypeFoldable, InferConst};
4 use crate::ty::fold::{TypeFolder, TypeVisitor};
6 ///////////////////////////////////////////////////////////////////////////
7 // OPPORTUNISTIC TYPE RESOLVER
9 /// The opportunistic type resolver can be used at any time. It simply replaces
10 /// type variables that have been unified with the things they have
11 /// been unified with (similar to `shallow_resolve_type`, but deep). This is
12 /// useful for printing messages etc but also required at various
13 /// points for correctness.
14 pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
15 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
18 impl<'a, 'gcx, 'tcx> OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
20 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
21 OpportunisticTypeResolver { infcx }
25 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
26 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
30 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
31 if !t.has_infer_types() {
32 t // micro-optimize -- if there is nothing in this type that this fold affects...
34 let t0 = self.infcx.shallow_resolve_type(t);
35 t0.super_fold_with(self)
40 /// The opportunistic type and region resolver is similar to the
41 /// opportunistic type resolver, but also opportunistically resolves
42 /// regions. It is useful for canonicalization.
43 pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
44 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
47 impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
48 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
49 OpportunisticTypeAndRegionResolver { infcx }
53 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
54 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
58 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
60 t // micro-optimize -- if there is nothing in this type that this fold affects...
62 let t0 = self.infcx.shallow_resolve_type(t);
63 t0.super_fold_with(self)
67 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
70 self.infcx.borrow_region_constraints()
71 .opportunistic_resolve_var(self.tcx(), rid),
77 fn fold_const(&mut self, ct: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
78 if !ct.needs_infer() {
79 ct // micro-optimize -- if there is nothing in this const that this fold affects...
81 let c0 = self.infcx.shallow_resolve_const(ct);
82 c0.super_fold_with(self)
87 ///////////////////////////////////////////////////////////////////////////
88 // UNRESOLVED TYPE FINDER
90 /// The unresolved type **finder** walks a type searching for
91 /// type variables that don't yet have a value. The first unresolved type is stored.
92 /// It does not construct the fully resolved type (which might
93 /// involve some hashing and so forth).
94 pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
95 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
97 /// Used to find the type parameter name and location for error reporting.
98 pub first_unresolved: Option<(Ty<'tcx>,Option<Span>)>,
101 impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
102 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
103 UnresolvedTypeFinder { infcx, first_unresolved: None }
107 impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
108 fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
109 let t = self.infcx.shallow_resolve_type(t);
110 if t.has_infer_types() {
111 if let ty::Infer(infer_ty) = t.sty {
112 // Since we called `shallow_resolve` above, this must
113 // be an (as yet...) unresolved inference variable.
115 if let ty::TyVar(ty_vid) = infer_ty {
116 let ty_vars = self.infcx.type_variables.borrow();
117 if let TypeVariableOrigin::TypeParameterDefinition(span, _name)
118 = *ty_vars.var_origin(ty_vid)
127 self.first_unresolved = Some((t, ty_var_span));
128 true // Halt visiting.
130 // Otherwise, visit its contents.
131 t.super_visit_with(self)
134 // All type variables in inference types must already be resolved,
135 // - no need to visit the contents, continue visiting.
142 ///////////////////////////////////////////////////////////////////////////
143 // FULL TYPE RESOLUTION
145 /// Full type resolution replaces all type and region variables with
146 /// their concrete results. If any variable cannot be replaced (never unified, etc)
147 /// then an `Err` result is returned.
148 pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
149 value: &T) -> FixupResult<'tcx, T>
150 where T : TypeFoldable<'tcx>
152 let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
153 let result = value.fold_with(&mut full_resolver);
154 match full_resolver.err {
160 // N.B. This type is not public because the protocol around checking the
161 // `err` field is not enforcable otherwise.
162 struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
163 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
164 err: Option<FixupError<'tcx>>,
167 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
168 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
172 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
173 if !t.needs_infer() && !ty::keep_local(&t) {
174 t // micro-optimize -- if there is nothing in this type that this fold affects...
175 // ^ we need to have the `keep_local` check to un-default
178 let t = self.infcx.shallow_resolve_type(t);
180 ty::Infer(ty::TyVar(vid)) => {
181 self.err = Some(FixupError::UnresolvedTy(vid));
184 ty::Infer(ty::IntVar(vid)) => {
185 self.err = Some(FixupError::UnresolvedIntTy(vid));
188 ty::Infer(ty::FloatVar(vid)) => {
189 self.err = Some(FixupError::UnresolvedFloatTy(vid));
193 bug!("Unexpected type in full type resolver: {:?}", t);
196 t.super_fold_with(self)
202 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
204 ty::ReVar(rid) => self.infcx.lexical_region_resolutions
207 .expect("region resolution not performed")
213 fn fold_const(&mut self, c: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
214 if !c.needs_infer() && !ty::keep_local(&c) {
215 c // micro-optimize -- if there is nothing in this const that this fold affects...
216 // ^ we need to have the `keep_local` check to un-default
219 let c = self.infcx.shallow_resolve_const(c);
221 ty::LazyConst::Evaluated(ty::Const { val, .. }) => {
223 ConstValue::Infer(InferConst::Var(vid)) => {
224 self.err = Some(FixupError::UnresolvedConst(*vid));
225 return self.tcx().types.ct_err;
227 ConstValue::Infer(InferConst::Fresh(_)) => {
228 bug!("Unexpected const in full const resolver: {:?}", c);
235 c.super_fold_with(self)