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};
21 use rustc_typeck::middle::region::CodeExtentData;
22 use rustc_typeck::middle::resolve_lifetime;
23 use rustc_typeck::middle::stability;
24 use rustc_typeck::middle::subst;
25 use rustc_typeck::middle::subst::Subst;
26 use rustc_typeck::middle::ty::{self, Ty, RegionEscape};
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};
36 use syntax::{abi, ast};
38 use syntax::codemap::{Span, CodeMap, DUMMY_SP};
39 use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
40 use syntax::parse::token;
41 use syntax::feature_gate::UnstableFeatures;
43 use rustc_front::lowering::{lower_crate, LoweringContext};
46 struct Env<'a, 'tcx: 'a> {
47 infcx: &'a infer::InferCtxt<'a, 'tcx>,
55 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
57 struct ExpectErrorEmitter {
58 messages: Vec<String>,
61 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
63 Bug | Fatal | Error => {}
64 Warning | Note | Help => {
69 debug!("Error: {}", msg);
70 match e.messages.iter().position(|m| msg.contains(m)) {
75 panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
80 impl Emitter for ExpectErrorEmitter {
82 _cmsp: Option<(&codemap::CodeMap, Span)>,
86 remove_message(self, msg, lvl);
89 fn custom_emit(&mut self, _cm: &codemap::CodeMap, _sp: RenderSpan, msg: &str, lvl: Level) {
90 remove_message(self, msg, lvl);
94 fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
95 let v = msgs.iter().map(|m| m.to_string()).collect();
96 (box ExpectErrorEmitter { messages: v } as Box<Emitter + Send>,
100 fn test_env<F>(source_string: &str,
101 (emitter, expected_err_count): (Box<Emitter + Send>, usize),
105 let mut options = config::basic_options();
106 options.debugging_opts.verbose = true;
107 options.unstable_features = UnstableFeatures::Allow;
108 let codemap = CodeMap::new();
109 let diagnostic_handler = diagnostic::Handler::with_emitter(true, emitter);
110 let span_diagnostic_handler = diagnostic::SpanHandler::new(diagnostic_handler, codemap);
112 let cstore = Rc::new(CStore::new(token::get_ident_interner()));
113 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, &cstore, krate, "test", None)
120 .expect("phase 2 aborted");
122 let krate = driver::assign_node_ids(&sess, krate);
123 let lcx = LoweringContext::new(&sess, Some(&krate));
124 let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate));
125 let arenas = ty::CtxtArenas::new();
126 let ast_map = driver::make_map(&sess, &mut hir_forest);
127 let krate = ast_map.krate();
129 // run just enough stuff to build a tcx:
130 let lang_items = lang_items::collect_language_items(&sess, &ast_map);
131 let resolve::CrateMap { def_map, freevars, .. } =
132 resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
133 let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map.borrow());
134 let region_map = region::resolve_crate(&sess, krate);
135 ty::ctxt::create_and_enter(&sess,
143 stability::Index::new(krate),
145 let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, None, false);
146 body(Env { infcx: &infcx });
147 let free_regions = FreeRegionMap::new();
148 infcx.resolve_regions_and_report_errors(&free_regions,
150 assert_eq!(tcx.sess.err_count(), expected_err_count);
154 impl<'a, 'tcx> Env<'a, 'tcx> {
155 pub fn tcx(&self) -> &ty::ctxt<'tcx> {
159 pub fn create_region_hierarchy(&self, rh: &RH, parent: CodeExtent) {
160 let me = self.infcx.tcx.region_maps.intern_node(rh.id, parent);
161 for child_rh in rh.sub {
162 self.create_region_hierarchy(child_rh, me);
166 pub fn create_simple_region_hierarchy(&self) {
167 // creates a region hierarchy where 1 is root, 10 and 11 are
168 // children of 1, etc
169 let dscope = self.infcx
172 .intern_code_extent(CodeExtentData::DestructionScope(1),
173 region::ROOT_CODE_EXTENT);
174 self.create_region_hierarchy(&RH {
176 sub: &[RH { id: 10, sub: &[] }, RH { id: 11, sub: &[] }],
181 #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
182 pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
183 return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
186 panic!("no item found: `{}`", names.join("::"));
190 fn search_mod(this: &Env,
194 -> Option<ast::NodeId> {
195 assert!(idx < names.len());
196 for item in &m.item_ids {
197 let item = this.infcx.tcx.map.expect_item(item.id);
198 if item.name.to_string() == names[idx] {
199 return search(this, item, idx + 1, names);
205 fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
206 if idx == names.len() {
210 return match it.node {
212 hir::ItemExternCrate(..) |
214 hir::ItemStatic(..) |
216 hir::ItemForeignMod(..) |
222 hir::ItemStruct(..) |
225 hir::ItemDefaultImpl(..) => {
229 hir::ItemMod(ref m) => {
230 search_mod(this, m, idx, names)
236 pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
237 match infer::mk_subty(self.infcx, true, TypeOrigin::Misc(DUMMY_SP), a, b) {
239 Err(ref e) => panic!("Encountered error: {}", e),
243 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
244 match infer::can_mk_subty(self.infcx, a, b) {
250 pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
251 if !self.is_subtype(a, b) {
252 panic!("{} is not a subtype of {}, but it should be", a, b);
256 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
257 self.assert_subtype(a, b);
258 self.assert_subtype(b, a);
261 pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
262 let input_args = input_tys.iter().cloned().collect();
263 self.infcx.tcx.mk_fn(None,
264 self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
265 unsafety: hir::Unsafety::Normal,
267 sig: ty::Binder(ty::FnSig {
269 output: ty::FnConverging(output_ty),
275 pub fn t_nil(&self) -> Ty<'tcx> {
276 self.infcx.tcx.mk_nil()
279 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
280 self.infcx.tcx.mk_tup(vec![ty1, ty2])
283 pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
284 let name = format!("T{}", index);
285 self.infcx.tcx.mk_param(space, index, token::intern(&name[..]))
288 pub fn re_early_bound(&self,
289 space: subst::ParamSpace,
293 let name = token::intern(name);
294 ty::ReEarlyBound(ty::EarlyBoundRegion {
295 def_id: self.infcx.tcx.map.local_def_id(ast::DUMMY_NODE_ID),
302 pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
303 ty::ReLateBound(debruijn, ty::BrAnon(id))
306 pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
307 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
310 pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
311 let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
312 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
315 pub fn t_rptr_late_bound_with_debruijn(&self,
317 debruijn: ty::DebruijnIndex)
319 let r = self.re_late_bound_with_debruijn(id, debruijn);
320 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
323 pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
324 let r = ty::ReScope(self.tcx().region_maps.node_extent(id));
325 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
328 pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
329 ty::ReFree(ty::FreeRegion {
330 scope: self.tcx().region_maps.item_extent(nid),
331 bound_region: ty::BrAnon(id),
335 pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
336 let r = self.re_free(nid, id);
337 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), 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 t_rptr_empty(&self) -> Ty<'tcx> {
346 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReEmpty),
347 self.tcx().types.isize)
350 pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
351 infer::TypeTrace::dummy(self.tcx())
354 pub fn sub(&self) -> Sub<'a, 'tcx> {
355 let trace = self.dummy_type_trace();
356 self.infcx.sub(true, trace)
359 pub fn lub(&self) -> Lub<'a, 'tcx> {
360 let trace = self.dummy_type_trace();
361 self.infcx.lub(true, trace)
364 pub fn glb(&self) -> Glb<'a, 'tcx> {
365 let trace = self.dummy_type_trace();
366 self.infcx.glb(true, trace)
369 pub fn make_lub_ty(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> Ty<'tcx> {
370 match self.lub().relate(&t1, &t2) {
372 Err(ref e) => panic!("unexpected error computing LUB: {}", e),
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());