1 use super::{InferCtxt, FixupError, FixupResult, Span};
2 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
3 use crate::mir::interpret::ConstValue;
4 use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst, TypeFlags};
5 use crate::ty::fold::{TypeFolder, TypeVisitor};
7 ///////////////////////////////////////////////////////////////////////////
8 // OPPORTUNISTIC VAR RESOLVER
10 /// The opportunistic resolver can be used at any time. It simply replaces
11 /// type/const variables that have been unified with the things they have
12 /// been unified with (similar to `shallow_resolve`, but deep). This is
13 /// useful for printing messages etc but also required at various
14 /// points for correctness.
15 pub struct OpportunisticVarResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
16 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
19 impl<'a, 'gcx, 'tcx> OpportunisticVarResolver<'a, 'gcx, 'tcx> {
21 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
22 OpportunisticVarResolver { infcx }
26 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticVarResolver<'a, 'gcx, 'tcx> {
27 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
31 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
32 if !t.has_infer_types() {
33 t // micro-optimize -- if there is nothing in this type that this fold affects...
35 let t = self.infcx.shallow_resolve(t);
36 t.super_fold_with(self)
40 fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
41 if !ct.has_type_flags(TypeFlags::HAS_CT_INFER) {
42 ct // micro-optimize -- if there is nothing in this const that this fold affects...
44 let ct = self.infcx.shallow_resolve(ct);
45 ct.super_fold_with(self)
50 /// The opportunistic type and region resolver is similar to the
51 /// opportunistic type resolver, but also opportunistically resolves
52 /// regions. It is useful for canonicalization.
53 pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
54 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
57 impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
58 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
59 OpportunisticTypeAndRegionResolver { infcx }
63 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
64 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
68 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
70 t // micro-optimize -- if there is nothing in this type that this fold affects...
72 let t0 = self.infcx.shallow_resolve(t);
73 t0.super_fold_with(self)
77 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
80 self.infcx.borrow_region_constraints()
81 .opportunistic_resolve_var(self.tcx(), rid),
87 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
88 if !ct.needs_infer() {
89 ct // micro-optimize -- if there is nothing in this const that this fold affects...
91 let c0 = self.infcx.shallow_resolve(ct);
92 c0.super_fold_with(self)
97 ///////////////////////////////////////////////////////////////////////////
98 // UNRESOLVED TYPE FINDER
100 /// The unresolved type **finder** walks a type searching for
101 /// type variables that don't yet have a value. The first unresolved type is stored.
102 /// It does not construct the fully resolved type (which might
103 /// involve some hashing and so forth).
104 pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
105 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
107 /// Used to find the type parameter name and location for error reporting.
108 pub first_unresolved: Option<(Ty<'tcx>,Option<Span>)>,
111 impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
112 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
113 UnresolvedTypeFinder { infcx, first_unresolved: None }
117 impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
118 fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
119 let t = self.infcx.shallow_resolve(t);
120 if t.has_infer_types() {
121 if let ty::Infer(infer_ty) = t.sty {
122 // Since we called `shallow_resolve` above, this must
123 // be an (as yet...) unresolved inference variable.
125 if let ty::TyVar(ty_vid) = infer_ty {
126 let ty_vars = self.infcx.type_variables.borrow();
127 if let TypeVariableOrigin {
128 kind: TypeVariableOriginKind::TypeParameterDefinition(_),
130 } = *ty_vars.var_origin(ty_vid)
139 self.first_unresolved = Some((t, ty_var_span));
140 true // Halt visiting.
142 // Otherwise, visit its contents.
143 t.super_visit_with(self)
146 // All type variables in inference types must already be resolved,
147 // - no need to visit the contents, continue visiting.
154 ///////////////////////////////////////////////////////////////////////////
155 // FULL TYPE RESOLUTION
157 /// Full type resolution replaces all type and region variables with
158 /// their concrete results. If any variable cannot be replaced (never unified, etc)
159 /// then an `Err` result is returned.
160 pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
161 value: &T) -> FixupResult<'tcx, T>
162 where T : TypeFoldable<'tcx>
164 let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
165 let result = value.fold_with(&mut full_resolver);
166 match full_resolver.err {
172 // N.B. This type is not public because the protocol around checking the
173 // `err` field is not enforcable otherwise.
174 struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
175 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
176 err: Option<FixupError<'tcx>>,
179 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
180 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
184 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
185 if !t.needs_infer() && !ty::keep_local(&t) {
186 t // micro-optimize -- if there is nothing in this type that this fold affects...
187 // ^ we need to have the `keep_local` check to un-default
190 let t = self.infcx.shallow_resolve(t);
192 ty::Infer(ty::TyVar(vid)) => {
193 self.err = Some(FixupError::UnresolvedTy(vid));
196 ty::Infer(ty::IntVar(vid)) => {
197 self.err = Some(FixupError::UnresolvedIntTy(vid));
200 ty::Infer(ty::FloatVar(vid)) => {
201 self.err = Some(FixupError::UnresolvedFloatTy(vid));
205 bug!("Unexpected type in full type resolver: {:?}", t);
208 t.super_fold_with(self)
214 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
216 ty::ReVar(rid) => self.infcx.lexical_region_resolutions
219 .expect("region resolution not performed")
225 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
226 if !c.needs_infer() && !ty::keep_local(&c) {
227 c // micro-optimize -- if there is nothing in this const that this fold affects...
228 // ^ we need to have the `keep_local` check to un-default
231 let c = self.infcx.shallow_resolve(c);
233 ConstValue::Infer(InferConst::Var(vid)) => {
234 self.err = Some(FixupError::UnresolvedConst(vid));
235 return self.tcx().consts.err;
237 ConstValue::Infer(InferConst::Fresh(_)) => {
238 bug!("Unexpected const in full const resolver: {:?}", c);
242 c.super_fold_with(self)