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
13 use std::path::PathBuf;
18 use rustc_resolve::MakeGlobMap;
20 use rustc::middle::free_region::FreeRegionMap;
21 use rustc::middle::region;
22 use rustc::middle::resolve_lifetime;
23 use rustc::ty::subst::{Kind, Subst};
24 use rustc::traits::{ObligationCause, Reveal};
25 use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
26 use rustc::infer::{self, InferOk, InferResult};
27 use rustc::infer::type_variable::TypeVariableOrigin;
28 use rustc_metadata::cstore::CStore;
29 use rustc::hir::map as hir_map;
30 use rustc::mir::transform::Passes;
31 use rustc::session::{self, config};
32 use rustc::session::config::{OutputFilenames, OutputTypes};
36 use syntax::codemap::{CodeMap, FilePathMapping};
38 use errors::emitter::Emitter;
39 use errors::{Level, DiagnosticBuilder};
40 use syntax::feature_gate::UnstableFeatures;
41 use syntax::symbol::Symbol;
42 use syntax_pos::DUMMY_SP;
43 use arena::DroplessArena;
47 struct Env<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
48 infcx: &'a infer::InferCtxt<'a, 'gcx, 'tcx>,
49 region_scope_tree: &'a mut region::ScopeTree,
50 param_env: ty::ParamEnv<'tcx>,
58 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
60 struct ExpectErrorEmitter {
61 messages: Vec<String>,
64 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
66 Level::Bug | Level::Fatal | Level::Error => {}
72 debug!("Error: {}", msg);
73 match e.messages.iter().position(|m| msg.contains(m)) {
78 debug!("Unexpected error: {} Expected: {:?}", msg, e.messages);
79 panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
84 impl Emitter for ExpectErrorEmitter {
85 fn emit(&mut self, db: &DiagnosticBuilder) {
86 remove_message(self, &db.message(), db.level);
87 for child in &db.children {
88 remove_message(self, &child.message(), child.level);
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),
103 let mut options = config::basic_options();
104 options.debugging_opts.verbose = true;
105 options.unstable_features = UnstableFeatures::Allow;
106 let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter);
108 let cstore = Rc::new(CStore::new(box ::MetadataLoader));
109 let sess = session::build_session_(options,
112 Rc::new(CodeMap::new(FilePathMapping::empty())));
113 rustc_trans::init(&sess);
114 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
115 let input = config::Input::Str {
116 name: driver::anon_src(),
117 input: source_string.to_string(),
119 let krate = driver::phase_1_parse_input(&driver::CompileController::basic(),
122 let driver::ExpansionResult { defs, resolutions, mut hir_forest, .. } = {
123 driver::phase_2_configure_and_expand(&sess,
131 .expect("phase 2 aborted")
134 let arena = DroplessArena::new();
135 let arenas = ty::GlobalArenas::new();
136 let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
138 // run just enough stuff to build a tcx:
139 let named_region_map = resolve_lifetime::krate(&sess, &*cstore, &hir_map);
140 let (tx, _rx) = mpsc::channel();
141 let outputs = OutputFilenames {
142 out_directory: PathBuf::new(),
143 out_filestem: String::new(),
144 single_output_file: None,
145 extra: String::new(),
146 outputs: OutputTypes::new(&[]),
148 TyCtxt::create_and_enter(&sess,
150 ty::maps::Providers::default(),
151 ty::maps::Providers::default(),
152 Rc::new(Passes::new()),
156 named_region_map.unwrap(),
162 tcx.infer_ctxt().enter(|infcx| {
163 let mut region_scope_tree = region::ScopeTree::default();
166 region_scope_tree: &mut region_scope_tree,
167 param_env: ty::ParamEnv::empty(Reveal::UserFacing),
169 let free_regions = FreeRegionMap::new();
170 let def_id = tcx.hir.local_def_id(ast::CRATE_NODE_ID);
171 infcx.resolve_regions_and_report_errors(def_id, ®ion_scope_tree, &free_regions);
172 assert_eq!(tcx.sess.err_count(), expected_err_count);
177 impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
178 pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
182 pub fn create_region_hierarchy(&mut self, rh: &RH, parent: region::Scope) {
183 let me = region::Scope::Node(rh.id);
184 self.region_scope_tree.record_scope_parent(me, Some(parent));
185 for child_rh in rh.sub {
186 self.create_region_hierarchy(child_rh, me);
190 pub fn create_simple_region_hierarchy(&mut self) {
191 // creates a region hierarchy where 1 is root, 10 and 11 are
192 // children of 1, etc
194 let dscope = region::Scope::Destruction(hir::ItemLocalId(1));
195 self.region_scope_tree.record_scope_parent(dscope, None);
196 self.create_region_hierarchy(&RH {
197 id: hir::ItemLocalId(1),
199 id: hir::ItemLocalId(10),
203 id: hir::ItemLocalId(11),
209 #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
210 pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
211 return match search_mod(self, &self.infcx.tcx.hir.krate().module, 0, names) {
214 panic!("no item found: `{}`", names.join("::"));
218 fn search_mod(this: &Env,
222 -> Option<ast::NodeId> {
223 assert!(idx < names.len());
224 for item in &m.item_ids {
225 let item = this.infcx.tcx.hir.expect_item(item.id);
226 if item.name.to_string() == names[idx] {
227 return search(this, item, idx + 1, names);
233 fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
234 if idx == names.len() {
238 return match it.node {
240 hir::ItemExternCrate(..) |
242 hir::ItemStatic(..) |
244 hir::ItemForeignMod(..) |
245 hir::ItemGlobalAsm(..) |
246 hir::ItemTy(..) => None,
249 hir::ItemStruct(..) |
253 hir::ItemDefaultImpl(..) => None,
255 hir::ItemMod(ref m) => search_mod(this, m, idx, names),
260 pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
261 match self.infcx.at(&ObligationCause::dummy(), self.param_env).sub(a, b) {
263 Err(ref e) => panic!("Encountered error: {}", e),
267 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
268 self.infcx.can_sub(self.param_env, a, b).is_ok()
271 pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
272 if !self.is_subtype(a, b) {
273 panic!("{} is not a subtype of {}, but it should be", a, b);
277 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
278 self.assert_subtype(a, b);
279 self.assert_subtype(b, a);
282 pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
283 self.infcx.tcx.mk_fn_ptr(ty::Binder(self.infcx.tcx.mk_fn_sig(
284 input_tys.iter().cloned(),
287 hir::Unsafety::Normal,
292 pub fn t_nil(&self) -> Ty<'tcx> {
293 self.infcx.tcx.mk_nil()
296 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
297 self.infcx.tcx.intern_tup(&[ty1, ty2], false)
300 pub fn t_param(&self, index: u32) -> Ty<'tcx> {
301 let name = format!("T{}", index);
302 self.infcx.tcx.mk_param(index, Symbol::intern(&name))
305 pub fn re_early_bound(&self, index: u32, name: &'static str) -> ty::Region<'tcx> {
306 let name = Symbol::intern(name);
307 self.infcx.tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
308 def_id: self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
314 pub fn re_late_bound_with_debruijn(&self,
316 debruijn: ty::DebruijnIndex)
317 -> ty::Region<'tcx> {
318 self.infcx.tcx.mk_region(ty::ReLateBound(debruijn, ty::BrAnon(id)))
321 pub fn t_rptr(&self, r: ty::Region<'tcx>) -> Ty<'tcx> {
322 self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
325 pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
326 let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
327 self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
330 pub fn t_rptr_late_bound_with_debruijn(&self,
332 debruijn: ty::DebruijnIndex)
334 let r = self.re_late_bound_with_debruijn(id, debruijn);
335 self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
338 pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
339 let r = ty::ReScope(region::Scope::Node(hir::ItemLocalId(id)));
340 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
343 pub fn re_free(&self, id: u32) -> ty::Region<'tcx> {
344 self.infcx.tcx.mk_region(ty::ReFree(ty::FreeRegion {
345 scope: self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
346 bound_region: ty::BrAnon(id),
350 pub fn t_rptr_free(&self, id: u32) -> Ty<'tcx> {
351 let r = self.re_free(id);
352 self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
355 pub fn t_rptr_static(&self) -> Ty<'tcx> {
356 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_static,
357 self.tcx().types.isize)
360 pub fn t_rptr_empty(&self) -> Ty<'tcx> {
361 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_empty,
362 self.tcx().types.isize)
365 pub fn sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, ()> {
366 self.infcx.at(&ObligationCause::dummy(), self.param_env).sub(t1, t2)
369 pub fn lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
370 self.infcx.at(&ObligationCause::dummy(), self.param_env).lub(t1, t2)
373 pub fn glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
374 self.infcx.at(&ObligationCause::dummy(), self.param_env).glb(t1, t2)
377 /// Checks that `t1 <: t2` is true (this may register additional
379 pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
380 match self.sub(t1, t2) {
381 Ok(InferOk { obligations, value: () }) => {
382 // None of these tests should require nested obligations:
383 assert!(obligations.is_empty());
386 panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
391 /// Checks that `t1 <: t2` is false (this may register additional
393 pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
394 match self.sub(t1, t2) {
397 panic!("unexpected success computing sub({:?},{:?})", t1, t2);
402 /// Checks that `LUB(t1,t2) == t_lub`
403 pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
404 match self.lub(t1, t2) {
405 Ok(InferOk { obligations, value: t }) => {
406 // None of these tests should require nested obligations:
407 assert!(obligations.is_empty());
409 self.assert_eq(t, t_lub);
411 Err(ref e) => panic!("unexpected error in LUB: {}", e),
415 /// Checks that `GLB(t1,t2) == t_glb`
416 pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
417 debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
418 match self.glb(t1, t2) {
419 Err(e) => panic!("unexpected error computing LUB: {:?}", e),
420 Ok(InferOk { obligations, value: t }) => {
421 // None of these tests should require nested obligations:
422 assert!(obligations.is_empty());
424 self.assert_eq(t, t_glb);
426 // sanity check for good measure:
427 self.assert_subtype(t, t1);
428 self.assert_subtype(t, t2);
435 fn contravariant_region_ptr_ok() {
436 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
437 env.create_simple_region_hierarchy();
438 let t_rptr1 = env.t_rptr_scope(1);
439 let t_rptr10 = env.t_rptr_scope(10);
440 env.assert_eq(t_rptr1, t_rptr1);
441 env.assert_eq(t_rptr10, t_rptr10);
442 env.make_subtype(t_rptr1, t_rptr10);
447 fn contravariant_region_ptr_err() {
448 test_env(EMPTY_SOURCE_STR, errors(&["mismatched types"]), |mut env| {
449 env.create_simple_region_hierarchy();
450 let t_rptr1 = env.t_rptr_scope(1);
451 let t_rptr10 = env.t_rptr_scope(10);
452 env.assert_eq(t_rptr1, t_rptr1);
453 env.assert_eq(t_rptr10, t_rptr10);
455 // will cause an error when regions are resolved
456 env.make_subtype(t_rptr10, t_rptr1);
461 fn sub_free_bound_false() {
464 //! fn(&'a isize) <: for<'b> fn(&'b isize)
468 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
469 env.create_simple_region_hierarchy();
470 let t_rptr_free1 = env.t_rptr_free(1);
471 let t_rptr_bound1 = env.t_rptr_late_bound(1);
472 env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
473 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
478 fn sub_bound_free_true() {
481 //! for<'a> fn(&'a isize) <: fn(&'b isize)
485 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
486 env.create_simple_region_hierarchy();
487 let t_rptr_bound1 = env.t_rptr_late_bound(1);
488 let t_rptr_free1 = env.t_rptr_free(1);
489 env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
490 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
495 fn sub_free_bound_false_infer() {
498 //! fn(_#1) <: for<'b> fn(&'b isize)
500 //! does NOT hold for any instantiation of `_#1`.
502 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
503 let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
504 let t_rptr_bound1 = env.t_rptr_late_bound(1);
505 env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
506 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
511 fn lub_free_bound_infer() {
514 //! LUB(fn(_#1), for<'b> fn(&'b isize))
516 //! This should yield `fn(&'_ isize)`. We check
517 //! that it yields `fn(&'x isize)` for some free `'x`,
520 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
521 env.create_simple_region_hierarchy();
522 let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
523 let t_rptr_bound1 = env.t_rptr_late_bound(1);
524 let t_rptr_free1 = env.t_rptr_free(1);
525 env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
526 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
527 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
532 fn lub_bound_bound() {
533 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
534 let t_rptr_bound1 = env.t_rptr_late_bound(1);
535 let t_rptr_bound2 = env.t_rptr_late_bound(2);
536 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
537 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
538 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
543 fn lub_bound_free() {
544 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
545 env.create_simple_region_hierarchy();
546 let t_rptr_bound1 = env.t_rptr_late_bound(1);
547 let t_rptr_free1 = env.t_rptr_free(1);
548 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
549 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
550 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
555 fn lub_bound_static() {
556 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
557 let t_rptr_bound1 = env.t_rptr_late_bound(1);
558 let t_rptr_static = env.t_rptr_static();
559 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
560 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
561 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
566 fn lub_bound_bound_inverse_order() {
567 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
568 let t_rptr_bound1 = env.t_rptr_late_bound(1);
569 let t_rptr_bound2 = env.t_rptr_late_bound(2);
570 env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
571 env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
572 env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
578 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
579 env.create_simple_region_hierarchy();
580 let t_rptr_free1 = env.t_rptr_free(1);
581 let t_rptr_free2 = env.t_rptr_free(2);
582 let t_rptr_static = env.t_rptr_static();
583 env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
584 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
585 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
590 fn lub_returning_scope() {
591 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
592 env.create_simple_region_hierarchy();
593 let t_rptr_scope10 = env.t_rptr_scope(10);
594 let t_rptr_scope11 = env.t_rptr_scope(11);
595 let t_rptr_empty = env.t_rptr_empty();
596 env.check_lub(env.t_fn(&[t_rptr_scope10], env.tcx().types.isize),
597 env.t_fn(&[t_rptr_scope11], env.tcx().types.isize),
598 env.t_fn(&[t_rptr_empty], env.tcx().types.isize));
603 fn glb_free_free_with_common_scope() {
604 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
605 env.create_simple_region_hierarchy();
606 let t_rptr_free1 = env.t_rptr_free(1);
607 let t_rptr_free2 = env.t_rptr_free(2);
608 let t_rptr_scope = env.t_rptr_scope(1);
609 env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
610 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
611 env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
616 fn glb_bound_bound() {
617 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
618 let t_rptr_bound1 = env.t_rptr_late_bound(1);
619 let t_rptr_bound2 = env.t_rptr_late_bound(2);
620 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
621 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
622 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
627 fn glb_bound_free() {
628 test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
629 env.create_simple_region_hierarchy();
630 let t_rptr_bound1 = env.t_rptr_late_bound(1);
631 let t_rptr_free1 = env.t_rptr_free(1);
632 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
633 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
634 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
639 fn glb_bound_free_infer() {
640 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
641 let t_rptr_bound1 = env.t_rptr_late_bound(1);
642 let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
644 // compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
645 // which should yield for<'b> fn(&'b isize) -> isize
646 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
647 env.t_fn(&[t_infer1], env.tcx().types.isize),
648 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
650 // as a side-effect, computing GLB should unify `_` with
652 let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
653 match t_resolve1.sty {
656 panic!("t_resolve1={:?}", t_resolve1);
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(0);
687 env.t_fn(&[t_param], env.t_nil())
690 let substs = env.infcx.tcx.intern_substs(&[Kind::from(t_rptr_bound1)]);
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={:?}",
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(0);
722 env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
725 let substs = env.infcx.tcx.intern_substs(&[Kind::from(t_rptr_bound1)]);
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={:?}",
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(&[]), |mut env| {
752 // Theta = [A -> &'a foo]
753 env.create_simple_region_hierarchy();
755 assert!(!env.t_nil().has_escaping_regions());
757 let t_rptr_free1 = env.t_rptr_free(1);
758 assert!(!t_rptr_free1.has_escaping_regions());
760 let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
761 assert!(t_rptr_bound1.has_escaping_regions());
763 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
764 assert!(t_rptr_bound2.has_escaping_regions());
767 let t_param = env.t_param(0);
768 assert!(!t_param.has_escaping_regions());
769 let t_fn = env.t_fn(&[t_param], env.t_nil());
770 assert!(!t_fn.has_escaping_regions());
774 /// Test applying a substitution where the value being substituted for an early-bound region is a
775 /// late-bound region.
777 fn subst_region_renumber_region() {
778 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
779 let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
781 // type t_source<'a> = fn(&'a isize)
783 let re_early = env.re_early_bound(0, "'a");
784 env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
787 let substs = env.infcx.tcx.intern_substs(&[Kind::from(re_bound1)]);
788 let t_substituted = t_source.subst(env.infcx.tcx, substs);
790 // t_expected = fn(&'a isize)
792 // but not that the Debruijn index is different in the different cases.
794 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
795 env.t_fn(&[t_rptr_bound2], env.t_nil())
798 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
804 assert_eq!(t_substituted, t_expected);
810 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
811 let tcx = env.infcx.tcx;
812 let int_ty = tcx.types.isize;
813 let usize_ty = tcx.types.usize;
814 let tup1_ty = tcx.intern_tup(&[int_ty, usize_ty, int_ty, usize_ty], false);
815 let tup2_ty = tcx.intern_tup(&[tup1_ty, tup1_ty, usize_ty], false);
816 let walked: Vec<_> = tup2_ty.walk().collect();
818 [tup2_ty, tup1_ty, int_ty, usize_ty, int_ty, usize_ty, tup1_ty, int_ty,
819 usize_ty, int_ty, usize_ty, usize_ty]);
824 fn walk_ty_skip_subtree() {
825 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
826 let tcx = env.infcx.tcx;
827 let int_ty = tcx.types.isize;
828 let usize_ty = tcx.types.usize;
829 let tup1_ty = tcx.intern_tup(&[int_ty, usize_ty, int_ty, usize_ty], false);
830 let tup2_ty = tcx.intern_tup(&[tup1_ty, tup1_ty, usize_ty], false);
832 // types we expect to see (in order), plus a boolean saying
833 // whether to skip the subtree.
834 let mut expected = vec![(tup2_ty, false),
840 (tup1_ty, true), // skip the isize/usize/isize/usize
844 let mut walker = tup2_ty.walk();
845 while let Some(t) = walker.next() {
846 debug!("walked to {:?}", t);
847 let (expected_ty, skip) = expected.pop().unwrap();
848 assert_eq!(t, expected_ty);
850 walker.skip_current_subtree();
854 assert!(expected.is_empty());