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 // Resolution is the process of removing type variables and replacing
12 // them with their inferred values. Unfortunately our inference has
13 // become fairly complex and so there are a number of options to
14 // control *just how much* you want to resolve and how you want to do
17 // # Controlling the scope of resolution
19 // The options resolve_* determine what kinds of variables get
20 // resolved. Generally resolution starts with a top-level type
21 // variable; we will always resolve this. However, once we have
22 // resolved that variable, we may end up with a type that still
23 // contains type variables. For example, if we resolve `<T0>` we may
24 // end up with something like `[<T1>]`. If the option
25 // `resolve_nested_tvar` is passed, we will then go and recursively
28 // The options `resolve_rvar` controls whether we resolve region
29 // variables. The options `resolve_fvar` and `resolve_ivar` control
30 // whether we resolve floating point and integral variables,
33 // # What do if things are unconstrained
35 // Sometimes we will encounter a variable that has no constraints, and
36 // therefore cannot sensibly be mapped to any particular result. By
37 // default, we will leave such variables as is (so you will get back a
38 // variable in your result). The options force_* will cause the
39 // resolution to fail in this case instead, except for the case of
40 // integral variables, which resolve to `int` if forced.
42 // # resolve_all and force_all
44 // The options are a bit set, so you can use the *_all to resolve or
45 // force all kinds of variables (including those we may add in the
46 // future). If you want to resolve everything but one type, you are
47 // probably better off writing `resolve_all - resolve_ivar`.
50 use middle::ty::{FloatVar, FloatVid, IntVar, IntVid, RegionVid, TyVar, TyVid};
51 use middle::ty::{IntType, UintType};
54 use middle::typeck::infer::{fixup_err, fres, InferCtxt};
55 use middle::typeck::infer::{unresolved_int_ty,unresolved_float_ty,unresolved_ty};
56 use syntax::codemap::Span;
57 use util::common::indent;
58 use util::ppaux::{Repr, ty_to_string};
60 pub static resolve_nested_tvar: uint = 0b0000000001;
61 pub static resolve_rvar: uint = 0b0000000010;
62 pub static resolve_ivar: uint = 0b0000000100;
63 pub static resolve_fvar: uint = 0b0000001000;
64 pub static resolve_all: uint = 0b0000001111;
65 pub static force_tvar: uint = 0b0000100000;
66 pub static force_rvar: uint = 0b0001000000;
67 pub static force_ivar: uint = 0b0010000000;
68 pub static force_fvar: uint = 0b0100000000;
69 pub static force_all: uint = 0b0111100000;
71 pub static not_regions: uint = !(force_rvar | resolve_rvar);
73 pub static try_resolve_tvar_shallow: uint = 0;
74 pub static resolve_and_force_all_but_regions: uint =
75 (resolve_all | force_all) & not_regions;
77 pub struct ResolveState<'a, 'tcx: 'a> {
78 infcx: &'a InferCtxt<'a, 'tcx>,
80 err: Option<fixup_err>,
84 pub fn resolver<'a, 'tcx>(infcx: &'a InferCtxt<'a, 'tcx>,
87 -> ResolveState<'a, 'tcx> {
96 impl<'a, 'tcx> ty_fold::TypeFolder<'tcx> for ResolveState<'a, 'tcx> {
97 fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
101 fn fold_ty(&mut self, t: ty::t) -> ty::t {
105 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
106 self.resolve_region(r)
110 impl<'a, 'tcx> ResolveState<'a, 'tcx> {
111 pub fn should(&mut self, mode: uint) -> bool {
112 (self.modes & mode) == mode
115 pub fn resolve_type_chk(&mut self,
120 debug!("Resolving {} (modes={:x})",
121 ty_to_string(self.infcx.tcx, typ),
124 // n.b. This is a hokey mess because the current fold doesn't
125 // allow us to pass back errors in any useful way.
127 let rty = self.resolve_type(typ);
130 debug!("Resolved {} to {} (modes={:x})",
131 ty_to_string(self.infcx.tcx, typ),
132 ty_to_string(self.infcx.tcx, rty),
136 Some(e) => return Err(e)
140 pub fn resolve_region_chk(&mut self,
142 -> fres<ty::Region> {
144 let resolved = indent(|| self.resolve_region(orig) );
146 None => Ok(resolved),
151 pub fn resolve_type(&mut self, typ: ty::t) -> ty::t {
152 debug!("resolve_type({})", typ.repr(self.infcx.tcx));
154 if !ty::type_needs_infer(typ) {
158 if self.type_depth > 0 && !self.should(resolve_nested_tvar) {
162 match ty::get(typ).sty {
163 ty::ty_infer(TyVar(vid)) => {
164 self.resolve_ty_var(vid)
166 ty::ty_infer(IntVar(vid)) => {
167 self.resolve_int_var(vid)
169 ty::ty_infer(FloatVar(vid)) => {
170 self.resolve_float_var(vid)
173 if self.modes & resolve_all == 0 {
174 // if we are only resolving top-level type
175 // variables, and this is not a top-level type
176 // variable, then shortcircuit for efficiency
179 self.type_depth += 1;
180 let result = ty_fold::super_fold_ty(self, typ);
181 self.type_depth -= 1;
188 pub fn resolve_region(&mut self, orig: ty::Region) -> ty::Region {
189 debug!("Resolve_region({})", orig.repr(self.infcx.tcx));
191 ty::ReInfer(ty::ReVar(rid)) => self.resolve_region_var(rid),
196 pub fn resolve_region_var(&mut self, rid: RegionVid) -> ty::Region {
197 if !self.should(resolve_rvar) {
198 return ty::ReInfer(ty::ReVar(rid));
200 self.infcx.region_vars.resolve_var(rid)
203 pub fn resolve_ty_var(&mut self, vid: TyVid) -> ty::t {
204 let tcx = self.infcx.tcx;
205 let t1 = match self.infcx.type_variables.borrow().probe(vid) {
210 if self.should(force_tvar) {
211 self.err = Some(unresolved_ty(vid));
219 pub fn resolve_int_var(&mut self, vid: IntVid) -> ty::t {
220 if !self.should(resolve_ivar) {
221 return ty::mk_int_var(self.infcx.tcx, vid);
224 let tcx = self.infcx.tcx;
225 let table = &self.infcx.int_unification_table;
226 let node = table.borrow_mut().get(tcx, vid);
228 Some(IntType(t)) => ty::mk_mach_int(t),
229 Some(UintType(t)) => ty::mk_mach_uint(t),
231 if self.should(force_ivar) {
232 // As a last resort, emit an error.
233 self.err = Some(unresolved_int_ty(vid));
235 ty::mk_int_var(self.infcx.tcx, vid)
240 pub fn resolve_float_var(&mut self, vid: FloatVid) -> ty::t {
241 if !self.should(resolve_fvar) {
242 return ty::mk_float_var(self.infcx.tcx, vid);
245 let tcx = self.infcx.tcx;
246 let table = &self.infcx.float_unification_table;
247 let node = table.borrow_mut().get(tcx, vid);
249 Some(t) => ty::mk_mach_float(t),
251 if self.should(force_fvar) {
252 // As a last resort, emit an error.
253 self.err = Some(unresolved_float_ty(vid));
255 ty::mk_float_var(self.infcx.tcx, vid)