2015-09-06 21:51:58 +03:00
|
|
|
// Copyright 2012-2015 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.
|
|
|
|
|
|
|
|
//! type context book-keeping
|
|
|
|
|
2017-02-07 11:47:35 +02:00
|
|
|
use dep_graph::DepGraph;
|
2017-07-26 21:51:09 -07:00
|
|
|
use errors::DiagnosticBuilder;
|
2015-09-06 21:51:58 +03:00
|
|
|
use session::Session;
|
|
|
|
use middle;
|
2017-09-08 13:51:57 -07:00
|
|
|
use hir::{TraitCandidate, HirId, ItemLocalId};
|
2017-08-29 11:10:22 -07:00
|
|
|
use hir::def::{Def, Export};
|
2017-09-08 13:51:57 -07:00
|
|
|
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
|
2017-01-26 03:21:50 +02:00
|
|
|
use hir::map as hir_map;
|
2017-07-20 15:32:06 +02:00
|
|
|
use hir::map::DefPathHash;
|
2017-07-26 21:51:09 -07:00
|
|
|
use lint::{self, Lint};
|
2017-08-10 16:10:04 +02:00
|
|
|
use ich::{self, StableHashingContext, NodeIdHashingMode};
|
2017-09-05 16:48:24 +02:00
|
|
|
<<<<<<< 817e1b81e230d599585f860cdcad96c5ed83b93e
|
2017-08-05 16:11:24 +03:00
|
|
|
use middle::const_val::ConstVal;
|
2017-09-05 16:48:24 +02:00
|
|
|
=======
|
|
|
|
use middle::cstore::CrateStore;
|
|
|
|
>>>>>>> Remove the `cstore` reference from Session in order to prepare encapsulating CrateStore access in tcx.
|
2015-09-06 21:51:58 +03:00
|
|
|
use middle::free_region::FreeRegionMap;
|
2017-01-21 17:40:31 +03:00
|
|
|
use middle::lang_items;
|
2017-08-30 09:31:14 -07:00
|
|
|
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
|
2015-09-06 21:51:58 +03:00
|
|
|
use middle::stability;
|
2016-10-28 13:55:49 +03:00
|
|
|
use mir::Mir;
|
2017-04-27 13:04:57 -04:00
|
|
|
use mir::transform::Passes;
|
2016-10-09 11:36:12 -07:00
|
|
|
use ty::subst::{Kind, Substs};
|
2017-02-06 15:26:32 -05:00
|
|
|
use ty::ReprOptions;
|
2016-03-22 17:30:57 +02:00
|
|
|
use traits;
|
2017-07-11 10:33:09 -04:00
|
|
|
use ty::{self, Ty, TypeAndMut};
|
2016-09-02 11:08:16 +03:00
|
|
|
use ty::{TyS, TypeVariants, Slice};
|
2017-08-04 11:25:13 +03:00
|
|
|
use ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorInterior, Region, Const};
|
2017-05-02 14:37:00 -04:00
|
|
|
use ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predicate};
|
2017-04-20 04:45:53 -04:00
|
|
|
use ty::RegionKind;
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
|
|
|
|
use ty::TypeVariants::*;
|
2016-04-19 09:11:46 +03:00
|
|
|
use ty::layout::{Layout, TargetDataLayout};
|
2016-12-31 07:35:43 +08:00
|
|
|
use ty::inhabitedness::DefIdForest;
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::maps;
|
2017-04-28 06:00:48 -04:00
|
|
|
use ty::steal::Steal;
|
2017-07-21 19:29:43 -04:00
|
|
|
use ty::BindingMode;
|
2017-08-04 09:49:40 +02:00
|
|
|
use util::nodemap::{NodeMap, NodeSet, DefIdSet, ItemLocalMap};
|
2016-11-08 14:02:55 +11:00
|
|
|
use util::nodemap::{FxHashMap, FxHashSet};
|
2016-10-24 18:23:29 -06:00
|
|
|
use rustc_data_structures::accumulate_vec::AccumulateVec;
|
2017-08-10 16:10:04 +02:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
|
|
|
|
StableHasherResult};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-12-23 20:48:21 -07:00
|
|
|
use arena::{TypedArena, DroplessArena};
|
2017-08-05 16:11:24 +03:00
|
|
|
use rustc_const_math::{ConstInt, ConstUsize};
|
2016-09-29 02:30:53 +03:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::borrow::Borrow;
|
2016-10-27 04:52:10 +03:00
|
|
|
use std::cell::{Cell, RefCell};
|
2017-04-27 13:04:57 -04:00
|
|
|
use std::cmp::Ordering;
|
2017-08-10 16:10:04 +02:00
|
|
|
use std::collections::hash_map::{self, Entry};
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::hash::{Hash, Hasher};
|
2016-05-11 04:14:41 +03:00
|
|
|
use std::mem;
|
2016-05-03 04:56:42 +03:00
|
|
|
use std::ops::Deref;
|
2016-10-24 18:23:29 -06:00
|
|
|
use std::iter;
|
2017-04-27 13:04:57 -04:00
|
|
|
use std::rc::Rc;
|
2017-02-13 10:51:06 +02:00
|
|
|
use syntax::abi;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast::{self, Name, NodeId};
|
|
|
|
use syntax::attr;
|
2017-07-26 21:51:09 -07:00
|
|
|
use syntax::codemap::MultiSpan;
|
2016-11-16 10:52:37 +00:00
|
|
|
use syntax::symbol::{Symbol, keywords};
|
2017-05-24 16:23:02 +01:00
|
|
|
use syntax_pos::Span;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Internal storage
|
2016-12-23 20:48:21 -07:00
|
|
|
pub struct GlobalArenas<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
// internings
|
2016-04-19 09:11:46 +03:00
|
|
|
layout: TypedArena<Layout>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
// references
|
2017-01-25 22:01:11 +02:00
|
|
|
generics: TypedArena<ty::Generics>,
|
2016-11-25 02:29:26 +02:00
|
|
|
trait_def: TypedArena<ty::TraitDef>,
|
2016-11-25 01:33:29 +02:00
|
|
|
adt_def: TypedArena<ty::AdtDef>,
|
2017-04-28 06:00:48 -04:00
|
|
|
steal_mir: TypedArena<Steal<Mir<'tcx>>>,
|
|
|
|
mir: TypedArena<Mir<'tcx>>,
|
2017-01-25 16:24:00 -05:00
|
|
|
tables: TypedArena<ty::TypeckTables<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:48:21 -07:00
|
|
|
impl<'tcx> GlobalArenas<'tcx> {
|
|
|
|
pub fn new() -> GlobalArenas<'tcx> {
|
|
|
|
GlobalArenas {
|
2016-04-19 09:11:46 +03:00
|
|
|
layout: TypedArena::new(),
|
2016-08-08 23:39:49 +03:00
|
|
|
generics: TypedArena::new(),
|
2016-10-28 13:55:49 +03:00
|
|
|
trait_def: TypedArena::new(),
|
|
|
|
adt_def: TypedArena::new(),
|
2017-04-28 06:00:48 -04:00
|
|
|
steal_mir: TypedArena::new(),
|
2016-12-23 20:48:21 -07:00
|
|
|
mir: TypedArena::new(),
|
2017-01-04 04:01:58 +02:00
|
|
|
tables: TypedArena::new(),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub struct CtxtInterners<'tcx> {
|
2016-12-23 20:48:21 -07:00
|
|
|
/// The arena that types, regions, etc are allocated from
|
|
|
|
arena: &'tcx DroplessArena,
|
2016-04-28 03:13:17 +03:00
|
|
|
|
|
|
|
/// Specifically use a speedy hash algorithm for these hash sets,
|
|
|
|
/// they're accessed quite often.
|
2016-11-08 14:02:55 +11:00
|
|
|
type_: RefCell<FxHashSet<Interned<'tcx, TyS<'tcx>>>>,
|
|
|
|
type_list: RefCell<FxHashSet<Interned<'tcx, Slice<Ty<'tcx>>>>>,
|
|
|
|
substs: RefCell<FxHashSet<Interned<'tcx, Substs<'tcx>>>>,
|
2017-05-11 16:10:47 +03:00
|
|
|
region: RefCell<FxHashSet<Interned<'tcx, RegionKind>>>,
|
2016-11-16 09:21:49 -07:00
|
|
|
existential_predicates: RefCell<FxHashSet<Interned<'tcx, Slice<ExistentialPredicate<'tcx>>>>>,
|
2017-05-02 14:37:00 -04:00
|
|
|
predicates: RefCell<FxHashSet<Interned<'tcx, Slice<Predicate<'tcx>>>>>,
|
2017-08-04 11:25:13 +03:00
|
|
|
const_: RefCell<FxHashSet<Interned<'tcx, Const<'tcx>>>>,
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
|
2016-12-23 20:48:21 -07:00
|
|
|
fn new(arena: &'tcx DroplessArena) -> CtxtInterners<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
CtxtInterners {
|
2017-07-03 11:19:51 -07:00
|
|
|
arena,
|
2016-11-08 14:02:55 +11:00
|
|
|
type_: RefCell::new(FxHashSet()),
|
|
|
|
type_list: RefCell::new(FxHashSet()),
|
|
|
|
substs: RefCell::new(FxHashSet()),
|
|
|
|
region: RefCell::new(FxHashSet()),
|
2016-11-16 09:21:49 -07:00
|
|
|
existential_predicates: RefCell::new(FxHashSet()),
|
2017-05-02 14:37:00 -04:00
|
|
|
predicates: RefCell::new(FxHashSet()),
|
2017-08-04 00:41:44 +03:00
|
|
|
const_: RefCell::new(FxHashSet()),
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Intern a type. global_interners is Some only if this is
|
|
|
|
/// a local interner and global_interners is its counterpart.
|
|
|
|
fn intern_ty(&self, st: TypeVariants<'tcx>,
|
|
|
|
global_interners: Option<&CtxtInterners<'gcx>>)
|
|
|
|
-> Ty<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
let ty = {
|
|
|
|
let mut interner = self.type_.borrow_mut();
|
2016-05-11 04:14:41 +03:00
|
|
|
let global_interner = global_interners.map(|interners| {
|
|
|
|
interners.type_.borrow_mut()
|
|
|
|
});
|
|
|
|
if let Some(&Interned(ty)) = interner.get(&st) {
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
if let Some(ref interner) = global_interner {
|
|
|
|
if let Some(&Interned(ty)) = interner.get(&st) {
|
|
|
|
return ty;
|
|
|
|
}
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let flags = super::flags::FlagComputation::for_sty(&st);
|
2016-05-11 04:14:41 +03:00
|
|
|
let ty_struct = TyS {
|
2016-04-28 03:13:17 +03:00
|
|
|
sty: st,
|
2017-05-12 12:01:43 -04:00
|
|
|
flags: flags.flags,
|
2016-04-28 03:13:17 +03:00
|
|
|
region_depth: flags.depth,
|
2016-05-11 04:14:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// HACK(eddyb) Depend on flags being accurate to
|
|
|
|
// determine that all contents are in the global tcx.
|
|
|
|
// See comments on Lift for why we can't use that.
|
|
|
|
if !flags.flags.intersects(ty::TypeFlags::KEEP_IN_LOCAL_TCX) {
|
|
|
|
if let Some(interner) = global_interners {
|
|
|
|
let ty_struct: TyS<'gcx> = unsafe {
|
|
|
|
mem::transmute(ty_struct)
|
|
|
|
};
|
2016-12-23 20:48:21 -07:00
|
|
|
let ty: Ty<'gcx> = interner.arena.alloc(ty_struct);
|
2016-05-11 04:14:41 +03:00
|
|
|
global_interner.unwrap().insert(Interned(ty));
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't end up with inference
|
|
|
|
// types/regions in the global tcx.
|
|
|
|
if global_interners.is_none() {
|
|
|
|
drop(interner);
|
|
|
|
bug!("Attempted to intern `{:?}` which contains \
|
|
|
|
inference types/regions in the global type context",
|
|
|
|
&ty_struct);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 03:13:17 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
// Don't be &mut TyS.
|
2016-12-23 20:48:21 -07:00
|
|
|
let ty: Ty<'tcx> = self.arena.alloc(ty_struct);
|
2016-05-11 04:14:41 +03:00
|
|
|
interner.insert(Interned(ty));
|
2016-04-28 03:13:17 +03:00
|
|
|
ty
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Interned type: {:?} Pointer: {:?}",
|
|
|
|
ty, ty as *const TyS);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub struct CommonTypes<'tcx> {
|
|
|
|
pub bool: Ty<'tcx>,
|
|
|
|
pub char: Ty<'tcx>,
|
|
|
|
pub isize: Ty<'tcx>,
|
|
|
|
pub i8: Ty<'tcx>,
|
|
|
|
pub i16: Ty<'tcx>,
|
|
|
|
pub i32: Ty<'tcx>,
|
|
|
|
pub i64: Ty<'tcx>,
|
2016-08-23 03:56:52 +03:00
|
|
|
pub i128: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub usize: Ty<'tcx>,
|
|
|
|
pub u8: Ty<'tcx>,
|
|
|
|
pub u16: Ty<'tcx>,
|
|
|
|
pub u32: Ty<'tcx>,
|
|
|
|
pub u64: Ty<'tcx>,
|
2016-08-23 03:56:52 +03:00
|
|
|
pub u128: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub f32: Ty<'tcx>,
|
|
|
|
pub f64: Ty<'tcx>,
|
2016-08-02 15:56:20 +08:00
|
|
|
pub never: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub err: Ty<'tcx>,
|
2017-04-20 01:58:12 +03:00
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
pub re_empty: Region<'tcx>,
|
|
|
|
pub re_static: Region<'tcx>,
|
|
|
|
pub re_erased: Region<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub struct LocalTableInContext<'a, V: 'a> {
|
2017-08-11 11:56:26 +02:00
|
|
|
local_id_root: Option<DefId>,
|
2017-08-10 16:10:04 +02:00
|
|
|
data: &'a ItemLocalMap<V>
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Validate that the given HirId (respectively its `local_id` part) can be
|
|
|
|
/// safely used as a key in the tables of a TypeckTable. For that to be
|
|
|
|
/// the case, the HirId must have the same `owner` as all the other IDs in
|
|
|
|
/// this table (signified by `local_id_root`). Otherwise the HirId
|
|
|
|
/// would be in a different frame of reference and using its `local_id`
|
|
|
|
/// would result in lookup errors, or worse, in silently wrong data being
|
|
|
|
/// stored/returned.
|
2017-08-11 11:56:26 +02:00
|
|
|
fn validate_hir_id_for_typeck_tables(local_id_root: Option<DefId>,
|
|
|
|
hir_id: hir::HirId,
|
|
|
|
mut_access: bool) {
|
2017-08-14 14:56:35 +02:00
|
|
|
if cfg!(debug_assertions) {
|
2017-08-11 11:56:26 +02:00
|
|
|
if let Some(local_id_root) = local_id_root {
|
|
|
|
if hir_id.owner != local_id_root.index {
|
2017-08-10 16:10:04 +02:00
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let node_id = tcx.hir
|
|
|
|
.definitions()
|
|
|
|
.find_node_for_hir_id(hir_id);
|
|
|
|
|
|
|
|
bug!("node {} with HirId::owner {:?} cannot be placed in \
|
|
|
|
TypeckTables with local_id_root {:?}",
|
|
|
|
tcx.hir.node_to_string(node_id),
|
|
|
|
DefId::local(hir_id.owner),
|
2017-08-11 11:56:26 +02:00
|
|
|
local_id_root)
|
2017-08-10 16:10:04 +02:00
|
|
|
});
|
|
|
|
}
|
2017-08-11 11:56:26 +02:00
|
|
|
} else {
|
|
|
|
// We use "Null Object" TypeckTables in some of the analysis passes.
|
|
|
|
// These are just expected to be empty and their `local_id_root` is
|
|
|
|
// `None`. Therefore we cannot verify whether a given `HirId` would
|
|
|
|
// be a valid key for the given table. Instead we make sure that
|
|
|
|
// nobody tries to write to such a Null Object table.
|
|
|
|
if mut_access {
|
|
|
|
bug!("access to invalid TypeckTables")
|
|
|
|
}
|
2017-08-10 16:10:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V> LocalTableInContext<'a, V> {
|
|
|
|
pub fn contains_key(&self, id: hir::HirId) -> bool {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.contains_key(&id.local_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, id: hir::HirId) -> Option<&V> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.get(&id.local_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> hash_map::Iter<hir::ItemLocalId, V> {
|
|
|
|
self.data.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V> ::std::ops::Index<hir::HirId> for LocalTableInContext<'a, V> {
|
|
|
|
type Output = V;
|
|
|
|
|
|
|
|
fn index(&self, key: hir::HirId) -> &V {
|
|
|
|
self.get(key).expect("LocalTableInContext: key not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LocalTableInContextMut<'a, V: 'a> {
|
2017-08-11 11:56:26 +02:00
|
|
|
local_id_root: Option<DefId>,
|
2017-08-10 16:10:04 +02:00
|
|
|
data: &'a mut ItemLocalMap<V>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V> LocalTableInContextMut<'a, V> {
|
|
|
|
pub fn get_mut(&mut self, id: hir::HirId) -> Option<&mut V> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.get_mut(&id.local_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn entry(&mut self, id: hir::HirId) -> Entry<hir::ItemLocalId, V> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.entry(id.local_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, id: hir::HirId, val: V) -> Option<V> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.insert(id.local_id, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove(&mut self, id: hir::HirId) -> Option<V> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.data.remove(&id.local_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:01:58 +02:00
|
|
|
#[derive(RustcEncodable, RustcDecodable)]
|
2017-01-25 16:24:00 -05:00
|
|
|
pub struct TypeckTables<'tcx> {
|
2017-08-04 09:49:40 +02:00
|
|
|
/// The HirId::owner all ItemLocalIds in this table are relative to.
|
2017-08-11 11:56:26 +02:00
|
|
|
pub local_id_root: Option<DefId>,
|
2017-08-04 09:49:40 +02:00
|
|
|
|
2017-05-20 16:11:07 +03:00
|
|
|
/// Resolved definitions for `<T>::X` associated paths and
|
|
|
|
/// method calls, including those of overloaded operators.
|
2017-08-10 16:10:04 +02:00
|
|
|
type_dependent_defs: ItemLocalMap<Def>,
|
2016-11-25 13:21:19 +02:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Stores the types for various nodes in the AST. Note that this table
|
|
|
|
/// is not guaranteed to be populated until after typeck. See
|
|
|
|
/// typeck::check::fn_ctxt for details.
|
2017-08-10 16:10:04 +02:00
|
|
|
node_types: ItemLocalMap<Ty<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Stores the type parameters which were substituted to obtain the type
|
|
|
|
/// of this node. This only applies to nodes that refer to entities
|
|
|
|
/// parameterized by type parameters, such as generic fns, types, or
|
|
|
|
/// other items.
|
2017-08-10 16:10:04 +02:00
|
|
|
node_substs: ItemLocalMap<&'tcx Substs<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
adjustments: ItemLocalMap<Vec<ty::adjustment::Adjustment<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-07-21 19:29:43 -04:00
|
|
|
// Stores the actual binding mode for all instances of hir::BindingAnnotation.
|
2017-08-10 16:10:04 +02:00
|
|
|
pat_binding_modes: ItemLocalMap<BindingMode>,
|
2017-07-21 19:29:43 -04:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Borrows
|
2016-08-25 23:58:52 +03:00
|
|
|
pub upvar_capture_map: ty::UpvarCaptureMap<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
/// Records the type of each closure.
|
2017-08-10 16:10:04 +02:00
|
|
|
closure_tys: ItemLocalMap<ty::PolyFnSig<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-05-28 22:08:51 +01:00
|
|
|
/// Records the kind of each closure and the span and name of the variable
|
|
|
|
/// that caused the closure to be this kind.
|
2017-08-10 16:10:04 +02:00
|
|
|
closure_kinds: ItemLocalMap<(ty::ClosureKind, Option<(Span, ast::Name)>)>,
|
2015-10-21 15:46:30 -04:00
|
|
|
|
2017-08-14 20:12:13 -07:00
|
|
|
generator_sigs: ItemLocalMap<Option<ty::GenSig<'tcx>>>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-14 20:12:13 -07:00
|
|
|
generator_interiors: ItemLocalMap<ty::GeneratorInterior<'tcx>>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2015-10-21 15:46:30 -04:00
|
|
|
/// For each fn, records the "liberated" types of its arguments
|
|
|
|
/// and return type. Liberated means that all bound regions
|
|
|
|
/// (including late-bound regions) are replaced with free
|
|
|
|
/// equivalents. This table is not used in trans (since regions
|
|
|
|
/// are erased there) and hence is not serialized to metadata.
|
2017-08-10 16:10:04 +02:00
|
|
|
liberated_fn_sigs: ItemLocalMap<ty::FnSig<'tcx>>,
|
2016-02-11 18:31:42 +02:00
|
|
|
|
|
|
|
/// For each FRU expression, record the normalized types of the fields
|
|
|
|
/// of the struct - this is needed because it is non-trivial to
|
|
|
|
/// normalize while preserving regions. This table is used only in
|
|
|
|
/// MIR construction and hence is not serialized to metadata.
|
2017-08-10 16:10:04 +02:00
|
|
|
fru_field_types: ItemLocalMap<Vec<Ty<'tcx>>>,
|
2017-01-27 16:16:43 -05:00
|
|
|
|
|
|
|
/// Maps a cast expression to its kind. This is keyed on the
|
|
|
|
/// *from* expression of the cast, not the cast itself.
|
2017-08-10 16:10:04 +02:00
|
|
|
cast_kinds: ItemLocalMap<ty::cast::CastKind>,
|
2017-01-28 18:13:21 -05:00
|
|
|
|
2017-02-14 11:32:00 +02:00
|
|
|
/// Set of trait imports actually used in the method resolution.
|
|
|
|
/// This is used for warning unused imports.
|
|
|
|
pub used_trait_imports: DefIdSet,
|
2017-02-15 15:00:20 +02:00
|
|
|
|
|
|
|
/// If any errors occurred while type-checking this body,
|
|
|
|
/// this field will be set to `true`.
|
|
|
|
pub tainted_by_errors: bool,
|
2017-02-16 12:46:44 -05:00
|
|
|
|
|
|
|
/// Stores the free-region relationships that were deduced from
|
|
|
|
/// its where clauses and parameter types. These are then
|
|
|
|
/// read-again by borrowck.
|
2017-04-20 04:45:53 -04:00
|
|
|
pub free_region_map: FreeRegionMap<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:24:00 -05:00
|
|
|
impl<'tcx> TypeckTables<'tcx> {
|
2017-08-11 11:56:26 +02:00
|
|
|
pub fn empty(local_id_root: Option<DefId>) -> TypeckTables<'tcx> {
|
2017-01-25 16:24:00 -05:00
|
|
|
TypeckTables {
|
2017-08-04 09:49:40 +02:00
|
|
|
local_id_root,
|
|
|
|
type_dependent_defs: ItemLocalMap(),
|
2017-08-07 14:43:43 +02:00
|
|
|
node_types: ItemLocalMap(),
|
|
|
|
node_substs: ItemLocalMap(),
|
|
|
|
adjustments: ItemLocalMap(),
|
2017-08-07 15:06:10 +02:00
|
|
|
pat_binding_modes: ItemLocalMap(),
|
2016-11-08 14:02:55 +11:00
|
|
|
upvar_capture_map: FxHashMap(),
|
2017-08-14 19:36:13 -07:00
|
|
|
generator_sigs: ItemLocalMap(),
|
|
|
|
generator_interiors: ItemLocalMap(),
|
2017-08-07 15:50:13 +02:00
|
|
|
closure_tys: ItemLocalMap(),
|
|
|
|
closure_kinds: ItemLocalMap(),
|
2017-08-07 17:45:06 +02:00
|
|
|
liberated_fn_sigs: ItemLocalMap(),
|
2017-08-08 12:13:16 +02:00
|
|
|
fru_field_types: ItemLocalMap(),
|
2017-08-08 12:25:22 +02:00
|
|
|
cast_kinds: ItemLocalMap(),
|
2017-02-14 11:32:00 +02:00
|
|
|
used_trait_imports: DefIdSet(),
|
2017-02-15 15:00:20 +02:00
|
|
|
tainted_by_errors: false,
|
2017-02-16 12:46:44 -05:00
|
|
|
free_region_map: FreeRegionMap::new(),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 04:52:10 +03:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
/// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
|
2017-08-04 09:49:40 +02:00
|
|
|
pub fn qpath_def(&self, qpath: &hir::QPath, id: hir::HirId) -> Def {
|
2016-11-25 13:21:19 +02:00
|
|
|
match *qpath {
|
|
|
|
hir::QPath::Resolved(_, ref path) => path.def,
|
|
|
|
hir::QPath::TypeRelative(..) => {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-04 09:49:40 +02:00
|
|
|
self.type_dependent_defs.get(&id.local_id).cloned().unwrap_or(Def::Err)
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn type_dependent_defs(&self) -> LocalTableInContext<Def> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.type_dependent_defs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_dependent_defs_mut(&mut self) -> LocalTableInContextMut<Def> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.type_dependent_defs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node_types(&self) -> LocalTableInContext<Ty<'tcx>> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.node_types
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node_types_mut(&mut self) -> LocalTableInContextMut<Ty<'tcx>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.node_types
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:43:43 +02:00
|
|
|
pub fn node_id_to_type(&self, id: hir::HirId) -> Ty<'tcx> {
|
2016-10-27 04:52:10 +03:00
|
|
|
match self.node_id_to_type_opt(id) {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => {
|
|
|
|
bug!("node_id_to_type: no type for node `{}`",
|
2017-08-07 14:43:43 +02:00
|
|
|
tls::with(|tcx| {
|
|
|
|
let id = tcx.hir.definitions().find_node_for_hir_id(id);
|
|
|
|
tcx.hir.node_to_string(id)
|
|
|
|
}))
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:43:43 +02:00
|
|
|
pub fn node_id_to_type_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_types.get(&id.local_id).cloned()
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn node_substs_mut(&mut self) -> LocalTableInContextMut<&'tcx Substs<'tcx>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.node_substs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:43:43 +02:00
|
|
|
pub fn node_substs(&self, id: hir::HirId) -> &'tcx Substs<'tcx> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_substs.get(&id.local_id).cloned().unwrap_or(Substs::empty())
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn node_substs_opt(&self, id: hir::HirId) -> Option<&'tcx Substs<'tcx>> {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
2017-08-10 16:10:04 +02:00
|
|
|
self.node_substs.get(&id.local_id).cloned()
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
|
|
|
|
// doesn't provide type parameter substitutions.
|
|
|
|
pub fn pat_ty(&self, pat: &hir::Pat) -> Ty<'tcx> {
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_id_to_type(pat.hir_id)
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pat_ty_opt(&self, pat: &hir::Pat) -> Option<Ty<'tcx>> {
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_id_to_type_opt(pat.hir_id)
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the type of an expression as a monotype.
|
|
|
|
//
|
|
|
|
// NB (1): This is the PRE-ADJUSTMENT TYPE for the expression. That is, in
|
2016-10-20 06:33:20 +03:00
|
|
|
// some cases, we insert `Adjustment` annotations such as auto-deref or
|
2016-10-27 04:52:10 +03:00
|
|
|
// auto-ref. The type returned by this function does not consider such
|
|
|
|
// adjustments. See `expr_ty_adjusted()` instead.
|
|
|
|
//
|
|
|
|
// NB (2): This type doesn't provide type parameter substitutions; e.g. if you
|
|
|
|
// ask for the type of "id" in "id(3)", it will return "fn(&isize) -> isize"
|
|
|
|
// instead of "fn(ty) -> T with T = isize".
|
|
|
|
pub fn expr_ty(&self, expr: &hir::Expr) -> Ty<'tcx> {
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_id_to_type(expr.hir_id)
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expr_ty_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
|
2017-08-07 14:43:43 +02:00
|
|
|
self.node_id_to_type_opt(expr.hir_id)
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn adjustments(&self) -> LocalTableInContext<Vec<ty::adjustment::Adjustment<'tcx>>> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.adjustments
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn adjustments_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<Vec<ty::adjustment::Adjustment<'tcx>>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.adjustments
|
|
|
|
}
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
2017-05-27 10:29:24 +03:00
|
|
|
pub fn expr_adjustments(&self, expr: &hir::Expr)
|
|
|
|
-> &[ty::adjustment::Adjustment<'tcx>] {
|
2017-08-11 11:56:26 +02:00
|
|
|
validate_hir_id_for_typeck_tables(self.local_id_root, expr.hir_id, false);
|
2017-08-07 14:43:43 +02:00
|
|
|
self.adjustments.get(&expr.hir_id.local_id).map_or(&[], |a| &a[..])
|
2017-05-27 10:29:24 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:33:20 +03:00
|
|
|
/// Returns the type of `expr`, considering any `Adjustment`
|
|
|
|
/// entry recorded for that expression.
|
|
|
|
pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> Ty<'tcx> {
|
2017-05-27 10:29:24 +03:00
|
|
|
self.expr_adjustments(expr)
|
|
|
|
.last()
|
2016-10-20 06:33:20 +03:00
|
|
|
.map_or_else(|| self.expr_ty(expr), |adj| adj.target)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expr_ty_adjusted_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
|
2017-05-27 10:29:24 +03:00
|
|
|
self.expr_adjustments(expr)
|
|
|
|
.last()
|
|
|
|
.map(|adj| adj.target)
|
|
|
|
.or_else(|| self.expr_ty_opt(expr))
|
2016-10-20 06:33:20 +03:00
|
|
|
}
|
2016-10-27 04:52:10 +03:00
|
|
|
|
2017-05-20 16:11:07 +03:00
|
|
|
pub fn is_method_call(&self, expr: &hir::Expr) -> bool {
|
|
|
|
// Only paths and method calls/overloaded operators have
|
|
|
|
// entries in type_dependent_defs, ignore the former here.
|
|
|
|
if let hir::ExprPath(_) = expr.node {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-27 04:52:10 +03:00
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
match self.type_dependent_defs().get(expr.hir_id) {
|
2017-05-20 16:11:07 +03:00
|
|
|
Some(&Def::Method(_)) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn pat_binding_modes(&self) -> LocalTableInContext<BindingMode> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.pat_binding_modes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pat_binding_modes_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<BindingMode> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.pat_binding_modes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 13:38:30 +03:00
|
|
|
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> ty::UpvarCapture<'tcx> {
|
|
|
|
self.upvar_capture_map[&upvar_id]
|
2016-10-27 04:52:10 +03:00
|
|
|
}
|
2017-08-04 09:49:40 +02:00
|
|
|
|
2017-08-10 16:10:04 +02:00
|
|
|
pub fn closure_tys(&self) -> LocalTableInContext<ty::PolyFnSig<'tcx>> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.closure_tys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closure_tys_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<ty::PolyFnSig<'tcx>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.closure_tys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closure_kinds(&self) -> LocalTableInContext<(ty::ClosureKind,
|
|
|
|
Option<(Span, ast::Name)>)> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.closure_kinds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closure_kinds_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<(ty::ClosureKind, Option<(Span, ast::Name)>)> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.closure_kinds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn liberated_fn_sigs(&self) -> LocalTableInContext<ty::FnSig<'tcx>> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.liberated_fn_sigs
|
2017-08-04 09:49:40 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:10:04 +02:00
|
|
|
|
|
|
|
pub fn liberated_fn_sigs_mut(&mut self) -> LocalTableInContextMut<ty::FnSig<'tcx>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.liberated_fn_sigs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fru_field_types(&self) -> LocalTableInContext<Vec<Ty<'tcx>>> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.fru_field_types
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fru_field_types_mut(&mut self) -> LocalTableInContextMut<Vec<Ty<'tcx>>> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.fru_field_types
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cast_kinds(&self) -> LocalTableInContext<ty::cast::CastKind> {
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.cast_kinds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cast_kinds_mut(&mut self) -> LocalTableInContextMut<ty::cast::CastKind> {
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.cast_kinds
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 20:12:13 -07:00
|
|
|
|
|
|
|
pub fn generator_sigs(&self)
|
|
|
|
-> LocalTableInContext<Option<ty::GenSig<'tcx>>>
|
|
|
|
{
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.generator_sigs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generator_sigs_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<Option<ty::GenSig<'tcx>>>
|
|
|
|
{
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.generator_sigs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generator_interiors(&self)
|
|
|
|
-> LocalTableInContext<ty::GeneratorInterior<'tcx>>
|
|
|
|
{
|
|
|
|
LocalTableInContext {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &self.generator_interiors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generator_interiors_mut(&mut self)
|
|
|
|
-> LocalTableInContextMut<ty::GeneratorInterior<'tcx>>
|
|
|
|
{
|
|
|
|
LocalTableInContextMut {
|
|
|
|
local_id_root: self.local_id_root,
|
|
|
|
data: &mut self.generator_interiors,
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for TypeckTables<'gcx> {
|
|
|
|
fn hash_stable<W: StableHasherResult>(&self,
|
|
|
|
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
|
|
|
|
hasher: &mut StableHasher<W>) {
|
|
|
|
let ty::TypeckTables {
|
|
|
|
local_id_root,
|
|
|
|
ref type_dependent_defs,
|
|
|
|
ref node_types,
|
|
|
|
ref node_substs,
|
|
|
|
ref adjustments,
|
|
|
|
ref pat_binding_modes,
|
|
|
|
ref upvar_capture_map,
|
|
|
|
ref closure_tys,
|
|
|
|
ref closure_kinds,
|
|
|
|
ref liberated_fn_sigs,
|
|
|
|
ref fru_field_types,
|
|
|
|
|
|
|
|
ref cast_kinds,
|
|
|
|
|
|
|
|
ref used_trait_imports,
|
|
|
|
tainted_by_errors,
|
|
|
|
ref free_region_map,
|
2017-08-14 20:12:13 -07:00
|
|
|
ref generator_sigs,
|
|
|
|
ref generator_interiors,
|
2017-08-10 16:10:04 +02:00
|
|
|
} = *self;
|
|
|
|
|
|
|
|
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, type_dependent_defs);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, node_types);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, node_substs);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, adjustments);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, pat_binding_modes);
|
|
|
|
ich::hash_stable_hashmap(hcx, hasher, upvar_capture_map, |hcx, up_var_id| {
|
|
|
|
let ty::UpvarId {
|
|
|
|
var_id,
|
|
|
|
closure_expr_id
|
|
|
|
} = *up_var_id;
|
|
|
|
|
2017-08-11 11:56:26 +02:00
|
|
|
let local_id_root =
|
|
|
|
local_id_root.expect("trying to hash invalid TypeckTables");
|
|
|
|
|
2017-04-29 14:39:47 +03:00
|
|
|
let var_owner_def_id = DefId {
|
2017-08-10 16:10:04 +02:00
|
|
|
krate: local_id_root.krate,
|
2017-04-29 14:39:47 +03:00
|
|
|
index: var_id.owner,
|
2017-08-10 16:10:04 +02:00
|
|
|
};
|
|
|
|
let closure_def_id = DefId {
|
|
|
|
krate: local_id_root.krate,
|
|
|
|
index: closure_expr_id,
|
|
|
|
};
|
2017-04-29 14:39:47 +03:00
|
|
|
((hcx.def_path_hash(var_owner_def_id), var_id.local_id),
|
|
|
|
hcx.def_path_hash(closure_def_id))
|
2017-08-10 16:10:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, closure_tys);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, closure_kinds);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, liberated_fn_sigs);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, fru_field_types);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, cast_kinds);
|
2017-08-14 20:12:13 -07:00
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, generator_sigs);
|
|
|
|
ich::hash_stable_itemlocalmap(hcx, hasher, generator_interiors);
|
2017-08-10 16:10:04 +02:00
|
|
|
|
|
|
|
ich::hash_stable_hashset(hcx, hasher, used_trait_imports, |hcx, def_id| {
|
|
|
|
hcx.def_path_hash(*def_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
tainted_by_errors.hash_stable(hcx, hasher);
|
|
|
|
free_region_map.hash_stable(hcx, hasher);
|
|
|
|
})
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CommonTypes<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
let mk = |sty| interners.intern_ty(sty, None);
|
2017-04-20 01:58:12 +03:00
|
|
|
let mk_region = |r| {
|
|
|
|
if let Some(r) = interners.region.borrow().get(&r) {
|
|
|
|
return r.0;
|
|
|
|
}
|
|
|
|
let r = interners.arena.alloc(r);
|
|
|
|
interners.region.borrow_mut().insert(Interned(r));
|
|
|
|
&*r
|
|
|
|
};
|
2015-09-06 21:51:58 +03:00
|
|
|
CommonTypes {
|
|
|
|
bool: mk(TyBool),
|
|
|
|
char: mk(TyChar),
|
2016-08-02 15:56:20 +08:00
|
|
|
never: mk(TyNever),
|
2015-09-06 21:51:58 +03:00
|
|
|
err: mk(TyError),
|
2016-02-08 16:20:57 +01:00
|
|
|
isize: mk(TyInt(ast::IntTy::Is)),
|
|
|
|
i8: mk(TyInt(ast::IntTy::I8)),
|
|
|
|
i16: mk(TyInt(ast::IntTy::I16)),
|
|
|
|
i32: mk(TyInt(ast::IntTy::I32)),
|
|
|
|
i64: mk(TyInt(ast::IntTy::I64)),
|
2016-08-23 03:56:52 +03:00
|
|
|
i128: mk(TyInt(ast::IntTy::I128)),
|
2016-02-08 16:20:57 +01:00
|
|
|
usize: mk(TyUint(ast::UintTy::Us)),
|
|
|
|
u8: mk(TyUint(ast::UintTy::U8)),
|
|
|
|
u16: mk(TyUint(ast::UintTy::U16)),
|
|
|
|
u32: mk(TyUint(ast::UintTy::U32)),
|
|
|
|
u64: mk(TyUint(ast::UintTy::U64)),
|
2016-08-23 03:56:52 +03:00
|
|
|
u128: mk(TyUint(ast::UintTy::U128)),
|
2016-02-08 16:09:01 +01:00
|
|
|
f32: mk(TyFloat(ast::FloatTy::F32)),
|
|
|
|
f64: mk(TyFloat(ast::FloatTy::F64)),
|
2017-04-20 01:58:12 +03:00
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
re_empty: mk_region(RegionKind::ReEmpty),
|
|
|
|
re_static: mk_region(RegionKind::ReStatic),
|
|
|
|
re_erased: mk_region(RegionKind::ReErased),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The data structure to keep track of all the information that typechecker
|
|
|
|
/// generates so that so that it can be reused and doesn't have to be redone
|
|
|
|
/// later on.
|
2016-05-03 04:56:42 +03:00
|
|
|
#[derive(Copy, Clone)]
|
2016-05-03 05:23:22 +03:00
|
|
|
pub struct TyCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
gcx: &'a GlobalCtxt<'gcx>,
|
2016-04-28 03:13:17 +03:00
|
|
|
interners: &'a CtxtInterners<'tcx>
|
2016-05-03 04:56:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
|
|
|
|
type Target = &'a GlobalCtxt<'gcx>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-05-03 04:56:42 +03:00
|
|
|
&self.gcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GlobalCtxt<'tcx> {
|
2016-12-23 20:48:21 -07:00
|
|
|
global_arenas: &'tcx GlobalArenas<'tcx>,
|
2016-04-28 03:13:17 +03:00
|
|
|
global_interners: CtxtInterners<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore: &'tcx CrateStore,
|
|
|
|
|
2016-09-29 02:30:53 +03:00
|
|
|
pub sess: &'tcx Session,
|
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
|
2017-04-17 12:35:53 -04:00
|
|
|
pub trans_trait_caches: traits::trans::TransTraitCaches<'tcx>,
|
|
|
|
|
2016-01-05 13:07:45 -05:00
|
|
|
pub dep_graph: DepGraph,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Common types, pre-interned for your convenience.
|
|
|
|
pub types: CommonTypes<'tcx>,
|
|
|
|
|
2016-07-27 17:26:55 -04:00
|
|
|
/// Map indicating what traits are in scope for places where this
|
|
|
|
/// is relevant; generated by resolve.
|
2017-09-08 13:51:57 -07:00
|
|
|
trait_map: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,
|
2016-07-27 17:26:55 -04:00
|
|
|
|
2017-03-23 14:18:25 -04:00
|
|
|
/// Export map produced by name resolution.
|
2017-09-08 13:51:57 -07:00
|
|
|
export_map: FxHashMap<DefId, Rc<Vec<Export>>>,
|
2017-03-23 14:18:25 -04:00
|
|
|
|
2017-08-30 09:31:14 -07:00
|
|
|
named_region_map: NamedRegionMap,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub hir: hir_map::Map<'tcx>,
|
2017-04-27 13:04:57 -04:00
|
|
|
|
2017-05-31 14:53:39 +02:00
|
|
|
/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
|
|
|
|
/// as well as all upstream crates. Only populated in incremental mode.
|
|
|
|
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,
|
|
|
|
|
2017-02-07 11:47:35 +02:00
|
|
|
pub maps: maps::Maps<'tcx>,
|
2016-10-28 13:55:49 +03:00
|
|
|
|
2017-04-27 13:04:57 -04:00
|
|
|
pub mir_passes: Rc<Passes>,
|
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
// Records the free variables refrenced by every closure
|
|
|
|
// expression. Do not track deps for this, just recompute it from
|
|
|
|
// scratch every time.
|
2017-09-08 13:51:57 -07:00
|
|
|
freevars: FxHashMap<DefId, Rc<Vec<hir::Freevar>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2017-09-08 13:51:57 -07:00
|
|
|
maybe_unused_trait_imports: FxHashSet<DefId>,
|
2016-04-19 22:43:10 +09:00
|
|
|
|
2017-09-08 13:51:57 -07:00
|
|
|
maybe_unused_extern_crates: Vec<(DefId, Span)>,
|
2017-06-24 17:48:27 +09:00
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
// Internal cache for metadata decoding. No need to track deps on this.
|
2016-11-08 14:02:55 +11:00
|
|
|
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
|
|
|
// FIXME dep tracking -- should be harmless enough
|
2016-11-08 14:02:55 +11:00
|
|
|
pub normalized_cache: RefCell<FxHashMap<Ty<'tcx>, Ty<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2016-12-31 07:35:43 +08:00
|
|
|
pub inhabitedness_cache: RefCell<FxHashMap<Ty<'tcx>, DefIdForest>>,
|
2016-12-11 22:30:14 +08:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Set of nodes which mark locals as mutable which end up getting used at
|
|
|
|
/// some point. Local variable definitions not in this set can be warned
|
|
|
|
/// about.
|
|
|
|
pub used_mut_nodes: RefCell<NodeSet>,
|
|
|
|
|
|
|
|
/// Caches the results of trait selection. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
pub selection_cache: traits::SelectionCache<'tcx>,
|
|
|
|
|
2015-10-21 14:50:38 +03:00
|
|
|
/// Caches the results of trait evaluation. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
/// Merge this with `selection_cache`?
|
|
|
|
pub evaluation_cache: traits::EvaluationCache<'tcx>,
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
/// Maps Expr NodeId's to `true` iff `&expr` can have 'static lifetime.
|
|
|
|
pub rvalue_promotable_to_static: RefCell<NodeMap<bool>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-02-14 12:30:38 -05:00
|
|
|
/// The definite name of the current crate after taking into account
|
|
|
|
/// attributes, commandline parameters, etc.
|
2016-11-16 10:52:37 +00:00
|
|
|
pub crate_name: Symbol,
|
2016-04-18 16:03:16 +03:00
|
|
|
|
|
|
|
/// Data layout specification for the current target.
|
|
|
|
pub data_layout: TargetDataLayout,
|
2016-04-19 09:11:46 +03:00
|
|
|
|
2016-09-25 19:55:43 -04:00
|
|
|
/// Used to prevent layout from recursing too deeply.
|
2016-08-28 20:44:19 -04:00
|
|
|
pub layout_depth: Cell<usize>,
|
|
|
|
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
/// Map from function to the `#[derive]` mode that it's defining. Only used
|
2016-10-03 09:49:39 -07:00
|
|
|
/// by `proc-macro` crates.
|
2016-11-16 10:52:37 +00:00
|
|
|
pub derive_macros: RefCell<NodeMap<Symbol>>,
|
2016-12-23 20:48:21 -07:00
|
|
|
|
|
|
|
stability_interner: RefCell<FxHashSet<&'tcx attr::Stability>>,
|
|
|
|
|
|
|
|
layout_interner: RefCell<FxHashSet<&'tcx Layout>>,
|
2016-10-04 02:19:40 +03:00
|
|
|
|
|
|
|
/// A vector of every trait accessible in the whole crate
|
|
|
|
/// (i.e. including those from subcrates). This is used only for
|
2017-08-15 21:45:21 +02:00
|
|
|
/// error reporting, and so is lazily initialized and generally
|
2016-10-04 02:19:40 +03:00
|
|
|
/// shouldn't taint the common path (hence the RefCell).
|
|
|
|
pub all_traits: RefCell<Option<Vec<DefId>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
impl<'tcx> GlobalCtxt<'tcx> {
|
|
|
|
/// Get the global TyCtxt.
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn global_tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
TyCtxt {
|
|
|
|
gcx: self,
|
|
|
|
interners: &self.global_interners
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2017-09-05 16:48:24 +02:00
|
|
|
pub fn cstore_untracked(&self) -> &CrateStore {
|
|
|
|
&*self.cstore
|
|
|
|
}
|
|
|
|
|
2017-01-25 22:01:11 +02:00
|
|
|
pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
|
2016-12-23 20:48:21 -07:00
|
|
|
self.global_arenas.generics.alloc(generics)
|
2016-08-08 23:39:49 +03:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:00:48 -04:00
|
|
|
pub fn alloc_steal_mir(self, mir: Mir<'gcx>) -> &'gcx Steal<Mir<'gcx>> {
|
|
|
|
self.global_arenas.steal_mir.alloc(Steal::new(mir))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn alloc_mir(self, mir: Mir<'gcx>) -> &'gcx Mir<'gcx> {
|
|
|
|
self.global_arenas.mir.alloc(mir)
|
2016-10-28 13:55:49 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:24:00 -05:00
|
|
|
pub fn alloc_tables(self, tables: ty::TypeckTables<'gcx>) -> &'gcx ty::TypeckTables<'gcx> {
|
2017-01-04 04:01:58 +02:00
|
|
|
self.global_arenas.tables.alloc(tables)
|
|
|
|
}
|
|
|
|
|
2016-11-25 02:29:26 +02:00
|
|
|
pub fn alloc_trait_def(self, def: ty::TraitDef) -> &'gcx ty::TraitDef {
|
2016-12-23 20:48:21 -07:00
|
|
|
self.global_arenas.trait_def.alloc(def)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:33:29 +02:00
|
|
|
pub fn alloc_adt_def(self,
|
|
|
|
did: DefId,
|
|
|
|
kind: AdtKind,
|
2017-02-06 15:26:32 -05:00
|
|
|
variants: Vec<ty::VariantDef>,
|
|
|
|
repr: ReprOptions)
|
2016-11-25 01:33:29 +02:00
|
|
|
-> &'gcx ty::AdtDef {
|
2017-02-05 07:01:48 +02:00
|
|
|
let def = ty::AdtDef::new(self, did, kind, variants, repr);
|
2016-12-23 20:48:21 -07:00
|
|
|
self.global_arenas.adt_def.alloc(def)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-08-04 00:41:44 +03:00
|
|
|
pub fn alloc_byte_array(self, bytes: &[u8]) -> &'gcx [u8] {
|
|
|
|
if bytes.is_empty() {
|
|
|
|
&[]
|
|
|
|
} else {
|
|
|
|
self.global_interners.arena.alloc_slice(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
pub fn alloc_const_slice(self, values: &[&'tcx ty::Const<'tcx>])
|
|
|
|
-> &'tcx [&'tcx ty::Const<'tcx>] {
|
2017-08-04 00:41:44 +03:00
|
|
|
if values.is_empty() {
|
|
|
|
&[]
|
|
|
|
} else {
|
2017-08-04 11:25:13 +03:00
|
|
|
self.interners.arena.alloc_slice(values)
|
2017-08-04 00:41:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
pub fn alloc_name_const_slice(self, values: &[(ast::Name, &'tcx ty::Const<'tcx>)])
|
|
|
|
-> &'tcx [(ast::Name, &'tcx ty::Const<'tcx>)] {
|
2017-08-04 00:41:44 +03:00
|
|
|
if values.is_empty() {
|
|
|
|
&[]
|
|
|
|
} else {
|
2017-08-04 11:25:13 +03:00
|
|
|
self.interners.arena.alloc_slice(values)
|
2017-08-04 00:41:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn intern_stability(self, stab: attr::Stability) -> &'gcx attr::Stability {
|
2016-12-23 20:48:21 -07:00
|
|
|
if let Some(st) = self.stability_interner.borrow().get(&stab) {
|
2015-09-06 21:51:58 +03:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:48:21 -07:00
|
|
|
let interned = self.global_interners.arena.alloc(stab);
|
|
|
|
if let Some(prev) = self.stability_interner.borrow_mut().replace(interned) {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Tried to overwrite interned Stability: {:?}", prev)
|
2015-12-01 09:50:07 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn intern_layout(self, layout: Layout) -> &'gcx Layout {
|
2016-12-23 20:48:21 -07:00
|
|
|
if let Some(layout) = self.layout_interner.borrow().get(&layout) {
|
2016-04-19 09:11:46 +03:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:48:21 -07:00
|
|
|
let interned = self.global_arenas.layout.alloc(layout);
|
|
|
|
if let Some(prev) = self.layout_interner.borrow_mut().replace(interned) {
|
2016-04-19 09:11:46 +03:00
|
|
|
bug!("Tried to overwrite interned Layout: {:?}", prev)
|
|
|
|
}
|
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn lift<T: ?Sized + Lift<'tcx>>(self, value: &T) -> Option<T::Lifted> {
|
2015-09-06 21:51:58 +03:00
|
|
|
value.lift_to_tcx(self)
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
/// Like lift, but only tries in the global tcx.
|
|
|
|
pub fn lift_to_global<T: ?Sized + Lift<'gcx>>(self, value: &T) -> Option<T::Lifted> {
|
|
|
|
value.lift_to_tcx(self.global_tcx())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if self is the same as self.global_tcx().
|
|
|
|
fn is_global(self) -> bool {
|
|
|
|
let local = self.interners as *const _;
|
|
|
|
let global = &self.global_interners as *const _;
|
|
|
|
local as usize == global as usize
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
/// Create a type context and call the closure with a `TyCtxt` reference
|
2015-09-06 21:51:58 +03:00
|
|
|
/// to the context. The closure enforces that the type context and any interned
|
|
|
|
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
|
|
|
|
/// reference to the context, to allow formatting values that need it.
|
2015-09-28 15:00:15 +13:00
|
|
|
pub fn create_and_enter<F, R>(s: &'tcx Session,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore: &'tcx CrateStore,
|
2016-09-29 02:30:53 +03:00
|
|
|
local_providers: ty::maps::Providers<'tcx>,
|
|
|
|
extern_providers: ty::maps::Providers<'tcx>,
|
2017-04-27 13:04:57 -04:00
|
|
|
mir_passes: Rc<Passes>,
|
2016-12-23 20:48:21 -07:00
|
|
|
arenas: &'tcx GlobalArenas<'tcx>,
|
|
|
|
arena: &'tcx DroplessArena,
|
2016-12-15 11:13:24 +00:00
|
|
|
resolutions: ty::Resolutions,
|
2016-05-03 04:56:42 +03:00
|
|
|
named_region_map: resolve_lifetime::NamedRegionMap,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir: hir_map::Map<'tcx>,
|
2016-12-23 20:48:21 -07:00
|
|
|
crate_name: &str,
|
2016-05-03 04:56:42 +03:00
|
|
|
f: F) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
where F: for<'b> FnOnce(TyCtxt<'b, 'tcx, 'tcx>) -> R
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
2016-04-18 16:03:16 +03:00
|
|
|
let data_layout = TargetDataLayout::parse(s);
|
2016-12-23 20:48:21 -07:00
|
|
|
let interners = CtxtInterners::new(arena);
|
2016-04-28 03:13:17 +03:00
|
|
|
let common_types = CommonTypes::new(&interners);
|
2017-01-26 02:41:06 +02:00
|
|
|
let dep_graph = hir.dep_graph.clone();
|
2017-09-05 16:48:24 +02:00
|
|
|
let max_cnum = cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
|
2016-09-29 02:30:53 +03:00
|
|
|
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
|
|
|
|
providers[LOCAL_CRATE] = local_providers;
|
2017-05-31 14:53:39 +02:00
|
|
|
|
|
|
|
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
|
2017-09-05 16:48:24 +02:00
|
|
|
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = cstore
|
2017-09-07 08:13:41 -07:00
|
|
|
.crates_untracked()
|
2017-05-31 14:53:39 +02:00
|
|
|
.iter()
|
2017-09-05 16:48:24 +02:00
|
|
|
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
|
2017-05-31 14:53:39 +02:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let def_path_tables = || {
|
|
|
|
upstream_def_path_tables
|
|
|
|
.iter()
|
|
|
|
.map(|&(cnum, ref rc)| (cnum, &**rc))
|
|
|
|
.chain(iter::once((LOCAL_CRATE, hir.definitions().def_path_table())))
|
|
|
|
};
|
|
|
|
|
|
|
|
// Precompute the capacity of the hashmap so we don't have to
|
|
|
|
// re-allocate when populating it.
|
|
|
|
let capacity = def_path_tables().map(|(_, t)| t.size()).sum::<usize>();
|
|
|
|
|
|
|
|
let mut map: FxHashMap<_, _> = FxHashMap::with_capacity_and_hasher(
|
|
|
|
capacity,
|
|
|
|
::std::default::Default::default()
|
|
|
|
);
|
|
|
|
|
|
|
|
for (cnum, def_path_table) in def_path_tables() {
|
|
|
|
def_path_table.add_def_path_hashes_to(cnum, &mut map);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(map)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2017-09-08 13:51:57 -07:00
|
|
|
let mut trait_map = FxHashMap();
|
|
|
|
for (k, v) in resolutions.trait_map {
|
|
|
|
let hir_id = hir.node_to_hir_id(k);
|
|
|
|
let map = trait_map.entry(hir_id.owner)
|
|
|
|
.or_insert_with(|| Rc::new(FxHashMap()));
|
|
|
|
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
|
|
|
|
}
|
|
|
|
let mut defs = FxHashMap();
|
|
|
|
for (k, v) in named_region_map.defs {
|
|
|
|
let hir_id = hir.node_to_hir_id(k);
|
|
|
|
let map = defs.entry(hir_id.owner)
|
|
|
|
.or_insert_with(|| Rc::new(FxHashMap()));
|
|
|
|
Rc::get_mut(map).unwrap().insert(hir_id.local_id, v);
|
|
|
|
}
|
|
|
|
let mut late_bound = FxHashMap();
|
|
|
|
for k in named_region_map.late_bound {
|
|
|
|
let hir_id = hir.node_to_hir_id(k);
|
|
|
|
let map = late_bound.entry(hir_id.owner)
|
|
|
|
.or_insert_with(|| Rc::new(FxHashSet()));
|
|
|
|
Rc::get_mut(map).unwrap().insert(hir_id.local_id);
|
|
|
|
}
|
|
|
|
let mut object_lifetime_defaults = FxHashMap();
|
|
|
|
for (k, v) in named_region_map.object_lifetime_defaults {
|
|
|
|
let hir_id = hir.node_to_hir_id(k);
|
|
|
|
let map = object_lifetime_defaults.entry(hir_id.owner)
|
|
|
|
.or_insert_with(|| Rc::new(FxHashMap()));
|
|
|
|
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
tls::enter_global(GlobalCtxt {
|
2016-09-29 02:30:53 +03:00
|
|
|
sess: s,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-04-17 12:35:53 -04:00
|
|
|
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
|
2016-12-23 20:48:21 -07:00
|
|
|
global_arenas: arenas,
|
2016-04-28 03:13:17 +03:00
|
|
|
global_interners: interners,
|
2016-01-05 13:07:45 -05:00
|
|
|
dep_graph: dep_graph.clone(),
|
2015-09-06 21:51:58 +03:00
|
|
|
types: common_types,
|
2017-08-30 09:31:14 -07:00
|
|
|
named_region_map: NamedRegionMap {
|
2017-09-08 13:51:57 -07:00
|
|
|
defs,
|
|
|
|
late_bound,
|
|
|
|
object_lifetime_defaults,
|
2017-08-30 09:31:14 -07:00
|
|
|
},
|
2017-09-08 13:51:57 -07:00
|
|
|
trait_map,
|
2017-08-29 11:10:22 -07:00
|
|
|
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
|
2017-09-08 13:51:57 -07:00
|
|
|
(k, Rc::new(v))
|
2017-08-29 11:10:22 -07:00
|
|
|
}).collect(),
|
2017-08-31 12:30:25 -07:00
|
|
|
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
|
2017-09-08 13:51:57 -07:00
|
|
|
(hir.local_def_id(k), Rc::new(v))
|
2017-08-31 12:30:25 -07:00
|
|
|
}).collect(),
|
2017-08-31 13:19:33 -07:00
|
|
|
maybe_unused_trait_imports:
|
|
|
|
resolutions.maybe_unused_trait_imports
|
|
|
|
.into_iter()
|
2017-09-08 13:51:57 -07:00
|
|
|
.map(|id| hir.local_def_id(id))
|
2017-08-31 13:19:33 -07:00
|
|
|
.collect(),
|
|
|
|
maybe_unused_extern_crates:
|
|
|
|
resolutions.maybe_unused_extern_crates
|
|
|
|
.into_iter()
|
2017-09-08 13:51:57 -07:00
|
|
|
.map(|(id, sp)| (hir.local_def_id(id), sp))
|
2017-08-31 13:19:33 -07:00
|
|
|
.collect(),
|
2017-07-03 11:19:51 -07:00
|
|
|
hir,
|
|
|
|
def_path_hash_to_def_id,
|
2017-05-16 18:48:42 +02:00
|
|
|
maps: maps::Maps::new(providers),
|
2017-04-27 13:04:57 -04:00
|
|
|
mir_passes,
|
2016-11-08 14:02:55 +11:00
|
|
|
rcache: RefCell::new(FxHashMap()),
|
|
|
|
normalized_cache: RefCell::new(FxHashMap()),
|
2016-12-11 22:30:14 +08:00
|
|
|
inhabitedness_cache: RefCell::new(FxHashMap()),
|
2015-09-06 21:51:58 +03:00
|
|
|
used_mut_nodes: RefCell::new(NodeSet()),
|
|
|
|
selection_cache: traits::SelectionCache::new(),
|
2015-10-21 14:50:38 +03:00
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2016-12-20 23:05:21 +02:00
|
|
|
rvalue_promotable_to_static: RefCell::new(NodeMap()),
|
2016-11-16 10:52:37 +00:00
|
|
|
crate_name: Symbol::intern(crate_name),
|
2017-07-03 11:19:51 -07:00
|
|
|
data_layout,
|
2016-12-23 20:48:21 -07:00
|
|
|
layout_interner: RefCell::new(FxHashSet()),
|
2016-08-28 20:44:19 -04:00
|
|
|
layout_depth: Cell::new(0),
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
derive_macros: RefCell::new(NodeMap()),
|
2016-12-23 20:48:21 -07:00
|
|
|
stability_interner: RefCell::new(FxHashSet()),
|
2016-10-04 02:19:40 +03:00
|
|
|
all_traits: RefCell::new(None),
|
2015-09-06 21:51:58 +03:00
|
|
|
}, f)
|
|
|
|
}
|
2017-03-08 16:28:47 -05:00
|
|
|
|
|
|
|
pub fn consider_optimizing<T: Fn() -> String>(&self, msg: T) -> bool {
|
|
|
|
let cname = self.crate_name(LOCAL_CRATE).as_str();
|
|
|
|
self.sess.consider_optimizing(&cname, msg)
|
|
|
|
}
|
2017-08-31 08:57:41 -07:00
|
|
|
|
|
|
|
pub fn lang_items(self) -> Rc<middle::lang_items::LanguageItems> {
|
2017-09-07 07:21:49 -07:00
|
|
|
// FIXME(#42293) Right now we insert a `with_ignore` node in the dep
|
|
|
|
// graph here to ignore the fact that `get_lang_items` below depends on
|
|
|
|
// the entire crate. For now this'll prevent false positives of
|
|
|
|
// recompiling too much when anything changes.
|
2017-08-31 09:19:33 -07:00
|
|
|
//
|
|
|
|
// Once red/green incremental compilation lands we should be able to
|
|
|
|
// remove this because while the crate changes often the lint level map
|
|
|
|
// will change rarely.
|
|
|
|
self.dep_graph.with_ignore(|| {
|
|
|
|
self.get_lang_items(LOCAL_CRATE)
|
|
|
|
})
|
2017-08-31 08:57:41 -07:00
|
|
|
}
|
2017-08-31 15:08:34 -07:00
|
|
|
|
|
|
|
pub fn stability(self) -> Rc<stability::Index<'tcx>> {
|
2017-09-07 07:21:49 -07:00
|
|
|
// FIXME(#42293) we should actually track this, but fails too many tests
|
|
|
|
// today.
|
2017-08-31 15:08:34 -07:00
|
|
|
self.dep_graph.with_ignore(|| {
|
|
|
|
self.stability_index(LOCAL_CRATE)
|
|
|
|
})
|
|
|
|
}
|
2017-09-07 08:13:41 -07:00
|
|
|
|
|
|
|
pub fn crates(self) -> Rc<Vec<CrateNum>> {
|
|
|
|
self.all_crate_nums(LOCAL_CRATE)
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
|
2016-12-23 20:48:21 -07:00
|
|
|
/// Call the closure with a local `TyCtxt` using the given arena.
|
|
|
|
pub fn enter_local<F, R>(&self, arena: &'tcx DroplessArena, f: F) -> R
|
2016-05-11 04:14:41 +03:00
|
|
|
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
|
|
|
{
|
2016-12-23 20:48:21 -07:00
|
|
|
let interners = CtxtInterners::new(arena);
|
2016-05-11 04:14:41 +03:00
|
|
|
tls::enter(self, &interners, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// A trait implemented for all X<'a> types which can be safely and
|
|
|
|
/// efficiently converted to X<'tcx> as long as they are part of the
|
2016-02-29 23:36:51 +00:00
|
|
|
/// provided TyCtxt<'tcx>.
|
2015-09-06 21:51:58 +03:00
|
|
|
/// This can be done, for example, for Ty<'tcx> or &'tcx Substs<'tcx>
|
|
|
|
/// by looking them up in their respective interners.
|
2016-05-11 04:14:41 +03:00
|
|
|
///
|
|
|
|
/// However, this is still not the best implementation as it does
|
|
|
|
/// need to compare the components, even for interned values.
|
|
|
|
/// It would be more efficient if TypedArena provided a way to
|
|
|
|
/// determine whether the address is in the allocated range.
|
|
|
|
///
|
2015-09-06 21:51:58 +03:00
|
|
|
/// None is returned if the value or one of the components is not part
|
|
|
|
/// of the provided context.
|
|
|
|
/// For Ty, None can be returned if either the type interner doesn't
|
|
|
|
/// contain the TypeVariants key or if the address of the interned
|
|
|
|
/// pointer differs. The latter case is possible if a primitive type,
|
|
|
|
/// e.g. `()` or `u8`, was interned in a different context.
|
|
|
|
pub trait Lift<'tcx> {
|
|
|
|
type Lifted;
|
2016-04-29 06:00:23 +03:00
|
|
|
fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted>;
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
|
|
|
|
type Lifted = Ty<'tcx>;
|
2016-04-29 06:00:23 +03:00
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
|
2016-12-31 08:38:27 -07:00
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for Region<'a> {
|
|
|
|
type Lifted = Region<'tcx>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Region<'tcx>> {
|
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
2016-12-31 08:38:27 -07:00
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Const<'a> {
|
|
|
|
type Lifted = &'tcx Const<'tcx>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Const<'tcx>> {
|
2016-12-31 08:38:27 -07:00
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
2016-05-02 18:07:47 +03:00
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
|
|
|
|
type Lifted = &'tcx Substs<'tcx>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
|
|
|
|
if self.len() == 0 {
|
|
|
|
return Some(Slice::empty());
|
|
|
|
}
|
|
|
|
if tcx.interners.arena.in_arena(&self[..] as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 11:08:16 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Slice<Ty<'a>> {
|
|
|
|
type Lifted = &'tcx Slice<Ty<'tcx>>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
|
|
|
|
-> Option<&'tcx Slice<Ty<'tcx>>> {
|
2016-10-24 18:23:29 -06:00
|
|
|
if self.len() == 0 {
|
|
|
|
return Some(Slice::empty());
|
|
|
|
}
|
2016-12-31 08:38:27 -07:00
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
2016-04-29 08:30:54 +03:00
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Slice<ExistentialPredicate<'a>> {
|
|
|
|
type Lifted = &'tcx Slice<ExistentialPredicate<'tcx>>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
|
|
|
|
-> Option<&'tcx Slice<ExistentialPredicate<'tcx>>> {
|
|
|
|
if self.is_empty() {
|
|
|
|
return Some(Slice::empty());
|
|
|
|
}
|
2016-12-31 08:38:27 -07:00
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
2016-11-16 09:21:49 -07:00
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Slice<Predicate<'a>> {
|
|
|
|
type Lifted = &'tcx Slice<Predicate<'tcx>>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
|
|
|
|
-> Option<&'tcx Slice<Predicate<'tcx>>> {
|
|
|
|
if self.is_empty() {
|
|
|
|
return Some(Slice::empty());
|
|
|
|
}
|
|
|
|
if tcx.interners.arena.in_arena(*self as *const _) {
|
|
|
|
return Some(unsafe { mem::transmute(*self) });
|
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub mod tls {
|
2016-05-11 04:14:41 +03:00
|
|
|
use super::{CtxtInterners, GlobalCtxt, TyCtxt};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
use std::cell::Cell;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::fmt;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Marker types used for the scoped TLS slot.
|
2015-09-06 21:51:58 +03:00
|
|
|
/// The type context cannot be used directly because the scoped TLS
|
|
|
|
/// in libstd doesn't allow types generic over lifetimes.
|
2016-05-03 04:56:42 +03:00
|
|
|
enum ThreadLocalGlobalCtxt {}
|
2016-05-11 04:14:41 +03:00
|
|
|
enum ThreadLocalInterners {}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
thread_local! {
|
2016-05-11 04:14:41 +03:00
|
|
|
static TLS_TCX: Cell<Option<(*const ThreadLocalGlobalCtxt,
|
|
|
|
*const ThreadLocalInterners)>> = Cell::new(None)
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
fn span_debug(span: syntax_pos::Span, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-09-06 21:51:58 +03:00
|
|
|
with(|tcx| {
|
|
|
|
write!(f, "{}", tcx.sess.codemap().span_to_string(span))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn enter_global<'gcx, F, R>(gcx: GlobalCtxt<'gcx>, f: F) -> R
|
|
|
|
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'gcx>) -> R
|
2016-04-28 03:13:17 +03:00
|
|
|
{
|
2016-06-21 18:08:13 -04:00
|
|
|
syntax_pos::SPAN_DEBUG.with(|span_dbg| {
|
2015-09-06 21:51:58 +03:00
|
|
|
let original_span_debug = span_dbg.get();
|
|
|
|
span_dbg.set(span_debug);
|
2016-05-11 04:14:41 +03:00
|
|
|
let result = enter(&gcx, &gcx.global_interners, f);
|
2015-09-06 21:51:58 +03:00
|
|
|
span_dbg.set(original_span_debug);
|
|
|
|
result
|
2015-09-28 15:00:15 +13:00
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn enter<'a, 'gcx: 'tcx, 'tcx, F, R>(gcx: &'a GlobalCtxt<'gcx>,
|
|
|
|
interners: &'a CtxtInterners<'tcx>,
|
|
|
|
f: F) -> R
|
|
|
|
where F: FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
|
|
|
{
|
|
|
|
let gcx_ptr = gcx as *const _ as *const ThreadLocalGlobalCtxt;
|
|
|
|
let interners_ptr = interners as *const _ as *const ThreadLocalInterners;
|
|
|
|
TLS_TCX.with(|tls| {
|
|
|
|
let prev = tls.get();
|
|
|
|
tls.set(Some((gcx_ptr, interners_ptr)));
|
|
|
|
let ret = f(TyCtxt {
|
2017-07-03 11:19:51 -07:00
|
|
|
gcx,
|
|
|
|
interners,
|
2016-05-11 04:14:41 +03:00
|
|
|
});
|
|
|
|
tls.set(prev);
|
|
|
|
ret
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn with<F, R>(f: F) -> R
|
2016-05-11 04:14:41 +03:00
|
|
|
where F: for<'a, 'gcx, 'tcx> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
TLS_TCX.with(|tcx| {
|
|
|
|
let (gcx, interners) = tcx.get().unwrap();
|
2016-04-28 03:13:17 +03:00
|
|
|
let gcx = unsafe { &*(gcx as *const GlobalCtxt) };
|
2016-05-11 04:14:41 +03:00
|
|
|
let interners = unsafe { &*(interners as *const CtxtInterners) };
|
2016-05-03 04:56:42 +03:00
|
|
|
f(TyCtxt {
|
2017-07-03 11:19:51 -07:00
|
|
|
gcx,
|
|
|
|
interners,
|
2016-05-03 04:56:42 +03:00
|
|
|
})
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn with_opt<F, R>(f: F) -> R
|
2016-05-11 04:14:41 +03:00
|
|
|
where F: for<'a, 'gcx, 'tcx> FnOnce(Option<TyCtxt<'a, 'gcx, 'tcx>>) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
if TLS_TCX.with(|tcx| tcx.get().is_some()) {
|
2015-09-06 21:51:58 +03:00
|
|
|
with(|v| f(Some(v)))
|
|
|
|
} else {
|
|
|
|
f(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! sty_debug_print {
|
|
|
|
($ctxt: expr, $($variant: ident),*) => {{
|
|
|
|
// curious inner module to allow variant names to be used as
|
|
|
|
// variable names.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
mod inner {
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{self, TyCtxt};
|
2016-05-11 04:14:41 +03:00
|
|
|
use ty::context::Interned;
|
2016-03-23 04:56:49 +02:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct DebugStat {
|
|
|
|
total: usize,
|
|
|
|
region_infer: usize,
|
|
|
|
ty_infer: usize,
|
|
|
|
both_infer: usize,
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn go(tcx: TyCtxt) {
|
2015-09-06 21:51:58 +03:00
|
|
|
let mut total = DebugStat {
|
|
|
|
total: 0,
|
|
|
|
region_infer: 0, ty_infer: 0, both_infer: 0,
|
|
|
|
};
|
|
|
|
$(let mut $variant = total;)*
|
|
|
|
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
for &Interned(t) in tcx.interners.type_.borrow().iter() {
|
2015-09-06 21:51:58 +03:00
|
|
|
let variant = match t.sty {
|
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(..) | ty::TyUint(..) |
|
2016-08-02 15:56:20 +08:00
|
|
|
ty::TyFloat(..) | ty::TyStr | ty::TyNever => continue,
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyError => /* unimportant */ continue,
|
|
|
|
$(ty::$variant(..) => &mut $variant,)*
|
|
|
|
};
|
2017-05-12 12:01:43 -04:00
|
|
|
let region = t.flags.intersects(ty::TypeFlags::HAS_RE_INFER);
|
|
|
|
let ty = t.flags.intersects(ty::TypeFlags::HAS_TY_INFER);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
variant.total += 1;
|
|
|
|
total.total += 1;
|
|
|
|
if region { total.region_infer += 1; variant.region_infer += 1 }
|
|
|
|
if ty { total.ty_infer += 1; variant.ty_infer += 1 }
|
|
|
|
if region && ty { total.both_infer += 1; variant.both_infer += 1 }
|
|
|
|
}
|
|
|
|
println!("Ty interner total ty region both");
|
|
|
|
$(println!(" {:18}: {uses:6} {usespc:4.1}%, \
|
|
|
|
{ty:4.1}% {region:5.1}% {both:4.1}%",
|
|
|
|
stringify!($variant),
|
|
|
|
uses = $variant.total,
|
|
|
|
usespc = $variant.total as f64 * 100.0 / total.total as f64,
|
|
|
|
ty = $variant.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
region = $variant.region_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
both = $variant.both_infer as f64 * 100.0 / total.total as f64);
|
|
|
|
)*
|
|
|
|
println!(" total {uses:6} \
|
|
|
|
{ty:4.1}% {region:5.1}% {both:4.1}%",
|
|
|
|
uses = total.total,
|
|
|
|
ty = total.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
region = total.region_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
both = total.both_infer as f64 * 100.0 / total.total as f64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner::go($ctxt)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn print_debug_stats(self) {
|
2015-09-06 21:51:58 +03:00
|
|
|
sty_debug_print!(
|
|
|
|
self,
|
2016-12-26 14:34:03 +01:00
|
|
|
TyAdt, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr, TyGenerator,
|
2016-11-16 09:21:49 -07:00
|
|
|
TyDynamic, TyClosure, TyTuple, TyParam, TyInfer, TyProjection, TyAnon);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
println!("Substs interner: #{}", self.interners.substs.borrow().len());
|
|
|
|
println!("Region interner: #{}", self.interners.region.borrow().len());
|
2016-12-23 20:48:21 -07:00
|
|
|
println!("Stability interner: #{}", self.stability_interner.borrow().len());
|
|
|
|
println!("Layout interner: #{}", self.layout_interner.borrow().len());
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// An entry in an interner.
|
|
|
|
struct Interned<'tcx, T: 'tcx+?Sized>(&'tcx T);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
// NB: An Interned<Ty> compares and hashes as a sty.
|
|
|
|
impl<'tcx> PartialEq for Interned<'tcx, TyS<'tcx>> {
|
|
|
|
fn eq(&self, other: &Interned<'tcx, TyS<'tcx>>) -> bool {
|
|
|
|
self.0.sty == other.0.sty
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx> Eq for Interned<'tcx, TyS<'tcx>> {}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx> Hash for Interned<'tcx, TyS<'tcx>> {
|
2015-09-06 21:51:58 +03:00
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
2016-05-11 04:14:41 +03:00
|
|
|
self.0.sty.hash(s)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<TypeVariants<'lcx>> for Interned<'tcx, TyS<'tcx>> {
|
2016-03-23 04:56:49 +02:00
|
|
|
fn borrow<'a>(&'a self) -> &'a TypeVariants<'lcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
&self.0.sty
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 11:08:16 +03:00
|
|
|
// NB: An Interned<Slice<T>> compares and hashes as its elements.
|
|
|
|
impl<'tcx, T: PartialEq> PartialEq for Interned<'tcx, Slice<T>> {
|
|
|
|
fn eq(&self, other: &Interned<'tcx, Slice<T>>) -> bool {
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, T: Eq> Eq for Interned<'tcx, Slice<T>> {}
|
|
|
|
|
|
|
|
impl<'tcx, T: Hash> Hash for Interned<'tcx, Slice<T>> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
self.0[..].hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<[Ty<'lcx>]> for Interned<'tcx, Slice<Ty<'tcx>>> {
|
2016-04-29 08:30:54 +03:00
|
|
|
fn borrow<'a>(&'a self) -> &'a [Ty<'lcx>] {
|
2016-09-02 11:08:16 +03:00
|
|
|
&self.0[..]
|
2016-04-29 08:30:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 11:36:12 -07:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<[Kind<'lcx>]> for Interned<'tcx, Substs<'tcx>> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a [Kind<'lcx>] {
|
|
|
|
&self.0[..]
|
2016-03-23 04:56:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:10:47 +03:00
|
|
|
impl<'tcx> Borrow<RegionKind> for Interned<'tcx, RegionKind> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a RegionKind {
|
2017-04-20 04:45:53 -04:00
|
|
|
&self.0
|
2016-05-02 18:07:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
|
|
|
|
for Interned<'tcx, Slice<ExistentialPredicate<'tcx>>> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:37:00 -04:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<[Predicate<'lcx>]>
|
|
|
|
for Interned<'tcx, Slice<Predicate<'tcx>>> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a [Predicate<'lcx>] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<Const<'lcx>> for Interned<'tcx, Const<'tcx>> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a Const<'lcx> {
|
2017-08-04 00:41:44 +03:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 11:08:16 +03:00
|
|
|
macro_rules! intern_method {
|
|
|
|
($lt_tcx:tt, $name:ident: $method:ident($alloc:ty,
|
2016-10-16 21:21:25 -06:00
|
|
|
$alloc_method:ident,
|
2016-09-02 11:08:16 +03:00
|
|
|
$alloc_to_key:expr,
|
|
|
|
$alloc_to_ret:expr,
|
|
|
|
$needs_infer:expr) -> $ty:ty) => {
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, $lt_tcx> TyCtxt<'a, 'gcx, $lt_tcx> {
|
|
|
|
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
|
2016-09-02 11:08:16 +03:00
|
|
|
{
|
|
|
|
let key = ($alloc_to_key)(&v);
|
|
|
|
if let Some(i) = self.interners.$name.borrow().get(key) {
|
2016-05-11 04:14:41 +03:00
|
|
|
return i.0;
|
|
|
|
}
|
2016-09-02 11:08:16 +03:00
|
|
|
if !self.is_global() {
|
|
|
|
if let Some(i) = self.global_interners.$name.borrow().get(key) {
|
|
|
|
return i.0;
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) Depend on flags being accurate to
|
|
|
|
// determine that all contents are in the global tcx.
|
|
|
|
// See comments on Lift for why we can't use that.
|
|
|
|
if !($needs_infer)(&v) {
|
|
|
|
if !self.is_global() {
|
|
|
|
let v = unsafe {
|
|
|
|
mem::transmute(v)
|
|
|
|
};
|
2016-12-23 20:48:21 -07:00
|
|
|
let i = ($alloc_to_ret)(self.global_interners.arena.$alloc_method(v));
|
2016-05-11 04:14:41 +03:00
|
|
|
self.global_interners.$name.borrow_mut().insert(Interned(i));
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't end up with inference
|
|
|
|
// types/regions in the global tcx.
|
|
|
|
if self.is_global() {
|
|
|
|
bug!("Attempted to intern `{:?}` which contains \
|
|
|
|
inference types/regions in the global type context",
|
|
|
|
v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:38:27 -07:00
|
|
|
let i = ($alloc_to_ret)(self.interners.arena.$alloc_method(v));
|
2016-05-11 04:14:41 +03:00
|
|
|
self.interners.$name.borrow_mut().insert(Interned(i));
|
|
|
|
i
|
|
|
|
}
|
2016-09-02 11:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! direct_interners {
|
|
|
|
($lt_tcx:tt, $($name:ident: $method:ident($needs_infer:expr) -> $ty:ty),+) => {
|
|
|
|
$(impl<$lt_tcx> PartialEq for Interned<$lt_tcx, $ty> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<$lt_tcx> Eq for Interned<$lt_tcx, $ty> {}
|
|
|
|
|
|
|
|
impl<$lt_tcx> Hash for Interned<$lt_tcx, $ty> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
self.0.hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:21:25 -06:00
|
|
|
intern_method!($lt_tcx, $name: $method($ty, alloc, |x| x, |x| x, $needs_infer) -> $ty);)+
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2017-08-14 13:23:56 +03:00
|
|
|
pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
|
2016-05-11 04:14:41 +03:00
|
|
|
x.has_type_flags(ty::TypeFlags::KEEP_IN_LOCAL_TCX)
|
|
|
|
}
|
|
|
|
|
2016-09-02 11:08:16 +03:00
|
|
|
direct_interners!('tcx,
|
|
|
|
region: mk_region(|r| {
|
2016-08-25 23:58:52 +03:00
|
|
|
match r {
|
|
|
|
&ty::ReVar(_) | &ty::ReSkolemized(..) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
2017-08-04 00:41:44 +03:00
|
|
|
}) -> RegionKind,
|
2017-08-04 11:25:13 +03:00
|
|
|
const_: mk_const(|c: &Const| keep_local(&c.ty) || keep_local(&c.val)) -> Const<'tcx>
|
2016-05-11 04:14:41 +03:00
|
|
|
);
|
|
|
|
|
2016-10-16 21:21:25 -06:00
|
|
|
macro_rules! slice_interners {
|
|
|
|
($($field:ident: $method:ident($ty:ident)),+) => (
|
|
|
|
$(intern_method!('tcx, $field: $method(&[$ty<'tcx>], alloc_slice, Deref::deref,
|
|
|
|
|xs: &[$ty]| -> &Slice<$ty> {
|
|
|
|
unsafe { mem::transmute(xs) }
|
|
|
|
}, |xs: &[$ty]| xs.iter().any(keep_local)) -> Slice<$ty<'tcx>>);)+
|
|
|
|
)
|
|
|
|
}
|
2016-09-02 11:08:16 +03:00
|
|
|
|
2016-10-16 21:21:25 -06:00
|
|
|
slice_interners!(
|
2016-11-16 09:21:49 -07:00
|
|
|
existential_predicates: _intern_existential_predicates(ExistentialPredicate),
|
2017-05-02 14:37:00 -04:00
|
|
|
predicates: _intern_predicates(Predicate),
|
2016-10-24 18:23:29 -06:00
|
|
|
type_list: _intern_type_list(Ty),
|
|
|
|
substs: _intern_substs(Kind)
|
2016-10-09 11:36:12 -07:00
|
|
|
);
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Create an unsafe fn ty based on a safe fn ty.
|
2017-02-13 10:51:06 +02:00
|
|
|
pub fn safe_to_unsafe_fn_ty(self, sig: PolyFnSig<'tcx>) -> Ty<'tcx> {
|
|
|
|
assert_eq!(sig.unsafety(), hir::Unsafety::Normal);
|
|
|
|
self.mk_fn_ptr(sig.map_bound(|sig| ty::FnSig {
|
2015-09-06 21:51:58 +03:00
|
|
|
unsafety: hir::Unsafety::Unsafe,
|
2017-02-13 10:51:06 +02:00
|
|
|
..sig
|
2016-04-29 08:30:54 +03:00
|
|
|
}))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
// Interns a type/name combination, stores the resulting box in cx.interners,
|
2015-09-06 21:51:58 +03:00
|
|
|
// and returns the box as cast to an unsafe ptr (see comments for Ty above).
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_ty(self, st: TypeVariants<'tcx>) -> Ty<'tcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
let global_interners = if !self.is_global() {
|
|
|
|
Some(&self.global_interners)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
self.interners.intern_ty(st, global_interners)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::IntTy::Is => self.types.isize,
|
|
|
|
ast::IntTy::I8 => self.types.i8,
|
|
|
|
ast::IntTy::I16 => self.types.i16,
|
|
|
|
ast::IntTy::I32 => self.types.i32,
|
|
|
|
ast::IntTy::I64 => self.types.i64,
|
2016-08-23 03:56:52 +03:00
|
|
|
ast::IntTy::I128 => self.types.i128,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_uint(self, tm: ast::UintTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::UintTy::Us => self.types.usize,
|
|
|
|
ast::UintTy::U8 => self.types.u8,
|
|
|
|
ast::UintTy::U16 => self.types.u16,
|
|
|
|
ast::UintTy::U32 => self.types.u32,
|
|
|
|
ast::UintTy::U64 => self.types.u64,
|
2016-08-23 03:56:52 +03:00
|
|
|
ast::UintTy::U128 => self.types.u128,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_float(self, tm: ast::FloatTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:09:01 +01:00
|
|
|
ast::FloatTy::F32 => self.types.f32,
|
|
|
|
ast::FloatTy::F64 => self.types.f64,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_str(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyStr)
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_static_str(self) -> Ty<'tcx> {
|
2017-04-20 01:58:12 +03:00
|
|
|
self.mk_imm_ref(self.types.re_static, self.mk_str())
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:33:29 +02:00
|
|
|
pub fn mk_adt(self, def: &'tcx AdtDef, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
// take a copy of substs so that we own the vectors inside
|
2016-09-06 01:26:02 +03:00
|
|
|
self.mk_ty(TyAdt(def, substs))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_box(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2017-01-21 17:40:31 +03:00
|
|
|
let def_id = self.require_lang_item(lang_items::OwnedBoxLangItem);
|
2017-04-24 15:20:46 +03:00
|
|
|
let adt_def = self.adt_def(def_id);
|
2017-01-21 17:40:31 +03:00
|
|
|
let substs = self.mk_substs(iter::once(Kind::from(ty)));
|
|
|
|
self.mk_ty(TyAdt(adt_def, substs))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_ptr(self, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyRawPtr(tm))
|
|
|
|
}
|
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
pub fn mk_ref(self, r: Region<'tcx>, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyRef(r, tm))
|
|
|
|
}
|
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
pub fn mk_mut_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ref(r, TypeAndMut {ty: ty, mutbl: hir::MutMutable})
|
|
|
|
}
|
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
pub fn mk_imm_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ref(r, TypeAndMut {ty: ty, mutbl: hir::MutImmutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mut_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ptr(TypeAndMut {ty: ty, mutbl: hir::MutMutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_imm_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ptr(TypeAndMut {ty: ty, mutbl: hir::MutImmutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_nil_ptr(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_imm_ptr(self.mk_nil())
|
|
|
|
}
|
|
|
|
|
2017-08-05 12:27:28 +03:00
|
|
|
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
|
|
|
|
let n = ConstUsize::new(n, self.sess.target.usize_ty).unwrap();
|
2017-08-05 16:11:24 +03:00
|
|
|
self.mk_array_const_usize(ty, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_array_const_usize(self, ty: Ty<'tcx>, n: ConstUsize) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyArray(ty, self.mk_const(ty::Const {
|
|
|
|
val: ConstVal::Integral(ConstInt::Usize(n)),
|
|
|
|
ty: self.types.usize
|
|
|
|
})))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_slice(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TySlice(ty))
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:58:37 +08:00
|
|
|
pub fn intern_tup(self, ts: &[Ty<'tcx>], defaulted: bool) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyTuple(self.intern_type_list(ts), defaulted))
|
2016-10-24 18:23:29 -06:00
|
|
|
}
|
|
|
|
|
2017-01-11 15:58:37 +08:00
|
|
|
pub fn mk_tup<I: InternAs<[Ty<'tcx>], Ty<'tcx>>>(self, iter: I,
|
|
|
|
defaulted: bool) -> I::Output {
|
|
|
|
iter.intern_with(|ts| self.mk_ty(TyTuple(self.intern_type_list(ts), defaulted)))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_nil(self) -> Ty<'tcx> {
|
2017-01-11 15:58:37 +08:00
|
|
|
self.intern_tup(&[], false)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-08-02 19:56:33 +08:00
|
|
|
pub fn mk_diverging_default(self) -> Ty<'tcx> {
|
|
|
|
if self.sess.features.borrow().never_type {
|
|
|
|
self.types.never
|
|
|
|
} else {
|
2017-01-11 15:58:37 +08:00
|
|
|
self.intern_tup(&[], true)
|
2016-08-02 19:56:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_bool(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyBool)
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_fn_def(self, def_id: DefId,
|
2017-05-13 17:11:52 +03:00
|
|
|
substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyFnDef(def_id, substs))
|
2015-06-13 13:15:03 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 10:51:06 +02:00
|
|
|
pub fn mk_fn_ptr(self, fty: PolyFnSig<'tcx>) -> Ty<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
self.mk_ty(TyFnPtr(fty))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
pub fn mk_dynamic(
|
|
|
|
self,
|
|
|
|
obj: ty::Binder<&'tcx Slice<ExistentialPredicate<'tcx>>>,
|
2017-04-20 04:45:53 -04:00
|
|
|
reg: ty::Region<'tcx>
|
2016-11-16 09:21:49 -07:00
|
|
|
) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyDynamic(obj, reg))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_projection(self,
|
2017-07-11 10:33:09 -04:00
|
|
|
item_def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>)
|
2016-11-16 09:21:49 -07:00
|
|
|
-> Ty<'tcx> {
|
2017-07-11 10:33:09 -04:00
|
|
|
self.mk_ty(TyProjection(ProjectionTy {
|
2017-08-06 22:54:09 -07:00
|
|
|
item_def_id,
|
|
|
|
substs,
|
2017-07-11 10:33:09 -04:00
|
|
|
}))
|
2016-11-16 09:21:49 -07:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_closure(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
closure_id: DefId,
|
2016-11-03 22:19:33 +02:00
|
|
|
substs: &'tcx Substs<'tcx>)
|
2016-11-16 09:21:49 -07:00
|
|
|
-> Ty<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
self.mk_closure_from_closure_substs(closure_id, ClosureSubsts {
|
2017-07-03 11:19:51 -07:00
|
|
|
substs,
|
2016-04-29 08:30:54 +03:00
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_closure_from_closure_substs(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
closure_id: DefId,
|
2016-04-29 08:30:54 +03:00
|
|
|
closure_substs: ClosureSubsts<'tcx>)
|
2015-09-06 21:51:58 +03:00
|
|
|
-> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyClosure(closure_id, closure_substs))
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:57:26 -07:00
|
|
|
pub fn mk_generator(self,
|
2016-12-26 14:34:03 +01:00
|
|
|
id: DefId,
|
|
|
|
closure_substs: ClosureSubsts<'tcx>,
|
|
|
|
interior: GeneratorInterior<'tcx>)
|
|
|
|
-> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyGenerator(id, closure_substs, interior))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_var(self, v: TyVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(TyVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_int_var(self, v: IntVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(IntVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_float_var(self, v: FloatVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(FloatVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_infer(self, it: InferTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyInfer(it))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_param(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
index: u32,
|
|
|
|
name: Name) -> Ty<'tcx> {
|
2016-08-17 06:32:00 +03:00
|
|
|
self.mk_ty(TyParam(ParamTy { idx: index, name: name }))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_self_type(self) -> Ty<'tcx> {
|
2016-08-17 06:32:00 +03:00
|
|
|
self.mk_param(0, keywords::SelfType.name())
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_param_from_def(self, def: &ty::TypeParameterDef) -> Ty<'tcx> {
|
2016-08-17 06:32:00 +03:00
|
|
|
self.mk_param(def.index, def.name)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2016-07-22 18:56:22 +03:00
|
|
|
pub fn mk_anon(self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyAnon(def_id, substs))
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
pub fn intern_existential_predicates(self, eps: &[ExistentialPredicate<'tcx>])
|
|
|
|
-> &'tcx Slice<ExistentialPredicate<'tcx>> {
|
|
|
|
assert!(!eps.is_empty());
|
|
|
|
assert!(eps.windows(2).all(|w| w[0].cmp(self, &w[1]) != Ordering::Greater));
|
|
|
|
self._intern_existential_predicates(eps)
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:37:00 -04:00
|
|
|
pub fn intern_predicates(self, preds: &[Predicate<'tcx>])
|
|
|
|
-> &'tcx Slice<Predicate<'tcx>> {
|
|
|
|
// FIXME consider asking the input slice to be sorted to avoid
|
|
|
|
// re-interning permutations, in which case that would be asserted
|
|
|
|
// here.
|
|
|
|
if preds.len() == 0 {
|
|
|
|
// The macro-generated method below asserts we don't intern an empty slice.
|
|
|
|
Slice::empty()
|
|
|
|
} else {
|
|
|
|
self._intern_predicates(preds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:23:29 -06:00
|
|
|
pub fn intern_type_list(self, ts: &[Ty<'tcx>]) -> &'tcx Slice<Ty<'tcx>> {
|
|
|
|
if ts.len() == 0 {
|
|
|
|
Slice::empty()
|
|
|
|
} else {
|
|
|
|
self._intern_type_list(ts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn intern_substs(self, ts: &[Kind<'tcx>]) -> &'tcx Slice<Kind<'tcx>> {
|
|
|
|
if ts.len() == 0 {
|
|
|
|
Slice::empty()
|
|
|
|
} else {
|
|
|
|
self._intern_substs(ts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 10:51:06 +02:00
|
|
|
pub fn mk_fn_sig<I>(self,
|
|
|
|
inputs: I,
|
|
|
|
output: I::Item,
|
|
|
|
variadic: bool,
|
|
|
|
unsafety: hir::Unsafety,
|
|
|
|
abi: abi::Abi)
|
2016-11-28 20:25:33 -07:00
|
|
|
-> <I::Item as InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>>::Output
|
|
|
|
where I: Iterator,
|
|
|
|
I::Item: InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>
|
|
|
|
{
|
|
|
|
inputs.chain(iter::once(output)).intern_with(|xs| ty::FnSig {
|
|
|
|
inputs_and_output: self.intern_type_list(xs),
|
2017-02-13 10:51:06 +02:00
|
|
|
variadic, unsafety, abi
|
2016-11-28 20:25:33 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
pub fn mk_existential_predicates<I: InternAs<[ExistentialPredicate<'tcx>],
|
|
|
|
&'tcx Slice<ExistentialPredicate<'tcx>>>>(self, iter: I)
|
|
|
|
-> I::Output {
|
|
|
|
iter.intern_with(|xs| self.intern_existential_predicates(xs))
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:37:00 -04:00
|
|
|
pub fn mk_predicates<I: InternAs<[Predicate<'tcx>],
|
|
|
|
&'tcx Slice<Predicate<'tcx>>>>(self, iter: I)
|
|
|
|
-> I::Output {
|
|
|
|
iter.intern_with(|xs| self.intern_predicates(xs))
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:23:29 -06:00
|
|
|
pub fn mk_type_list<I: InternAs<[Ty<'tcx>],
|
|
|
|
&'tcx Slice<Ty<'tcx>>>>(self, iter: I) -> I::Output {
|
|
|
|
iter.intern_with(|xs| self.intern_type_list(xs))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_substs<I: InternAs<[Kind<'tcx>],
|
|
|
|
&'tcx Slice<Kind<'tcx>>>>(self, iter: I) -> I::Output {
|
|
|
|
iter.intern_with(|xs| self.intern_substs(xs))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_substs_trait(self,
|
|
|
|
s: Ty<'tcx>,
|
|
|
|
t: &[Ty<'tcx>])
|
|
|
|
-> &'tcx Substs<'tcx>
|
|
|
|
{
|
|
|
|
self.mk_substs(iter::once(s).chain(t.into_iter().cloned()).map(Kind::from))
|
|
|
|
}
|
2017-07-26 21:51:09 -07:00
|
|
|
|
|
|
|
pub fn lint_node<S: Into<MultiSpan>>(self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: NodeId,
|
|
|
|
span: S,
|
|
|
|
msg: &str) {
|
|
|
|
self.struct_span_lint_node(lint, id, span.into(), msg).emit()
|
|
|
|
}
|
|
|
|
|
2017-08-13 20:07:09 +02:00
|
|
|
pub fn lint_node_note<S: Into<MultiSpan>>(self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: NodeId,
|
|
|
|
span: S,
|
|
|
|
msg: &str,
|
|
|
|
note: &str) {
|
|
|
|
let mut err = self.struct_span_lint_node(lint, id, span.into(), msg);
|
|
|
|
err.note(note);
|
|
|
|
err.emit()
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:51:09 -07:00
|
|
|
pub fn lint_level_at_node(self, lint: &'static Lint, mut id: NodeId)
|
|
|
|
-> (lint::Level, lint::LintSource)
|
|
|
|
{
|
|
|
|
// Right now we insert a `with_ignore` node in the dep graph here to
|
|
|
|
// ignore the fact that `lint_levels` below depends on the entire crate.
|
|
|
|
// For now this'll prevent false positives of recompiling too much when
|
|
|
|
// anything changes.
|
|
|
|
//
|
|
|
|
// Once red/green incremental compilation lands we should be able to
|
|
|
|
// remove this because while the crate changes often the lint level map
|
|
|
|
// will change rarely.
|
|
|
|
self.dep_graph.with_ignore(|| {
|
|
|
|
let sets = self.lint_levels(LOCAL_CRATE);
|
|
|
|
loop {
|
|
|
|
let hir_id = self.hir.definitions().node_to_hir_id(id);
|
|
|
|
if let Some(pair) = sets.level_and_source(lint, hir_id) {
|
|
|
|
return pair
|
|
|
|
}
|
|
|
|
let next = self.hir.get_parent_node(id);
|
|
|
|
if next == id {
|
|
|
|
bug!("lint traversal reached the root of the crate");
|
|
|
|
}
|
|
|
|
id = next;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_span_lint_node<S: Into<MultiSpan>>(self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: NodeId,
|
|
|
|
span: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'tcx>
|
|
|
|
{
|
|
|
|
let (level, src) = self.lint_level_at_node(lint, id);
|
|
|
|
lint::struct_lint_level(self.sess, lint, level, src, Some(span.into()), msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_lint_node(self, lint: &'static Lint, id: NodeId, msg: &str)
|
|
|
|
-> DiagnosticBuilder<'tcx>
|
|
|
|
{
|
|
|
|
let (level, src) = self.lint_level_at_node(lint, id);
|
|
|
|
lint::struct_lint_level(self.sess, lint, level, src, None, msg)
|
|
|
|
}
|
2017-09-08 13:51:57 -07:00
|
|
|
|
|
|
|
pub fn in_scope_traits(self, id: HirId) -> Option<Rc<Vec<TraitCandidate>>> {
|
|
|
|
self.in_scope_traits_map(id.owner)
|
|
|
|
.and_then(|map| map.get(&id.local_id).cloned())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
|
|
|
|
self.named_region_map(id.owner)
|
|
|
|
.and_then(|map| map.get(&id.local_id).cloned())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_late_bound(self, id: HirId) -> bool {
|
|
|
|
self.is_late_bound_map(id.owner)
|
|
|
|
.map(|set| set.contains(&id.local_id))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn object_lifetime_defaults(self, id: HirId)
|
|
|
|
-> Option<Rc<Vec<ObjectLifetimeDefault>>>
|
|
|
|
{
|
|
|
|
self.object_lifetime_defaults_map(id.owner)
|
|
|
|
.and_then(|map| map.get(&id.local_id).cloned())
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-10-24 18:23:29 -06:00
|
|
|
|
|
|
|
pub trait InternAs<T: ?Sized, R> {
|
|
|
|
type Output;
|
2017-05-02 05:55:20 +02:00
|
|
|
fn intern_with<F>(self, f: F) -> Self::Output
|
2016-10-24 18:23:29 -06:00
|
|
|
where F: FnOnce(&T) -> R;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I, T, R, E> InternAs<[T], R> for I
|
|
|
|
where E: InternIteratorElement<T, R>,
|
|
|
|
I: Iterator<Item=E> {
|
|
|
|
type Output = E::Output;
|
|
|
|
fn intern_with<F>(self, f: F) -> Self::Output
|
|
|
|
where F: FnOnce(&[T]) -> R {
|
|
|
|
E::intern_with(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait InternIteratorElement<T, R>: Sized {
|
|
|
|
type Output;
|
|
|
|
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, R> InternIteratorElement<T, R> for T {
|
|
|
|
type Output = R;
|
|
|
|
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {
|
|
|
|
f(&iter.collect::<AccumulateVec<[_; 8]>>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 01:41:26 +02:00
|
|
|
impl<'a, T, R> InternIteratorElement<T, R> for &'a T
|
|
|
|
where T: Clone + 'a
|
|
|
|
{
|
|
|
|
type Output = R;
|
|
|
|
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {
|
|
|
|
f(&iter.cloned().collect::<AccumulateVec<[_; 8]>>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:23:29 -06:00
|
|
|
impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
|
|
|
|
type Output = Result<R, E>;
|
|
|
|
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {
|
|
|
|
Ok(f(&iter.collect::<Result<AccumulateVec<[_; 8]>, _>>()?))
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 11:10:22 -07:00
|
|
|
|
2017-08-30 09:31:14 -07:00
|
|
|
struct NamedRegionMap {
|
2017-09-08 13:51:57 -07:00
|
|
|
defs: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, resolve_lifetime::Region>>>,
|
|
|
|
late_bound: FxHashMap<DefIndex, Rc<FxHashSet<ItemLocalId>>>,
|
|
|
|
object_lifetime_defaults:
|
|
|
|
FxHashMap<
|
|
|
|
DefIndex,
|
|
|
|
Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>,
|
|
|
|
>,
|
2017-08-29 11:10:22 -07:00
|
|
|
}
|
|
|
|
|
2017-08-29 11:10:22 -07:00
|
|
|
pub fn provide(providers: &mut ty::maps::Providers) {
|
2017-08-31 15:08:34 -07:00
|
|
|
// FIXME(#44234) - almost all of these queries have no sub-queries and
|
|
|
|
// therefore no actual inputs, they're just reading tables calculated in
|
|
|
|
// resolve! Does this work? Unsure! That's what the issue is about
|
2017-09-08 13:51:57 -07:00
|
|
|
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
|
2017-08-30 09:31:14 -07:00
|
|
|
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).cloned();
|
2017-09-08 13:51:57 -07:00
|
|
|
providers.named_region_map = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
|
|
|
|
providers.is_late_bound_map = |tcx, id| tcx.gcx.named_region_map.late_bound.get(&id).cloned();
|
|
|
|
providers.object_lifetime_defaults_map = |tcx, id| {
|
2017-08-30 09:31:14 -07:00
|
|
|
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
|
|
|
|
};
|
2017-08-31 08:07:39 -07:00
|
|
|
providers.crate_name = |tcx, id| {
|
|
|
|
assert_eq!(id, LOCAL_CRATE);
|
|
|
|
tcx.crate_name
|
|
|
|
};
|
2017-08-31 08:57:41 -07:00
|
|
|
providers.get_lang_items = |tcx, id| {
|
|
|
|
assert_eq!(id, LOCAL_CRATE);
|
|
|
|
Rc::new(middle::lang_items::collect(tcx))
|
|
|
|
};
|
2017-08-31 12:30:25 -07:00
|
|
|
providers.freevars = |tcx, id| tcx.gcx.freevars.get(&id).cloned();
|
2017-08-31 13:19:33 -07:00
|
|
|
providers.maybe_unused_trait_import = |tcx, id| {
|
|
|
|
tcx.maybe_unused_trait_imports.contains(&id)
|
|
|
|
};
|
|
|
|
providers.maybe_unused_extern_crates = |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
Rc::new(tcx.maybe_unused_extern_crates.clone())
|
|
|
|
};
|
2017-08-31 15:08:34 -07:00
|
|
|
|
|
|
|
providers.stability_index = |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
Rc::new(stability::Index::new(tcx))
|
|
|
|
};
|
|
|
|
providers.lookup_stability = |tcx, id| {
|
|
|
|
assert_eq!(id.krate, LOCAL_CRATE);
|
|
|
|
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
|
|
|
|
tcx.stability().local_stability(id)
|
|
|
|
};
|
|
|
|
providers.lookup_deprecation_entry = |tcx, id| {
|
|
|
|
assert_eq!(id.krate, LOCAL_CRATE);
|
|
|
|
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
|
|
|
|
tcx.stability().local_deprecation_entry(id)
|
|
|
|
};
|
2017-08-29 11:10:22 -07:00
|
|
|
}
|