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 rustc::dep_graph::DepGraph;
16 use rustc_resolve as resolve;
17 use rustc_typeck::middle::lang_items;
18 use rustc_typeck::middle::free_region::FreeRegionMap;
19 use rustc_typeck::middle::region::{self, CodeExtent};
20 use rustc_typeck::middle::region::CodeExtentData;
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::traits::ProjectionMode;
26 use rustc_typeck::middle::ty::{self, Ty, TyCtxt, TypeFoldable};
27 use rustc_typeck::middle::ty::relate::TypeRelation;
28 use rustc_typeck::middle::infer::{self, TypeOrigin};
29 use rustc_typeck::middle::infer::lub::Lub;
30 use rustc_typeck::middle::infer::glb::Glb;
31 use rustc_typeck::middle::infer::sub::Sub;
32 use rustc_metadata::cstore::CStore;
33 use rustc::front::map as hir_map;
34 use rustc::session::{self, config};
38 use syntax::codemap::{MultiSpan, CodeMap, DUMMY_SP};
40 use syntax::errors::emitter::Emitter;
41 use syntax::errors::{Level, RenderSpan};
42 use syntax::parse::token;
43 use syntax::feature_gate::UnstableFeatures;
45 use rustc_front::lowering::{lower_crate, LoweringContext};
48 struct Env<'a, 'tcx: 'a> {
49 infcx: &'a infer::InferCtxt<'a, 'tcx>,
57 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
59 struct ExpectErrorEmitter {
60 messages: Vec<String>,
63 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
65 Level::Bug | Level::Fatal | Level::Error => {}
71 debug!("Error: {}", msg);
72 match e.messages.iter().position(|m| msg.contains(m)) {
77 panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
82 impl Emitter for ExpectErrorEmitter {
84 _sp: Option<&MultiSpan>,
88 remove_message(self, msg, lvl);
91 fn custom_emit(&mut self, _sp: &RenderSpan, msg: &str, lvl: Level) {
92 remove_message(self, msg, lvl);
96 fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
97 let v = msgs.iter().map(|m| m.to_string()).collect();
98 (box ExpectErrorEmitter { messages: v } as Box<Emitter + Send>,
102 fn test_env<F>(source_string: &str,
103 (emitter, expected_err_count): (Box<Emitter + Send>, usize),
107 let mut options = config::basic_options();
108 options.debugging_opts.verbose = true;
109 options.unstable_features = UnstableFeatures::Allow;
110 let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter);
112 let cstore = Rc::new(CStore::new(token::get_ident_interner()));
113 let sess = session::build_session_(options, None, diagnostic_handler,
114 Rc::new(CodeMap::new()), cstore.clone());
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 {
118 name: driver::anon_src(),
119 input: source_string.to_string(),
121 let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
122 let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
123 .expect("phase 2 aborted");
125 let krate = driver::assign_node_ids(&sess, krate);
126 let lcx = LoweringContext::new(&sess, Some(&krate));
127 let dep_graph = DepGraph::new(false);
128 let _ignore = dep_graph.in_ignore();
129 let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph.clone());
130 let arenas = ty::CtxtArenas::new();
131 let ast_map = driver::make_map(&sess, &mut hir_forest);
133 // run just enough stuff to build a tcx:
134 let lang_items = lang_items::collect_language_items(&sess, &ast_map);
135 let resolve::CrateMap { def_map, freevars, .. } =
136 resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
137 let named_region_map = resolve_lifetime::krate(&sess, &ast_map, &def_map.borrow());
138 let region_map = region::resolve_crate(&sess, &ast_map);
139 let index = stability::Index::new(&ast_map);
140 TyCtxt::create_and_enter(&sess,
143 named_region_map.unwrap(),
151 let infcx = infer::new_infer_ctxt(tcx,
154 ProjectionMode::AnyFinal);
155 body(Env { infcx: &infcx });
156 let free_regions = FreeRegionMap::new();
157 infcx.resolve_regions_and_report_errors(&free_regions,
159 assert_eq!(tcx.sess.err_count(), expected_err_count);
163 impl<'a, 'tcx> Env<'a, 'tcx> {
164 pub fn tcx(&self) -> &TyCtxt<'tcx> {
168 pub fn create_region_hierarchy(&self, rh: &RH, parent: CodeExtent) {
169 let me = self.infcx.tcx.region_maps.intern_node(rh.id, parent);
170 for child_rh in rh.sub {
171 self.create_region_hierarchy(child_rh, me);
175 pub fn create_simple_region_hierarchy(&self) {
176 // creates a region hierarchy where 1 is root, 10 and 11 are
177 // children of 1, etc
178 let dscope = self.infcx
181 .intern_code_extent(CodeExtentData::DestructionScope(1),
182 region::ROOT_CODE_EXTENT);
183 self.create_region_hierarchy(&RH {
185 sub: &[RH { id: 10, sub: &[] }, RH { id: 11, sub: &[] }],
190 #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
191 pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
192 return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
195 panic!("no item found: `{}`", names.join("::"));
199 fn search_mod(this: &Env,
203 -> Option<ast::NodeId> {
204 assert!(idx < names.len());
205 for item in &m.item_ids {
206 let item = this.infcx.tcx.map.expect_item(item.id);
207 if item.name.to_string() == names[idx] {
208 return search(this, item, idx + 1, names);
214 fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
215 if idx == names.len() {
219 return match it.node {
221 hir::ItemExternCrate(..) |
223 hir::ItemStatic(..) |
225 hir::ItemForeignMod(..) |
231 hir::ItemStruct(..) |
234 hir::ItemDefaultImpl(..) => {
238 hir::ItemMod(ref m) => {
239 search_mod(this, m, idx, names)
245 pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
246 match infer::mk_subty(self.infcx, true, TypeOrigin::Misc(DUMMY_SP), a, b) {
248 Err(ref e) => panic!("Encountered error: {}", e),
252 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
253 match infer::can_mk_subty(self.infcx, a, b) {
259 pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
260 if !self.is_subtype(a, b) {
261 panic!("{} is not a subtype of {}, but it should be", a, b);
265 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
266 self.assert_subtype(a, b);
267 self.assert_subtype(b, a);
270 pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
271 let input_args = input_tys.iter().cloned().collect();
272 self.infcx.tcx.mk_fn_ptr(ty::BareFnTy {
273 unsafety: hir::Unsafety::Normal,
275 sig: ty::Binder(ty::FnSig {
277 output: ty::FnConverging(output_ty),
283 pub fn t_nil(&self) -> Ty<'tcx> {
284 self.infcx.tcx.mk_nil()
287 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
288 self.infcx.tcx.mk_tup(vec![ty1, ty2])
291 pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
292 let name = format!("T{}", index);
293 self.infcx.tcx.mk_param(space, index, token::intern(&name[..]))
296 pub fn re_early_bound(&self,
297 space: subst::ParamSpace,
301 let name = token::intern(name);
302 ty::ReEarlyBound(ty::EarlyBoundRegion {
309 pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
310 ty::ReLateBound(debruijn, ty::BrAnon(id))
313 pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
314 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
317 pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
318 let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
319 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
322 pub fn t_rptr_late_bound_with_debruijn(&self,
324 debruijn: ty::DebruijnIndex)
326 let r = self.re_late_bound_with_debruijn(id, debruijn);
327 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
330 pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
331 let r = ty::ReScope(self.tcx().region_maps.node_extent(id));
332 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
335 pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
336 ty::ReFree(ty::FreeRegion {
337 scope: self.tcx().region_maps.item_extent(nid),
338 bound_region: ty::BrAnon(id),
342 pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
343 let r = self.re_free(nid, id);
344 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
347 pub fn t_rptr_static(&self) -> Ty<'tcx> {
348 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReStatic),
349 self.tcx().types.isize)
352 pub fn t_rptr_empty(&self) -> Ty<'tcx> {
353 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReEmpty),
354 self.tcx().types.isize)
357 pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
358 infer::TypeTrace::dummy(self.tcx())
361 pub fn sub(&self) -> Sub<'a, 'tcx> {
362 let trace = self.dummy_type_trace();
363 self.infcx.sub(true, trace)
366 pub fn lub(&self) -> Lub<'a, 'tcx> {
367 let trace = self.dummy_type_trace();
368 self.infcx.lub(true, trace)
371 pub fn glb(&self) -> Glb<'a, 'tcx> {
372 let trace = self.dummy_type_trace();
373 self.infcx.glb(true, trace)
376 /// Checks that `t1 <: t2` is true (this may register additional
378 pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
379 match self.sub().relate(&t1, &t2) {
382 panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
387 /// Checks that `t1 <: t2` is false (this may register additional
389 pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
390 match self.sub().relate(&t1, &t2) {
393 panic!("unexpected success computing sub({:?},{:?})", t1, t2);
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, errors(&["lifetime mismatch"]), |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);
449 // will cause an error when regions are resolved
450 env.make_subtype(t_rptr10, t_rptr1);
455 fn sub_free_bound_false() {
458 //! fn(&'a isize) <: for<'b> fn(&'b isize)
462 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
463 env.create_simple_region_hierarchy();
464 let t_rptr_free1 = env.t_rptr_free(1, 1);
465 let t_rptr_bound1 = env.t_rptr_late_bound(1);
466 env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
467 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
472 fn sub_bound_free_true() {
475 //! for<'a> fn(&'a isize) <: fn(&'b isize)
479 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
480 env.create_simple_region_hierarchy();
481 let t_rptr_bound1 = env.t_rptr_late_bound(1);
482 let t_rptr_free1 = env.t_rptr_free(1, 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 env.create_simple_region_hierarchy();
516 let t_infer1 = env.infcx.next_ty_var();
517 let t_rptr_bound1 = env.t_rptr_late_bound(1);
518 let t_rptr_free1 = env.t_rptr_free(1, 1);
519 env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
520 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
521 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
526 fn lub_bound_bound() {
527 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
528 let t_rptr_bound1 = env.t_rptr_late_bound(1);
529 let t_rptr_bound2 = env.t_rptr_late_bound(2);
530 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
531 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
532 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
537 fn lub_bound_free() {
538 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
539 env.create_simple_region_hierarchy();
540 let t_rptr_bound1 = env.t_rptr_late_bound(1);
541 let t_rptr_free1 = env.t_rptr_free(1, 1);
542 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
543 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
544 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
549 fn lub_bound_static() {
550 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
551 let t_rptr_bound1 = env.t_rptr_late_bound(1);
552 let t_rptr_static = env.t_rptr_static();
553 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
554 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
555 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
560 fn lub_bound_bound_inverse_order() {
561 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
562 let t_rptr_bound1 = env.t_rptr_late_bound(1);
563 let t_rptr_bound2 = env.t_rptr_late_bound(2);
564 env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
565 env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
566 env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
572 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
573 env.create_simple_region_hierarchy();
574 let t_rptr_free1 = env.t_rptr_free(1, 1);
575 let t_rptr_free2 = env.t_rptr_free(1, 2);
576 let t_rptr_static = env.t_rptr_static();
577 env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
578 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
579 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
584 fn lub_returning_scope() {
585 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
586 env.create_simple_region_hierarchy();
587 let t_rptr_scope10 = env.t_rptr_scope(10);
588 let t_rptr_scope11 = env.t_rptr_scope(11);
589 let t_rptr_empty = env.t_rptr_empty();
590 env.check_lub(env.t_fn(&[t_rptr_scope10], env.tcx().types.isize),
591 env.t_fn(&[t_rptr_scope11], env.tcx().types.isize),
592 env.t_fn(&[t_rptr_empty], env.tcx().types.isize));
597 fn glb_free_free_with_common_scope() {
598 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
599 env.create_simple_region_hierarchy();
600 let t_rptr_free1 = env.t_rptr_free(1, 1);
601 let t_rptr_free2 = env.t_rptr_free(1, 2);
602 let t_rptr_scope = env.t_rptr_scope(1);
603 env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
604 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
605 env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
610 fn glb_bound_bound() {
611 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
612 let t_rptr_bound1 = env.t_rptr_late_bound(1);
613 let t_rptr_bound2 = env.t_rptr_late_bound(2);
614 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
615 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
616 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
621 fn glb_bound_free() {
622 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
623 env.create_simple_region_hierarchy();
624 let t_rptr_bound1 = env.t_rptr_late_bound(1);
625 let t_rptr_free1 = env.t_rptr_free(1, 1);
626 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
627 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
628 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
633 fn glb_bound_free_infer() {
634 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
635 let t_rptr_bound1 = env.t_rptr_late_bound(1);
636 let t_infer1 = env.infcx.next_ty_var();
638 // compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
639 // which should yield for<'b> fn(&'b isize) -> isize
640 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
641 env.t_fn(&[t_infer1], env.tcx().types.isize),
642 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
644 // as a side-effect, computing GLB should unify `_` with
646 let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
647 match t_resolve1.sty {
650 panic!("t_resolve1={:?}", t_resolve1);
657 fn glb_bound_static() {
658 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
659 let t_rptr_bound1 = env.t_rptr_late_bound(1);
660 let t_rptr_static = env.t_rptr_static();
661 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
662 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
663 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
667 /// Test substituting a bound region into a function, which introduces another level of binding.
668 /// This requires adjusting the Debruijn index.
670 fn subst_ty_renumber_bound() {
672 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
674 // Theta = [A -> &'a foo]
676 let t_rptr_bound1 = env.t_rptr_late_bound(1);
680 let t_param = env.t_param(subst::TypeSpace, 0);
681 env.t_fn(&[t_param], env.t_nil())
684 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
685 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
687 // t_expected = fn(&'a isize)
689 let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
690 env.t_fn(&[t_ptr_bound2], env.t_nil())
693 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
699 assert_eq!(t_substituted, t_expected);
703 /// Test substituting a bound region into a function, which introduces another level of binding.
704 /// This requires adjusting the Debruijn index.
706 fn subst_ty_renumber_some_bounds() {
707 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
709 // Theta = [A -> &'a foo]
711 let t_rptr_bound1 = env.t_rptr_late_bound(1);
713 // t_source = (A, fn(A))
715 let t_param = env.t_param(subst::TypeSpace, 0);
716 env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
719 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
720 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
722 // t_expected = (&'a isize, fn(&'a isize))
724 // but not that the Debruijn index is different in the different cases.
726 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
727 env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
730 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
736 assert_eq!(t_substituted, t_expected);
740 /// Test that we correctly compute whether a type has escaping regions or not.
744 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
746 // Theta = [A -> &'a foo]
747 env.create_simple_region_hierarchy();
749 assert!(!env.t_nil().has_escaping_regions());
751 let t_rptr_free1 = env.t_rptr_free(1, 1);
752 assert!(!t_rptr_free1.has_escaping_regions());
754 let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
755 assert!(t_rptr_bound1.has_escaping_regions());
757 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
758 assert!(t_rptr_bound2.has_escaping_regions());
761 let t_param = env.t_param(subst::TypeSpace, 0);
762 assert!(!t_param.has_escaping_regions());
763 let t_fn = env.t_fn(&[t_param], env.t_nil());
764 assert!(!t_fn.has_escaping_regions());
768 /// Test applying a substitution where the value being substituted for an early-bound region is a
769 /// late-bound region.
771 fn subst_region_renumber_region() {
772 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
773 let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
775 // type t_source<'a> = fn(&'a isize)
777 let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
778 env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
781 let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
782 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
784 // t_expected = fn(&'a isize)
786 // but not that the Debruijn index is different in the different cases.
788 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
789 env.t_fn(&[t_rptr_bound2], env.t_nil())
792 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
798 assert_eq!(t_substituted, t_expected);
804 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
805 let tcx = env.infcx.tcx;
806 let int_ty = tcx.types.isize;
807 let uint_ty = tcx.types.usize;
808 let tup1_ty = tcx.mk_tup(vec![int_ty, uint_ty, int_ty, uint_ty]);
809 let tup2_ty = tcx.mk_tup(vec![tup1_ty, tup1_ty, uint_ty]);
810 let uniq_ty = tcx.mk_box(tup2_ty);
811 let walked: Vec<_> = uniq_ty.walk().collect();
813 [uniq_ty, tup2_ty, tup1_ty, int_ty, uint_ty, int_ty, uint_ty, tup1_ty, int_ty,
814 uint_ty, int_ty, uint_ty, uint_ty]);
819 fn walk_ty_skip_subtree() {
820 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
821 let tcx = env.infcx.tcx;
822 let int_ty = tcx.types.isize;
823 let uint_ty = tcx.types.usize;
824 let tup1_ty = tcx.mk_tup(vec![int_ty, uint_ty, int_ty, uint_ty]);
825 let tup2_ty = tcx.mk_tup(vec![tup1_ty, tup1_ty, uint_ty]);
826 let uniq_ty = tcx.mk_box(tup2_ty);
828 // types we expect to see (in order), plus a boolean saying
829 // whether to skip the subtree.
830 let mut expected = vec![(uniq_ty, false),
837 (tup1_ty, true), // skip the isize/usize/isize/usize
841 let mut walker = uniq_ty.walk();
842 while let Some(t) = walker.next() {
843 debug!("walked to {:?}", t);
844 let (expected_ty, skip) = expected.pop().unwrap();
845 assert_eq!(t, expected_ty);
847 walker.skip_current_subtree();
851 assert!(expected.is_empty());