forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rs
842 lines (733 loc) · 29.3 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! # Standalone Tests for the Inference Module
use driver;
use rustc_lint;
use rustc_resolve as resolve;
use rustc_typeck::middle::lang_items;
use rustc_typeck::middle::free_region::FreeRegionMap;
use rustc_typeck::middle::region::{self, CodeExtent};
use rustc_typeck::middle::region::CodeExtentData;
use rustc_typeck::middle::resolve_lifetime;
use rustc_typeck::middle::stability;
use rustc_typeck::middle::subst;
use rustc_typeck::middle::subst::Subst;
use rustc_typeck::middle::ty::{self, Ty, RegionEscape};
use rustc_typeck::middle::ty::relate::TypeRelation;
use rustc_typeck::middle::infer::{self, TypeOrigin};
use rustc_typeck::middle::infer::lub::Lub;
use rustc_typeck::middle::infer::glb::Glb;
use rustc_typeck::middle::infer::sub::Sub;
use rustc_metadata::cstore::CStore;
use rustc::front::map as hir_map;
use rustc::session::{self, config};
use std::rc::Rc;
use syntax::{abi, ast};
use syntax::codemap::{Span, CodeMap, DUMMY_SP};
use syntax::errors;
use syntax::errors::emitter::Emitter;
use syntax::errors::{Level, RenderSpan};
use syntax::parse::token;
use syntax::feature_gate::UnstableFeatures;
use rustc_front::lowering::{lower_crate, LoweringContext};
use rustc_front::hir;
struct Env<'a, 'tcx: 'a> {
infcx: &'a infer::InferCtxt<'a, 'tcx>,
}
struct RH<'a> {
id: ast::NodeId,
sub: &'a [RH<'a>],
}
const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
struct ExpectErrorEmitter {
messages: Vec<String>,
}
fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
match lvl {
Level::Bug | Level::Fatal | Level::Error => {}
Level::Warning | Level::Note | Level::Help => {
return;
}
}
debug!("Error: {}", msg);
match e.messages.iter().position(|m| msg.contains(m)) {
Some(i) => {
e.messages.remove(i);
}
None => {
panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
}
}
}
impl Emitter for ExpectErrorEmitter {
fn emit(&mut self,
_sp: Option<Span>,
msg: &str,
_: Option<&str>,
lvl: Level) {
remove_message(self, msg, lvl);
}
fn custom_emit(&mut self, _sp: RenderSpan, msg: &str, lvl: Level) {
remove_message(self, msg, lvl);
}
}
fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
let v = msgs.iter().map(|m| m.to_string()).collect();
(box ExpectErrorEmitter { messages: v } as Box<Emitter + Send>,
msgs.len())
}
fn test_env<F>(source_string: &str,
(emitter, expected_err_count): (Box<Emitter + Send>, usize),
body: F)
where F: FnOnce(Env)
{
let mut options = config::basic_options();
options.debugging_opts.verbose = true;
options.unstable_features = UnstableFeatures::Allow;
let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter);
let cstore = Rc::new(CStore::new(token::get_ident_interner()));
let sess = session::build_session_(options, None, diagnostic_handler,
Rc::new(CodeMap::new()), cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let krate_config = Vec::new();
let input = config::Input::Str(source_string.to_string());
let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
.expect("phase 2 aborted");
let krate = driver::assign_node_ids(&sess, krate);
let lcx = LoweringContext::new(&sess, Some(&krate));
let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate));
let arenas = ty::CtxtArenas::new();
let ast_map = driver::make_map(&sess, &mut hir_forest);
let krate = ast_map.krate();
// run just enough stuff to build a tcx:
let lang_items = lang_items::collect_language_items(&sess, &ast_map);
let resolve::CrateMap { def_map, freevars, .. } =
resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map.borrow());
let region_map = region::resolve_crate(&sess, krate);
ty::ctxt::create_and_enter(&sess,
&arenas,
def_map,
named_region_map,
ast_map,
freevars,
region_map,
lang_items,
stability::Index::new(krate),
|tcx| {
let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, None, false);
body(Env { infcx: &infcx });
let free_regions = FreeRegionMap::new();
infcx.resolve_regions_and_report_errors(&free_regions,
ast::CRATE_NODE_ID);
assert_eq!(tcx.sess.err_count(), expected_err_count);
});
}
impl<'a, 'tcx> Env<'a, 'tcx> {
pub fn tcx(&self) -> &ty::ctxt<'tcx> {
self.infcx.tcx
}
pub fn create_region_hierarchy(&self, rh: &RH, parent: CodeExtent) {
let me = self.infcx.tcx.region_maps.intern_node(rh.id, parent);
for child_rh in rh.sub {
self.create_region_hierarchy(child_rh, me);
}
}
pub fn create_simple_region_hierarchy(&self) {
// creates a region hierarchy where 1 is root, 10 and 11 are
// children of 1, etc
let dscope = self.infcx
.tcx
.region_maps
.intern_code_extent(CodeExtentData::DestructionScope(1),
region::ROOT_CODE_EXTENT);
self.create_region_hierarchy(&RH {
id: 1,
sub: &[RH { id: 10, sub: &[] }, RH { id: 11, sub: &[] }],
},
dscope);
}
#[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
return match search_mod(self, &self.infcx.tcx.map.krate().module, 0, names) {
Some(id) => id,
None => {
panic!("no item found: `{}`", names.join("::"));
}
};
fn search_mod(this: &Env,
m: &hir::Mod,
idx: usize,
names: &[String])
-> Option<ast::NodeId> {
assert!(idx < names.len());
for item in &m.item_ids {
let item = this.infcx.tcx.map.expect_item(item.id);
if item.name.to_string() == names[idx] {
return search(this, item, idx + 1, names);
}
}
return None;
}
fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
if idx == names.len() {
return Some(it.id);
}
return match it.node {
hir::ItemUse(..) |
hir::ItemExternCrate(..) |
hir::ItemConst(..) |
hir::ItemStatic(..) |
hir::ItemFn(..) |
hir::ItemForeignMod(..) |
hir::ItemTy(..) => {
None
}
hir::ItemEnum(..) |
hir::ItemStruct(..) |
hir::ItemTrait(..) |
hir::ItemImpl(..) |
hir::ItemDefaultImpl(..) => {
None
}
hir::ItemMod(ref m) => {
search_mod(this, m, idx, names)
}
};
}
}
pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
match infer::mk_subty(self.infcx, true, TypeOrigin::Misc(DUMMY_SP), a, b) {
Ok(_) => true,
Err(ref e) => panic!("Encountered error: {}", e),
}
}
pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
match infer::can_mk_subty(self.infcx, a, b) {
Ok(_) => true,
Err(_) => false,
}
}
pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
if !self.is_subtype(a, b) {
panic!("{} is not a subtype of {}, but it should be", a, b);
}
}
pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.assert_subtype(a, b);
self.assert_subtype(b, a);
}
pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
let input_args = input_tys.iter().cloned().collect();
self.infcx.tcx.mk_fn(None,
self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
unsafety: hir::Unsafety::Normal,
abi: abi::Rust,
sig: ty::Binder(ty::FnSig {
inputs: input_args,
output: ty::FnConverging(output_ty),
variadic: false,
}),
}))
}
pub fn t_nil(&self) -> Ty<'tcx> {
self.infcx.tcx.mk_nil()
}
pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
self.infcx.tcx.mk_tup(vec![ty1, ty2])
}
pub fn t_param(&self, space: subst::ParamSpace, index: u32) -> Ty<'tcx> {
let name = format!("T{}", index);
self.infcx.tcx.mk_param(space, index, token::intern(&name[..]))
}
pub fn re_early_bound(&self,
space: subst::ParamSpace,
index: u32,
name: &'static str)
-> ty::Region {
let name = token::intern(name);
ty::ReEarlyBound(ty::EarlyBoundRegion {
space: space,
index: index,
name: name,
})
}
pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
ty::ReLateBound(debruijn, ty::BrAnon(id))
}
pub fn t_rptr(&self, r: ty::Region) -> Ty<'tcx> {
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}
pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}
pub fn t_rptr_late_bound_with_debruijn(&self,
id: u32,
debruijn: ty::DebruijnIndex)
-> Ty<'tcx> {
let r = self.re_late_bound_with_debruijn(id, debruijn);
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}
pub fn t_rptr_scope(&self, id: ast::NodeId) -> Ty<'tcx> {
let r = ty::ReScope(self.tcx().region_maps.node_extent(id));
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}
pub fn re_free(&self, nid: ast::NodeId, id: u32) -> ty::Region {
ty::ReFree(ty::FreeRegion {
scope: self.tcx().region_maps.item_extent(nid),
bound_region: ty::BrAnon(id),
})
}
pub fn t_rptr_free(&self, nid: ast::NodeId, id: u32) -> Ty<'tcx> {
let r = self.re_free(nid, id);
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}
pub fn t_rptr_static(&self) -> Ty<'tcx> {
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReStatic),
self.tcx().types.isize)
}
pub fn t_rptr_empty(&self) -> Ty<'tcx> {
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReEmpty),
self.tcx().types.isize)
}
pub fn dummy_type_trace(&self) -> infer::TypeTrace<'tcx> {
infer::TypeTrace::dummy(self.tcx())
}
pub fn sub(&self) -> Sub<'a, 'tcx> {
let trace = self.dummy_type_trace();
self.infcx.sub(true, trace)
}
pub fn lub(&self) -> Lub<'a, 'tcx> {
let trace = self.dummy_type_trace();
self.infcx.lub(true, trace)
}
pub fn glb(&self) -> Glb<'a, 'tcx> {
let trace = self.dummy_type_trace();
self.infcx.glb(true, trace)
}
/// Checks that `t1 <: t2` is true (this may register additional
/// region checks).
pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
match self.sub().relate(&t1, &t2) {
Ok(_) => {}
Err(ref e) => {
panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
}
}
}
/// Checks that `t1 <: t2` is false (this may register additional
/// region checks).
pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
match self.sub().relate(&t1, &t2) {
Err(_) => {}
Ok(_) => {
panic!("unexpected success computing sub({:?},{:?})", t1, t2);
}
}
}
/// Checks that `LUB(t1,t2) == t_lub`
pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
match self.lub().relate(&t1, &t2) {
Ok(t) => {
self.assert_eq(t, t_lub);
}
Err(ref e) => {
panic!("unexpected error in LUB: {}", e)
}
}
}
/// Checks that `GLB(t1,t2) == t_glb`
pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
match self.glb().relate(&t1, &t2) {
Err(e) => {
panic!("unexpected error computing LUB: {:?}", e)
}
Ok(t) => {
self.assert_eq(t, t_glb);
// sanity check for good measure:
self.assert_subtype(t, t1);
self.assert_subtype(t, t2);
}
}
}
}
#[test]
fn contravariant_region_ptr_ok() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr1 = env.t_rptr_scope(1);
let t_rptr10 = env.t_rptr_scope(10);
env.assert_eq(t_rptr1, t_rptr1);
env.assert_eq(t_rptr10, t_rptr10);
env.make_subtype(t_rptr1, t_rptr10);
})
}
#[test]
fn contravariant_region_ptr_err() {
test_env(EMPTY_SOURCE_STR, errors(&["lifetime mismatch"]), |env| {
env.create_simple_region_hierarchy();
let t_rptr1 = env.t_rptr_scope(1);
let t_rptr10 = env.t_rptr_scope(10);
env.assert_eq(t_rptr1, t_rptr1);
env.assert_eq(t_rptr10, t_rptr10);
// will cause an error when regions are resolved
env.make_subtype(t_rptr10, t_rptr1);
})
}
#[test]
fn sub_free_bound_false() {
//! Test that:
//!
//! fn(&'a isize) <: for<'b> fn(&'b isize)
//!
//! does NOT hold.
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_free1 = env.t_rptr_free(1, 1);
let t_rptr_bound1 = env.t_rptr_late_bound(1);
env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
#[test]
fn sub_bound_free_true() {
//! Test that:
//!
//! for<'a> fn(&'a isize) <: fn(&'b isize)
//!
//! DOES hold.
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_free1 = env.t_rptr_free(1, 1);
env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
})
}
#[test]
fn sub_free_bound_false_infer() {
//! Test that:
//!
//! fn(_#1) <: for<'b> fn(&'b isize)
//!
//! does NOT hold for any instantiation of `_#1`.
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_infer1 = env.infcx.next_ty_var();
let t_rptr_bound1 = env.t_rptr_late_bound(1);
env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
#[test]
fn lub_free_bound_infer() {
//! Test result of:
//!
//! LUB(fn(_#1), for<'b> fn(&'b isize))
//!
//! This should yield `fn(&'_ isize)`. We check
//! that it yields `fn(&'x isize)` for some free `'x`,
//! anyhow.
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_infer1 = env.infcx.next_ty_var();
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_free1 = env.t_rptr_free(1, 1);
env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
});
}
#[test]
fn lub_bound_bound() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_bound2 = env.t_rptr_late_bound(2);
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
#[test]
fn lub_bound_free() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_free1 = env.t_rptr_free(1, 1);
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
})
}
#[test]
fn lub_bound_static() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_static = env.t_rptr_static();
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_static], env.tcx().types.isize),
env.t_fn(&[t_rptr_static], env.tcx().types.isize));
})
}
#[test]
fn lub_bound_bound_inverse_order() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_bound2 = env.t_rptr_late_bound(2);
env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
})
}
#[test]
fn lub_free_free() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_free1 = env.t_rptr_free(1, 1);
let t_rptr_free2 = env.t_rptr_free(1, 2);
let t_rptr_static = env.t_rptr_static();
env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
env.t_fn(&[t_rptr_static], env.tcx().types.isize));
})
}
#[test]
fn lub_returning_scope() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_scope10 = env.t_rptr_scope(10);
let t_rptr_scope11 = env.t_rptr_scope(11);
let t_rptr_empty = env.t_rptr_empty();
env.check_lub(env.t_fn(&[t_rptr_scope10], env.tcx().types.isize),
env.t_fn(&[t_rptr_scope11], env.tcx().types.isize),
env.t_fn(&[t_rptr_empty], env.tcx().types.isize));
});
}
#[test]
fn glb_free_free_with_common_scope() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_free1 = env.t_rptr_free(1, 1);
let t_rptr_free2 = env.t_rptr_free(1, 2);
let t_rptr_scope = env.t_rptr_scope(1);
env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
})
}
#[test]
fn glb_bound_bound() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_bound2 = env.t_rptr_late_bound(2);
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
#[test]
fn glb_bound_free() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
env.create_simple_region_hierarchy();
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_free1 = env.t_rptr_free(1, 1);
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
#[test]
fn glb_bound_free_infer() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_infer1 = env.infcx.next_ty_var();
// compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
// which should yield for<'b> fn(&'b isize) -> isize
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_infer1], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
// as a side-effect, computing GLB should unify `_` with
// `&'_ isize`
let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
match t_resolve1.sty {
ty::TyRef(..) => {}
_ => {
panic!("t_resolve1={:?}", t_resolve1);
}
}
})
}
#[test]
fn glb_bound_static() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let t_rptr_bound1 = env.t_rptr_late_bound(1);
let t_rptr_static = env.t_rptr_static();
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
env.t_fn(&[t_rptr_static], env.tcx().types.isize),
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
})
}
/// Test substituting a bound region into a function, which introduces another level of binding.
/// This requires adjusting the Debruijn index.
#[test]
fn subst_ty_renumber_bound() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
// Situation:
// Theta = [A -> &'a foo]
let t_rptr_bound1 = env.t_rptr_late_bound(1);
// t_source = fn(A)
let t_source = {
let t_param = env.t_param(subst::TypeSpace, 0);
env.t_fn(&[t_param], env.t_nil())
};
let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
let t_substituted = t_source.subst(env.infcx.tcx, &substs);
// t_expected = fn(&'a isize)
let t_expected = {
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
env.t_fn(&[t_ptr_bound2], env.t_nil())
};
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
t_source,
substs,
t_substituted,
t_expected);
assert_eq!(t_substituted, t_expected);
})
}
/// Test substituting a bound region into a function, which introduces another level of binding.
/// This requires adjusting the Debruijn index.
#[test]
fn subst_ty_renumber_some_bounds() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
// Situation:
// Theta = [A -> &'a foo]
let t_rptr_bound1 = env.t_rptr_late_bound(1);
// t_source = (A, fn(A))
let t_source = {
let t_param = env.t_param(subst::TypeSpace, 0);
env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
};
let substs = subst::Substs::new_type(vec![t_rptr_bound1], vec![]);
let t_substituted = t_source.subst(env.infcx.tcx, &substs);
// t_expected = (&'a isize, fn(&'a isize))
//
// but not that the Debruijn index is different in the different cases.
let t_expected = {
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
};
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
t_source,
substs,
t_substituted,
t_expected);
assert_eq!(t_substituted, t_expected);
})
}
/// Test that we correctly compute whether a type has escaping regions or not.
#[test]
fn escaping() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
// Situation:
// Theta = [A -> &'a foo]
env.create_simple_region_hierarchy();
assert!(!env.t_nil().has_escaping_regions());
let t_rptr_free1 = env.t_rptr_free(1, 1);
assert!(!t_rptr_free1.has_escaping_regions());
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
assert!(t_rptr_bound1.has_escaping_regions());
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
assert!(t_rptr_bound2.has_escaping_regions());
// t_fn = fn(A)
let t_param = env.t_param(subst::TypeSpace, 0);
assert!(!t_param.has_escaping_regions());
let t_fn = env.t_fn(&[t_param], env.t_nil());
assert!(!t_fn.has_escaping_regions());
})
}
/// Test applying a substitution where the value being substituted for an early-bound region is a
/// late-bound region.
#[test]
fn subst_region_renumber_region() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
// type t_source<'a> = fn(&'a isize)
let t_source = {
let re_early = env.re_early_bound(subst::TypeSpace, 0, "'a");
env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
};
let substs = subst::Substs::new_type(vec![], vec![re_bound1]);
let t_substituted = t_source.subst(env.infcx.tcx, &substs);
// t_expected = fn(&'a isize)
//
// but not that the Debruijn index is different in the different cases.
let t_expected = {
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
env.t_fn(&[t_rptr_bound2], env.t_nil())
};
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
t_source,
substs,
t_substituted,
t_expected);
assert_eq!(t_substituted, t_expected);
})
}
#[test]
fn walk_ty() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let tcx = env.infcx.tcx;
let int_ty = tcx.types.isize;
let uint_ty = tcx.types.usize;
let tup1_ty = tcx.mk_tup(vec![int_ty, uint_ty, int_ty, uint_ty]);
let tup2_ty = tcx.mk_tup(vec![tup1_ty, tup1_ty, uint_ty]);
let uniq_ty = tcx.mk_box(tup2_ty);
let walked: Vec<_> = uniq_ty.walk().collect();
assert_eq!(walked,
[uniq_ty, tup2_ty, tup1_ty, int_ty, uint_ty, int_ty, uint_ty, tup1_ty, int_ty,
uint_ty, int_ty, uint_ty, uint_ty]);
})
}
#[test]
fn walk_ty_skip_subtree() {
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
let tcx = env.infcx.tcx;
let int_ty = tcx.types.isize;
let uint_ty = tcx.types.usize;
let tup1_ty = tcx.mk_tup(vec![int_ty, uint_ty, int_ty, uint_ty]);
let tup2_ty = tcx.mk_tup(vec![tup1_ty, tup1_ty, uint_ty]);
let uniq_ty = tcx.mk_box(tup2_ty);
// types we expect to see (in order), plus a boolean saying
// whether to skip the subtree.
let mut expected = vec![(uniq_ty, false),
(tup2_ty, false),
(tup1_ty, false),
(int_ty, false),
(uint_ty, false),
(int_ty, false),
(uint_ty, false),
(tup1_ty, true), // skip the isize/usize/isize/usize
(uint_ty, false)];
expected.reverse();
let mut walker = uniq_ty.walk();
while let Some(t) = walker.next() {
debug!("walked to {:?}", t);
let (expected_ty, skip) = expected.pop().unwrap();
assert_eq!(t, expected_ty);
if skip {
walker.skip_current_subtree();
}
}
assert!(expected.is_empty());
})
}