]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/combine.rs
doc: remove incomplete sentence
[rust.git] / src / librustc / middle / infer / combine.rs
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
11 ///////////////////////////////////////////////////////////////////////////
12 // # Type combining
13 //
14 // There are four type combiners: equate, sub, lub, and glb.  Each
15 // implements the trait `Combine` and contains methods for combining
16 // two instances of various things and yielding a new instance.  These
17 // combiner methods always yield a `Result<T>`.  There is a lot of
18 // common code for these operations, implemented as default methods on
19 // the `Combine` trait.
20 //
21 // Each operation may have side-effects on the inference context,
22 // though these can be unrolled using snapshots. On success, the
23 // LUB/GLB operations return the appropriate bound. The Eq and Sub
24 // operations generally return the first operand.
25 //
26 // ## Contravariance
27 //
28 // When you are relating two things which have a contravariant
29 // relationship, you should use `contratys()` or `contraregions()`,
30 // rather than inversing the order of arguments!  This is necessary
31 // because the order of arguments is not relevant for LUB and GLB.  It
32 // is also useful to track which value is the "expected" value in
33 // terms of error reporting.
34
35 use super::equate::Equate;
36 use super::glb::Glb;
37 use super::lub::Lub;
38 use super::sub::Sub;
39 use super::unify::InferCtxtMethodsForSimplyUnifiableTypes;
40 use super::{InferCtxt, cres};
41 use super::{MiscVariable, TypeTrace};
42 use super::type_variable::{RelationDir, EqTo, SubtypeOf, SupertypeOf};
43
44 use middle::subst;
45 use middle::subst::{ErasedRegions, NonerasedRegions, Substs};
46 use middle::ty::{FloatVar, FnSig, IntVar, TyVar};
47 use middle::ty::{IntType, UintType};
48 use middle::ty::{BuiltinBounds};
49 use middle::ty::{mod, Ty};
50 use middle::ty_fold;
51 use middle::ty_fold::{TypeFoldable};
52 use util::ppaux::Repr;
53
54 use std::rc::Rc;
55 use syntax::ast::{Onceness, Unsafety};
56 use syntax::ast;
57 use syntax::abi;
58 use syntax::codemap::Span;
59
60 pub trait Combine<'tcx> : Sized {
61     fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx>;
62     fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> { self.infcx().tcx }
63     fn tag(&self) -> String;
64     fn a_is_expected(&self) -> bool;
65     fn trace(&self) -> TypeTrace<'tcx>;
66
67     fn equate<'a>(&'a self) -> Equate<'a, 'tcx>;
68     fn sub<'a>(&'a self) -> Sub<'a, 'tcx>;
69     fn lub<'a>(&'a self) -> Lub<'a, 'tcx>;
70     fn glb<'a>(&'a self) -> Glb<'a, 'tcx>;
71
72     fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>>;
73     fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>>;
74     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>>;
75
76     fn tps(&self,
77            _: subst::ParamSpace,
78            as_: &[Ty<'tcx>],
79            bs: &[Ty<'tcx>])
80            -> cres<'tcx, Vec<Ty<'tcx>>> {
81         // FIXME -- In general, we treat variance a bit wrong
82         // here. For historical reasons, we treat tps and Self
83         // as invariant. This is overly conservative.
84
85         if as_.len() != bs.len() {
86             return Err(ty::terr_ty_param_size(expected_found(self,
87                                                              as_.len(),
88                                                              bs.len())));
89         }
90
91         try!(as_.iter().zip(bs.iter())
92                 .map(|(a, b)| self.equate().tys(*a, *b))
93                 .collect::<cres<Vec<Ty>>>());
94         Ok(as_.to_vec())
95     }
96
97     fn substs(&self,
98               item_def_id: ast::DefId,
99               a_subst: &subst::Substs<'tcx>,
100               b_subst: &subst::Substs<'tcx>)
101               -> cres<'tcx, subst::Substs<'tcx>>
102     {
103         let variances = if self.infcx().tcx.variance_computed.get() {
104             Some(ty::item_variances(self.infcx().tcx, item_def_id))
105         } else {
106             None
107         };
108         self.substs_variances(variances.as_ref().map(|v| &**v), a_subst, b_subst)
109     }
110
111     fn substs_variances(&self,
112                         variances: Option<&ty::ItemVariances>,
113                         a_subst: &subst::Substs<'tcx>,
114                         b_subst: &subst::Substs<'tcx>)
115                         -> cres<'tcx, subst::Substs<'tcx>>
116     {
117         let mut substs = subst::Substs::empty();
118
119         for &space in subst::ParamSpace::all().iter() {
120             let a_tps = a_subst.types.get_slice(space);
121             let b_tps = b_subst.types.get_slice(space);
122             let tps = try!(self.tps(space, a_tps, b_tps));
123             substs.types.replace(space, tps);
124         }
125
126         match (&a_subst.regions, &b_subst.regions) {
127             (&ErasedRegions, _) | (_, &ErasedRegions) => {
128                 substs.regions = ErasedRegions;
129             }
130
131             (&NonerasedRegions(ref a), &NonerasedRegions(ref b)) => {
132                 for &space in subst::ParamSpace::all().iter() {
133                     let a_regions = a.get_slice(space);
134                     let b_regions = b.get_slice(space);
135
136                     let mut invariance = Vec::new();
137                     let r_variances = match variances {
138                         Some(variances) => {
139                             variances.regions.get_slice(space)
140                         }
141                         None => {
142                             for _ in a_regions.iter() {
143                                 invariance.push(ty::Invariant);
144                             }
145                             invariance[]
146                         }
147                     };
148
149                     let regions = try!(relate_region_params(self,
150                                                             r_variances,
151                                                             a_regions,
152                                                             b_regions));
153                     substs.mut_regions().replace(space, regions);
154                 }
155             }
156         }
157
158         return Ok(substs);
159
160         fn relate_region_params<'tcx, C: Combine<'tcx>>(this: &C,
161                                                         variances: &[ty::Variance],
162                                                         a_rs: &[ty::Region],
163                                                         b_rs: &[ty::Region])
164                                                         -> cres<'tcx, Vec<ty::Region>> {
165             let tcx = this.infcx().tcx;
166             let num_region_params = variances.len();
167
168             debug!("relate_region_params(\
169                    a_rs={}, \
170                    b_rs={},
171                    variances={})",
172                    a_rs.repr(tcx),
173                    b_rs.repr(tcx),
174                    variances.repr(tcx));
175
176             assert_eq!(num_region_params, a_rs.len());
177             assert_eq!(num_region_params, b_rs.len());
178             let mut rs = vec!();
179             for i in range(0, num_region_params) {
180                 let a_r = a_rs[i];
181                 let b_r = b_rs[i];
182                 let variance = variances[i];
183                 let r = match variance {
184                     ty::Invariant => this.equate().regions(a_r, b_r),
185                     ty::Covariant => this.regions(a_r, b_r),
186                     ty::Contravariant => this.contraregions(a_r, b_r),
187                     ty::Bivariant => Ok(a_r),
188                 };
189                 rs.push(try!(r));
190             }
191             Ok(rs)
192         }
193     }
194
195     fn bare_fn_tys(&self, a: &ty::BareFnTy<'tcx>,
196                    b: &ty::BareFnTy<'tcx>) -> cres<'tcx, ty::BareFnTy<'tcx>> {
197         let unsafety = try!(self.unsafeties(a.unsafety, b.unsafety));
198         let abi = try!(self.abi(a.abi, b.abi));
199         let sig = try!(self.binders(&a.sig, &b.sig));
200         Ok(ty::BareFnTy {unsafety: unsafety,
201                          abi: abi,
202                          sig: sig})
203     }
204
205     fn closure_tys(&self, a: &ty::ClosureTy<'tcx>,
206                    b: &ty::ClosureTy<'tcx>) -> cres<'tcx, ty::ClosureTy<'tcx>> {
207
208         let store = match (a.store, b.store) {
209             (ty::RegionTraitStore(a_r, a_m),
210              ty::RegionTraitStore(b_r, b_m)) if a_m == b_m => {
211                 let r = try!(self.contraregions(a_r, b_r));
212                 ty::RegionTraitStore(r, a_m)
213             }
214
215             _ if a.store == b.store => {
216                 a.store
217             }
218
219             _ => {
220                 return Err(ty::terr_sigil_mismatch(expected_found(self, a.store, b.store)))
221             }
222         };
223         let unsafety = try!(self.unsafeties(a.unsafety, b.unsafety));
224         let onceness = try!(self.oncenesses(a.onceness, b.onceness));
225         let bounds = try!(self.existential_bounds(&a.bounds, &b.bounds));
226         let sig = try!(self.binders(&a.sig, &b.sig));
227         let abi = try!(self.abi(a.abi, b.abi));
228         Ok(ty::ClosureTy {
229             unsafety: unsafety,
230             onceness: onceness,
231             store: store,
232             bounds: bounds,
233             sig: sig,
234             abi: abi,
235         })
236     }
237
238     fn fn_sigs(&self, a: &ty::FnSig<'tcx>, b: &ty::FnSig<'tcx>) -> cres<'tcx, ty::FnSig<'tcx>> {
239         if a.variadic != b.variadic {
240             return Err(ty::terr_variadic_mismatch(expected_found(self, a.variadic, b.variadic)));
241         }
242
243         let inputs = try!(argvecs(self,
244                                   a.inputs.as_slice(),
245                                   b.inputs.as_slice()));
246
247         let output = try!(match (a.output, b.output) {
248             (ty::FnConverging(a_ty), ty::FnConverging(b_ty)) =>
249                 Ok(ty::FnConverging(try!(self.tys(a_ty, b_ty)))),
250             (ty::FnDiverging, ty::FnDiverging) =>
251                 Ok(ty::FnDiverging),
252             (a, b) =>
253                 Err(ty::terr_convergence_mismatch(
254                     expected_found(self, a != ty::FnDiverging, b != ty::FnDiverging))),
255         });
256
257         return Ok(ty::FnSig {inputs: inputs,
258                              output: output,
259                              variadic: a.variadic});
260
261
262         fn argvecs<'tcx, C: Combine<'tcx>>(combiner: &C,
263                                            a_args: &[Ty<'tcx>],
264                                            b_args: &[Ty<'tcx>])
265                                            -> cres<'tcx, Vec<Ty<'tcx>>>
266         {
267             if a_args.len() == b_args.len() {
268                 a_args.iter().zip(b_args.iter())
269                     .map(|(a, b)| combiner.args(*a, *b)).collect()
270             } else {
271                 Err(ty::terr_arg_count)
272             }
273         }
274     }
275
276     fn args(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
277         self.contratys(a, b).and_then(|t| Ok(t))
278     }
279
280     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety>;
281
282     fn abi(&self, a: abi::Abi, b: abi::Abi) -> cres<'tcx, abi::Abi> {
283         if a == b {
284             Ok(a)
285         } else {
286             Err(ty::terr_abi_mismatch(expected_found(self, a, b)))
287         }
288     }
289
290     fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness>;
291
292     fn projection_tys(&self,
293                       a: &ty::ProjectionTy<'tcx>,
294                       b: &ty::ProjectionTy<'tcx>)
295                       -> cres<'tcx, ty::ProjectionTy<'tcx>>
296     {
297         if a.item_name != b.item_name {
298             Err(ty::terr_projection_name_mismatched(
299                 expected_found(self, a.item_name, b.item_name)))
300         } else {
301             let trait_ref = try!(self.trait_refs(&*a.trait_ref, &*b.trait_ref));
302             Ok(ty::ProjectionTy { trait_ref: Rc::new(trait_ref), item_name: a.item_name })
303         }
304     }
305
306     fn projection_predicates(&self,
307                              a: &ty::ProjectionPredicate<'tcx>,
308                              b: &ty::ProjectionPredicate<'tcx>)
309                              -> cres<'tcx, ty::ProjectionPredicate<'tcx>>
310     {
311         let projection_ty = try!(self.projection_tys(&a.projection_ty, &b.projection_ty));
312         let ty = try!(self.tys(a.ty, b.ty));
313         Ok(ty::ProjectionPredicate { projection_ty: projection_ty, ty: ty })
314     }
315
316     fn projection_bounds(&self,
317                          a: &Vec<ty::PolyProjectionPredicate<'tcx>>,
318                          b: &Vec<ty::PolyProjectionPredicate<'tcx>>)
319                          -> cres<'tcx, Vec<ty::PolyProjectionPredicate<'tcx>>>
320     {
321         // To be compatible, `a` and `b` must be for precisely the
322         // same set of traits and item names. We always require that
323         // projection bounds lists are sorted by trait-def-id and item-name,
324         // so we can just iterate through the lists pairwise, so long as they are the
325         // same length.
326         if a.len() != b.len() {
327             Err(ty::terr_projection_bounds_length(expected_found(self, a.len(), b.len())))
328         } else {
329             a.iter()
330                 .zip(b.iter())
331                 .map(|(a, b)| self.binders(a, b))
332                 .collect()
333         }
334     }
335
336     fn existential_bounds(&self,
337                           a: &ty::ExistentialBounds<'tcx>,
338                           b: &ty::ExistentialBounds<'tcx>)
339                           -> cres<'tcx, ty::ExistentialBounds<'tcx>>
340     {
341         let r = try!(self.contraregions(a.region_bound, b.region_bound));
342         let nb = try!(self.builtin_bounds(a.builtin_bounds, b.builtin_bounds));
343         let pb = try!(self.projection_bounds(&a.projection_bounds, &b.projection_bounds));
344         Ok(ty::ExistentialBounds { region_bound: r,
345                                    builtin_bounds: nb,
346                                    projection_bounds: pb })
347     }
348
349     fn builtin_bounds(&self,
350                       a: ty::BuiltinBounds,
351                       b: ty::BuiltinBounds)
352                       -> cres<'tcx, ty::BuiltinBounds>;
353
354     fn contraregions(&self, a: ty::Region, b: ty::Region)
355                   -> cres<'tcx, ty::Region>;
356
357     fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region>;
358
359     fn trait_stores(&self,
360                     vk: ty::terr_vstore_kind,
361                     a: ty::TraitStore,
362                     b: ty::TraitStore)
363                     -> cres<'tcx, ty::TraitStore> {
364         debug!("{}.trait_stores(a={}, b={})", self.tag(), a, b);
365
366         match (a, b) {
367             (ty::RegionTraitStore(a_r, a_m),
368              ty::RegionTraitStore(b_r, b_m)) if a_m == b_m => {
369                 self.contraregions(a_r, b_r).and_then(|r| {
370                     Ok(ty::RegionTraitStore(r, a_m))
371                 })
372             }
373
374             _ if a == b => {
375                 Ok(a)
376             }
377
378             _ => {
379                 Err(ty::terr_trait_stores_differ(vk, expected_found(self, a, b)))
380             }
381         }
382     }
383
384     fn trait_refs(&self,
385                   a: &ty::TraitRef<'tcx>,
386                   b: &ty::TraitRef<'tcx>)
387                   -> cres<'tcx, ty::TraitRef<'tcx>>
388     {
389         // Different traits cannot be related
390         if a.def_id != b.def_id {
391             Err(ty::terr_traits(expected_found(self, a.def_id, b.def_id)))
392         } else {
393             let substs = try!(self.substs(a.def_id, a.substs, b.substs));
394             Ok(ty::TraitRef { def_id: a.def_id, substs: self.tcx().mk_substs(substs) })
395         }
396     }
397
398     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
399         where T : Combineable<'tcx>;
400     // this must be overridden to do correctly, so as to account for higher-ranked
401     // behavior
402 }
403
404 pub trait Combineable<'tcx> : Repr<'tcx> + TypeFoldable<'tcx> {
405     fn combine<C:Combine<'tcx>>(combiner: &C, a: &Self, b: &Self) -> cres<'tcx, Self>;
406 }
407
408 impl<'tcx,T> Combineable<'tcx> for Rc<T>
409     where T : Combineable<'tcx>
410 {
411     fn combine<C:Combine<'tcx>>(combiner: &C,
412                                 a: &Rc<T>,
413                                 b: &Rc<T>)
414                                 -> cres<'tcx, Rc<T>>
415     {
416         Ok(Rc::new(try!(Combineable::combine(combiner, &**a, &**b))))
417     }
418 }
419
420 impl<'tcx> Combineable<'tcx> for ty::TraitRef<'tcx> {
421     fn combine<C:Combine<'tcx>>(combiner: &C,
422                                 a: &ty::TraitRef<'tcx>,
423                                 b: &ty::TraitRef<'tcx>)
424                                 -> cres<'tcx, ty::TraitRef<'tcx>>
425     {
426         combiner.trait_refs(a, b)
427     }
428 }
429
430 impl<'tcx> Combineable<'tcx> for ty::ProjectionPredicate<'tcx> {
431     fn combine<C:Combine<'tcx>>(combiner: &C,
432                                 a: &ty::ProjectionPredicate<'tcx>,
433                                 b: &ty::ProjectionPredicate<'tcx>)
434                                 -> cres<'tcx, ty::ProjectionPredicate<'tcx>>
435     {
436         combiner.projection_predicates(a, b)
437     }
438 }
439
440 impl<'tcx> Combineable<'tcx> for ty::FnSig<'tcx> {
441     fn combine<C:Combine<'tcx>>(combiner: &C,
442                                 a: &ty::FnSig<'tcx>,
443                                 b: &ty::FnSig<'tcx>)
444                                 -> cres<'tcx, ty::FnSig<'tcx>>
445     {
446         combiner.fn_sigs(a, b)
447     }
448 }
449
450 #[deriving(Clone)]
451 pub struct CombineFields<'a, 'tcx: 'a> {
452     pub infcx: &'a InferCtxt<'a, 'tcx>,
453     pub a_is_expected: bool,
454     pub trace: TypeTrace<'tcx>,
455 }
456
457 pub fn expected_found<'tcx, C: Combine<'tcx>, T>(
458         this: &C, a: T, b: T) -> ty::expected_found<T> {
459     if this.a_is_expected() {
460         ty::expected_found {expected: a, found: b}
461     } else {
462         ty::expected_found {expected: b, found: a}
463     }
464 }
465
466 pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C,
467                                          a: Ty<'tcx>,
468                                          b: Ty<'tcx>)
469                                          -> cres<'tcx, Ty<'tcx>>
470 {
471     let tcx = this.infcx().tcx;
472     let a_sty = &a.sty;
473     let b_sty = &b.sty;
474     debug!("super_tys: a_sty={} b_sty={}", a_sty, b_sty);
475     return match (a_sty, b_sty) {
476       // The "subtype" ought to be handling cases involving var:
477       (&ty::ty_infer(TyVar(_)), _) |
478       (_, &ty::ty_infer(TyVar(_))) => {
479         tcx.sess.bug(
480             format!("{}: bot and var types should have been handled ({},{})",
481                     this.tag(),
482                     a.repr(this.infcx().tcx),
483                     b.repr(this.infcx().tcx))[]);
484       }
485
486       (&ty::ty_err, _) | (_, &ty::ty_err) => {
487           Ok(tcx.types.err)
488       }
489
490         // Relate integral variables to other types
491         (&ty::ty_infer(IntVar(a_id)), &ty::ty_infer(IntVar(b_id))) => {
492             try!(this.infcx().simple_vars(this.a_is_expected(),
493                                             a_id, b_id));
494             Ok(a)
495         }
496         (&ty::ty_infer(IntVar(v_id)), &ty::ty_int(v)) => {
497             unify_integral_variable(this, this.a_is_expected(),
498                                     v_id, IntType(v))
499         }
500         (&ty::ty_int(v), &ty::ty_infer(IntVar(v_id))) => {
501             unify_integral_variable(this, !this.a_is_expected(),
502                                     v_id, IntType(v))
503         }
504         (&ty::ty_infer(IntVar(v_id)), &ty::ty_uint(v)) => {
505             unify_integral_variable(this, this.a_is_expected(),
506                                     v_id, UintType(v))
507         }
508         (&ty::ty_uint(v), &ty::ty_infer(IntVar(v_id))) => {
509             unify_integral_variable(this, !this.a_is_expected(),
510                                     v_id, UintType(v))
511         }
512
513         // Relate floating-point variables to other types
514         (&ty::ty_infer(FloatVar(a_id)), &ty::ty_infer(FloatVar(b_id))) => {
515             try!(this.infcx().simple_vars(this.a_is_expected(), a_id, b_id));
516             Ok(a)
517         }
518         (&ty::ty_infer(FloatVar(v_id)), &ty::ty_float(v)) => {
519             unify_float_variable(this, this.a_is_expected(), v_id, v)
520         }
521         (&ty::ty_float(v), &ty::ty_infer(FloatVar(v_id))) => {
522             unify_float_variable(this, !this.a_is_expected(), v_id, v)
523         }
524
525       (&ty::ty_char, _) |
526       (&ty::ty_bool, _) |
527       (&ty::ty_int(_), _) |
528       (&ty::ty_uint(_), _) |
529       (&ty::ty_float(_), _) => {
530         if a == b {
531             Ok(a)
532         } else {
533             Err(ty::terr_sorts(expected_found(this, a, b)))
534         }
535       }
536
537       (&ty::ty_param(ref a_p), &ty::ty_param(ref b_p)) if
538           a_p.idx == b_p.idx && a_p.space == b_p.space => {
539         Ok(a)
540       }
541
542       (&ty::ty_enum(a_id, a_substs),
543        &ty::ty_enum(b_id, b_substs))
544       if a_id == b_id => {
545           let substs = try!(this.substs(a_id,
546                                           a_substs,
547                                           b_substs));
548           Ok(ty::mk_enum(tcx, a_id, tcx.mk_substs(substs)))
549       }
550
551       (&ty::ty_trait(ref a_),
552        &ty::ty_trait(ref b_)) => {
553           debug!("Trying to match traits {} and {}", a, b);
554           let principal = try!(this.binders(&a_.principal, &b_.principal));
555           let bounds = try!(this.existential_bounds(&a_.bounds, &b_.bounds));
556           Ok(ty::mk_trait(tcx, principal, bounds))
557       }
558
559       (&ty::ty_struct(a_id, a_substs), &ty::ty_struct(b_id, b_substs))
560       if a_id == b_id => {
561             let substs = try!(this.substs(a_id, a_substs, b_substs));
562             Ok(ty::mk_struct(tcx, a_id, tcx.mk_substs(substs)))
563       }
564
565       (&ty::ty_unboxed_closure(a_id, a_region, a_substs),
566        &ty::ty_unboxed_closure(b_id, b_region, b_substs))
567       if a_id == b_id => {
568           // All ty_unboxed_closure types with the same id represent
569           // the (anonymous) type of the same closure expression. So
570           // all of their regions should be equated.
571           let region = try!(this.equate().regions(*a_region, *b_region));
572           let substs = try!(this.substs_variances(None, a_substs, b_substs));
573           Ok(ty::mk_unboxed_closure(tcx, a_id, tcx.mk_region(region), tcx.mk_substs(substs)))
574       }
575
576       (&ty::ty_uniq(a_inner), &ty::ty_uniq(b_inner)) => {
577           let typ = try!(this.tys(a_inner, b_inner));
578           Ok(ty::mk_uniq(tcx, typ))
579       }
580
581       (&ty::ty_ptr(ref a_mt), &ty::ty_ptr(ref b_mt)) => {
582           let mt = try!(this.mts(a_mt, b_mt));
583           Ok(ty::mk_ptr(tcx, mt))
584       }
585
586       (&ty::ty_rptr(a_r, ref a_mt), &ty::ty_rptr(b_r, ref b_mt)) => {
587             let r = try!(this.contraregions(*a_r, *b_r));
588             // FIXME(14985)  If we have mutable references to trait objects, we
589             // used to use covariant subtyping. I have preserved this behaviour,
590             // even though it is probably incorrect. So don't go down the usual
591             // path which would require invariance.
592             let mt = match (&a_mt.ty.sty, &b_mt.ty.sty) {
593                 (&ty::ty_trait(..), &ty::ty_trait(..)) if a_mt.mutbl == b_mt.mutbl => {
594                     let ty = try!(this.tys(a_mt.ty, b_mt.ty));
595                     ty::mt { ty: ty, mutbl: a_mt.mutbl }
596                 }
597                 _ => try!(this.mts(a_mt, b_mt))
598             };
599             Ok(ty::mk_rptr(tcx, tcx.mk_region(r), mt))
600       }
601
602       (&ty::ty_vec(a_t, Some(sz_a)), &ty::ty_vec(b_t, Some(sz_b))) => {
603         this.tys(a_t, b_t).and_then(|t| {
604             if sz_a == sz_b {
605                 Ok(ty::mk_vec(tcx, t, Some(sz_a)))
606             } else {
607                 Err(ty::terr_fixed_array_size(expected_found(this, sz_a, sz_b)))
608             }
609         })
610       }
611
612       (&ty::ty_vec(a_t, sz_a), &ty::ty_vec(b_t, sz_b)) => {
613         this.tys(a_t, b_t).and_then(|t| {
614             if sz_a == sz_b {
615                 Ok(ty::mk_vec(tcx, t, sz_a))
616             } else {
617                 Err(ty::terr_sorts(expected_found(this, a, b)))
618             }
619         })
620       }
621
622       (&ty::ty_str, &ty::ty_str) => {
623             Ok(ty::mk_str(tcx))
624       }
625
626       (&ty::ty_tup(ref as_), &ty::ty_tup(ref bs)) => {
627         if as_.len() == bs.len() {
628             as_.iter().zip(bs.iter())
629                .map(|(a, b)| this.tys(*a, *b))
630                .collect::<Result<_, _>>()
631                .map(|ts| ty::mk_tup(tcx, ts))
632         } else if as_.len() != 0 && bs.len() != 0 {
633             Err(ty::terr_tuple_size(
634                 expected_found(this, as_.len(), bs.len())))
635         } else {
636             Err(ty::terr_sorts(expected_found(this, a, b)))
637         }
638       }
639
640         (&ty::ty_bare_fn(a_opt_def_id, a_fty), &ty::ty_bare_fn(b_opt_def_id, b_fty))
641             if a_opt_def_id == b_opt_def_id =>
642         {
643             let fty = try!(this.bare_fn_tys(a_fty, b_fty));
644             Ok(ty::mk_bare_fn(tcx, a_opt_def_id, tcx.mk_bare_fn(fty)))
645         }
646
647       (&ty::ty_closure(ref a_fty), &ty::ty_closure(ref b_fty)) => {
648         this.closure_tys(&**a_fty, &**b_fty).and_then(|fty| {
649             Ok(ty::mk_closure(tcx, fty))
650         })
651       }
652
653       (&ty::ty_projection(ref a_data), &ty::ty_projection(ref b_data)) => {
654           let projection_ty = try!(this.projection_tys(a_data, b_data));
655           Ok(ty::mk_projection(tcx, projection_ty.trait_ref, projection_ty.item_name))
656       }
657
658       _ => Err(ty::terr_sorts(expected_found(this, a, b)))
659     };
660
661     fn unify_integral_variable<'tcx, C: Combine<'tcx>>(
662         this: &C,
663         vid_is_expected: bool,
664         vid: ty::IntVid,
665         val: ty::IntVarValue) -> cres<'tcx, Ty<'tcx>>
666     {
667         try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
668         match val {
669             IntType(v) => Ok(ty::mk_mach_int(this.tcx(), v)),
670             UintType(v) => Ok(ty::mk_mach_uint(this.tcx(), v))
671         }
672     }
673
674     fn unify_float_variable<'tcx, C: Combine<'tcx>>(
675         this: &C,
676         vid_is_expected: bool,
677         vid: ty::FloatVid,
678         val: ast::FloatTy) -> cres<'tcx, Ty<'tcx>>
679     {
680         try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
681         Ok(ty::mk_mach_float(this.tcx(), val))
682     }
683 }
684
685 impl<'f, 'tcx> CombineFields<'f, 'tcx> {
686     pub fn switch_expected(&self) -> CombineFields<'f, 'tcx> {
687         CombineFields {
688             a_is_expected: !self.a_is_expected,
689             ..(*self).clone()
690         }
691     }
692
693     fn equate(&self) -> Equate<'f, 'tcx> {
694         Equate((*self).clone())
695     }
696
697     fn sub(&self) -> Sub<'f, 'tcx> {
698         Sub((*self).clone())
699     }
700
701     pub fn instantiate(&self,
702                        a_ty: Ty<'tcx>,
703                        dir: RelationDir,
704                        b_vid: ty::TyVid)
705                        -> cres<'tcx, ()>
706     {
707         let tcx = self.infcx.tcx;
708         let mut stack = Vec::new();
709         stack.push((a_ty, dir, b_vid));
710         loop {
711             // For each turn of the loop, we extract a tuple
712             //
713             //     (a_ty, dir, b_vid)
714             //
715             // to relate. Here dir is either SubtypeOf or
716             // SupertypeOf. The idea is that we should ensure that
717             // the type `a_ty` is a subtype or supertype (respectively) of the
718             // type to which `b_vid` is bound.
719             //
720             // If `b_vid` has not yet been instantiated with a type
721             // (which is always true on the first iteration, but not
722             // necessarily true on later iterations), we will first
723             // instantiate `b_vid` with a *generalized* version of
724             // `a_ty`. Generalization introduces other inference
725             // variables wherever subtyping could occur (at time of
726             // this writing, this means replacing free regions with
727             // region variables).
728             let (a_ty, dir, b_vid) = match stack.pop() {
729                 None => break,
730                 Some(e) => e,
731             };
732
733             debug!("instantiate(a_ty={} dir={} b_vid={})",
734                    a_ty.repr(tcx),
735                    dir,
736                    b_vid.repr(tcx));
737
738             // Check whether `vid` has been instantiated yet.  If not,
739             // make a generalized form of `ty` and instantiate with
740             // that.
741             let b_ty = self.infcx.type_variables.borrow().probe(b_vid);
742             let b_ty = match b_ty {
743                 Some(t) => t, // ...already instantiated.
744                 None => {     // ...not yet instantiated:
745                     // Generalize type if necessary.
746                     let generalized_ty = try!(match dir {
747                         EqTo => {
748                             self.generalize(a_ty, b_vid, false)
749                         }
750                         SupertypeOf | SubtypeOf => {
751                             self.generalize(a_ty, b_vid, true)
752                         }
753                     });
754                     debug!("instantiate(a_ty={}, dir={}, \
755                                         b_vid={}, generalized_ty={})",
756                            a_ty.repr(tcx), dir, b_vid.repr(tcx),
757                            generalized_ty.repr(tcx));
758                     self.infcx.type_variables
759                         .borrow_mut()
760                         .instantiate_and_push(
761                             b_vid, generalized_ty, &mut stack);
762                     generalized_ty
763                 }
764             };
765
766             // The original triple was `(a_ty, dir, b_vid)` -- now we have
767             // resolved `b_vid` to `b_ty`, so apply `(a_ty, dir, b_ty)`:
768             //
769             // FIXME(#16847): This code is non-ideal because all these subtype
770             // relations wind up attributed to the same spans. We need
771             // to associate causes/spans with each of the relations in
772             // the stack to get this right.
773             match dir {
774                 EqTo => {
775                     try!(self.equate().tys(a_ty, b_ty));
776                 }
777
778                 SubtypeOf => {
779                     try!(self.sub().tys(a_ty, b_ty));
780                 }
781
782                 SupertypeOf => {
783                     try!(self.sub().contratys(a_ty, b_ty));
784                 }
785             }
786         }
787
788         Ok(())
789     }
790
791     /// Attempts to generalize `ty` for the type variable `for_vid`.  This checks for cycle -- that
792     /// is, whether the type `ty` references `for_vid`. If `make_region_vars` is true, it will also
793     /// replace all regions with fresh variables. Returns `ty_err` in the case of a cycle, `Ok`
794     /// otherwise.
795     fn generalize(&self,
796                   ty: Ty<'tcx>,
797                   for_vid: ty::TyVid,
798                   make_region_vars: bool)
799                   -> cres<'tcx, Ty<'tcx>>
800     {
801         let mut generalize = Generalizer { infcx: self.infcx,
802                                            span: self.trace.origin.span(),
803                                            for_vid: for_vid,
804                                            make_region_vars: make_region_vars,
805                                            cycle_detected: false };
806         let u = ty.fold_with(&mut generalize);
807         if generalize.cycle_detected {
808             Err(ty::terr_cyclic_ty)
809         } else {
810             Ok(u)
811         }
812     }
813 }
814
815 struct Generalizer<'cx, 'tcx:'cx> {
816     infcx: &'cx InferCtxt<'cx, 'tcx>,
817     span: Span,
818     for_vid: ty::TyVid,
819     make_region_vars: bool,
820     cycle_detected: bool,
821 }
822
823 impl<'cx, 'tcx> ty_fold::TypeFolder<'tcx> for Generalizer<'cx, 'tcx> {
824     fn tcx(&self) -> &ty::ctxt<'tcx> {
825         self.infcx.tcx
826     }
827
828     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
829         // Check to see whether the type we are genealizing references
830         // `vid`. At the same time, also update any type variables to
831         // the values that they are bound to. This is needed to truly
832         // check for cycles, but also just makes things readable.
833         //
834         // (In particular, you could have something like `$0 = Box<$1>`
835         //  where `$1` has already been instantiated with `Box<$0>`)
836         match t.sty {
837             ty::ty_infer(ty::TyVar(vid)) => {
838                 if vid == self.for_vid {
839                     self.cycle_detected = true;
840                     self.tcx().types.err
841                 } else {
842                     match self.infcx.type_variables.borrow().probe(vid) {
843                         Some(u) => self.fold_ty(u),
844                         None => t,
845                     }
846                 }
847             }
848             _ => {
849                 ty_fold::super_fold_ty(self, t)
850             }
851         }
852     }
853
854     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
855         match r {
856             // Never make variables for regions bound within the type itself.
857             ty::ReLateBound(..) => { return r; }
858
859             // Early-bound regions should really have been substituted away before
860             // we get to this point.
861             ty::ReEarlyBound(..) => {
862                 self.tcx().sess.span_bug(
863                     self.span,
864                     format!("Encountered early bound region when generalizing: {}",
865                             r.repr(self.tcx()))[]);
866             }
867
868             // Always make a fresh region variable for skolemized regions;
869             // the higher-ranked decision procedures rely on this.
870             ty::ReInfer(ty::ReSkolemized(..)) => { }
871
872             // For anything else, we make a region variable, unless we
873             // are *equating*, in which case it's just wasteful.
874             ty::ReEmpty |
875             ty::ReStatic |
876             ty::ReScope(..) |
877             ty::ReInfer(ty::ReVar(..)) |
878             ty::ReFree(..) => {
879                 if !self.make_region_vars {
880                     return r;
881                 }
882             }
883         }
884
885         // FIXME: This is non-ideal because we don't give a
886         // very descriptive origin for this region variable.
887         self.infcx.next_region_var(MiscVariable(self.span))
888     }
889 }