]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/test.rs
core: Fix size_hint for signed integer Range<T> iterators
[rust.git] / src / librustc_driver / test.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 //! # Standalone Tests for the Inference Module
12
13 use diagnostic;
14 use diagnostic::Emitter;
15 use driver;
16 use rustc_lint;
17 use rustc_resolve as resolve;
18 use rustc_typeck::middle::lang_items;
19 use rustc_typeck::middle::region::{self, CodeExtent, DestructionScopeData};
20 use rustc_typeck::middle::resolve_lifetime;
21 use rustc_typeck::middle::stability;
22 use rustc_typeck::middle::subst;
23 use rustc_typeck::middle::subst::Subst;
24 use rustc_typeck::middle::ty::{self, Ty};
25 use rustc_typeck::middle::ty_relate::TypeRelation;
26 use rustc_typeck::middle::infer;
27 use rustc_typeck::middle::infer::lub::Lub;
28 use rustc_typeck::middle::infer::glb::Glb;
29 use rustc_typeck::middle::infer::sub::Sub;
30 use rustc_typeck::util::ppaux::{ty_to_string, Repr, UserString};
31 use rustc::session::{self,config};
32 use syntax::{abi, ast, ast_map};
33 use syntax::codemap;
34 use syntax::codemap::{Span, CodeMap, DUMMY_SP};
35 use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
36 use syntax::parse::token;
37
38 struct Env<'a, 'tcx: 'a> {
39     infcx: &'a infer::InferCtxt<'a, 'tcx>,
40 }
41
42 struct RH<'a> {
43     id: ast::NodeId,
44     sub: &'a [RH<'a>]
45 }
46
47 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_std)] #![no_std]";
48
49 struct ExpectErrorEmitter {
50     messages: Vec<String>
51 }
52
53 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
54     match lvl {
55         Bug | Fatal | Error => { }
56         Warning | Note | Help => { return; }
57     }
58
59     debug!("Error: {}", msg);
60     match e.messages.iter().position(|m| msg.contains(m)) {
61         Some(i) => {
62             e.messages.remove(i);
63         }
64         None => {
65             panic!("Unexpected error: {} Expected: {:?}",
66                   msg, e.messages);
67         }
68     }
69 }
70
71 impl Emitter for ExpectErrorEmitter {
72     fn emit(&mut self,
73             _cmsp: Option<(&codemap::CodeMap, Span)>,
74             msg: &str,
75             _: Option<&str>,
76             lvl: Level)
77     {
78         remove_message(self, msg, lvl);
79     }
80
81     fn custom_emit(&mut self,
82                    _cm: &codemap::CodeMap,
83                    _sp: RenderSpan,
84                    msg: &str,
85                    lvl: Level)
86     {
87         remove_message(self, msg, lvl);
88     }
89 }
90
91 fn errors(msgs: &[&str]) -> (Box<Emitter+Send>, usize) {
92     let v = msgs.iter().map(|m| m.to_string()).collect();
93     (box ExpectErrorEmitter { messages: v } as Box<Emitter+Send>, msgs.len())
94 }
95
96 fn test_env<F>(source_string: &str,
97                (emitter, expected_err_count): (Box<Emitter+Send>, usize),
98                body: F) where
99     F: FnOnce(Env),
100 {
101     let mut options =
102         config::basic_options();
103     options.debugging_opts.verbose = true;
104     let codemap =
105         CodeMap::new();
106     let diagnostic_handler =
107         diagnostic::mk_handler(true, emitter);
108     let span_diagnostic_handler =
109         diagnostic::mk_span_handler(diagnostic_handler, codemap);
110
111     let sess = session::build_session_(options, None, span_diagnostic_handler);
112     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
113     let krate_config = Vec::new();
114     let input = config::Input::Str(source_string.to_string());
115     let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
116     let krate = driver::phase_2_configure_and_expand(&sess, krate, "test", None)
117                     .expect("phase 2 aborted");
118
119     let mut forest = ast_map::Forest::new(krate);
120     let arenas = ty::CtxtArenas::new();
121     let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
122     let krate = ast_map.krate();
123
124     // run just enough stuff to build a tcx:
125     let lang_items = lang_items::collect_language_items(krate, &sess);
126     let resolve::CrateMap { def_map, freevars, .. } =
127         resolve::resolve_crate(&sess, &ast_map, &lang_items, krate, resolve::MakeGlobMap::No);
128     let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map);
129     let region_map = region::resolve_crate(&sess, krate);
130     let tcx = ty::mk_ctxt(sess,
131                           &arenas,
132                           def_map,
133                           named_region_map,
134                           ast_map,
135                           freevars,
136                           region_map,
137                           lang_items,
138                           stability::Index::new(krate));
139     let infcx = infer::new_infer_ctxt(&tcx);
140     body(Env { infcx: &infcx });
141     infcx.resolve_regions_and_report_errors(ast::CRATE_NODE_ID);
142     assert_eq!(tcx.sess.err_count(), expected_err_count);
143 }
144
145 impl<'a, 'tcx> Env<'a, 'tcx> {
146     pub fn tcx(&self) -> &ty::ctxt<'tcx> {
147         self.infcx.tcx
148     }
149
150     pub fn create_region_hierarchy(&self, rh: &RH) {
151         for child_rh in rh.sub {
152             self.create_region_hierarchy(child_rh);
153             self.infcx.tcx.region_maps.record_encl_scope(
154                 CodeExtent::from_node_id(child_rh.id),
155                 CodeExtent::from_node_id(rh.id));
156         }
157     }
158
159     pub fn create_simple_region_hierarchy(&self) {
160         // creates a region hierarchy where 1 is root, 10 and 11 are
161         // children of 1, etc
162         self.create_region_hierarchy(
163             &RH {id: 1,
164                  sub: &[RH {id: 10,
165                             sub: &[]},
166                         RH {id: 11,
167                             sub: &[]}]});
168     }
169
170     #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
171     pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
172         return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
173             Some(id) => id,
174             None => {
175                 panic!("no item found: `{}`", names.connect("::"));
176             }
177         };
178
179         fn search_mod(this: &Env,
180                       m: &ast::Mod,
181                       idx: usize,
182                       names: &[String])
183                       -> Option<ast::NodeId> {
184             assert!(idx < names.len());
185             for item in &m.items {
186                 if item.ident.user_string(this.infcx.tcx) == names[idx] {
187                     return search(this, &**item, idx+1, names);
188                 }
189             }
190             return None;
191         }
192
193         fn search(this: &Env,
194                   it: &ast::Item,
195                   idx: usize,
196                   names: &[String])
197                   -> Option<ast::NodeId> {
198             if idx == names.len() {
199                 return Some(it.id);
200             }
201
202             return match it.node {
203                 ast::ItemUse(..) | ast::ItemExternCrate(..) |
204                 ast::ItemConst(..) | ast::ItemStatic(..) | ast::ItemFn(..) |
205                 ast::ItemForeignMod(..) | ast::ItemTy(..) => {
206                     None
207                 }
208
209                 ast::ItemEnum(..) | ast::ItemStruct(..) |
210                 ast::ItemTrait(..) | ast::ItemImpl(..) |
211                 ast::ItemMac(..) | ast::ItemDefaultImpl(..) => {
212                     None
213                 }
214
215                 ast::ItemMod(ref m) => {
216                     search_mod(this, m, idx, names)
217                 }
218             };
219         }
220     }
221
222     pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
223         match infer::mk_subty(self.infcx, true, infer::Misc(DUMMY_SP), a, b) {
224             Ok(_) => true,
225             Err(ref e) => panic!("Encountered error: {}",
226                                 ty::type_err_to_str(self.infcx.tcx, e))
227         }
228     }
229
230     pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
231         match infer::can_mk_subty(self.infcx, a, b) {
232             Ok(_) => true,
233             Err(_) => false
234         }
235     }
236
237     pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
238         if !self.is_subtype(a, b) {
239             panic!("{} is not a subtype of {}, but it should be",
240                   self.ty_to_string(a),
241                   self.ty_to_string(b));
242         }
243     }
244
245     pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
246         self.assert_subtype(a, b);
247         self.assert_subtype(b, a);
248     }
249
250     pub fn ty_to_string(&self, a: Ty<'tcx>) -> String {
251         ty_to_string(self.infcx.tcx, a)
252     }
253
254     pub fn t_fn(&self,
255                 input_tys: &[Ty<'tcx>],
256                 output_ty: Ty<'tcx>)
257                 -> Ty<'tcx>
258     {
259         let input_args = input_tys.iter().cloned().collect();
260         ty::mk_bare_fn(self.infcx.tcx,
261                        None,
262                        self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
263                            unsafety: ast::Unsafety::Normal,
264                            abi: abi::Rust,
265                            sig: ty::Binder(ty::FnSig {
266                                inputs: input_args,
267                                output: ty::FnConverging(output_ty),
268                                variadic: false
269                            })
270                        }))
271     }
272
273     pub fn t_nil(&self) -> Ty<'tcx> {
274         ty::mk_nil(self.infcx.tcx)
275     }
276
277     pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
278         ty::mk_tup(self.infcx.tcx, vec![ty1, ty2])
279     }
280
281     pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
282         let name = format!("T{}", index);
283         ty::mk_param(self.infcx.tcx, space, index, token::intern(&name[..]))
284     }
285
286     pub fn re_early_bound(&self,
287                           space: subst::ParamSpace,
288                           index: u32,
289                           name: &'static str)
290                           -> ty::Region
291     {
292         let name = token::intern(name);
293         ty::ReEarlyBound(ty::EarlyBoundRegion {
294             param_id: ast::DUMMY_NODE_ID,
295             space: space,
296             index: index,
297             name: name
298         })
299     }
300
301     pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
302         ty::ReLateBound(debruijn, ty::BrAnon(id))
303     }
304
305     pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
306         ty::mk_imm_rptr(self.infcx.tcx,
307                         self.infcx.tcx.mk_region(r),
308                         self.tcx().types.isize)
309     }
310
311     pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
312         let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
313         ty::mk_imm_rptr(self.infcx.tcx,
314                         self.infcx.tcx.mk_region(r),
315                         self.tcx().types.isize)
316     }
317
318     pub fn t_rptr_late_bound_with_debruijn(&self,
319                                            id: u32,
320                                            debruijn: ty::DebruijnIndex)
321                                            -> Ty<'tcx> {
322         let r = self.re_late_bound_with_debruijn(id, debruijn);
323         ty::mk_imm_rptr(self.infcx.tcx,
324                         self.infcx.tcx.mk_region(r),
325                         self.tcx().types.isize)
326     }
327
328     pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
329         let r = ty::ReScope(CodeExtent::from_node_id(id));
330         ty::mk_imm_rptr(self.infcx.tcx, self.infcx.tcx.mk_region(r),
331                         self.tcx().types.isize)
332     }
333
334     pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
335         ty::ReFree(ty::FreeRegion { scope: DestructionScopeData::new(nid),
336                                     bound_region: ty::BrAnon(id)})
337     }
338
339     pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
340         let r = self.re_free(nid, id);
341         ty::mk_imm_rptr(self.infcx.tcx,
342                         self.infcx.tcx.mk_region(r),
343                         self.tcx().types.isize)
344     }
345
346     pub fn t_rptr_static(&self) -> Ty<'tcx> {
347         ty::mk_imm_rptr(self.infcx.tcx,
348                         self.infcx.tcx.mk_region(ty::ReStatic),
349                         self.tcx().types.isize)
350     }
351
352     pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
353         infer::TypeTrace::dummy(self.tcx())
354     }
355
356     pub fn sub(&self) -> Sub<'a, 'tcx> {
357         let trace = self.dummy_type_trace();
358         self.infcx.sub(true, trace)
359     }
360
361     pub fn lub(&self) -> Lub<'a, 'tcx> {
362         let trace = self.dummy_type_trace();
363         self.infcx.lub(true, trace)
364     }
365
366     pub fn glb(&self) -> Glb<'a, 'tcx> {
367         let trace = self.dummy_type_trace();
368         self.infcx.glb(true, trace)
369     }
370
371     pub fn make_lub_ty(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> Ty<'tcx> {
372         match self.lub().relate(&t1, &t2) {
373             Ok(t) => t,
374             Err(ref e) => panic!("unexpected error computing LUB: {}",
375                                 ty::type_err_to_str(self.infcx.tcx, e))
376         }
377     }
378
379     /// Checks that `t1 <: t2` is true (this may register additional
380     /// region checks).
381     pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
382         match self.sub().relate(&t1, &t2) {
383             Ok(_) => { }
384             Err(ref e) => {
385                 panic!("unexpected error computing sub({},{}): {}",
386                        t1.repr(self.infcx.tcx),
387                        t2.repr(self.infcx.tcx),
388                        ty::type_err_to_str(self.infcx.tcx, e));
389             }
390         }
391     }
392
393     /// Checks that `t1 <: t2` is false (this may register additional
394     /// region checks).
395     pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
396         match self.sub().relate(&t1, &t2) {
397             Err(_) => { }
398             Ok(_) => {
399                 panic!("unexpected success computing sub({},{})",
400                        t1.repr(self.infcx.tcx),
401                        t2.repr(self.infcx.tcx));
402             }
403         }
404     }
405
406     /// Checks that `LUB(t1,t2) == t_lub`
407     pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
408         match self.lub().relate(&t1, &t2) {
409             Ok(t) => {
410                 self.assert_eq(t, t_lub);
411             }
412             Err(ref e) => {
413                 panic!("unexpected error in LUB: {}",
414                       ty::type_err_to_str(self.infcx.tcx, e))
415             }
416         }
417     }
418
419     /// Checks that `GLB(t1,t2) == t_glb`
420     pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
421         debug!("check_glb(t1={}, t2={}, t_glb={})",
422                self.ty_to_string(t1),
423                self.ty_to_string(t2),
424                self.ty_to_string(t_glb));
425         match self.glb().relate(&t1, &t2) {
426             Err(e) => {
427                 panic!("unexpected error computing LUB: {:?}", e)
428             }
429             Ok(t) => {
430                 self.assert_eq(t, t_glb);
431
432                 // sanity check for good measure:
433                 self.assert_subtype(t, t1);
434                 self.assert_subtype(t, t2);
435             }
436         }
437     }
438 }
439
440 #[test]
441 fn contravariant_region_ptr_ok() {
442     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
443         env.create_simple_region_hierarchy();
444         let t_rptr1 = env.t_rptr_scope(1);
445         let t_rptr10 = env.t_rptr_scope(10);
446         env.assert_eq(t_rptr1, t_rptr1);
447         env.assert_eq(t_rptr10, t_rptr10);
448         env.make_subtype(t_rptr1, t_rptr10);
449     })
450 }
451
452 #[test]
453 fn contravariant_region_ptr_err() {
454     test_env(EMPTY_SOURCE_STR,
455              errors(&["lifetime mismatch"]),
456              |env| {
457                  env.create_simple_region_hierarchy();
458                  let t_rptr1 = env.t_rptr_scope(1);
459                  let t_rptr10 = env.t_rptr_scope(10);
460                  env.assert_eq(t_rptr1, t_rptr1);
461                  env.assert_eq(t_rptr10, t_rptr10);
462
463                  // will cause an error when regions are resolved
464                  env.make_subtype(t_rptr10, t_rptr1);
465              })
466 }
467
468 #[test]
469 fn sub_free_bound_false() {
470     //! Test that:
471     //!
472     //!     fn(&'a isize) <: for<'b> fn(&'b isize)
473     //!
474     //! does NOT hold.
475
476     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
477         let t_rptr_free1 = env.t_rptr_free(0, 1);
478         let t_rptr_bound1 = env.t_rptr_late_bound(1);
479         env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
480                           env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
481     })
482 }
483
484 #[test]
485 fn sub_bound_free_true() {
486     //! Test that:
487     //!
488     //!     for<'a> fn(&'a isize) <: fn(&'b isize)
489     //!
490     //! DOES hold.
491
492     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
493         let t_rptr_bound1 = env.t_rptr_late_bound(1);
494         let t_rptr_free1 = env.t_rptr_free(0, 1);
495         env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
496                       env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
497     })
498 }
499
500 #[test]
501 fn sub_free_bound_false_infer() {
502     //! Test that:
503     //!
504     //!     fn(_#1) <: for<'b> fn(&'b isize)
505     //!
506     //! does NOT hold for any instantiation of `_#1`.
507
508     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
509         let t_infer1 = env.infcx.next_ty_var();
510         let t_rptr_bound1 = env.t_rptr_late_bound(1);
511         env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
512                           env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
513     })
514 }
515
516 #[test]
517 fn lub_free_bound_infer() {
518     //! Test result of:
519     //!
520     //!     LUB(fn(_#1), for<'b> fn(&'b isize))
521     //!
522     //! This should yield `fn(&'_ isize)`. We check
523     //! that it yields `fn(&'x isize)` for some free `'x`,
524     //! anyhow.
525
526     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
527         let t_infer1 = env.infcx.next_ty_var();
528         let t_rptr_bound1 = env.t_rptr_late_bound(1);
529         let t_rptr_free1 = env.t_rptr_free(0, 1);
530         env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
531                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
532                       env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
533     });
534 }
535
536 #[test]
537 fn lub_bound_bound() {
538     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
539         let t_rptr_bound1 = env.t_rptr_late_bound(1);
540         let t_rptr_bound2 = env.t_rptr_late_bound(2);
541         env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
542                       env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
543                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
544     })
545 }
546
547 #[test]
548 fn lub_bound_free() {
549     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
550         let t_rptr_bound1 = env.t_rptr_late_bound(1);
551         let t_rptr_free1 = env.t_rptr_free(0, 1);
552         env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
553                       env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
554                       env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
555     })
556 }
557
558 #[test]
559 fn lub_bound_static() {
560     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
561         let t_rptr_bound1 = env.t_rptr_late_bound(1);
562         let t_rptr_static = env.t_rptr_static();
563         env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
564                       env.t_fn(&[t_rptr_static], env.tcx().types.isize),
565                       env.t_fn(&[t_rptr_static], env.tcx().types.isize));
566     })
567 }
568
569 #[test]
570 fn lub_bound_bound_inverse_order() {
571     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
572         let t_rptr_bound1 = env.t_rptr_late_bound(1);
573         let t_rptr_bound2 = env.t_rptr_late_bound(2);
574         env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
575                       env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
576                       env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
577     })
578 }
579
580 #[test]
581 fn lub_free_free() {
582     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
583         let t_rptr_free1 = env.t_rptr_free(0, 1);
584         let t_rptr_free2 = env.t_rptr_free(0, 2);
585         let t_rptr_static = env.t_rptr_static();
586         env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
587                       env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
588                       env.t_fn(&[t_rptr_static], env.tcx().types.isize));
589     })
590 }
591
592 #[test]
593 fn lub_returning_scope() {
594     test_env(EMPTY_SOURCE_STR,
595              errors(&["cannot infer an appropriate lifetime"]), |env| {
596                  env.create_simple_region_hierarchy();
597                  let t_rptr_scope10 = env.t_rptr_scope(10);
598                  let t_rptr_scope11 = env.t_rptr_scope(11);
599
600                  // this should generate an error when regions are resolved
601                  env.make_lub_ty(env.t_fn(&[], t_rptr_scope10),
602                                  env.t_fn(&[], t_rptr_scope11));
603              })
604 }
605
606 #[test]
607 fn glb_free_free_with_common_scope() {
608     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
609         let t_rptr_free1 = env.t_rptr_free(0, 1);
610         let t_rptr_free2 = env.t_rptr_free(0, 2);
611         let t_rptr_scope = env.t_rptr_scope(0);
612         env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
613                       env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
614                       env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
615     })
616 }
617
618 #[test]
619 fn glb_bound_bound() {
620     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
621         let t_rptr_bound1 = env.t_rptr_late_bound(1);
622         let t_rptr_bound2 = env.t_rptr_late_bound(2);
623         env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
624                       env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
625                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
626     })
627 }
628
629 #[test]
630 fn glb_bound_free() {
631     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
632         let t_rptr_bound1 = env.t_rptr_late_bound(1);
633         let t_rptr_free1 = env.t_rptr_free(0, 1);
634         env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
635                       env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
636                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
637     })
638 }
639
640 #[test]
641 fn glb_bound_free_infer() {
642     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
643         let t_rptr_bound1 = env.t_rptr_late_bound(1);
644         let t_infer1 = env.infcx.next_ty_var();
645
646         // compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
647         // which should yield for<'b> fn(&'b isize) -> isize
648         env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
649                       env.t_fn(&[t_infer1], env.tcx().types.isize),
650                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
651
652         // as a side-effect, computing GLB should unify `_` with
653         // `&'_ isize`
654         let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
655         match t_resolve1.sty {
656             ty::ty_rptr(..) => { }
657             _ => { panic!("t_resolve1={}", t_resolve1.repr(env.infcx.tcx)); }
658         }
659     })
660 }
661
662 #[test]
663 fn glb_bound_static() {
664     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
665         let t_rptr_bound1 = env.t_rptr_late_bound(1);
666         let t_rptr_static = env.t_rptr_static();
667         env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
668                       env.t_fn(&[t_rptr_static], env.tcx().types.isize),
669                       env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
670     })
671 }
672
673 /// Test substituting a bound region into a function, which introduces another level of binding.
674 /// This requires adjusting the Debruijn index.
675 #[test]
676 fn subst_ty_renumber_bound() {
677
678     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
679         // Situation:
680         // Theta = [A -> &'a foo]
681
682         let t_rptr_bound1 = env.t_rptr_late_bound(1);
683
684         // t_source = fn(A)
685         let t_source = {
686             let t_param = env.t_param(subst::TypeSpace, 0);
687             env.t_fn(&[t_param], env.t_nil())
688         };
689
690         let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
691         let t_substituted = t_source.subst(env.infcx.tcx, &substs);
692
693         // t_expected = fn(&'a isize)
694         let t_expected = {
695             let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
696             env.t_fn(&[t_ptr_bound2], env.t_nil())
697         };
698
699         debug!("subst_bound: t_source={} substs={} t_substituted={} t_expected={}",
700                t_source.repr(env.infcx.tcx),
701                substs.repr(env.infcx.tcx),
702                t_substituted.repr(env.infcx.tcx),
703                t_expected.repr(env.infcx.tcx));
704
705         assert_eq!(t_substituted, t_expected);
706     })
707 }
708
709 /// Test substituting a bound region into a function, which introduces another level of binding.
710 /// This requires adjusting the Debruijn index.
711 #[test]
712 fn subst_ty_renumber_some_bounds() {
713     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
714         // Situation:
715         // Theta = [A -> &'a foo]
716
717         let t_rptr_bound1 = env.t_rptr_late_bound(1);
718
719         // t_source = (A, fn(A))
720         let t_source = {
721             let t_param = env.t_param(subst::TypeSpace, 0);
722             env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
723         };
724
725         let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
726         let t_substituted = t_source.subst(env.infcx.tcx, &substs);
727
728         // t_expected = (&'a isize, fn(&'a isize))
729         //
730         // but not that the Debruijn index is different in the different cases.
731         let t_expected = {
732             let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
733             env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
734         };
735
736         debug!("subst_bound: t_source={} substs={} t_substituted={} t_expected={}",
737                t_source.repr(env.infcx.tcx),
738                substs.repr(env.infcx.tcx),
739                t_substituted.repr(env.infcx.tcx),
740                t_expected.repr(env.infcx.tcx));
741
742         assert_eq!(t_substituted, t_expected);
743     })
744 }
745
746 /// Test that we correctly compute whether a type has escaping regions or not.
747 #[test]
748 fn escaping() {
749
750     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
751         // Situation:
752         // Theta = [A -> &'a foo]
753
754         assert!(!ty::type_has_escaping_regions(env.t_nil()));
755
756         let t_rptr_free1 = env.t_rptr_free(0, 1);
757         assert!(!ty::type_has_escaping_regions(t_rptr_free1));
758
759         let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
760         assert!(ty::type_has_escaping_regions(t_rptr_bound1));
761
762         let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
763         assert!(ty::type_has_escaping_regions(t_rptr_bound2));
764
765         // t_fn = fn(A)
766         let t_param = env.t_param(subst::TypeSpace, 0);
767         assert!(!ty::type_has_escaping_regions(t_param));
768         let t_fn = env.t_fn(&[t_param], env.t_nil());
769         assert!(!ty::type_has_escaping_regions(t_fn));
770     })
771 }
772
773 /// Test applying a substitution where the value being substituted for an early-bound region is a
774 /// late-bound region.
775 #[test]
776 fn subst_region_renumber_region() {
777     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
778         let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
779
780         // type t_source<'a> = fn(&'a isize)
781         let t_source = {
782             let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
783             env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
784         };
785
786         let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
787         let t_substituted = t_source.subst(env.infcx.tcx, &substs);
788
789         // t_expected = fn(&'a isize)
790         //
791         // but not that the Debruijn index is different in the different cases.
792         let t_expected = {
793             let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
794             env.t_fn(&[t_rptr_bound2], env.t_nil())
795         };
796
797         debug!("subst_bound: t_source={} substs={} t_substituted={} t_expected={}",
798                t_source.repr(env.infcx.tcx),
799                substs.repr(env.infcx.tcx),
800                t_substituted.repr(env.infcx.tcx),
801                t_expected.repr(env.infcx.tcx));
802
803         assert_eq!(t_substituted, t_expected);
804     })
805 }
806
807 #[test]
808 fn walk_ty() {
809     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
810         let tcx = env.infcx.tcx;
811         let int_ty = tcx.types.isize;
812         let uint_ty = tcx.types.usize;
813         let tup1_ty = ty::mk_tup(tcx, vec!(int_ty, uint_ty, int_ty, uint_ty));
814         let tup2_ty = ty::mk_tup(tcx, vec!(tup1_ty, tup1_ty, uint_ty));
815         let uniq_ty = ty::mk_uniq(tcx, tup2_ty);
816         let walked: Vec<_> = uniq_ty.walk().collect();
817         assert_eq!(walked, [uniq_ty,
818                             tup2_ty,
819                             tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
820                             tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
821                             uint_ty]);
822     })
823 }
824
825 #[test]
826 fn walk_ty_skip_subtree() {
827     test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
828         let tcx = env.infcx.tcx;
829         let int_ty = tcx.types.isize;
830         let uint_ty = tcx.types.usize;
831         let tup1_ty = ty::mk_tup(tcx, vec!(int_ty, uint_ty, int_ty, uint_ty));
832         let tup2_ty = ty::mk_tup(tcx, vec!(tup1_ty, tup1_ty, uint_ty));
833         let uniq_ty = ty::mk_uniq(tcx, tup2_ty);
834
835         // types we expect to see (in order), plus a boolean saying
836         // whether to skip the subtree.
837         let mut expected = vec!((uniq_ty, false),
838                                 (tup2_ty, false),
839                                 (tup1_ty, false),
840                                 (int_ty, false),
841                                 (uint_ty, false),
842                                 (int_ty, false),
843                                 (uint_ty, false),
844                                 (tup1_ty, true), // skip the isize/usize/isize/usize
845                                 (uint_ty, false));
846         expected.reverse();
847
848         let mut walker = uniq_ty.walk();
849         while let Some(t) = walker.next() {
850             debug!("walked to {:?}", t);
851             let (expected_ty, skip) = expected.pop().unwrap();
852             assert_eq!(t, expected_ty);
853             if skip { walker.skip_current_subtree(); }
854         }
855
856         assert!(expected.is_empty());
857     })
858 }