]> git.proxmox.com Git - rustc.git/blame - src/librustc/infer/resolve.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc / infer / resolve.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
c1a9b12d 11use super::{InferCtxt, FixupError, FixupResult};
54a0048b 12use ty::{self, Ty, TyCtxt, TypeFoldable};
3b2f2976 13use ty::fold::{TypeFolder, TypeVisitor};
1a4d82fc
JJ
14
15///////////////////////////////////////////////////////////////////////////
16// OPPORTUNISTIC TYPE RESOLVER
17
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.
a7813a04
XL
23pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
24 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
1a4d82fc
JJ
25}
26
a7813a04
XL
27impl<'a, 'gcx, 'tcx> OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
28 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
1a4d82fc
JJ
29 OpportunisticTypeResolver { infcx: infcx }
30 }
31}
32
a7813a04
XL
33impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
34 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
1a4d82fc
JJ
35 self.infcx.tcx
36 }
37
38 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
c1a9b12d 39 if !t.has_infer_types() {
1a4d82fc
JJ
40 t // micro-optimize -- if there is nothing in this type that this fold affects...
41 } else {
42 let t0 = self.infcx.shallow_resolve(t);
9cc50fc6
SL
43 t0.super_fold_with(self)
44 }
45 }
46}
47
48/// The opportunistic type and region resolver is similar to the
3b2f2976 49/// opportunistic type resolver, but also opportunistically resolves
9cc50fc6 50/// regions. It is useful for canonicalization.
a7813a04
XL
51pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
52 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
9cc50fc6
SL
53}
54
a7813a04
XL
55impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
56 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
9cc50fc6
SL
57 OpportunisticTypeAndRegionResolver { infcx: infcx }
58 }
59}
60
a7813a04
XL
61impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
62 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
9cc50fc6
SL
63 self.infcx.tcx
64 }
65
66 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
67 if !t.needs_infer() {
68 t // micro-optimize -- if there is nothing in this type that this fold affects...
69 } else {
70 let t0 = self.infcx.shallow_resolve(t);
71 t0.super_fold_with(self)
72 }
73 }
74
7cac9316 75 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
9e0c209e 76 match *r {
abe05a73
XL
77 ty::ReVar(rid) =>
78 self.infcx.borrow_region_constraints()
79 .opportunistic_resolve_var(self.tcx(), rid),
80 _ =>
81 r,
1a4d82fc
JJ
82 }
83 }
84}
85
3b2f2976
XL
86///////////////////////////////////////////////////////////////////////////
87// UNRESOLVED TYPE FINDER
88
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).
93pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
94 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
95}
96
97impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
98 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
99 UnresolvedTypeFinder { infcx }
100 }
101}
102
103impl<'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() {
b7449926 107 if let ty::Infer(_) = t.sty {
3b2f2976
XL
108 // Since we called `shallow_resolve` above, this must
109 // be an (as yet...) unresolved inference variable.
110 true
111 } else {
112 // Otherwise, visit its contents.
113 t.super_visit_with(self)
114 }
115 } else {
116 // Micro-optimize: no inference types at all Can't have unresolved type
117 // variables, no need to visit the contents.
118 false
119 }
120 }
121}
122
1a4d82fc
JJ
123///////////////////////////////////////////////////////////////////////////
124// FULL TYPE RESOLUTION
125
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.
a7813a04
XL
129pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
130 value: &T) -> FixupResult<T>
1a4d82fc
JJ
131 where T : TypeFoldable<'tcx>
132{
133 let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
134 let result = value.fold_with(&mut full_resolver);
135 match full_resolver.err {
136 None => Ok(result),
137 Some(e) => Err(e),
138 }
139}
140
141// N.B. This type is not public because the protocol around checking the
142// `err` field is not enforcable otherwise.
a7813a04
XL
143struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
144 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
c1a9b12d 145 err: Option<FixupError>,
1a4d82fc
JJ
146}
147
a7813a04
XL
148impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
149 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
1a4d82fc
JJ
150 self.infcx.tcx
151 }
152
153 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
3b2f2976 154 if !t.needs_infer() && !ty::keep_local(&t) {
1a4d82fc 155 t // micro-optimize -- if there is nothing in this type that this fold affects...
0bf4aa26
XL
156 // ^ we need to have the `keep_local` check to un-default
157 // defaulted tuples.
1a4d82fc
JJ
158 } else {
159 let t = self.infcx.shallow_resolve(t);
160 match t.sty {
b7449926 161 ty::Infer(ty::TyVar(vid)) => {
c1a9b12d 162 self.err = Some(FixupError::UnresolvedTy(vid));
1a4d82fc
JJ
163 self.tcx().types.err
164 }
b7449926 165 ty::Infer(ty::IntVar(vid)) => {
c1a9b12d 166 self.err = Some(FixupError::UnresolvedIntTy(vid));
1a4d82fc
JJ
167 self.tcx().types.err
168 }
b7449926 169 ty::Infer(ty::FloatVar(vid)) => {
c1a9b12d 170 self.err = Some(FixupError::UnresolvedFloatTy(vid));
1a4d82fc
JJ
171 self.tcx().types.err
172 }
b7449926 173 ty::Infer(_) => {
54a0048b 174 bug!("Unexpected type in full type resolver: {:?}", t);
1a4d82fc
JJ
175 }
176 _ => {
9cc50fc6 177 t.super_fold_with(self)
1a4d82fc
JJ
178 }
179 }
180 }
181 }
182
7cac9316 183 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
9e0c209e 184 match *r {
abe05a73
XL
185 ty::ReVar(rid) => self.infcx.lexical_region_resolutions
186 .borrow()
187 .as_ref()
188 .expect("region resolution not performed")
189 .resolve_var(rid),
9e0c209e 190 _ => r,
1a4d82fc
JJ
191 }
192 }
193}