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;
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::front::map as hir_map;
33 use rustc::session::{self, config};
34 use syntax::{abi, ast};
36 use syntax::codemap::{Span, CodeMap, DUMMY_SP};
37 use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
38 use syntax::parse::token;
39 use syntax::feature_gate::UnstableFeatures;
41 use rustc_front::lowering::{lower_crate, LoweringContext};
44 struct Env<'a, 'tcx: 'a> {
45 infcx: &'a infer::InferCtxt<'a, 'tcx>,
53 const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
55 struct ExpectErrorEmitter {
56 messages: Vec<String>,
59 fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
61 Bug | Fatal | Error => {}
62 Warning | Note | Help => {
67 debug!("Error: {}", msg);
68 match e.messages.iter().position(|m| msg.contains(m)) {
73 panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
78 impl Emitter for ExpectErrorEmitter {
80 _cmsp: Option<(&codemap::CodeMap, Span)>,
84 remove_message(self, msg, lvl);
87 fn custom_emit(&mut self, _cm: &codemap::CodeMap, _sp: RenderSpan, msg: &str, lvl: Level) {
88 remove_message(self, msg, lvl);
92 fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
93 let v = msgs.iter().map(|m| m.to_string()).collect();
94 (box ExpectErrorEmitter { messages: v } as Box<Emitter + Send>,
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 codemap = CodeMap::new();
107 let diagnostic_handler = diagnostic::Handler::with_emitter(true, emitter);
108 let span_diagnostic_handler = diagnostic::SpanHandler::new(diagnostic_handler, codemap);
110 let sess = session::build_session_(options, None, span_diagnostic_handler);
111 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
112 let krate_config = Vec::new();
113 let input = config::Input::Str(source_string.to_string());
114 let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
115 let krate = driver::phase_2_configure_and_expand(&sess, krate, "test", None)
116 .expect("phase 2 aborted");
118 let krate = driver::assign_node_ids(&sess, krate);
119 let lcx = LoweringContext::new(&sess, Some(&krate));
120 let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate));
121 let arenas = ty::CtxtArenas::new();
122 let ast_map = driver::make_map(&sess, &mut hir_forest);
123 let krate = ast_map.krate();
125 // run just enough stuff to build a tcx:
126 let lang_items = lang_items::collect_language_items(&sess, &ast_map);
127 let resolve::CrateMap { def_map, freevars, .. } =
128 resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
129 let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map.borrow());
130 let region_map = region::resolve_crate(&sess, krate);
131 ty::ctxt::create_and_enter(&sess,
139 stability::Index::new(krate),
141 let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, None, false);
142 body(Env { infcx: &infcx });
143 let free_regions = FreeRegionMap::new();
144 infcx.resolve_regions_and_report_errors(&free_regions,
146 assert_eq!(tcx.sess.err_count(), expected_err_count);
150 impl<'a, 'tcx> Env<'a, 'tcx> {
151 pub fn tcx(&self) -> &ty::ctxt<'tcx> {
155 pub fn create_region_hierarchy(&self, rh: &RH, parent: CodeExtent) {
156 let me = self.infcx.tcx.region_maps.intern_node(rh.id, parent);
157 for child_rh in rh.sub {
158 self.create_region_hierarchy(child_rh, me);
162 pub fn create_simple_region_hierarchy(&self) {
163 // creates a region hierarchy where 1 is root, 10 and 11 are
164 // children of 1, etc
165 let dscope = self.infcx
168 .intern_code_extent(CodeExtentData::DestructionScope(1),
169 region::ROOT_CODE_EXTENT);
170 self.create_region_hierarchy(&RH {
172 sub: &[RH { id: 10, sub: &[] }, RH { id: 11, sub: &[] }],
177 #[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
178 pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
179 return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
182 panic!("no item found: `{}`", names.join("::"));
186 fn search_mod(this: &Env,
190 -> Option<ast::NodeId> {
191 assert!(idx < names.len());
192 for item in &m.items {
193 if item.name.to_string() == names[idx] {
194 return search(this, &**item, idx + 1, names);
200 fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
201 if idx == names.len() {
205 return match it.node {
207 hir::ItemExternCrate(..) |
209 hir::ItemStatic(..) |
211 hir::ItemForeignMod(..) |
217 hir::ItemStruct(..) |
220 hir::ItemDefaultImpl(..) => {
224 hir::ItemMod(ref m) => {
225 search_mod(this, m, idx, names)
231 pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
232 match infer::mk_subty(self.infcx, true, infer::Misc(DUMMY_SP), a, b) {
234 Err(ref e) => panic!("Encountered error: {}", e),
238 pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
239 match infer::can_mk_subty(self.infcx, a, b) {
245 pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
246 if !self.is_subtype(a, b) {
247 panic!("{} is not a subtype of {}, but it should be", a, b);
251 pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
252 self.assert_subtype(a, b);
253 self.assert_subtype(b, a);
256 pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
257 let input_args = input_tys.iter().cloned().collect();
258 self.infcx.tcx.mk_fn(None,
259 self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
260 unsafety: hir::Unsafety::Normal,
262 sig: ty::Binder(ty::FnSig {
264 output: ty::FnConverging(output_ty),
270 pub fn t_nil(&self) -> Ty<'tcx> {
271 self.infcx.tcx.mk_nil()
274 pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
275 self.infcx.tcx.mk_tup(vec![ty1, ty2])
278 pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
279 let name = format!("T{}", index);
280 self.infcx.tcx.mk_param(space, index, token::intern(&name[..]))
283 pub fn re_early_bound(&self,
284 space: subst::ParamSpace,
288 let name = token::intern(name);
289 ty::ReEarlyBound(ty::EarlyBoundRegion {
290 def_id: self.infcx.tcx.map.local_def_id(ast::DUMMY_NODE_ID),
297 pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
298 ty::ReLateBound(debruijn, ty::BrAnon(id))
301 pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
302 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
305 pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
306 let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
307 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
310 pub fn t_rptr_late_bound_with_debruijn(&self,
312 debruijn: ty::DebruijnIndex)
314 let r = self.re_late_bound_with_debruijn(id, debruijn);
315 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
318 pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
319 let r = ty::ReScope(self.tcx().region_maps.node_extent(id));
320 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
323 pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
324 ty::ReFree(ty::FreeRegion {
325 scope: self.tcx().region_maps.item_extent(nid),
326 bound_region: ty::BrAnon(id),
330 pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
331 let r = self.re_free(nid, id);
332 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
335 pub fn t_rptr_static(&self) -> Ty<'tcx> {
336 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReStatic),
337 self.tcx().types.isize)
340 pub fn t_rptr_empty(&self) -> Ty<'tcx> {
341 self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReEmpty),
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({:?},{:?}): {}", t1, t2, e);
382 /// Checks that `t1 <: t2` is false (this may register additional
384 pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
385 match self.sub().relate(&t1, &t2) {
388 panic!("unexpected success computing sub({:?},{:?})", t1, t2);
393 /// Checks that `LUB(t1,t2) == t_lub`
394 pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
395 match self.lub().relate(&t1, &t2) {
397 self.assert_eq(t, t_lub);
400 panic!("unexpected error in LUB: {}", e)
405 /// Checks that `GLB(t1,t2) == t_glb`
406 pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
407 debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
408 match self.glb().relate(&t1, &t2) {
410 panic!("unexpected error computing LUB: {:?}", e)
413 self.assert_eq(t, t_glb);
415 // sanity check for good measure:
416 self.assert_subtype(t, t1);
417 self.assert_subtype(t, t2);
424 fn contravariant_region_ptr_ok() {
425 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
426 env.create_simple_region_hierarchy();
427 let t_rptr1 = env.t_rptr_scope(1);
428 let t_rptr10 = env.t_rptr_scope(10);
429 env.assert_eq(t_rptr1, t_rptr1);
430 env.assert_eq(t_rptr10, t_rptr10);
431 env.make_subtype(t_rptr1, t_rptr10);
436 fn contravariant_region_ptr_err() {
437 test_env(EMPTY_SOURCE_STR, errors(&["lifetime mismatch"]), |env| {
438 env.create_simple_region_hierarchy();
439 let t_rptr1 = env.t_rptr_scope(1);
440 let t_rptr10 = env.t_rptr_scope(10);
441 env.assert_eq(t_rptr1, t_rptr1);
442 env.assert_eq(t_rptr10, t_rptr10);
444 // will cause an error when regions are resolved
445 env.make_subtype(t_rptr10, t_rptr1);
450 fn sub_free_bound_false() {
453 //! fn(&'a isize) <: for<'b> fn(&'b isize)
457 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
458 env.create_simple_region_hierarchy();
459 let t_rptr_free1 = env.t_rptr_free(1, 1);
460 let t_rptr_bound1 = env.t_rptr_late_bound(1);
461 env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
462 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
467 fn sub_bound_free_true() {
470 //! for<'a> fn(&'a isize) <: fn(&'b isize)
474 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
475 env.create_simple_region_hierarchy();
476 let t_rptr_bound1 = env.t_rptr_late_bound(1);
477 let t_rptr_free1 = env.t_rptr_free(1, 1);
478 env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
479 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
484 fn sub_free_bound_false_infer() {
487 //! fn(_#1) <: for<'b> fn(&'b isize)
489 //! does NOT hold for any instantiation of `_#1`.
491 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
492 let t_infer1 = env.infcx.next_ty_var();
493 let t_rptr_bound1 = env.t_rptr_late_bound(1);
494 env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
495 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
500 fn lub_free_bound_infer() {
503 //! LUB(fn(_#1), for<'b> fn(&'b isize))
505 //! This should yield `fn(&'_ isize)`. We check
506 //! that it yields `fn(&'x isize)` for some free `'x`,
509 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
510 env.create_simple_region_hierarchy();
511 let t_infer1 = env.infcx.next_ty_var();
512 let t_rptr_bound1 = env.t_rptr_late_bound(1);
513 let t_rptr_free1 = env.t_rptr_free(1, 1);
514 env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
515 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
516 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
521 fn lub_bound_bound() {
522 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
523 let t_rptr_bound1 = env.t_rptr_late_bound(1);
524 let t_rptr_bound2 = env.t_rptr_late_bound(2);
525 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
526 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
527 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
532 fn lub_bound_free() {
533 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
534 env.create_simple_region_hierarchy();
535 let t_rptr_bound1 = env.t_rptr_late_bound(1);
536 let t_rptr_free1 = env.t_rptr_free(1, 1);
537 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
538 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
539 env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
544 fn lub_bound_static() {
545 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
546 let t_rptr_bound1 = env.t_rptr_late_bound(1);
547 let t_rptr_static = env.t_rptr_static();
548 env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
549 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
550 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
555 fn lub_bound_bound_inverse_order() {
556 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
557 let t_rptr_bound1 = env.t_rptr_late_bound(1);
558 let t_rptr_bound2 = env.t_rptr_late_bound(2);
559 env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
560 env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
561 env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
567 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
568 env.create_simple_region_hierarchy();
569 let t_rptr_free1 = env.t_rptr_free(1, 1);
570 let t_rptr_free2 = env.t_rptr_free(1, 2);
571 let t_rptr_static = env.t_rptr_static();
572 env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
573 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
574 env.t_fn(&[t_rptr_static], env.tcx().types.isize));
579 fn lub_returning_scope() {
580 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
581 env.create_simple_region_hierarchy();
582 let t_rptr_scope10 = env.t_rptr_scope(10);
583 let t_rptr_scope11 = env.t_rptr_scope(11);
584 let t_rptr_empty = env.t_rptr_empty();
585 env.check_lub(env.t_fn(&[t_rptr_scope10], env.tcx().types.isize),
586 env.t_fn(&[t_rptr_scope11], env.tcx().types.isize),
587 env.t_fn(&[t_rptr_empty], env.tcx().types.isize));
592 fn glb_free_free_with_common_scope() {
593 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
594 env.create_simple_region_hierarchy();
595 let t_rptr_free1 = env.t_rptr_free(1, 1);
596 let t_rptr_free2 = env.t_rptr_free(1, 2);
597 let t_rptr_scope = env.t_rptr_scope(1);
598 env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
599 env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
600 env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
605 fn glb_bound_bound() {
606 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
607 let t_rptr_bound1 = env.t_rptr_late_bound(1);
608 let t_rptr_bound2 = env.t_rptr_late_bound(2);
609 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
610 env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
611 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
616 fn glb_bound_free() {
617 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
618 env.create_simple_region_hierarchy();
619 let t_rptr_bound1 = env.t_rptr_late_bound(1);
620 let t_rptr_free1 = env.t_rptr_free(1, 1);
621 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
622 env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
623 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
628 fn glb_bound_free_infer() {
629 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
630 let t_rptr_bound1 = env.t_rptr_late_bound(1);
631 let t_infer1 = env.infcx.next_ty_var();
633 // compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
634 // which should yield for<'b> fn(&'b isize) -> isize
635 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
636 env.t_fn(&[t_infer1], env.tcx().types.isize),
637 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
639 // as a side-effect, computing GLB should unify `_` with
641 let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
642 match t_resolve1.sty {
645 panic!("t_resolve1={:?}", t_resolve1);
652 fn glb_bound_static() {
653 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
654 let t_rptr_bound1 = env.t_rptr_late_bound(1);
655 let t_rptr_static = env.t_rptr_static();
656 env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
657 env.t_fn(&[t_rptr_static], env.tcx().types.isize),
658 env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
662 /// Test substituting a bound region into a function, which introduces another level of binding.
663 /// This requires adjusting the Debruijn index.
665 fn subst_ty_renumber_bound() {
667 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
669 // Theta = [A -> &'a foo]
671 let t_rptr_bound1 = env.t_rptr_late_bound(1);
675 let t_param = env.t_param(subst::TypeSpace, 0);
676 env.t_fn(&[t_param], env.t_nil())
679 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
680 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
682 // t_expected = fn(&'a isize)
684 let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
685 env.t_fn(&[t_ptr_bound2], env.t_nil())
688 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
694 assert_eq!(t_substituted, t_expected);
698 /// Test substituting a bound region into a function, which introduces another level of binding.
699 /// This requires adjusting the Debruijn index.
701 fn subst_ty_renumber_some_bounds() {
702 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
704 // Theta = [A -> &'a foo]
706 let t_rptr_bound1 = env.t_rptr_late_bound(1);
708 // t_source = (A, fn(A))
710 let t_param = env.t_param(subst::TypeSpace, 0);
711 env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
714 let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
715 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
717 // t_expected = (&'a isize, fn(&'a isize))
719 // but not that the Debruijn index is different in the different cases.
721 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
722 env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
725 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
731 assert_eq!(t_substituted, t_expected);
735 /// Test that we correctly compute whether a type has escaping regions or not.
739 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
741 // Theta = [A -> &'a foo]
742 env.create_simple_region_hierarchy();
744 assert!(!env.t_nil().has_escaping_regions());
746 let t_rptr_free1 = env.t_rptr_free(1, 1);
747 assert!(!t_rptr_free1.has_escaping_regions());
749 let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
750 assert!(t_rptr_bound1.has_escaping_regions());
752 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
753 assert!(t_rptr_bound2.has_escaping_regions());
756 let t_param = env.t_param(subst::TypeSpace, 0);
757 assert!(!t_param.has_escaping_regions());
758 let t_fn = env.t_fn(&[t_param], env.t_nil());
759 assert!(!t_fn.has_escaping_regions());
763 /// Test applying a substitution where the value being substituted for an early-bound region is a
764 /// late-bound region.
766 fn subst_region_renumber_region() {
767 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
768 let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
770 // type t_source<'a> = fn(&'a isize)
772 let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
773 env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
776 let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
777 let t_substituted = t_source.subst(env.infcx.tcx, &substs);
779 // t_expected = fn(&'a isize)
781 // but not that the Debruijn index is different in the different cases.
783 let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
784 env.t_fn(&[t_rptr_bound2], env.t_nil())
787 debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
793 assert_eq!(t_substituted, t_expected);
799 test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
800 let tcx = env.infcx.tcx;
801 let int_ty = tcx.types.isize;
802 let uint_ty = tcx.types.usize;
803 let tup1_ty = tcx.mk_tup(vec![int_ty, uint_ty, int_ty, uint_ty]);
804 let tup2_ty = tcx.mk_tup(vec![tup1_ty, tup1_ty, uint_ty]);
805 let uniq_ty = tcx.mk_box(tup2_ty);
806 let walked: Vec<_> = uniq_ty.walk().collect();
808 [uniq_ty, tup2_ty, tup1_ty, int_ty, uint_ty, int_ty, uint_ty, tup1_ty, int_ty,
809 uint_ty, int_ty, uint_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);
842 walker.skip_current_subtree();
846 assert!(expected.is_empty());