2012-12-04 01:48:01 +01:00
|
|
|
// 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.
|
|
|
|
|
2014-11-26 03:17:11 +01:00
|
|
|
//! # Standalone Tests for the Inference Module
|
2012-11-01 23:16:46 +01:00
|
|
|
|
2017-09-14 05:26:39 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
2014-12-05 06:00:06 +01:00
|
|
|
use driver;
|
2015-02-25 12:44:44 +01:00
|
|
|
use rustc_lint;
|
2016-05-08 06:18:55 +02:00
|
|
|
use rustc_resolve::MakeGlobMap;
|
2017-04-26 23:22:45 +02:00
|
|
|
use rustc_trans;
|
2016-03-22 16:30:57 +01:00
|
|
|
use rustc::middle::free_region::FreeRegionMap;
|
2017-08-31 20:37:38 +02:00
|
|
|
use rustc::middle::region;
|
2016-03-22 16:30:57 +01:00
|
|
|
use rustc::middle::resolve_lifetime;
|
2016-10-25 02:23:29 +02:00
|
|
|
use rustc::ty::subst::{Kind, Subst};
|
2016-11-07 19:25:06 +01:00
|
|
|
use rustc::traits::{ObligationCause, Reveal};
|
2016-03-22 16:30:57 +01:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-11-07 19:25:06 +01:00
|
|
|
use rustc::infer::{self, InferOk, InferResult};
|
2016-11-28 19:08:08 +01:00
|
|
|
use rustc::infer::type_variable::TypeVariableOrigin;
|
2015-11-26 18:19:54 +01:00
|
|
|
use rustc_metadata::cstore::CStore;
|
2016-03-29 07:50:44 +02:00
|
|
|
use rustc::hir::map as hir_map;
|
2017-05-02 19:58:49 +02:00
|
|
|
use rustc::mir::transform::Passes;
|
2015-11-10 21:48:44 +01:00
|
|
|
use rustc::session::{self, config};
|
2017-09-14 05:26:39 +02:00
|
|
|
use rustc::session::config::{OutputFilenames, OutputTypes};
|
2017-09-23 16:50:05 +02:00
|
|
|
use rustc_trans_utils::trans_crate::TransCrate;
|
2015-11-26 18:19:54 +01:00
|
|
|
use std::rc::Rc;
|
2016-02-05 13:13:36 +01:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::abi::Abi;
|
2017-04-24 19:01:19 +02:00
|
|
|
use syntax::codemap::{CodeMap, FilePathMapping};
|
2016-06-22 00:08:13 +02:00
|
|
|
use errors;
|
2016-07-07 20:57:09 +02:00
|
|
|
use errors::emitter::Emitter;
|
2016-07-15 14:47:12 +02:00
|
|
|
use errors::{Level, DiagnosticBuilder};
|
2015-06-18 02:48:16 +02:00
|
|
|
use syntax::feature_gate::UnstableFeatures;
|
2016-11-17 15:04:36 +01:00
|
|
|
use syntax::symbol::Symbol;
|
2016-11-28 19:08:08 +01:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2016-12-24 04:48:21 +01:00
|
|
|
use arena::DroplessArena;
|
2014-06-20 12:35:06 +02:00
|
|
|
|
2016-03-29 07:50:44 +02:00
|
|
|
use rustc::hir;
|
2015-07-31 09:04:06 +02:00
|
|
|
|
2016-10-20 03:38:43 +02:00
|
|
|
struct Env<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
|
2016-05-11 03:14:41 +02:00
|
|
|
infcx: &'a infer::InferCtxt<'a, 'gcx, 'tcx>,
|
2017-08-31 20:37:38 +02:00
|
|
|
region_scope_tree: &'a mut region::ScopeTree,
|
2017-05-24 22:15:04 +02:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RH<'a> {
|
2017-08-29 18:24:49 +02:00
|
|
|
id: hir::ItemLocalId,
|
2015-11-10 21:48:44 +01:00
|
|
|
sub: &'a [RH<'a>],
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:01:14 +02:00
|
|
|
const EMPTY_SOURCE_STR: &'static str = "#![feature(no_core)] #![no_core]";
|
2014-06-20 12:35:06 +02:00
|
|
|
|
|
|
|
struct ExpectErrorEmitter {
|
2015-11-10 21:48:44 +01:00
|
|
|
messages: Vec<String>,
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 12:35:06 +02:00
|
|
|
fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
|
|
|
|
match lvl {
|
2015-12-15 04:51:13 +01:00
|
|
|
Level::Bug | Level::Fatal | Level::Error => {}
|
2015-12-23 07:27:20 +01:00
|
|
|
_ => {
|
2015-11-10 21:48:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Error: {}", msg);
|
2015-02-02 03:53:25 +01:00
|
|
|
match e.messages.iter().position(|m| msg.contains(m)) {
|
2014-06-20 12:35:06 +02:00
|
|
|
Some(i) => {
|
|
|
|
e.messages.remove(i);
|
|
|
|
}
|
|
|
|
None => {
|
2016-04-21 10:47:01 +02:00
|
|
|
debug!("Unexpected error: {} Expected: {:?}", msg, e.messages);
|
2015-11-10 21:48:44 +01:00
|
|
|
panic!("Unexpected error: {} Expected: {:?}", msg, e.messages);
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:57:09 +02:00
|
|
|
impl Emitter for ExpectErrorEmitter {
|
|
|
|
fn emit(&mut self, db: &DiagnosticBuilder) {
|
2017-01-11 22:55:41 +01:00
|
|
|
remove_message(self, &db.message(), db.level);
|
2016-07-07 20:57:09 +02:00
|
|
|
for child in &db.children {
|
2017-01-11 22:55:41 +01:00
|
|
|
remove_message(self, &child.message(), child.level);
|
2016-07-07 20:57:09 +02:00
|
|
|
}
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
|
2014-07-16 22:37:28 +02:00
|
|
|
let v = msgs.iter().map(|m| m.to_string()).collect();
|
2016-10-20 03:38:43 +02:00
|
|
|
(box ExpectErrorEmitter { messages: v } as Box<Emitter + Send>, msgs.len())
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 22:32:45 +01:00
|
|
|
fn test_env<F>(source_string: &str,
|
2015-11-10 21:48:44 +01:00
|
|
|
(emitter, expected_err_count): (Box<Emitter + Send>, usize),
|
|
|
|
body: F)
|
|
|
|
where F: FnOnce(Env)
|
2014-12-09 22:32:45 +01:00
|
|
|
{
|
2015-11-10 21:48:44 +01:00
|
|
|
let mut options = config::basic_options();
|
2014-12-09 10:55:49 +01:00
|
|
|
options.debugging_opts.verbose = true;
|
2015-06-18 02:48:16 +02:00
|
|
|
options.unstable_features = UnstableFeatures::Allow;
|
2015-12-15 04:51:13 +01:00
|
|
|
let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter);
|
2014-06-20 12:35:06 +02:00
|
|
|
|
2017-09-23 16:50:05 +02:00
|
|
|
let cstore = Rc::new(CStore::new(::DefaultTransCrate::metadata_loader()));
|
2016-10-20 03:38:43 +02:00
|
|
|
let sess = session::build_session_(options,
|
|
|
|
None,
|
|
|
|
diagnostic_handler,
|
2017-09-05 16:48:24 +02:00
|
|
|
Rc::new(CodeMap::new(FilePathMapping::empty())));
|
2017-04-30 20:04:35 +02:00
|
|
|
rustc_trans::init(&sess);
|
2015-02-25 12:44:44 +01:00
|
|
|
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
|
2016-03-10 04:49:40 +01:00
|
|
|
let input = config::Input::Str {
|
|
|
|
name: driver::anon_src(),
|
|
|
|
input: source_string.to_string(),
|
|
|
|
};
|
2017-08-08 07:10:08 +02:00
|
|
|
let krate = driver::phase_1_parse_input(&driver::CompileController::basic(),
|
|
|
|
&sess,
|
|
|
|
&input).unwrap();
|
2016-06-28 03:45:54 +02:00
|
|
|
let driver::ExpansionResult { defs, resolutions, mut hir_forest, .. } = {
|
2016-10-20 03:38:43 +02:00
|
|
|
driver::phase_2_configure_and_expand(&sess,
|
|
|
|
&cstore,
|
|
|
|
krate,
|
|
|
|
None,
|
|
|
|
"test",
|
|
|
|
None,
|
|
|
|
MakeGlobMap::No,
|
|
|
|
|_| Ok(()))
|
|
|
|
.expect("phase 2 aborted")
|
2016-06-28 03:45:54 +02:00
|
|
|
};
|
2016-05-08 06:18:55 +02:00
|
|
|
|
2016-12-24 04:48:21 +01:00
|
|
|
let arena = DroplessArena::new();
|
|
|
|
let arenas = ty::GlobalArenas::new();
|
2017-09-19 12:13:09 +02:00
|
|
|
let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
|
2014-06-20 12:35:06 +02:00
|
|
|
|
|
|
|
// run just enough stuff to build a tcx:
|
2017-09-05 16:48:24 +02:00
|
|
|
let named_region_map = resolve_lifetime::krate(&sess, &*cstore, &hir_map);
|
2017-09-14 05:26:39 +02:00
|
|
|
let (tx, _rx) = mpsc::channel();
|
|
|
|
let outputs = OutputFilenames {
|
|
|
|
out_directory: PathBuf::new(),
|
|
|
|
out_filestem: String::new(),
|
|
|
|
single_output_file: None,
|
|
|
|
extra: String::new(),
|
|
|
|
outputs: OutputTypes::new(&[]),
|
|
|
|
};
|
2016-03-01 00:36:51 +01:00
|
|
|
TyCtxt::create_and_enter(&sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
&*cstore,
|
2016-09-29 01:30:53 +02:00
|
|
|
ty::maps::Providers::default(),
|
|
|
|
ty::maps::Providers::default(),
|
2017-05-02 19:58:49 +02:00
|
|
|
Rc::new(Passes::new()),
|
2016-03-25 04:22:44 +01:00
|
|
|
&arenas,
|
2016-12-24 04:48:21 +01:00
|
|
|
&arena,
|
2016-12-15 12:13:24 +01:00
|
|
|
resolutions,
|
2016-03-25 04:22:44 +01:00
|
|
|
named_region_map.unwrap(),
|
2017-01-26 02:21:50 +01:00
|
|
|
hir_map,
|
2016-03-25 04:22:44 +01:00
|
|
|
"test_crate",
|
2017-09-14 05:26:39 +02:00
|
|
|
tx,
|
|
|
|
&outputs,
|
2016-03-25 04:22:44 +01:00
|
|
|
|tcx| {
|
2017-06-09 09:55:16 +02:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-08-31 20:37:38 +02:00
|
|
|
let mut region_scope_tree = region::ScopeTree::default();
|
2017-05-24 22:15:04 +02:00
|
|
|
body(Env {
|
|
|
|
infcx: &infcx,
|
2017-08-31 20:37:38 +02:00
|
|
|
region_scope_tree: &mut region_scope_tree,
|
2017-05-24 22:15:04 +02:00
|
|
|
param_env: ty::ParamEnv::empty(Reveal::UserFacing),
|
|
|
|
});
|
2016-03-25 04:22:44 +01:00
|
|
|
let free_regions = FreeRegionMap::new();
|
2017-05-02 02:11:36 +02:00
|
|
|
let def_id = tcx.hir.local_def_id(ast::CRATE_NODE_ID);
|
2017-08-31 20:37:38 +02:00
|
|
|
infcx.resolve_regions_and_report_errors(def_id, ®ion_scope_tree, &free_regions);
|
2016-03-25 04:22:44 +01:00
|
|
|
assert_eq!(tcx.sess.err_count(), expected_err_count);
|
|
|
|
});
|
|
|
|
});
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 03:14:41 +02:00
|
|
|
impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
|
|
|
|
pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
|
2014-12-25 13:20:48 +01:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2017-08-31 20:37:38 +02:00
|
|
|
pub fn create_region_hierarchy(&mut self, rh: &RH, parent: region::Scope) {
|
|
|
|
let me = region::Scope::Node(rh.id);
|
|
|
|
self.region_scope_tree.record_scope_parent(me, Some(parent));
|
2015-01-31 18:20:46 +01:00
|
|
|
for child_rh in rh.sub {
|
2015-08-20 00:46:28 +02:00
|
|
|
self.create_region_hierarchy(child_rh, me);
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:11:36 +02:00
|
|
|
pub fn create_simple_region_hierarchy(&mut self) {
|
2012-11-01 23:16:46 +01:00
|
|
|
// creates a region hierarchy where 1 is root, 10 and 11 are
|
|
|
|
// children of 1, etc
|
2016-08-31 13:00:29 +02:00
|
|
|
|
2017-08-31 20:37:38 +02:00
|
|
|
let dscope = region::Scope::Destruction(hir::ItemLocalId(1));
|
|
|
|
self.region_scope_tree.record_scope_parent(dscope, None);
|
2015-11-10 21:48:44 +01:00
|
|
|
self.create_region_hierarchy(&RH {
|
2017-08-29 18:24:49 +02:00
|
|
|
id: hir::ItemLocalId(1),
|
|
|
|
sub: &[RH {
|
|
|
|
id: hir::ItemLocalId(10),
|
|
|
|
sub: &[],
|
|
|
|
},
|
|
|
|
RH {
|
|
|
|
id: hir::ItemLocalId(11),
|
|
|
|
sub: &[],
|
|
|
|
}],
|
|
|
|
}, dscope);
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-11-15 22:47:59 +01:00
|
|
|
#[allow(dead_code)] // this seems like it could be useful, even if we don't use it now
|
2014-06-20 12:35:06 +02:00
|
|
|
pub fn lookup_item(&self, names: &[String]) -> ast::NodeId {
|
2017-01-26 01:41:06 +01:00
|
|
|
return match search_mod(self, &self.infcx.tcx.hir.krate().module, 0, names) {
|
2012-11-01 23:16:46 +01:00
|
|
|
Some(id) => id,
|
|
|
|
None => {
|
2015-07-10 14:19:21 +02:00
|
|
|
panic!("no item found: `{}`", names.join("::"));
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-20 12:35:06 +02:00
|
|
|
fn search_mod(this: &Env,
|
2015-07-31 09:04:06 +02:00
|
|
|
m: &hir::Mod,
|
2015-03-26 01:06:52 +01:00
|
|
|
idx: usize,
|
2014-06-20 12:35:06 +02:00
|
|
|
names: &[String])
|
|
|
|
-> Option<ast::NodeId> {
|
2013-03-29 02:39:09 +01:00
|
|
|
assert!(idx < names.len());
|
2015-11-18 18:47:37 +01:00
|
|
|
for item in &m.item_ids {
|
2017-01-26 01:41:06 +01:00
|
|
|
let item = this.infcx.tcx.hir.expect_item(item.id);
|
2015-09-20 03:50:30 +02:00
|
|
|
if item.name.to_string() == names[idx] {
|
2015-11-18 18:47:37 +01:00
|
|
|
return search(this, item, idx + 1, names);
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
fn search(this: &Env, it: &hir::Item, idx: usize, names: &[String]) -> Option<ast::NodeId> {
|
2012-11-01 23:16:46 +01:00
|
|
|
if idx == names.len() {
|
|
|
|
return Some(it.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return match it.node {
|
2015-11-10 21:48:44 +01:00
|
|
|
hir::ItemUse(..) |
|
|
|
|
hir::ItemExternCrate(..) |
|
|
|
|
hir::ItemConst(..) |
|
|
|
|
hir::ItemStatic(..) |
|
|
|
|
hir::ItemFn(..) |
|
|
|
|
hir::ItemForeignMod(..) |
|
2017-03-22 05:47:25 +01:00
|
|
|
hir::ItemGlobalAsm(..) |
|
2016-10-20 03:38:43 +02:00
|
|
|
hir::ItemTy(..) => None,
|
2012-11-01 23:16:46 +01:00
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
hir::ItemEnum(..) |
|
|
|
|
hir::ItemStruct(..) |
|
2016-08-10 20:00:17 +02:00
|
|
|
hir::ItemUnion(..) |
|
2015-11-10 21:48:44 +01:00
|
|
|
hir::ItemTrait(..) |
|
|
|
|
hir::ItemImpl(..) |
|
2016-10-20 03:38:43 +02:00
|
|
|
hir::ItemDefaultImpl(..) => None,
|
2012-11-01 23:16:46 +01:00
|
|
|
|
2016-10-20 03:38:43 +02:00
|
|
|
hir::ItemMod(ref m) => search_mod(this, m, idx, names),
|
2012-11-01 23:16:46 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn make_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
|
2017-05-24 22:15:04 +02:00
|
|
|
match self.infcx.at(&ObligationCause::dummy(), self.param_env).sub(a, b) {
|
2014-06-20 12:35:06 +02:00
|
|
|
Ok(_) => true,
|
2015-11-10 21:48:44 +01:00
|
|
|
Err(ref e) => panic!("Encountered error: {}", e),
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn is_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
|
2017-05-24 22:15:04 +02:00
|
|
|
self.infcx.can_sub(self.param_env, a, b).is_ok()
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn assert_subtype(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
|
2012-11-01 23:16:46 +01:00
|
|
|
if !self.is_subtype(a, b) {
|
2015-06-18 19:25:05 +02:00
|
|
|
panic!("{} is not a subtype of {}, but it should be", a, b);
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn assert_eq(&self, a: Ty<'tcx>, b: Ty<'tcx>) {
|
2012-11-01 23:16:46 +01:00
|
|
|
self.assert_subtype(a, b);
|
|
|
|
self.assert_subtype(b, a);
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
|
2017-02-13 09:51:06 +01:00
|
|
|
self.infcx.tcx.mk_fn_ptr(ty::Binder(self.infcx.tcx.mk_fn_sig(
|
|
|
|
input_tys.iter().cloned(),
|
|
|
|
output_ty,
|
|
|
|
false,
|
|
|
|
hir::Unsafety::Normal,
|
|
|
|
Abi::Rust
|
|
|
|
)))
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn t_nil(&self) -> Ty<'tcx> {
|
2015-06-25 03:09:46 +02:00
|
|
|
self.infcx.tcx.mk_nil()
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn t_pair(&self, ty1: Ty<'tcx>, ty2: Ty<'tcx>) -> Ty<'tcx> {
|
2017-01-11 08:58:37 +01:00
|
|
|
self.infcx.tcx.intern_tup(&[ty1, ty2], false)
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2016-08-17 05:32:00 +02:00
|
|
|
pub fn t_param(&self, index: u32) -> Ty<'tcx> {
|
2014-12-25 13:20:48 +01:00
|
|
|
let name = format!("T{}", index);
|
2017-03-24 09:31:26 +01:00
|
|
|
self.infcx.tcx.mk_param(index, Symbol::intern(&name))
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:45:53 +02:00
|
|
|
pub fn re_early_bound(&self, index: u32, name: &'static str) -> ty::Region<'tcx> {
|
2016-11-17 15:04:36 +01:00
|
|
|
let name = Symbol::intern(name);
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
|
2017-05-11 14:05:00 +02:00
|
|
|
def_id: self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
|
|
|
|
index,
|
|
|
|
name,
|
2016-08-25 22:58:52 +02:00
|
|
|
}))
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 03:38:43 +02:00
|
|
|
pub fn re_late_bound_with_debruijn(&self,
|
|
|
|
id: u32,
|
|
|
|
debruijn: ty::DebruijnIndex)
|
2017-04-20 10:45:53 +02:00
|
|
|
-> ty::Region<'tcx> {
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_region(ty::ReLateBound(debruijn, ty::BrAnon(id)))
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:45:53 +02:00
|
|
|
pub fn t_rptr(&self, r: ty::Region<'tcx>) -> Ty<'tcx> {
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-26 07:52:57 +01:00
|
|
|
pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
|
|
|
|
let r = self.re_late_bound_with_debruijn(id, ty::DebruijnIndex::new(1));
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn t_rptr_late_bound_with_debruijn(&self,
|
2014-12-26 07:52:57 +01:00
|
|
|
id: u32,
|
2014-09-29 21:11:30 +02:00
|
|
|
debruijn: ty::DebruijnIndex)
|
|
|
|
-> Ty<'tcx> {
|
2014-12-26 07:52:57 +01:00
|
|
|
let r = self.re_late_bound_with_debruijn(id, debruijn);
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:00:29 +02:00
|
|
|
pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
|
2017-08-31 20:37:38 +02:00
|
|
|
let r = ty::ReScope(region::Scope::Node(hir::ItemLocalId(id)));
|
2015-11-10 21:48:44 +01:00
|
|
|
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
|
2014-11-15 22:47:59 +01:00
|
|
|
}
|
|
|
|
|
2017-05-07 18:57:51 +02:00
|
|
|
pub fn re_free(&self, id: u32) -> ty::Region<'tcx> {
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_region(ty::ReFree(ty::FreeRegion {
|
2017-05-07 18:57:51 +02:00
|
|
|
scope: self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
|
2015-11-10 21:48:44 +01:00
|
|
|
bound_region: ty::BrAnon(id),
|
2016-08-25 22:58:52 +02:00
|
|
|
}))
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2017-05-07 18:57:51 +02:00
|
|
|
pub fn t_rptr_free(&self, id: u32) -> Ty<'tcx> {
|
|
|
|
let r = self.re_free(id);
|
2016-08-25 22:58:52 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn t_rptr_static(&self) -> Ty<'tcx> {
|
2017-04-20 00:58:12 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_static,
|
2015-11-10 21:48:44 +01:00
|
|
|
self.tcx().types.isize)
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2015-10-17 02:19:25 +02:00
|
|
|
pub fn t_rptr_empty(&self) -> Ty<'tcx> {
|
2017-04-20 00:58:12 +02:00
|
|
|
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_empty,
|
2015-11-10 21:48:44 +01:00
|
|
|
self.tcx().types.isize)
|
2015-10-17 02:19:25 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 22:15:04 +02:00
|
|
|
pub fn sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, ()> {
|
|
|
|
self.infcx.at(&ObligationCause::dummy(), self.param_env).sub(t1, t2)
|
2014-11-29 14:06:42 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:23:22 +02:00
|
|
|
pub fn lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
|
2017-05-24 22:15:04 +02:00
|
|
|
self.infcx.at(&ObligationCause::dummy(), self.param_env).lub(t1, t2)
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
2012-11-01 23:16:46 +01:00
|
|
|
|
2016-05-03 04:23:22 +02:00
|
|
|
pub fn glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
|
2017-05-24 22:15:04 +02:00
|
|
|
self.infcx.at(&ObligationCause::dummy(), self.param_env).glb(t1, t2)
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
2012-12-06 00:13:24 +01:00
|
|
|
|
2014-11-29 14:06:42 +01:00
|
|
|
/// Checks that `t1 <: t2` is true (this may register additional
|
|
|
|
/// region checks).
|
|
|
|
pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
|
2016-05-03 04:23:22 +02:00
|
|
|
match self.sub(t1, t2) {
|
2017-05-24 22:15:04 +02:00
|
|
|
Ok(InferOk { obligations, value: () }) => {
|
2017-04-18 21:48:32 +02:00
|
|
|
// None of these tests should require nested obligations:
|
2016-03-30 05:06:42 +02:00
|
|
|
assert!(obligations.is_empty());
|
|
|
|
}
|
2014-11-29 14:06:42 +01:00
|
|
|
Err(ref e) => {
|
2015-11-10 21:48:44 +01:00
|
|
|
panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
|
2014-11-29 14:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that `t1 <: t2` is false (this may register additional
|
|
|
|
/// region checks).
|
|
|
|
pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
|
2016-05-03 04:23:22 +02:00
|
|
|
match self.sub(t1, t2) {
|
2015-11-10 21:48:44 +01:00
|
|
|
Err(_) => {}
|
2014-11-29 14:06:42 +01:00
|
|
|
Ok(_) => {
|
2015-11-10 21:48:44 +01:00
|
|
|
panic!("unexpected success computing sub({:?},{:?})", t1, t2);
|
2014-11-29 14:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 23:16:46 +01:00
|
|
|
/// Checks that `LUB(t1,t2) == t_lub`
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
|
2016-05-03 04:23:22 +02:00
|
|
|
match self.lub(t1, t2) {
|
2016-03-30 05:06:42 +02:00
|
|
|
Ok(InferOk { obligations, value: t }) => {
|
2017-04-18 21:48:32 +02:00
|
|
|
// None of these tests should require nested obligations:
|
2016-03-30 05:06:42 +02:00
|
|
|
assert!(obligations.is_empty());
|
|
|
|
|
2012-11-01 23:16:46 +01:00
|
|
|
self.assert_eq(t, t_lub);
|
2014-06-20 12:35:06 +02:00
|
|
|
}
|
2016-10-20 03:38:43 +02:00
|
|
|
Err(ref e) => panic!("unexpected error in LUB: {}", e),
|
2012-12-06 00:13:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that `GLB(t1,t2) == t_glb`
|
2014-09-29 21:11:30 +02:00
|
|
|
pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
|
2015-06-18 19:25:05 +02:00
|
|
|
debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
|
2016-05-03 04:23:22 +02:00
|
|
|
match self.glb(t1, t2) {
|
2016-10-20 03:38:43 +02:00
|
|
|
Err(e) => panic!("unexpected error computing LUB: {:?}", e),
|
2016-03-30 05:06:42 +02:00
|
|
|
Ok(InferOk { obligations, value: t }) => {
|
2017-04-18 21:48:32 +02:00
|
|
|
// None of these tests should require nested obligations:
|
2016-03-30 05:06:42 +02:00
|
|
|
assert!(obligations.is_empty());
|
|
|
|
|
2012-12-06 00:13:24 +01:00
|
|
|
self.assert_eq(t, t_glb);
|
|
|
|
|
|
|
|
// sanity check for good measure:
|
|
|
|
self.assert_subtype(t, t1);
|
|
|
|
self.assert_subtype(t, t2);
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-06-20 12:35:06 +02:00
|
|
|
fn contravariant_region_ptr_ok() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2014-06-20 12:35:06 +02:00
|
|
|
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() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&["mismatched types"]), |mut env| {
|
2015-11-10 21:48:44 +01:00
|
|
|
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);
|
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 14:06:42 +01:00
|
|
|
#[test]
|
|
|
|
fn sub_free_bound_false() {
|
|
|
|
//! Test that:
|
|
|
|
//!
|
2015-03-26 01:06:52 +01:00
|
|
|
//! fn(&'a isize) <: for<'b> fn(&'b isize)
|
2014-11-29 14:06:42 +01:00
|
|
|
//!
|
|
|
|
//! does NOT hold.
|
|
|
|
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2014-11-29 14:06:42 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
env.check_not_sub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
|
|
|
|
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
|
2014-11-29 14:06:42 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sub_bound_free_true() {
|
|
|
|
//! Test that:
|
|
|
|
//!
|
2015-03-26 01:06:52 +01:00
|
|
|
//! for<'a> fn(&'a isize) <: fn(&'b isize)
|
2014-11-29 14:06:42 +01:00
|
|
|
//!
|
|
|
|
//! DOES hold.
|
|
|
|
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2014-11-29 14:06:42 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
env.check_sub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
|
|
|
|
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
|
2014-11-29 14:06:42 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sub_free_bound_false_infer() {
|
|
|
|
//! Test that:
|
|
|
|
//!
|
2015-03-26 01:06:52 +01:00
|
|
|
//! fn(_#1) <: for<'b> fn(&'b isize)
|
2014-11-29 14:06:42 +01:00
|
|
|
//!
|
|
|
|
//! does NOT hold for any instantiation of `_#1`.
|
|
|
|
|
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
2016-11-28 19:08:08 +01:00
|
|
|
let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
|
2014-11-29 14:06:42 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
env.check_not_sub(env.t_fn(&[t_infer1], env.tcx().types.isize),
|
|
|
|
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
|
2014-11-29 14:06:42 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-01 16:11:59 +01:00
|
|
|
#[test]
|
|
|
|
fn lub_free_bound_infer() {
|
|
|
|
//! Test result of:
|
|
|
|
//!
|
2015-03-26 01:06:52 +01:00
|
|
|
//! LUB(fn(_#1), for<'b> fn(&'b isize))
|
2014-12-01 16:11:59 +01:00
|
|
|
//!
|
2015-03-26 01:06:52 +01:00
|
|
|
//! This should yield `fn(&'_ isize)`. We check
|
|
|
|
//! that it yields `fn(&'x isize)` for some free `'x`,
|
2014-12-01 16:11:59 +01:00
|
|
|
//! anyhow.
|
|
|
|
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2016-11-28 19:08:08 +01:00
|
|
|
let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
|
2014-12-01 16:11:59 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-12-01 16:11:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-01 23:16:46 +01:00
|
|
|
#[test]
|
|
|
|
fn lub_bound_bound() {
|
2014-11-15 22:47:59 +01:00
|
|
|
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);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lub_bound_free() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2014-11-15 22:47:59 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lub_bound_static() {
|
2014-11-15 22:47:59 +01:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2014-06-20 12:35:06 +02:00
|
|
|
let t_rptr_static = env.t_rptr_static();
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lub_bound_bound_inverse_order() {
|
2014-11-15 22:47:59 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lub_free_free() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
|
|
|
let t_rptr_free2 = env.t_rptr_free(2);
|
2014-06-20 12:35:06 +02:00
|
|
|
let t_rptr_static = env.t_rptr_static();
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lub_returning_scope() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-10-17 02:19:25 +02:00
|
|
|
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));
|
|
|
|
});
|
2012-11-01 23:16:46 +01:00
|
|
|
}
|
|
|
|
|
2012-12-06 00:13:24 +01:00
|
|
|
#[test]
|
|
|
|
fn glb_free_free_with_common_scope() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
|
|
|
let t_rptr_free2 = env.t_rptr_free(2);
|
2015-08-20 00:46:28 +02:00
|
|
|
let t_rptr_scope = env.t_rptr_scope(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-12-06 00:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn glb_bound_bound() {
|
2014-11-15 22:47:59 +01:00
|
|
|
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);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-12-06 00:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn glb_bound_free() {
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2014-11-15 22:47:59 +01:00
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-12-06 00:13:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-01 16:11:59 +01:00
|
|
|
#[test]
|
|
|
|
fn glb_bound_free_infer() {
|
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2016-11-28 19:08:08 +01:00
|
|
|
let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
|
2014-12-01 16:11:59 +01:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
// 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));
|
2014-12-01 16:11:59 +01:00
|
|
|
|
|
|
|
// as a side-effect, computing GLB should unify `_` with
|
2015-03-26 01:06:52 +01:00
|
|
|
// `&'_ isize`
|
2014-12-01 16:11:59 +01:00
|
|
|
let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
|
|
|
|
match t_resolve1.sty {
|
2015-11-10 21:48:44 +01:00
|
|
|
ty::TyRef(..) => {}
|
|
|
|
_ => {
|
|
|
|
panic!("t_resolve1={:?}", t_resolve1);
|
|
|
|
}
|
2014-12-01 16:11:59 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-12-06 00:13:24 +01:00
|
|
|
#[test]
|
|
|
|
fn glb_bound_static() {
|
2014-11-15 22:47:59 +01:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
2014-06-20 12:35:06 +02:00
|
|
|
let t_rptr_static = env.t_rptr_static();
|
2015-03-26 01:06:52 +01:00
|
|
|
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));
|
2014-06-20 12:35:06 +02:00
|
|
|
})
|
2012-12-06 00:13:24 +01:00
|
|
|
}
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2014-11-26 03:17:11 +01:00
|
|
|
/// Test substituting a bound region into a function, which introduces another level of binding.
|
|
|
|
/// This requires adjusting the Debruijn index.
|
2014-11-15 22:47:59 +01:00
|
|
|
#[test]
|
|
|
|
fn subst_ty_renumber_bound() {
|
|
|
|
|
2014-11-18 19:20:59 +01:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
2014-11-15 22:47:59 +01:00
|
|
|
// Situation:
|
|
|
|
// Theta = [A -> &'a foo]
|
|
|
|
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
|
|
|
|
|
|
|
// t_source = fn(A)
|
|
|
|
let t_source = {
|
2016-08-17 05:32:00 +02:00
|
|
|
let t_param = env.t_param(0);
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_fn(&[t_param], env.t_nil())
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2016-10-25 02:23:29 +02:00
|
|
|
let substs = env.infcx.tcx.intern_substs(&[Kind::from(t_rptr_bound1)]);
|
2016-08-08 22:39:49 +02:00
|
|
|
let t_substituted = t_source.subst(env.infcx.tcx, substs);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
// t_expected = fn(&'a isize)
|
2014-11-15 22:47:59 +01:00
|
|
|
let t_expected = {
|
|
|
|
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_fn(&[t_ptr_bound2], env.t_nil())
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2015-06-18 19:25:05 +02:00
|
|
|
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
|
|
|
|
t_source,
|
|
|
|
substs,
|
|
|
|
t_substituted,
|
|
|
|
t_expected);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
assert_eq!(t_substituted, t_expected);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:17:11 +01:00
|
|
|
/// Test substituting a bound region into a function, which introduces another level of binding.
|
|
|
|
/// This requires adjusting the Debruijn index.
|
2014-11-15 22:47:59 +01:00
|
|
|
#[test]
|
|
|
|
fn subst_ty_renumber_some_bounds() {
|
2014-11-18 19:20:59 +01:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
2014-11-15 22:47:59 +01:00
|
|
|
// Situation:
|
|
|
|
// Theta = [A -> &'a foo]
|
|
|
|
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound(1);
|
|
|
|
|
|
|
|
// t_source = (A, fn(A))
|
|
|
|
let t_source = {
|
2016-08-17 05:32:00 +02:00
|
|
|
let t_param = env.t_param(0);
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_pair(t_param, env.t_fn(&[t_param], env.t_nil()))
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2016-10-25 02:23:29 +02:00
|
|
|
let substs = env.infcx.tcx.intern_substs(&[Kind::from(t_rptr_bound1)]);
|
2016-08-08 22:39:49 +02:00
|
|
|
let t_substituted = t_source.subst(env.infcx.tcx, substs);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
// t_expected = (&'a isize, fn(&'a isize))
|
2014-11-15 22:47:59 +01:00
|
|
|
//
|
|
|
|
// 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));
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2015-06-18 19:25:05 +02:00
|
|
|
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
|
|
|
|
t_source,
|
|
|
|
substs,
|
|
|
|
t_substituted,
|
|
|
|
t_expected);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
assert_eq!(t_substituted, t_expected);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:17:11 +01:00
|
|
|
/// Test that we correctly compute whether a type has escaping regions or not.
|
2014-11-15 22:47:59 +01:00
|
|
|
#[test]
|
|
|
|
fn escaping() {
|
|
|
|
|
2017-05-02 02:11:36 +02:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
|
2014-11-15 22:47:59 +01:00
|
|
|
// Situation:
|
|
|
|
// Theta = [A -> &'a foo]
|
2015-08-20 00:46:28 +02:00
|
|
|
env.create_simple_region_hierarchy();
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(!env.t_nil().has_escaping_regions());
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2017-05-07 18:57:51 +02:00
|
|
|
let t_rptr_free1 = env.t_rptr_free(1);
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(!t_rptr_free1.has_escaping_regions());
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(t_rptr_bound1.has_escaping_regions());
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, ty::DebruijnIndex::new(2));
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(t_rptr_bound2.has_escaping_regions());
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
// t_fn = fn(A)
|
2016-08-17 05:32:00 +02:00
|
|
|
let t_param = env.t_param(0);
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(!t_param.has_escaping_regions());
|
2014-11-18 19:20:59 +01:00
|
|
|
let t_fn = env.t_fn(&[t_param], env.t_nil());
|
2015-06-24 01:54:32 +02:00
|
|
|
assert!(!t_fn.has_escaping_regions());
|
2014-11-15 22:47:59 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:17:11 +01:00
|
|
|
/// Test applying a substitution where the value being substituted for an early-bound region is a
|
|
|
|
/// late-bound region.
|
2014-11-15 22:47:59 +01:00
|
|
|
#[test]
|
|
|
|
fn subst_region_renumber_region() {
|
2014-11-18 19:20:59 +01:00
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
2014-11-15 22:47:59 +01:00
|
|
|
let re_bound1 = env.re_late_bound_with_debruijn(1, ty::DebruijnIndex::new(1));
|
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
// type t_source<'a> = fn(&'a isize)
|
2014-11-15 22:47:59 +01:00
|
|
|
let t_source = {
|
2016-08-17 05:32:00 +02:00
|
|
|
let re_early = env.re_early_bound(0, "'a");
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_fn(&[env.t_rptr(re_early)], env.t_nil())
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2016-10-25 02:23:29 +02:00
|
|
|
let substs = env.infcx.tcx.intern_substs(&[Kind::from(re_bound1)]);
|
2016-08-08 22:39:49 +02:00
|
|
|
let t_substituted = t_source.subst(env.infcx.tcx, substs);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
// t_expected = fn(&'a isize)
|
2014-11-15 22:47:59 +01:00
|
|
|
//
|
|
|
|
// 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));
|
2014-11-18 19:20:59 +01:00
|
|
|
env.t_fn(&[t_rptr_bound2], env.t_nil())
|
2014-11-15 22:47:59 +01:00
|
|
|
};
|
|
|
|
|
2015-06-18 19:25:05 +02:00
|
|
|
debug!("subst_bound: t_source={:?} substs={:?} t_substituted={:?} t_expected={:?}",
|
|
|
|
t_source,
|
|
|
|
substs,
|
|
|
|
t_substituted,
|
|
|
|
t_expected);
|
2014-11-15 22:47:59 +01:00
|
|
|
|
|
|
|
assert_eq!(t_substituted, t_expected);
|
|
|
|
})
|
|
|
|
}
|
2014-12-26 09:20:01 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn walk_ty() {
|
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
|
|
|
let tcx = env.infcx.tcx;
|
2015-03-26 01:06:52 +01:00
|
|
|
let int_ty = tcx.types.isize;
|
2017-08-05 11:27:28 +02:00
|
|
|
let usize_ty = tcx.types.usize;
|
|
|
|
let tup1_ty = tcx.intern_tup(&[int_ty, usize_ty, int_ty, usize_ty], false);
|
|
|
|
let tup2_ty = tcx.intern_tup(&[tup1_ty, tup1_ty, usize_ty], false);
|
2017-01-21 15:40:31 +01:00
|
|
|
let walked: Vec<_> = tup2_ty.walk().collect();
|
2015-11-10 21:48:44 +01:00
|
|
|
assert_eq!(walked,
|
2017-08-05 11:27:28 +02:00
|
|
|
[tup2_ty, tup1_ty, int_ty, usize_ty, int_ty, usize_ty, tup1_ty, int_ty,
|
|
|
|
usize_ty, int_ty, usize_ty, usize_ty]);
|
2014-12-26 09:20:01 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn walk_ty_skip_subtree() {
|
|
|
|
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
|
|
|
|
let tcx = env.infcx.tcx;
|
2015-03-26 01:06:52 +01:00
|
|
|
let int_ty = tcx.types.isize;
|
2017-08-05 11:27:28 +02:00
|
|
|
let usize_ty = tcx.types.usize;
|
|
|
|
let tup1_ty = tcx.intern_tup(&[int_ty, usize_ty, int_ty, usize_ty], false);
|
|
|
|
let tup2_ty = tcx.intern_tup(&[tup1_ty, tup1_ty, usize_ty], false);
|
2014-12-26 09:20:01 +01:00
|
|
|
|
|
|
|
// types we expect to see (in order), plus a boolean saying
|
|
|
|
// whether to skip the subtree.
|
2017-01-21 15:40:31 +01:00
|
|
|
let mut expected = vec![(tup2_ty, false),
|
2014-12-26 09:20:01 +01:00
|
|
|
(tup1_ty, false),
|
|
|
|
(int_ty, false),
|
2017-08-05 11:27:28 +02:00
|
|
|
(usize_ty, false),
|
2014-12-26 09:20:01 +01:00
|
|
|
(int_ty, false),
|
2017-08-05 11:27:28 +02:00
|
|
|
(usize_ty, false),
|
2015-03-26 01:06:52 +01:00
|
|
|
(tup1_ty, true), // skip the isize/usize/isize/usize
|
2017-08-05 11:27:28 +02:00
|
|
|
(usize_ty, false)];
|
2014-12-26 09:20:01 +01:00
|
|
|
expected.reverse();
|
|
|
|
|
2017-01-21 15:40:31 +01:00
|
|
|
let mut walker = tup2_ty.walk();
|
2014-12-26 09:20:01 +01:00
|
|
|
while let Some(t) = walker.next() {
|
2015-01-07 01:16:35 +01:00
|
|
|
debug!("walked to {:?}", t);
|
2014-12-26 09:20:01 +01:00
|
|
|
let (expected_ty, skip) = expected.pop().unwrap();
|
|
|
|
assert_eq!(t, expected_ty);
|
2015-11-10 21:48:44 +01:00
|
|
|
if skip {
|
|
|
|
walker.skip_current_subtree();
|
|
|
|
}
|
2014-12-26 09:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(expected.is_empty());
|
|
|
|
})
|
|
|
|
}
|