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::free_region::FreeRegionMap;
20 use rustc_typeck::middle::region::{self, CodeExtent, DestructionScopeData};
21 use rustc_typeck::middle::resolve_lifetime;
22 use rustc_typeck::middle::stability;
23 use rustc_typeck::middle::subst;
24 use rustc_typeck::middle::subst::Subst;
25 use rustc_typeck::middle::ty::{self, Ty, RegionEscape};
26 use rustc_typeck::middle::ty_relate::TypeRelation;
27 use rustc_typeck::middle::infer;
28 use rustc_typeck::middle::infer::lub::Lub;
29 use rustc_typeck::middle::infer::glb::Glb;
30 use rustc_typeck::middle::infer::sub::Sub;
32 use rustc::session::{self,config};
33 use syntax::{abi, ast};
35 use syntax::codemap::{Span, CodeMap, DUMMY_SP};
36 use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
37 use syntax::parse::token;
38 use syntax::feature_gate::UnstableFeatures;
40 struct Env<'a, 'tcx: 'a> {
41 infcx: &'a infer::InferCtxt<'a, 'tcx>,
49 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_std)] #![no_std]";
51 struct ExpectErrorEmitter {
55 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
57 Bug | Fatal | Error => { }
58 Warning | Note | Help => { return; }
61 debug!("Error: {}", msg);
62 match e.messages.iter().position(|m| msg.contains(m)) {
67 panic!("Unexpected error: {} Expected: {:?}",
73 impl Emitter for ExpectErrorEmitter {
75 _cmsp: Option<(&codemap::CodeMap, Span)>,
80 remove_message(self, msg, lvl);
83 fn custom_emit(&mut self,
84 _cm: &codemap::CodeMap,
89 remove_message(self, msg, lvl);
93 fn errors(msgs: &[&str]) -> (Box<Emitter+Send>, usize) {
94 let v = msgs.iter().map(|m| m.to_string()).collect();
95 (box ExpectErrorEmitter { messages: v } as Box<Emitter+Send>, msgs.len())
98 fn test_env<F>(source_string: &str,
99 (emitter, expected_err_count): (Box<Emitter+Send>, usize),
104 config::basic_options();
105 options.debugging_opts.verbose = true;
106 options.unstable_features = UnstableFeatures::Allow;
109 let diagnostic_handler =
110 diagnostic::Handler::with_emitter(true, emitter);
111 let span_diagnostic_handler =
112 diagnostic::SpanHandler::new(diagnostic_handler, codemap);
114 let sess = session::build_session_(options, None, span_diagnostic_handler);
115 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
116 let krate_config = Vec::new();
117 let input = config::Input::Str(source_string.to_string());
118 let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
119 let krate = driver::phase_2_configure_and_expand(&sess, krate, "test", None)
120 .expect("phase 2 aborted");
122 let mut forest = ast_map::Forest::new(krate);
123 let arenas = ty::CtxtArenas::new();
124 let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
125 let krate = ast_map.krate();
127 // run just enough stuff to build a tcx:
128 let lang_items = lang_items::collect_language_items(krate, &sess);
129 let resolve::CrateMap { def_map, freevars, .. } =
130 resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
131 let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map);
132 let region_map = region::resolve_crate(&sess, krate);
133 ty::ctxt::create_and_enter(sess,
141 stability::Index::new(krate),
143 let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, None, false);
144 body(Env { infcx: &infcx });
145 let free_regions = FreeRegionMap::new();
146 infcx.resolve_regions_and_report_errors(&free_regions, ast::CRATE_NODE_ID);
147 assert_eq!(tcx.sess.err_count(), expected_err_count);
151 impl<'a, 'tcx> Env<'a, 'tcx> {
152 pub fn tcx(&self) -> &ty::ctxt<'tcx> {
156 pub fn create_region_hierarchy(&self, rh: &RH) {
157 for child_rh in rh.sub {
158 self.create_region_hierarchy(child_rh);
159 self.infcx.tcx.region_maps.record_encl_scope(
160 CodeExtent::from_node_id(child_rh.id),
161 CodeExtent::from_node_id(rh.id));
165 pub fn create_simple_region_hierarchy(&self) {
166 // creates a region hierarchy where 1 is root, 10 and 11 are
167 // children of 1, etc
168 self.create_region_hierarchy(
176 #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
177 pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
178 return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
181 panic!("no item found: `{}`", names.join("::"));
185 fn search_mod(this: &Env,
189 -> Option<ast::NodeId> {
190 assert!(idx < names.len());
191 for item in &m.items {
192 if item.ident.to_string() == names[idx] {
193 return search(this, &**item, idx+1, names);
199 fn search(this: &Env,
203 -> Option<ast::NodeId> {
204 if idx == names.len() {
208 return match it.node {
209 ast::ItemUse(..) | ast::ItemExternCrate(..) |
210 ast::ItemConst(..) | ast::ItemStatic(..) | ast::ItemFn(..) |
211 ast::ItemForeignMod(..) | ast::ItemTy(..) => {
215 ast::ItemEnum(..) | ast::ItemStruct(..) |
216 ast::ItemTrait(..) | ast::ItemImpl(..) |
217 ast::ItemMac(..) | ast::ItemDefaultImpl(..) => {
221 ast::ItemMod(ref m) => {
222 search_mod(this, m, idx, names)
228 pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
229 match infer::mk_subty(self.infcx, true, infer::Misc(DUMMY_SP), a, b) {
231 Err(ref e) => panic!("Encountered error: {}", e)
235 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
236 match infer::can_mk_subty(self.infcx, a, b) {
242 pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
243 if !self.is_subtype(a, b) {
244 panic!("{} is not a subtype of {}, but it should be", a, b);
248 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
249 self.assert_subtype(a, b);
250 self.assert_subtype(b, a);
254 input_tys: &[Ty<'tcx>],
258 let input_args = input_tys.iter().cloned().collect();
259 self.infcx.tcx.mk_fn(None,
260 self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
261 unsafety: ast::Unsafety::Normal,
263 sig: ty::Binder(ty::FnSig {
265 output: ty::FnConverging(output_ty),
271 pub fn t_nil(&self) -> Ty<'tcx> {
272 self.infcx.tcx.mk_nil()
275 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
276 self.infcx.tcx.mk_tup(vec![ty1, ty2])
279 pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
280 let name = format!("T{}", index);
281 self.infcx.tcx.mk_param(space, index, token::intern(&name[..]))
284 pub fn re_early_bound(&self,
285 space: subst::ParamSpace,
290 let name = token::intern(name);
291 ty::ReEarlyBound(ty::EarlyBoundRegion {
292 param_id: ast::DUMMY_NODE_ID,
299 pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
300 ty::ReLateBound(debruijn, ty::BrAnon(id))
303 pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
304 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r),
305 self.tcx().types.isize)
308 pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
309 let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
310 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r),
311 self.tcx().types.isize)
314 pub fn t_rptr_late_bound_with_debruijn(&self,
316 debruijn: ty::DebruijnIndex)
318 let r = self.re_late_bound_with_debruijn(id, debruijn);
319 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r),
320 self.tcx().types.isize)
323 pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
324 let r = ty::ReScope(CodeExtent::from_node_id(id));
325 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r),
326 self.tcx().types.isize)
329 pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
330 ty::ReFree(ty::FreeRegion { scope: DestructionScopeData::new(nid),
331 bound_region: ty::BrAnon(id)})
334 pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
335 let r = self.re_free(nid, id);
336 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r),
337 self.tcx().types.isize)
340 pub fn t_rptr_static(&self) -> Ty<'tcx> {
341 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReStatic),
342 self.tcx().types.isize)
345 pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
346 infer::TypeTrace::dummy(self.tcx())
349 pub fn sub(&self) -> Sub<'a, 'tcx> {
350 let trace = self.dummy_type_trace();
351 self.infcx.sub(true, trace)
354 pub fn lub(&self) -> Lub<'a, 'tcx> {
355 let trace = self.dummy_type_trace();
356 self.infcx.lub(true, trace)
359 pub fn glb(&self) -> Glb<'a, 'tcx> {
360 let trace = self.dummy_type_trace();
361 self.infcx.glb(true, trace)
364 pub fn make_lub_ty(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> Ty<'tcx> {
365 match self.lub().relate(&t1, &t2) {
367 Err(ref e) => panic!("unexpected error computing LUB: {}", e)
371 /// Checks that `t1 <: t2` is true (this may register additional
373 pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
374 match self.sub().relate(&t1, &t2) {
377 panic!("unexpected error computing sub({:?},{:?}): {}",
385 /// Checks that `t1 <: t2` is false (this may register additional
387 pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
388 match self.sub().relate(&t1, &t2) {
391 panic!("unexpected success computing sub({:?},{:?})",
398 /// Checks that `LUB(t1,t2) == t_lub`
399 pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
400 match self.lub().relate(&t1, &t2) {
402 self.assert_eq(t, t_lub);
405 panic!("unexpected error in LUB: {}", e)
410 /// Checks that `GLB(t1,t2) == t_glb`
411 pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
412 debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
413 match self.glb().relate(&t1, &t2) {
415 panic!("unexpected error computing LUB: {:?}", e)
418 self.assert_eq(t, t_glb);
420 // sanity check for good measure:
421 self.assert_subtype(t, t1);
422 self.assert_subtype(t, t2);
429 fn contravariant_region_ptr_ok() {
430 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
431 env.create_simple_region_hierarchy();
432 let t_rptr1 = env.t_rptr_scope(1);
433 let t_rptr10 = env.t_rptr_scope(10);
434 env.assert_eq(t_rptr1, t_rptr1);
435 env.assert_eq(t_rptr10, t_rptr10);
436 env.make_subtype(t_rptr1, t_rptr10);
441 fn contravariant_region_ptr_err() {
442 test_env(EMPTY_SOURCE_STR,
443 errors(&["lifetime mismatch"]),
445 env.create_simple_region_hierarchy();
446 let t_rptr1 = env.t_rptr_scope(1);
447 let t_rptr10 = env.t_rptr_scope(10);
448 env.assert_eq(t_rptr1, t_rptr1);
449 env.assert_eq(t_rptr10, t_rptr10);
451 // will cause an error when regions are resolved
452 env.make_subtype(t_rptr10, t_rptr1);
457 fn sub_free_bound_false() {
460 //! fn(&'a isize) <: for<'b> fn(&'b isize)
464 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
465 let t_rptr_free1 = env.t_rptr_free(0, 1);
466 let t_rptr_bound1 = env.t_rptr_late_bound(1);
467 env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
468 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
473 fn sub_bound_free_true() {
476 //! for<'a> fn(&'a isize) <: fn(&'b isize)
480 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
481 let t_rptr_bound1 = env.t_rptr_late_bound(1);
482 let t_rptr_free1 = env.t_rptr_free(0, 1);
483 env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
484 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
489 fn sub_free_bound_false_infer() {
492 //! fn(_#1) <: for<'b> fn(&'b isize)
494 //! does NOT hold for any instantiation of `_#1`.
496 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
497 let t_infer1 = env.infcx.next_ty_var();
498 let t_rptr_bound1 = env.t_rptr_late_bound(1);
499 env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
500 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
505 fn lub_free_bound_infer() {
508 //! LUB(fn(_#1), for<'b> fn(&'b isize))
510 //! This should yield `fn(&'_ isize)`. We check
511 //! that it yields `fn(&'x isize)` for some free `'x`,
514 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
515 let t_infer1 = env.infcx.next_ty_var();
516 let t_rptr_bound1 = env.t_rptr_late_bound(1);
517 let t_rptr_free1 = env.t_rptr_free(0, 1);
518 env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
519 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
520 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
525 fn lub_bound_bound() {
526 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
527 let t_rptr_bound1 = env.t_rptr_late_bound(1);
528 let t_rptr_bound2 = env.t_rptr_late_bound(2);
529 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
530 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
531 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
536 fn lub_bound_free() {
537 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
538 let t_rptr_bound1 = env.t_rptr_late_bound(1);
539 let t_rptr_free1 = env.t_rptr_free(0, 1);
540 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
541 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
542 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
547 fn lub_bound_static() {
548 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
549 let t_rptr_bound1 = env.t_rptr_late_bound(1);
550 let t_rptr_static = env.t_rptr_static();
551 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
552 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
553 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
558 fn lub_bound_bound_inverse_order() {
559 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
560 let t_rptr_bound1 = env.t_rptr_late_bound(1);
561 let t_rptr_bound2 = env.t_rptr_late_bound(2);
562 env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
563 env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
564 env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
570 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
571 let t_rptr_free1 = env.t_rptr_free(0, 1);
572 let t_rptr_free2 = env.t_rptr_free(0, 2);
573 let t_rptr_static = env.t_rptr_static();
574 env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
575 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
576 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
581 fn lub_returning_scope() {
582 test_env(EMPTY_SOURCE_STR,
583 errors(&["cannot infer an appropriate lifetime"]), |env| {
584 env.create_simple_region_hierarchy();
585 let t_rptr_scope10 = env.t_rptr_scope(10);
586 let t_rptr_scope11 = env.t_rptr_scope(11);
588 // this should generate an error when regions are resolved
589 env.make_lub_ty(env.t_fn(&[], t_rptr_scope10),
590 env.t_fn(&[], t_rptr_scope11));
595 fn glb_free_free_with_common_scope() {
596 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
597 let t_rptr_free1 = env.t_rptr_free(0, 1);
598 let t_rptr_free2 = env.t_rptr_free(0, 2);
599 let t_rptr_scope = env.t_rptr_scope(0);
600 env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
601 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
602 env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
607 fn glb_bound_bound() {
608 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
609 let t_rptr_bound1 = env.t_rptr_late_bound(1);
610 let t_rptr_bound2 = env.t_rptr_late_bound(2);
611 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
612 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
613 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
618 fn glb_bound_free() {
619 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
620 let t_rptr_bound1 = env.t_rptr_late_bound(1);
621 let t_rptr_free1 = env.t_rptr_free(0, 1);
622 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
623 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
624 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
629 fn glb_bound_free_infer() {
630 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
631 let t_rptr_bound1 = env.t_rptr_late_bound(1);
632 let t_infer1 = env.infcx.next_ty_var();
634 // compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
635 // which should yield for<'b> fn(&'b isize) -> isize
636 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
637 env.t_fn(&[t_infer1], env.tcx().types.isize),
638 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
640 // as a side-effect, computing GLB should unify `_` with
642 let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
643 match t_resolve1.sty {
645 _ => { panic!("t_resolve1={:?}", t_resolve1); }
651 fn glb_bound_static() {
652 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
653 let t_rptr_bound1 = env.t_rptr_late_bound(1);
654 let t_rptr_static = env.t_rptr_static();
655 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
656 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
657 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
661 /// Test substituting a bound region into a function, which introduces another level of binding.
662 /// This requires adjusting the Debruijn index.
664 fn subst_ty_renumber_bound() {
666 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
668 // Theta = [A -> &'a foo]
670 let t_rptr_bound1 = env.t_rptr_late_bound(1);
674 let t_param = env.t_param(subst::TypeSpace, 0);
675 env.t_fn(&[t_param], env.t_nil())
678 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
679 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
681 // t_expected = fn(&'a isize)
683 let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
684 env.t_fn(&[t_ptr_bound2], env.t_nil())
687 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
693 assert_eq!(t_substituted, t_expected);
697 /// Test substituting a bound region into a function, which introduces another level of binding.
698 /// This requires adjusting the Debruijn index.
700 fn subst_ty_renumber_some_bounds() {
701 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
703 // Theta = [A -> &'a foo]
705 let t_rptr_bound1 = env.t_rptr_late_bound(1);
707 // t_source = (A, fn(A))
709 let t_param = env.t_param(subst::TypeSpace, 0);
710 env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
713 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
714 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
716 // t_expected = (&'a isize, fn(&'a isize))
718 // but not that the Debruijn index is different in the different cases.
720 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
721 env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
724 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
730 assert_eq!(t_substituted, t_expected);
734 /// Test that we correctly compute whether a type has escaping regions or not.
738 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
740 // Theta = [A -> &'a foo]
742 assert!(!env.t_nil().has_escaping_regions());
744 let t_rptr_free1 = env.t_rptr_free(0, 1);
745 assert!(!t_rptr_free1.has_escaping_regions());
747 let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
748 assert!(t_rptr_bound1.has_escaping_regions());
750 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
751 assert!(t_rptr_bound2.has_escaping_regions());
754 let t_param = env.t_param(subst::TypeSpace, 0);
755 assert!(!t_param.has_escaping_regions());
756 let t_fn = env.t_fn(&[t_param], env.t_nil());
757 assert!(!t_fn.has_escaping_regions());
761 /// Test applying a substitution where the value being substituted for an early-bound region is a
762 /// late-bound region.
764 fn subst_region_renumber_region() {
765 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
766 let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
768 // type t_source<'a> = fn(&'a isize)
770 let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
771 env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
774 let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
775 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
777 // t_expected = fn(&'a isize)
779 // but not that the Debruijn index is different in the different cases.
781 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
782 env.t_fn(&[t_rptr_bound2], env.t_nil())
785 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
791 assert_eq!(t_substituted, t_expected);
797 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
798 let tcx = env.infcx.tcx;
799 let int_ty = tcx.types.isize;
800 let uint_ty = tcx.types.usize;
801 let tup1_ty = tcx.mk_tup(vec!(int_ty, uint_ty, int_ty, uint_ty));
802 let tup2_ty = tcx.mk_tup(vec!(tup1_ty, tup1_ty, uint_ty));
803 let uniq_ty = tcx.mk_box(tup2_ty);
804 let walked: Vec<_> = uniq_ty.walk().collect();
805 assert_eq!(walked, [uniq_ty,
807 tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
808 tup1_ty, int_ty, uint_ty, int_ty, uint_ty,
814 fn walk_ty_skip_subtree() {
815 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
816 let tcx = env.infcx.tcx;
817 let int_ty = tcx.types.isize;
818 let uint_ty = tcx.types.usize;
819 let tup1_ty = tcx.mk_tup(vec!(int_ty, uint_ty, int_ty, uint_ty));
820 let tup2_ty = tcx.mk_tup(vec!(tup1_ty, tup1_ty, uint_ty));
821 let uniq_ty = tcx.mk_box(tup2_ty);
823 // types we expect to see (in order), plus a boolean saying
824 // whether to skip the subtree.
825 let mut expected = vec!((uniq_ty, false),
832 (tup1_ty, true), // skip the isize/usize/isize/usize
836 let mut walker = uniq_ty.walk();
837 while let Some(t) = walker.next() {
838 debug!("walked to {:?}", t);
839 let (expected_ty, skip) = expected.pop().unwrap();
840 assert_eq!(t, expected_ty);
841 if skip { walker.skip_current_subtree(); }
844 assert!(expected.is_empty());