1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use super::{InferCtxt, FixupError, FixupResult};
12 use ty::{self, Ty, TyCtxt, TypeFoldable};
13 use ty::fold::{TypeFolder, TypeVisitor};
15 ///////////////////////////////////////////////////////////////////////////
16 // OPPORTUNISTIC TYPE RESOLVER
18 /// The opportunistic type resolver can be used at any time. It simply replaces
19 /// type variables that have been unified with the things they have
20 /// been unified with (similar to `shallow_resolve`, but deep). This is
21 /// useful for printing messages etc but also required at various
22 /// points for correctness.
23 pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
24 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
27 impl<'a, 'gcx, 'tcx> OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
28 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
29 OpportunisticTypeResolver { infcx: infcx }
33 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
34 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
38 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
39 if !t.has_infer_types() {
40 t // micro-optimize -- if there is nothing in this type that this fold affects...
42 let t0 = self.infcx.shallow_resolve(t);
43 t0.super_fold_with(self)
48 /// The opportunistic type and region resolver is similar to the
49 /// opportunistic type resolver, but also opportunistically resolves
50 /// regions. It is useful for canonicalization.
51 pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
52 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
55 impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
56 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
57 OpportunisticTypeAndRegionResolver { infcx: infcx }
61 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
62 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
66 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
68 t // micro-optimize -- if there is nothing in this type that this fold affects...
70 let t0 = self.infcx.shallow_resolve(t);
71 t0.super_fold_with(self)
75 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
78 self.infcx.borrow_region_constraints()
79 .opportunistic_resolve_var(self.tcx(), rid),
86 ///////////////////////////////////////////////////////////////////////////
87 // UNRESOLVED TYPE FINDER
89 /// The unresolved type **finder** walks your type and searches for
90 /// type variables that don't yet have a value. They get pushed into a
91 /// vector. It does not construct the fully resolved type (which might
92 /// involve some hashing and so forth).
93 pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
94 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
97 impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
98 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
99 UnresolvedTypeFinder { infcx }
103 impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
104 fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
105 let t = self.infcx.shallow_resolve(t);
106 if t.has_infer_types() {
107 if let ty::Infer(_) = t.sty {
108 // Since we called `shallow_resolve` above, this must
109 // be an (as yet...) unresolved inference variable.
112 // Otherwise, visit its contents.
113 t.super_visit_with(self)
116 // Micro-optimize: no inference types at all Can't have unresolved type
117 // variables, no need to visit the contents.
123 ///////////////////////////////////////////////////////////////////////////
124 // FULL TYPE RESOLUTION
126 /// Full type resolution replaces all type and region variables with
127 /// their concrete results. If any variable cannot be replaced (never unified, etc)
128 /// then an `Err` result is returned.
129 pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
130 value: &T) -> FixupResult<T>
131 where T : TypeFoldable<'tcx>
133 let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
134 let result = value.fold_with(&mut full_resolver);
135 match full_resolver.err {
141 // N.B. This type is not public because the protocol around checking the
142 // `err` field is not enforcable otherwise.
143 struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
144 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
145 err: Option<FixupError>,
148 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
149 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
153 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
154 if !t.needs_infer() && !ty::keep_local(&t) {
155 t // micro-optimize -- if there is nothing in this type that this fold affects...
156 // ^ we need to have the `keep_local` check to un-default
159 let t = self.infcx.shallow_resolve(t);
161 ty::Infer(ty::TyVar(vid)) => {
162 self.err = Some(FixupError::UnresolvedTy(vid));
165 ty::Infer(ty::IntVar(vid)) => {
166 self.err = Some(FixupError::UnresolvedIntTy(vid));
169 ty::Infer(ty::FloatVar(vid)) => {
170 self.err = Some(FixupError::UnresolvedFloatTy(vid));
174 bug!("Unexpected type in full type resolver: {:?}", t);
177 t.super_fold_with(self)
183 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
185 ty::ReVar(rid) => self.infcx.lexical_region_resolutions
188 .expect("region resolution not performed")