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 //! # Standalone Tests for the Inference Module
14 use diagnostic::Emitter;
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};
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;
38 struct Env<'a, 'tcx: 'a> {
39 infcx: &'a infer::InferCtxt<'a, 'tcx>,
47 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_std)] #![no_std]";
49 struct ExpectErrorEmitter {
53 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
55 Bug | Fatal | Error => { }
56 Warning | Note | Help => { return; }
59 debug!("Error: {}", msg);
60 match e.messages.iter().position(|m| msg.contains(m)) {
65 panic!("Unexpected error: {} Expected: {:?}",
71 impl Emitter for ExpectErrorEmitter {
73 _cmsp: Option<(&codemap::CodeMap, Span)>,
78 remove_message(self, msg, lvl);
81 fn custom_emit(&mut self,
82 _cm: &codemap::CodeMap,
87 remove_message(self, msg, lvl);
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())
96 fn test_env<F>(source_string: &str,
97 (emitter, expected_err_count): (Box<Emitter+Send>, usize),
102 config::basic_options();
103 options.debugging_opts.verbose = true;
106 let diagnostic_handler =
107 diagnostic::mk_handler(true, emitter);
108 let span_diagnostic_handler =
109 diagnostic::mk_span_handler(diagnostic_handler, codemap);
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");
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();
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,
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);
145 impl<'a, 'tcx> Env<'a, 'tcx> {
146 pub fn tcx(&self) -> &ty::ctxt<'tcx> {
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));
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(
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) {
175 panic!("no item found: `{}`", names.connect("::"));
179 fn search_mod(this: &Env,
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);
193 fn search(this: &Env,
197 -> Option<ast::NodeId> {
198 if idx == names.len() {
202 return match it.node {
203 ast::ItemUse(..) | ast::ItemExternCrate(..) |
204 ast::ItemConst(..) | ast::ItemStatic(..) | ast::ItemFn(..) |
205 ast::ItemForeignMod(..) | ast::ItemTy(..) => {
209 ast::ItemEnum(..) | ast::ItemStruct(..) |
210 ast::ItemTrait(..) | ast::ItemImpl(..) |
211 ast::ItemMac(..) | ast::ItemDefaultImpl(..) => {
215 ast::ItemMod(ref m) => {
216 search_mod(this, m, idx, names)
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) {
225 Err(ref e) => panic!("Encountered error: {}",
226 ty::type_err_to_str(self.infcx.tcx, e))
230 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
231 match infer::can_mk_subty(self.infcx, a, b) {
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));
245 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
246 self.assert_subtype(a, b);
247 self.assert_subtype(b, a);
250 pub fn ty_to_string(&self, a: Ty<'tcx>) -> String {
251 ty_to_string(self.infcx.tcx, a)
255 input_tys: &[Ty<'tcx>],
259 let input_args = input_tys.iter().cloned().collect();
260 ty::mk_bare_fn(self.infcx.tcx,
262 self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
263 unsafety: ast::Unsafety::Normal,
265 sig: ty::Binder(ty::FnSig {
267 output: ty::FnConverging(output_ty),
273 pub fn t_nil(&self) -> Ty<'tcx> {
274 ty::mk_nil(self.infcx.tcx)
277 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
278 ty::mk_tup(self.infcx.tcx, vec![ty1, ty2])
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[..]))
286 pub fn re_early_bound(&self,
287 space: subst::ParamSpace,
292 let name = token::intern(name);
293 ty::ReEarlyBound(ty::EarlyBoundRegion {
294 param_id: ast::DUMMY_NODE_ID,
301 pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
302 ty::ReLateBound(debruijn, ty::BrAnon(id))
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)
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)
318 pub fn t_rptr_late_bound_with_debruijn(&self,
320 debruijn: ty::DebruijnIndex)
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)
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)
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)})
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)
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)
352 pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
353 infer::TypeTrace::dummy(self.tcx())
356 pub fn sub(&self) -> Sub<'a, 'tcx> {
357 let trace = self.dummy_type_trace();
358 self.infcx.sub(true, trace)
361 pub fn lub(&self) -> Lub<'a, 'tcx> {
362 let trace = self.dummy_type_trace();
363 self.infcx.lub(true, trace)
366 pub fn glb(&self) -> Glb<'a, 'tcx> {
367 let trace = self.dummy_type_trace();
368 self.infcx.glb(true, trace)
371 pub fn make_lub_ty(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> Ty<'tcx> {
372 match self.lub().relate(&t1, &t2) {
374 Err(ref e) => panic!("unexpected error computing LUB: {}",
375 ty::type_err_to_str(self.infcx.tcx, e))
379 /// Checks that `t1 <: t2` is true (this may register additional
381 pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
382 match self.sub().relate(&t1, &t2) {
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));
393 /// Checks that `t1 <: t2` is false (this may register additional
395 pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
396 match self.sub().relate(&t1, &t2) {
399 panic!("unexpected success computing sub({},{})",
400 t1.repr(self.infcx.tcx),
401 t2.repr(self.infcx.tcx));
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) {
410 self.assert_eq(t, t_lub);
413 panic!("unexpected error in LUB: {}",
414 ty::type_err_to_str(self.infcx.tcx, e))
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) {
427 panic!("unexpected error computing LUB: {:?}", e)
430 self.assert_eq(t, t_glb);
432 // sanity check for good measure:
433 self.assert_subtype(t, t1);
434 self.assert_subtype(t, t2);
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);
453 fn contravariant_region_ptr_err() {
454 test_env(EMPTY_SOURCE_STR,
455 errors(&["lifetime mismatch"]),
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);
463 // will cause an error when regions are resolved
464 env.make_subtype(t_rptr10, t_rptr1);
469 fn sub_free_bound_false() {
472 //! fn(&'a isize) <: for<'b> fn(&'b isize)
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));
485 fn sub_bound_free_true() {
488 //! for<'a> fn(&'a isize) <: fn(&'b isize)
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));
501 fn sub_free_bound_false_infer() {
504 //! fn(_#1) <: for<'b> fn(&'b isize)
506 //! does NOT hold for any instantiation of `_#1`.
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));
517 fn lub_free_bound_infer() {
520 //! LUB(fn(_#1), for<'b> fn(&'b isize))
522 //! This should yield `fn(&'_ isize)`. We check
523 //! that it yields `fn(&'x isize)` for some free `'x`,
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));
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));
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));
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));
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));
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));
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);
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));
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));
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));
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));
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();
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));
652 // as a side-effect, computing GLB should unify `_` with
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)); }
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));
673 /// Test substituting a bound region into a function, which introduces another level of binding.
674 /// This requires adjusting the Debruijn index.
676 fn subst_ty_renumber_bound() {
678 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
680 // Theta = [A -> &'a foo]
682 let t_rptr_bound1 = env.t_rptr_late_bound(1);
686 let t_param = env.t_param(subst::TypeSpace, 0);
687 env.t_fn(&[t_param], env.t_nil())
690 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
691 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
693 // t_expected = fn(&'a isize)
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())
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));
705 assert_eq!(t_substituted, t_expected);
709 /// Test substituting a bound region into a function, which introduces another level of binding.
710 /// This requires adjusting the Debruijn index.
712 fn subst_ty_renumber_some_bounds() {
713 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
715 // Theta = [A -> &'a foo]
717 let t_rptr_bound1 = env.t_rptr_late_bound(1);
719 // t_source = (A, fn(A))
721 let t_param = env.t_param(subst::TypeSpace, 0);
722 env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
725 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
726 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
728 // t_expected = (&'a isize, fn(&'a isize))
730 // but not that the Debruijn index is different in the different cases.
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()))
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));
742 assert_eq!(t_substituted, t_expected);
746 /// Test that we correctly compute whether a type has escaping regions or not.
750 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
752 // Theta = [A -> &'a foo]
754 assert!(!ty::type_has_escaping_regions(env.t_nil()));
756 let t_rptr_free1 = env.t_rptr_free(0, 1);
757 assert!(!ty::type_has_escaping_regions(t_rptr_free1));
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));
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));
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));
773 /// Test applying a substitution where the value being substituted for an early-bound region is a
774 /// late-bound region.
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));
780 // type t_source<'a> = fn(&'a isize)
782 let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
783 env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
786 let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
787 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
789 // t_expected = fn(&'a isize)
791 // but not that the Debruijn index is different in the different cases.
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())
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));
803 assert_eq!(t_substituted, t_expected);
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,
819 tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
820 tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
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);
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),
844 (tup1_ty, true), // skip the isize/usize/isize/usize
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(); }
856 assert!(expected.is_empty());